Skip to content

Commit d5eb2f4

Browse files
miss-islingtonFFY00iritkatrielhauntsaninja
authored
bpo-40447: accept all path-like objects in compileall.compile_file (GH-19883)
(cherry picked from commit 1ecfd1e) Co-authored-by: Filipe Laíns <lains@riseup.net> Signed-off-by: Filipe Laíns <lains@archlinux.org> Signed-off-by: Filipe Laíns <lains@riseup.net> Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
1 parent 4b3b642 commit d5eb2f4

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

Lib/compileall.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
154154
"in combination with stripdir or prependdir"))
155155

156156
success = True
157-
if quiet < 2 and isinstance(fullname, os.PathLike):
158-
fullname = os.fspath(fullname)
157+
fullname = os.fspath(fullname)
158+
stripdir = os.fspath(stripdir) if stripdir is not None else None
159159
name = os.path.basename(fullname)
160160

161161
dfile = None

Lib/test/test_compileall.py

+28
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ def test_compile_file_pathlike_ddir(self):
167167
quiet=2))
168168
self.assertTrue(os.path.isfile(self.bc_path))
169169

170+
def test_compile_file_pathlike_stripdir(self):
171+
self.assertFalse(os.path.isfile(self.bc_path))
172+
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
173+
stripdir=pathlib.Path('stripdir_path'),
174+
quiet=2))
175+
self.assertTrue(os.path.isfile(self.bc_path))
176+
177+
def test_compile_file_pathlike_prependdir(self):
178+
self.assertFalse(os.path.isfile(self.bc_path))
179+
self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
180+
prependdir=pathlib.Path('prependdir_path'),
181+
quiet=2))
182+
self.assertTrue(os.path.isfile(self.bc_path))
183+
170184
def test_compile_path(self):
171185
with test.test_importlib.util.import_state(path=[self.directory]):
172186
self.assertTrue(compileall.compile_path(quiet=2))
@@ -219,6 +233,20 @@ def test_compile_dir_pathlike(self):
219233
self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
220234
self.assertTrue(os.path.isfile(self.bc_path))
221235

236+
def test_compile_dir_pathlike_stripdir(self):
237+
self.assertFalse(os.path.isfile(self.bc_path))
238+
self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
239+
stripdir=pathlib.Path('stripdir_path'),
240+
quiet=2))
241+
self.assertTrue(os.path.isfile(self.bc_path))
242+
243+
def test_compile_dir_pathlike_prependdir(self):
244+
self.assertFalse(os.path.isfile(self.bc_path))
245+
self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
246+
prependdir=pathlib.Path('prependdir_path'),
247+
quiet=2))
248+
self.assertTrue(os.path.isfile(self.bc_path))
249+
222250
@skipUnless(_have_multiprocessing, "requires multiprocessing")
223251
@mock.patch('concurrent.futures.ProcessPoolExecutor')
224252
def test_compile_pool_called(self, pool_mock):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Accept :class:`os.PathLike` (such as :class:`pathlib.Path`) in the ``stripdir`` arguments of
2+
:meth:`compileall.compile_file` and :meth:`compileall.compile_dir`.

0 commit comments

Comments
 (0)