GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-25872: Add unit tests for linecache and threading by uniocto · Pull Request #25913 · python/cpython

bpo-25872: Add unit tests for linecache and threading #25913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
iritkatriel merged 21 commits into python:main from uniocto:fix-issue-25872
May 18, 2021

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-25872: Add unit tests for linecache and threading #25913

Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5143e74
bpo-25872: add test_oserror
uniocto May 5, 2021
fc1f92d
refactor linecache
uniocto May 5, 2021
fc4df5d
add test_multithread_modify_file()
uniocto May 5, 2021
a977922
refactor test_threading
uniocto May 5, 2021
759de2b
refactor test_multithread_modify_file_noerror()
uniocto May 5, 2021
5666a1d
refactor raise_oserror()
uniocto May 5, 2021
4513859
Change black box tests and refactoring
uniocto May 8, 2021
8376d21
refactor LineCacheInvalidationTests
uniocto May 9, 2021
708e348
refactor test_multithread_modify_file_noerror
uniocto May 9, 2021
f021de2
refactor assertions
uniocto May 9, 2021
a396fad
Update Lib/test/test_linecache.py
uniocto May 9, 2021
833c9b9
Update Lib/test/test_linecache.py
uniocto May 9, 2021
bcc20ab
Update Lib/test/test_threading.py
uniocto May 9, 2021
ba62b83
change __file__ and rm traceback.format_stack
uniocto May 13, 2021
420a638
add traceback
uniocto May 13, 2021
2c8269b
fix type in field name
iritkatriel May 14, 2021
8efe040
reduce repetition in setUp
iritkatriel May 14, 2021
240eb9a
check cache keys directly, not count
iritkatriel May 14, 2021
f66a050
Merge pull request #2 from iritkatriel/pr25913
uniocto May 15, 2021
78897e7
addCleanup to outside of the mofidy_file()
uniocto May 15, 2021
ac550ef
Merge remote-tracking branch 'upstream/main' into fix-issue-25872
uniocto May 17, 2021
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
refactor raise_oserror()
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@uniocto
uniocto committed May 5, 2021
commit 5666a1dbf04d8a6da50a21962e985cd799da25d9

There are no files selected for viewing

10 changes: 5 additions & 5 deletions 10 Lib/test/test_linecache.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ def f():
def f():
return 3''' # No ending newline


SOURCE_4 = '''
raise OSError
'''

def raise_oserror(*args, **kwargs):
raise OSError


class TempFile:

def setUp(self):
Expand Down Expand Up @@ -190,8 +193,7 @@ def test_checkcache_oserror(self):
_ = linecache.getlines(FILENAME)
self.assertTrue(_)
self.assertEqual(1, len(linecache.cache.keys()))
def raise_oserror(*args, **kwargs):
raise OSError

with support.swap_attr(os, 'stat', raise_oserror):
Comment thread Outdated

@iritkatriel iritkatriel May 7, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that os.stat is used in checkcache is an implementation detail, not a part of that's function's external API, so I don't think the unit test should depend on that.

I would write this as a black-box test. And also test that the cache clearing is selective. Note that there are two cases in checkcache() where the cache is popped: OSError from stat, or size/timestamp don't match.

So, create three files f1, f2, f3 and load them into the cache. Then delete f1, modify f2 and call checkcache. Ensure that the cache entries for f1 and f2 were removed from the cache but the entries for f3 are still there.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

uniocto reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@uniocto uniocto May 8, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback.
As you said, other tests are written in such a way that they do not depend on the internal implementation.
I will try to write them in a black box as you suggested, so I would appreciate it if you could take look again !!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@uniocto uniocto May 8, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I write a black box test, and would be happy if you could check it out.

def test_oserror(self):
    def _oserror_helper():
        linecache.clearcache()
        be_deleted_file = os_helper.TESTFN + '.1'
        be_modified_file = os_helper.TESTFN + '.2'
        unchange_file = os_helper.TESTFN + '.3'
        self.addCleanup(os_helper.unlink, be_deleted_file)
        self.addCleanup(os_helper.unlink, be_modified_file)
        self.addCleanup(os_helper.unlink, unchange_file)
        with open(be_deleted_file, 'w', encoding='utf-8') as source:
            source.write('print("will be deleted")')
        with open(be_modified_file, 'w', encoding='utf-8') as source:
            source.write('print("will be modified")')
        with open(unchange_file, 'w', encoding='utf-8') as source:
            source.write('print("unchange")')

        _ = linecache.getlines(be_deleted_file)
        _ = linecache.getlines(be_modified_file)
        _ = linecache.getlines(unchange_file)
        self.assertEqual(3, len(linecache.cache.keys()))

        os.remove(be_deleted_file)
        with open(be_modified_file, 'w', encoding='utf-8') as source:
            source.write('print("was modified")')
        return (be_deleted_file, be_modified_file, unchange_file)

    deleted_file, modified_file, unchange_file = _oserror_helper()
    _ = linecache.checkcache(deleted_file)
    self.assertEqual(2, len(linecache.cache.keys()))
    _ = linecache.checkcache(modified_file)
    self.assertEqual(1, len(linecache.cache.keys()))
    _ = linecache.checkcache(unchange_file)
    self.assertEqual(1, len(linecache.cache.keys()))

    deleted_file, modified_file, unchange_file = _oserror_helper()
    _ = linecache.updatecache(deleted_file)
    self.assertEqual(2, len(linecache.cache.keys()))
    _ = linecache.updatecache(modified_file)
    self.assertEqual(2, len(linecache.cache.keys()))
    _ = linecache.updatecache(unchange_file)
    self.assertEqual(2, len(linecache.cache.keys()))

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
# pop all cache
_ = linecache.checkcache()
Expand All @@ -202,8 +204,6 @@ def test_updatecache_oserror(self):
source_name = os_helper.TESTFN
self.addCleanup(os_helper.unlink, os_helper.TESTFN)

def raise_oserror(*args, **kwargs):
raise OSError
with open(source_name, 'w', encoding='utf-8') as source:
source.write(SOURCE_4)
_ = linecache.getlines(source_name)
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.