|
19 | 19 | from random import randint, random |
20 | 20 | from unittest import skipUnless |
21 | 21 |
|
22 | | -from test.support import TESTFN, run_unittest, findfile, unlink |
| 22 | +from test.support import (TESTFN, run_unittest, findfile, unlink, |
| 23 | + captured_stdout) |
23 | 24 |
|
24 | 25 | TESTFN2 = TESTFN + "2" |
25 | 26 | TESTFNDIR = TESTFN + "d" |
@@ -735,6 +736,28 @@ def test_write_non_pyfile(self): |
735 | 736 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
736 | 737 | os.remove(TESTFN) |
737 | 738 |
|
| 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) |
738 | 761 |
|
739 | 762 | class OtherTests(unittest.TestCase): |
740 | 763 | zips_with_bad_crc = { |
|
0 commit comments