diff -r 7fa3e824a4ee Lib/py_compile.py --- a/Lib/py_compile.py Tue Oct 29 22:25:06 2013 -0400 +++ b/Lib/py_compile.py Wed Oct 30 11:24:00 2013 +0100 @@ -108,15 +108,15 @@ byte-compile all installed files (or all files in selected directories). """ - with tokenize.open(file) as f: - try: - st = os.fstat(f.fileno()) - except AttributeError: - st = os.stat(file) - timestamp = int(st.st_mtime) - size = st.st_size & 0xFFFFFFFF - codestring = f.read() try: + with tokenize.open(file) as f: + try: + st = os.fstat(f.fileno()) + except AttributeError: + st = os.stat(file) + timestamp = int(st.st_mtime) + size = st.st_size & 0xFFFFFFFF + codestring = f.read() codeobject = builtins.compile(codestring, dfile or file, 'exec', optimize=optimize) except Exception as err: diff -r 7fa3e824a4ee Lib/test/test_py_compile.py --- a/Lib/test/test_py_compile.py Tue Oct 29 22:25:06 2013 -0400 +++ b/Lib/test/test_py_compile.py Wed Oct 30 11:24:00 2013 +0100 @@ -54,6 +54,10 @@ self.assertTrue(os.path.exists(self.pyc_path)) self.assertFalse(os.path.exists(self.cache_path)) + def test_bad_coding(self): + bad_coding = os.path.join(os.path.dirname(__file__), 'bad_coding2.py') + self.assertIsNone(py_compile.compile(bad_coding, doraise=False)) + def test_main(): support.run_unittest(PyCompileTests)