Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45c4375

Browse files
Issue #12004: Fix an internal error in PyZipFile when writing an invalid
Python file. Patch by Ben Morgan.
1 parent a97c57c commit 45c4375

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

Lib/test/test_zipfile.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
from random import randint, random
2020
from unittest import skipUnless
2121

22-
from test.support import TESTFN, run_unittest, findfile, unlink
22+
from test.support import (TESTFN, run_unittest, findfile, unlink,
23+
captured_stdout)
2324

2425
TESTFN2 = TESTFN + "2"
2526
TESTFNDIR = TESTFN + "d"
@@ -735,6 +736,28 @@ def test_write_non_pyfile(self):
735736
self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
736737
os.remove(TESTFN)
737738

739+
def test_write_pyfile_bad_syntax(self):
740+
os.mkdir(TESTFN2)
741+
try:
742+
with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
743+
fp.write("Bad syntax in python file\n")
744+
745+
with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
746+
# syntax errors are printed to stdout
747+
with captured_stdout() as s:
748+
zipfp.writepy(os.path.join(TESTFN2, "mod1.py"))
749+
750+
self.assertIn("SyntaxError", s.getvalue())
751+
752+
# as it will not have compiled the python file, it will
753+
# include the .py file not .pyc or .pyo
754+
names = zipfp.namelist()
755+
self.assertIn('mod1.py', names)
756+
self.assertNotIn('mod1.pyc', names)
757+
self.assertNotIn('mod1.pyo', names)
758+
759+
finally:
760+
shutil.rmtree(TESTFN2)
738761

739762
class OtherTests(unittest.TestCase):
740763
zips_with_bad_crc = {

Lib/zipfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ def _compile(file, optimize=-1):
14361436
print("Compiling", file)
14371437
try:
14381438
py_compile.compile(file, doraise=True, optimize=optimize)
1439-
except py_compile.PyCompileError as error:
1439+
except py_compile.PyCompileError as err:
14401440
print(err.msg)
14411441
return False
14421442
return True

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ The Dragon De Monsyne
741741
Skip Montanaro
742742
Paul Moore
743743
Ross Moore
744+
Ben Morgan
744745
Derek Morr
745746
James A Morrison
746747
Alessandro Moura

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ Core and Builtins
216216
Library
217217
-------
218218

219+
- Issue #12004: Fix an internal error in PyZipFile when writing an invalid
220+
Python file. Patch by Ben Morgan.
221+
219222
- Issue #9290: In IDLE the sys.std* streams now implement io.TextIOBase
220223
interface and support all mandatory methods and properties.
221224

0 commit comments

Comments
 (0)