`__file__` may be set inconsistently with CPython 3.9 when a path contains `..`
I have a test that broke on an update of PyPy to 7.3.10 (for Python 3.9). It seems to be due to a change in how __file__
is set when Python is invoked on a script whose path contains ..
.
In the below example code, the final line is ../foo.py → /realdir/foo.py
rather than the expected ../foo.py → /realdir/child/../foo.py
. This means that on CPython, the command python3 ../foo.py
retains ..
in __file__
, whereas on PyPy, it doesn't. I can only reproduce this issue when I run PyPy in a virtual environment. Also note that I'm testing this on Linux; I know that CPython itself is inconsistent between Windows and Linux in its behavior here.
from os import chdir
from pathlib import Path
import tempfile, subprocess
def test_uufileuu(tmp_path):
(tmp_path / "realdir").mkdir()
(tmp_path / "realdir" / "foo.py").write_text('print(__file__)')
def run(arg):
print(arg, '→', subprocess
.check_output(["python3", arg])
.decode('UTF-8')
.removeprefix(str(tmp_path))
.rstrip())
chdir(tmp_path)
run("realdir/foo.py")
chdir(tmp_path / "realdir")
run("foo.py")
(tmp_path / "symdir").symlink_to("realdir", target_is_directory = True)
chdir(tmp_path)
run("symdir/foo.py")
(tmp_path / "realdir" / "child").mkdir()
chdir(tmp_path / "realdir" / "child")
run("../foo.py")
# N.B. difference w.r.t. Unix and Windows
with tempfile.TemporaryDirectory() as tmp_path:
test_uufileuu(Path(tmp_path))