Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WASI] test cases with sys.path.insert(0, os.curdir) fail on WASI #93461

Open
tiran opened this issue Jun 3, 2022 · 2 comments
Open

[WASI] test cases with sys.path.insert(0, os.curdir) fail on WASI #93461

tiran opened this issue Jun 3, 2022 · 2 comments
Assignees
Labels
3.11 3.12 expert-importlib type-bug

Comments

@tiran
Copy link
Member

@tiran tiran commented Jun 3, 2022

Bug report

Test cases that insert os.curdir ('.') into sys.path fail on WASI platforms. I think it is related how wasmtime maps directories into the runtime environment and how it sets up the current working directory. The test passes if I replace '.' with an empty string '' or remove the cached FileFinder instance for '.' with sys.path_importer_cache.pop('.').

 ./test-wasi.sh test_support -m test_forget
0:00:00 Run tests sequentially
0:00:00 [1/1] test_support
test test_support failed -- Traceback (most recent call last):
  File "/Lib/test/test_support.py", line 116, in test_forget
    mod = __import__(TESTFN)
          ^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '@test_66600_tmpæ'

test_support failed (1 error)

== Tests result: FAILURE ==

The culprit seems to be an outdated FileFinder instance: {'.': FileFinder('/.')}. The CWD is '/python-wasm/cpython/builddir/wasi/build/test_python_66600æ' when the test case is executed.

0:00:00 [1/1] test_support
> /Lib/test/test_support.py(114)test_forget()
-> sys.path.insert(0, os.curdir)
(Pdb) p sys.path
['.', '/usr/local/lib/python312.zip', '/Lib', '/build/lib.wasi-wasm32-3.12']
(Pdb) pp sys.path_importer_cache
{'.': FileFinder('/.'),
 '/Lib': FileFinder('/Lib'),
 '/Lib/collections': FileFinder('/Lib/collections'),
 '/Lib/encodings': FileFinder('/Lib/encodings'),
 '/Lib/importlib': FileFinder('/Lib/importlib'),
 '/Lib/re': FileFinder('/Lib/re'),
 '/Lib/test': FileFinder('/Lib/test'),
 '/Lib/test/libregrtest': FileFinder('/Lib/test/libregrtest'),
 '/Lib/test/support': FileFinder('/Lib/test/support'),
 '/Lib/unittest': FileFinder('/Lib/unittest'),
 '/Lib/urllib': FileFinder('/Lib/urllib'),
 '/build/lib.wasi-wasm32-3.12': FileFinder('/build/lib.wasi-wasm32-3.12'),
 '/usr/local/lib/python312.zip': None}
(Pdb) p os.getcwd()
'/python-wasm/cpython/builddir/wasi/build/test_python_66600æ'
(Pdb) n
> /Lib/test/test_support.py(115)test_forget()
-> importlib.invalidate_caches()
(Pdb) n
> /Lib/test/test_support.py(116)test_forget()
-> try:
(Pdb) p sys.path
['.', '.', '/usr/local/lib/python312.zip', '/Lib', '/build/lib.wasi-wasm32-3.12']
(Pdb) pp sys.path_importer_cache
{'.': FileFinder('/.'),
 '/Lib': FileFinder('/Lib'),
 '/Lib/collections': FileFinder('/Lib/collections'),
 '/Lib/encodings': FileFinder('/Lib/encodings'),
 '/Lib/importlib': FileFinder('/Lib/importlib'),
 '/Lib/re': FileFinder('/Lib/re'),
 '/Lib/test': FileFinder('/Lib/test'),
 '/Lib/test/libregrtest': FileFinder('/Lib/test/libregrtest'),
 '/Lib/test/support': FileFinder('/Lib/test/support'),
 '/Lib/unittest': FileFinder('/Lib/unittest'),
 '/Lib/urllib': FileFinder('/Lib/urllib'),
 '/build/lib.wasi-wasm32-3.12': FileFinder('/build/lib.wasi-wasm32-3.12'),
 '/usr/local/lib/python312.zip': None}

A build on Linux has an absolute path to build directory as first sys.path entry.

reproducer

$ podman run --rm -ti -v $(pwd):/python-wasm/cpython:Z quay.io/tiran/cpythonbuild:emsdk3
# ./build-python-build.sh
# ./build-python-wasi.sh
# ./test-wasi.sh test_support -m test_forget
@tiran tiran added type-bug 3.11 3.12 labels Jun 3, 2022
tiran added a commit to tiran/cpython that referenced this issue Jun 9, 2022
@tiran
Copy link
Member Author

@tiran tiran commented Jun 9, 2022

GH-93461 is the simplest solution I could come up with. importlib.invalidate_caches now drops all FileFinder instances if the cache key is not an absolute path.

@brettcannon
Copy link
Member

@brettcannon brettcannon commented Jun 9, 2022

Seems like a reasonable solution to the problem.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 10, 2022
…ive paths (pythonGH-93653)

(cherry picked from commit 09243b8)

Co-authored-by: Christian Heimes <christian@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 3.12 expert-importlib type-bug
Projects
None yet
Development

No branches or pull requests

2 participants