]> granicus.if.org Git - python/commitdiff
Issue #12004: Fix an internal error in PyZipFile when writing an invalid
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 29 Jan 2013 18:10:28 +0000 (20:10 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 29 Jan 2013 18:10:28 +0000 (20:10 +0200)
Python file.  Patch by Ben Morgan.

Lib/test/test_zipfile.py
Lib/zipfile.py
Misc/ACKS
Misc/NEWS

index e07380dd16d5febd1e3e253cf050b31faf07ba5e..367f37ee04d12a227e25cd680cda7d48ea0e7775 100644 (file)
@@ -19,7 +19,8 @@ from tempfile import TemporaryFile
 from random import randint, random
 from unittest import skipUnless
 
-from test.support import TESTFN, run_unittest, findfile, unlink
+from test.support import (TESTFN, run_unittest, findfile, unlink,
+                            captured_stdout)
 
 TESTFN2 = TESTFN + "2"
 TESTFNDIR = TESTFN + "d"
@@ -735,6 +736,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 = {
index 32cf42c19870988c280ae88955849e17c8e225af..a7cee72ae67aa45d1296e4c22b41b178cb5b3e10 100644 (file)
@@ -1436,7 +1436,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
index 6e7dfe4a8de11e3f95edca6417c71b8bb27c6780..7ed6b317ec71cc63f8252b36d620070afe3ce14d 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -741,6 +741,7 @@ The Dragon De Monsyne
 Skip Montanaro
 Paul Moore
 Ross Moore
+Ben Morgan
 Derek Morr
 James A Morrison
 Alessandro Moura
index 1282647f5ab393e832da90bc510a769b7d8ca6a3..7ebe3f8e471300509ef53624a504f65c09df0323 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -216,6 +216,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #12004: Fix an internal error in PyZipFile when writing an invalid
+  Python file.  Patch by Ben Morgan.
+
 - Issue #9290: In IDLE the sys.std* streams now implement io.TextIOBase
   interface and support all mandatory methods and properties.