Skip to content

Commit c2735b7

Browse files
bpo-46678: Fix Invalid cross device link in Lib/test/support/import_helper.py (GH-31204) (GH-31207)
In `Lib/test/support/import_helper.py`, the function `make_legacy_pyc` makes a call to `os.rename` which can fail when the source and target live on different devices. This happens (for example) when `PYTHONPYCACHEPREFIX` is set to a directory anywhere on disk, while a ramdisk is mounted on `/tmp` (the latter of which is the default on various Linux distros). Replacing `os.rename` with `shutil.move` fixes this. Automerge-Triggered-By: GH:brettcannon (cherry picked from commit da576e0) Co-authored-by: Jason Wilkes <[email protected]>
1 parent 5b58db7 commit c2735b7

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

Lib/test/support/import_helper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import importlib
33
import importlib.util
44
import os
5+
import shutil
56
import sys
67
import unittest
78
import warnings
@@ -58,7 +59,7 @@ def make_legacy_pyc(source):
5859
pyc_file = importlib.util.cache_from_source(source)
5960
up_one = os.path.dirname(os.path.abspath(source))
6061
legacy_pyc = os.path.join(up_one, source + 'c')
61-
os.rename(pyc_file, legacy_pyc)
62+
shutil.move(pyc_file, legacy_pyc)
6263
return legacy_pyc
6364

6465

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The function ``make_legacy_pyc`` in ``Lib/test/support/import_helper.py`` no
2+
longer fails when ``PYTHONPYCACHEPREFIX`` is set to a directory on a
3+
different device from where tempfiles are stored.

0 commit comments

Comments
 (0)