summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-01-29 18:10:28 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-01-29 18:10:28 (GMT)
commit45c4375ea7da4a78e9d5dc7b79f98b7882cc7640 (patch)
tree417b552a3c2b7798011c4587208f046e330cf2e3
parenta97c57c8fdb5d91771635633f081f5150eb101c5 (diff)
downloadcpython-45c4375ea7da4a78e9d5dc7b79f98b7882cc7640.zip
cpython-45c4375ea7da4a78e9d5dc7b79f98b7882cc7640.tar.gz
cpython-45c4375ea7da4a78e9d5dc7b79f98b7882cc7640.tar.bz2
Issue #12004: Fix an internal error in PyZipFile when writing an invalid
Python file. Patch by Ben Morgan.
-rw-r--r--Lib/test/test_zipfile.py25
-rw-r--r--Lib/zipfile.py2
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
4 files changed, 29 insertions, 2 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index e07380d..367f37e 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -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 = {
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index 32cf42c..a7cee72 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -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
diff --git a/Misc/ACKS b/Misc/ACKS
index 6e7dfe4..7ed6b31 100644
--- 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
diff --git a/Misc/NEWS b/Misc/NEWS
index 1282647..7ebe3f8 100644
--- 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.