bpo-31602: Fix an assertion failure in zipimporter.get_source() in case of a bad zlib.decompress()#3784
Conversation
brettcannon
left a comment
There was a problem hiding this comment.
The fix LGTM, but some tweaks to make to the test to use some specific idioms.
| import zlib | ||
| def bad_decompress(*args): | ||
| return None | ||
| z = ZipFile(TEMP_ZIP, 'w') |
There was a problem hiding this comment.
We tend to prefer people use more descriptive variable names and avoid single-letter ones unless it's a loop counter.
| self.assertRaises(TypeError, zi.get_source, 'bar') | ||
| finally: | ||
| z.close() | ||
| os.remove(TEMP_ZIP) |
There was a problem hiding this comment.
You can use self.addCleanup() so you don't have to specify this in the try block.
| with support.swap_attr(zlib, 'decompress', bad_decompress): | ||
| self.assertRaises(TypeError, zi.get_source, 'bar') | ||
| finally: | ||
| z.close() |
There was a problem hiding this comment.
You also called z.close() above, so I would use my self.addCleanup() suggestion below and rework this to drop the try block and use the context manager for zipfiles:
name = 'bar.py'
data = b'spam'
with ZipFile(...) as zip_file:
self.addCleanup(os.remove, TEMP_ZIP)
zip_file.writestr(...)
zi = ...| def test_issue31602(self): | ||
| # There shouldn't be an assertion failure in zipimporter.get_source() | ||
| # in case of a bad zlib.decompress(). | ||
| import zlib |
There was a problem hiding this comment.
There is no guarantee the zlib module will be available, so at the top of the module you should do:
try:
import zlib
except ImportError:
zlib = NoneThen decorate this method with unittest.skipIf(zlib is None, "zlib not available").
There was a problem hiding this comment.
Or use the test.support.requires_zlib decorator.
There was a problem hiding this comment.
doesn't this decorator already guarantee that zlib will be available for the test?
@support.requires_zlib
class CompressedZipImportTestCase(UncompressedZipImportTestCase):
There was a problem hiding this comment.
Yes, it was just out of the visible context.
There was a problem hiding this comment.
Ah, then keep the decorator but still switch to the try block at the top for the import (importing in methods is icky).
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
| self.assertRaises(TypeError, zi.get_source, 'bar') | ||
| finally: | ||
| z.close() | ||
| os.remove(TEMP_ZIP) |
There was a problem hiding this comment.
It is better to use test.support.unlink().
| def test_issue31602(self): | ||
| # There shouldn't be an assertion failure in zipimporter.get_source() | ||
| # in case of a bad zlib.decompress(). | ||
| import zlib |
There was a problem hiding this comment.
Or use the test.support.requires_zlib decorator.
|
I didn't expect the Spanish Inquisition! |
|
Nobody expects the Spanish Inquisition! @brettcannon: please review the changes made to this pull request. |
| def test_issue31602(self): | ||
| # There shouldn't be an assertion failure in zipimporter.get_source() | ||
| # in case of a bad zlib.decompress(). | ||
| import zlib |
There was a problem hiding this comment.
Yes, it was just out of the visible context.
| return None | ||
| with ZipFile(TEMP_ZIP, 'w') as zip_file: | ||
| self.addCleanup(support.unlink, TEMP_ZIP) | ||
| name = 'bar.py' |
There was a problem hiding this comment.
Why not inline these values? For clarity use a valid Python statement instead of 'spam'.
There was a problem hiding this comment.
No idea why not. I just copied the style of all other tests in this file.
brettcannon
left a comment
There was a problem hiding this comment.
One very minor cleanup left and then this PR is ready for merging!
| def test_issue31602(self): | ||
| # There shouldn't be an assertion failure in zipimporter.get_source() | ||
| # in case of a bad zlib.decompress(). | ||
| import zlib |
There was a problem hiding this comment.
Ah, then keep the decorator but still switch to the try block at the top for the import (importing in methods is icky).
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
The test now looks much nicer!
|
It's so weird to be looking at the latest update to the PR and have the latest commit from @orenmn literally land right in front of me. 😄 |
|
This happened only thanks to Murphy's law. |
|
The failure was from an automatic cancellation on the part of Travis; it decided to start a new build and thus cancelled the already-running one. But everything is good now! |
|
Thanks for the pull request! |
zipimport.c: add a check whetherzlib.decompress()returned a bytes object.test_zipimport.py: add a test to verify that the assertion failure is no more.https://bugs.python.org/issue31602