[Python-checkins] cpython (merge 3.3 -> default): Issue #12004: Fix an internal error in PyZipFile when writing an invalid
serhiy.storchaka
python-checkins at python.org
Tue Jan 29 19:22:43 CET 2013
http://hg.python.org/cpython/rev/e24fd2c35d27
changeset: 81830:e24fd2c35d27
parent: 81827:82be592d764d
parent: 81829:678320c7f63d
user: Serhiy Storchaka <storchaka at gmail.com>
date: Tue Jan 29 20:15:45 2013 +0200
summary:
Issue #12004: Fix an internal error in PyZipFile when writing an invalid
Python file. Patch by Ben Morgan.
files:
Lib/test/test_zipfile.py | 26 +++++++++++++++++++++++++-
Lib/zipfile.py | 2 +-
Misc/ACKS | 1 +
Misc/NEWS | 3 +++
4 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -13,7 +13,9 @@
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 @@
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
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -1604,7 +1604,7 @@
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
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -825,6 +825,7 @@
Peter Moody
Paul Moore
Ross Moore
+Ben Morgan
Derek Morr
James A Morrison
Derek McTavish Mounce
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -236,6 +236,9 @@
Library
-------
+- Issue #12004: Fix an internal error in PyZipFile when writing an invalid
+ Python file. Patch by Ben Morgan.
+
Have py_compile use importlib as much as possible to avoid code duplication.
- Issue #180022: Have site.addpackage() consider already known paths even when
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list