Skip to content

Commit 312499d

Browse files
Resolve the tests directory before changing into it
tests/utf8_locale.py computed os.path.dirname(os.path.abspath(__file__)) twice : once to chdir into tests, then again inside rerun_with_non_utf8_locale() to give the child its cwd. Up to Python 3.8 __file__ stays relative to the directory the interpreter was started from, so the second call resolved against the new directory and produced tests/tests. `PYTHONPATH=. python3.8 tests/utf8_locale.py` from the repository root died with FileNotFoundError before the child even started, while Python 3.9+ passed because __file__ is absolute there. Resolve the directory and the script name once, at import time, and reuse them for both os.chdir() and the child cwd, so neither depends on the current directory any more. The PYTHONPATH handed to the child had the same problem : the child runs in the tests directory, so a relative entry such as "." no longer pointed at the repository root and docxtpl was not importable. Make its entries absolute against the startup directory.
1 parent 286eaf0 commit 312499d

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

‎tests/utf8_locale.py‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323

2424
CHILD_ENV_FLAG = "DOCXTPL_NON_UTF8_LOCALE_CHILD"
2525

26+
# Resolved once, at import time : until Python 3.9 __file__ is relative to the
27+
# directory the interpreter was started from, so recomputing it after the
28+
# os.chdir() below would append a second "tests" component.
29+
TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
30+
SCRIPT_NAME = os.path.basename(__file__)
31+
STARTUP_DIR = os.getcwd()
32+
2633
TEMPLATE_PATH = "templates/module_execute_tpl.docx"
2734
JSON_PATH = "templates/module_execute_utf8.json"
2835
XML_TEMPLATE_PATH = "templates/richtext_eastAsia_tpl.docx"
@@ -41,11 +48,20 @@ def rerun_with_non_utf8_locale():
4148
env["PYTHONCOERCECLOCALE"] = "0" # disable C locale coercion (PEP 538)
4249
env["LC_ALL"] = "C"
4350
env["LANG"] = "C"
51+
# PYTHONPATH entries are relative to the directory this script was started
52+
# from, and the child runs in TESTS_DIR : make them absolute so that a
53+
# `PYTHONPATH=. python tests/utf8_locale.py` still finds docxtpl.
54+
python_path = env.get("PYTHONPATH")
55+
if python_path:
56+
env["PYTHONPATH"] = os.pathsep.join(
57+
os.path.join(STARTUP_DIR, entry) if entry else STARTUP_DIR
58+
for entry in python_path.split(os.pathsep)
59+
)
4460
# Give the bare file name : the child filesystem encoding is ASCII, so an
4561
# accented character in the path would not survive as an argument.
4662
return subprocess.call(
47-
[sys.executable, os.path.basename(__file__)],
48-
cwd=os.path.dirname(os.path.abspath(__file__)),
63+
[sys.executable, SCRIPT_NAME],
64+
cwd=TESTS_DIR,
4965
env=env,
5066
)
5167

@@ -98,12 +114,12 @@ def check_xml_is_written_as_utf8():
98114

99115

100116
if __name__ == "__main__":
101-
os.chdir(os.path.dirname(os.path.abspath(__file__)))
117+
os.chdir(TESTS_DIR)
102118
if not os.path.exists("output"):
103119
os.mkdir("output")
104120
if os.environ.get(CHILD_ENV_FLAG) != "1":
105121
print(
106-
"Re-running %s with a non UTF-8 locale ..." % os.path.basename(__file__),
122+
"Re-running %s with a non UTF-8 locale ..." % SCRIPT_NAME,
107123
flush=True,
108124
)
109125
sys.exit(rerun_with_non_utf8_locale())

0 commit comments

Comments
 (0)