diff --git a/Lib/py_compile.py b/Lib/py_compile.py --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -130,7 +130,9 @@ def compile(file, cfile=None, dfile=None else: cfile = imp.cache_from_source(file) try: - os.makedirs(os.path.dirname(cfile)) + dirname = os.path.dirname(cfile) + if dirname: + os.makedirs(dirname) except OSError as error: if error.errno != errno.EEXIST: raise diff --git a/Lib/test/test_py_compile.py b/Lib/test/test_py_compile.py --- a/Lib/test/test_py_compile.py +++ b/Lib/test/test_py_compile.py @@ -39,6 +39,15 @@ class PyCompileTests(unittest.TestCase): py_compile.compile(self.source_path) self.assertTrue(os.path.exists(self.cache_path)) + def test_cwd(self): + cwd = os.getcwd() + os.chdir(self.directory) + py_compile.compile(os.path.basename(self.source_path), + os.path.basename(self.pyc_path)) + os.chdir(cwd) + self.assertTrue(os.path.exists(self.pyc_path)) + self.assertFalse(os.path.exists(self.cache_path)) + def test_relative_path(self): py_compile.compile(os.path.relpath(self.source_path), os.path.relpath(self.pyc_path))