diff -r 5176e8a2e258 Doc/library/compileall.rst --- a/Doc/library/compileall.rst Wed Dec 09 19:45:07 2015 +0200 +++ b/Doc/library/compileall.rst Wed Dec 09 16:12:33 2015 -0500 @@ -103,7 +103,8 @@ .. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1) Recursively descend the directory tree named by *dir*, compiling all :file:`.py` - files along the way. + files along the way. Return 1 if all the files compiled successfully, and 0 + otherwise. The *maxlevels* parameter is used to limit the depth of the recursion; it defaults to ``10``. @@ -155,7 +156,8 @@ .. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1) - Compile the file with path *fullname*. + Compile the file with path *fullname*. Return 1 if the file compiled successfully, + and 0 otherwise. If *ddir* is given, it is prepended to the path to the file being compiled for use in compilation time tracebacks, and is also compiled in to the @@ -191,8 +193,10 @@ .. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1) - Byte-compile all the :file:`.py` files found along ``sys.path``. If - *skip_curdir* is true (the default), the current directory is not included + Byte-compile all the :file:`.py` files found along ``sys.path``. Return 1 if + all the files compiled successfully, and 0 otherwise. + + If *skip_curdir* is true (the default), the current directory is not included in the search. All other parameters are passed to the :func:`compile_dir` function. Note that unlike the other compile functions, ``maxlevels`` defaults to ``0``. diff -r 5176e8a2e258 Lib/test/test_compileall.py --- a/Lib/test/test_compileall.py Wed Dec 09 19:45:07 2015 +0200 +++ b/Lib/test/test_compileall.py Wed Dec 09 16:12:33 2015 -0500 @@ -37,8 +37,19 @@ self.source_path3 = os.path.join(self.subdirectory, '_test3.py') shutil.copyfile(self.source_path, self.source_path3) + # Bad files + self.bad_directory = tempfile.mkdtemp() + self.bad_source_path = os.path.join(self.bad_directory, '_test.py') + with open(self.bad_source_path, 'w') as file: + file.write('x (\n') + self.bad_subdirectory = os.path.join(self.bad_directory, '_subdir') + os.mkdir(self.bad_subdirectory) + self.bad_source_path2 = os.path.join(self.bad_subdirectory, '_test2.py') + shutil.copyfile(self.bad_source_path, self.bad_source_path2) + def tearDown(self): shutil.rmtree(self.directory) + shutil.rmtree(self.bad_directory) def data(self): with open(self.bc_path, 'rb') as file: @@ -78,15 +89,37 @@ os.unlink(fn) except: pass - compileall.compile_file(self.source_path, force=False, quiet=True) + ret = compileall.compile_file(self.source_path, + force=False, quiet=True) self.assertTrue(os.path.isfile(self.bc_path) and not os.path.isfile(self.bc_path2)) + self.assertEqual(ret, 1) os.unlink(self.bc_path) - compileall.compile_dir(self.directory, force=False, quiet=True) + ret = compileall.compile_dir(self.directory, force=False, quiet=True) self.assertTrue(os.path.isfile(self.bc_path) and os.path.isfile(self.bc_path2)) + self.assertEqual(ret, 1) os.unlink(self.bc_path) os.unlink(self.bc_path2) + # Test against bad files + ret = compileall.compile_file(self.bad_source_path, + force=False, quiet=True) + self.assertEqual(ret, 0) + ret = compileall.compile_dir(self.bad_directory, + force=False, quiet=True) + self.assertEqual(ret, 0) + + def test_compile_path(self): + ret = compileall.compile_path(quiet=True) + self.assertEqual(ret, 1) + cwd = os.getcwd() + try: + os.chdir(self.bad_directory) + # skip_curdir doesn't seem to take effect. Don't understand why. + ret = compileall.compile_path(skip_curdir=False, force=True, quiet=True) + self.assertEqual(ret, 0) + finally: + os.chdir(cwd) def test_no_pycache_in_non_package(self): # Bug 8563 reported that __pycache__ directories got created by