diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-29 18:14:08 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-29 18:14:08 (GMT) |
commit | c5b75db5def1890924d914bc36b54e6a120bf2d7 (patch) | |
tree | a5ac5240a7986637cc303f8d4dd34a02f7ffb26f /Lib | |
parent | afb1cb55793669b70f355540389409cedc731d93 (diff) | |
parent | 45c4375ea7da4a78e9d5dc7b79f98b7882cc7640 (diff) | |
download | cpython-c5b75db5def1890924d914bc36b54e6a120bf2d7.zip cpython-c5b75db5def1890924d914bc36b54e6a120bf2d7.tar.gz cpython-c5b75db5def1890924d914bc36b54e6a120bf2d7.tar.bz2 |
Issue #12004: Fix an internal error in PyZipFile when writing an invalid
Python file. Patch by Ben Morgan.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_zipfile.py | 26 | ||||
-rw-r--r-- | Lib/zipfile.py | 2 |
2 files changed, 26 insertions, 2 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index ac6983f..e7acc30 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -13,7 +13,9 @@ from tempfile import TemporaryFile from random import randint, random from unittest import skipUnless -from test.support import TESTFN, run_unittest, findfile, unlink, requires_zlib, requires_bz2, requires_lzma +from test.support import (TESTFN, run_unittest, findfile, unlink, + requires_zlib, requires_bz2, requires_lzma, + captured_stdout) TESTFN2 = TESTFN + "2" TESTFNDIR = TESTFN + "d" @@ -854,6 +856,28 @@ class PyZipFileTests(unittest.TestCase): self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) os.remove(TESTFN) + def test_write_pyfile_bad_syntax(self): + os.mkdir(TESTFN2) + try: + with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: + fp.write("Bad syntax in python file\n") + + with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: + # syntax errors are printed to stdout + with captured_stdout() as s: + zipfp.writepy(os.path.join(TESTFN2, "mod1.py")) + + self.assertIn("SyntaxError", s.getvalue()) + + # as it will not have compiled the python file, it will + # include the .py file not .pyc or .pyo + names = zipfp.namelist() + self.assertIn('mod1.py', names) + self.assertNotIn('mod1.pyc', names) + self.assertNotIn('mod1.pyo', names) + + finally: + shutil.rmtree(TESTFN2) class OtherTests(unittest.TestCase): zips_with_bad_crc = { diff --git a/Lib/zipfile.py b/Lib/zipfile.py index dcebf72..4cad400 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -1604,7 +1604,7 @@ class PyZipFile(ZipFile): print("Compiling", file) try: py_compile.compile(file, doraise=True, optimize=optimize) - except py_compile.PyCompileError as error: + except py_compile.PyCompileError as err: print(err.msg) return False return True |