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

test_unittest failure triggered by test_import + test_importlib #108927

Closed
Yhg1s opened this issue Sep 5, 2023 · 6 comments
Closed

test_unittest failure triggered by test_import + test_importlib #108927

Yhg1s opened this issue Sep 5, 2023 · 6 comments
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@Yhg1s
Copy link
Member

Yhg1s commented Sep 5, 2023

Bug report

Bug description:

test_unittest fails if (and only if) test_import and test_importlib are run (in the same process) before it:

./python -m test test_import test_importlib test_unittest
0:00:00 load avg: 3.83 Run tests sequentially
0:00:00 load avg: 3.83 [1/3] test_import
0:00:02 load avg: 3.52 [2/3] test_importlib
0:00:06 load avg: 3.24 [3/3] test_unittest
test test_unittest failed -- Traceback (most recent call last):
  File "/home/thomas/python/python/cpython/Lib/test/test_unittest/test_discovery.py", line 840, in test_discovery_failed_discovery
    with test.test_importlib.util.uncache('package'):
         ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'test.test_importlib' has no attribute 'util'

test_unittest failed (1 error)

CPython versions tested on:

3.12, CPython main branch

Operating systems tested on:

Linux

Linked PRs

@Yhg1s Yhg1s added the type-bug An unexpected behavior, bug, or error label Sep 5, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Sep 5, 2023
@serhiy-storchaka
Copy link
Member

  1. test_import imports test.test_importlib.util. 'test.test_importlib' and 'test.test_importlib.util' are now in sys.modules.
  2. Somebody removes 'test.test_importlib' from sys.modules but leaves 'test.test_importlib.util'.
  3. libregrtest imports test.test_importlib. test now has attribute test_importlib.
  4. test_unittest.test_discovery requests to import test.test_importlib.util at import time, and since it is found in sys.modules, it simply sets module global test.
  5. It now tries to access test.test_importlib.util. Global test has attribute test_importlib, but test.test_importlib has no attribute util.

@serhiy-storchaka
Copy link
Member

2. Somebody removes 'test.test_importlib' from sys.modules but leaves 'test.test_importlib.util'.

libregrtest does this for every tested module. Now this sequence is clear:

  1. test_import imports test.test_importlib.util.
  2. libregrtest messes with import machinery by removing and re-importing test_importlib.
  3. test_unittest imports and use test.test_importlib.util.

You can use other tests which import test.test_importlib.util, just add test_importlib between two of such tests.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Sep 5, 2023
It breaks import machinery if the test module has submodules used in
other tests.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Sep 5, 2023
It breaks import machinery if the test module has submodules used in
other tests.
@serhiy-storchaka
Copy link
Member

test_compileall yet one test which can fail after test_importlib.

$ ./python -m test -v test_import test_importlib test_compileall
...
---------------------------------------------------------
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/test/test_py_compile.py", line 22, in wrapper
    return fxn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/test/test_compileall.py", line 331, in test_ddir_multiple_workers
    return self._test_ddir_only(ddir="<a prefix>", parallel=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/test/test_compileall.py", line 320, in _test_ddir_only
    mod_code_obj = test.test_importlib.util.get_code_from_pyc(modcode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'test.test_importlib' has no attribute 'util'

======================================================================
ERROR: test_ddir_only_one_worker (test.test_compileall.CompileallTestsWithoutSourceEpoch.test_ddir_only_one_worker)
Recursive compile_dir ddir= contains package paths; bpo39769.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/test/test_py_compile.py", line 22, in wrapper
    return fxn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/test/test_compileall.py", line 326, in test_ddir_only_one_worker
    return self._test_ddir_only(ddir="<a prefix>", parallel=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/test/test_compileall.py", line 320, in _test_ddir_only
    mod_code_obj = test.test_importlib.util.get_code_from_pyc(modcode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'test.test_importlib' has no attribute 'util'

----------------------------------------------------------------------

#108952 fixes all such cases.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 5, 2023
…blem (pythonGH-108929)

(cherry picked from commit 3f89b25)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Yhg1s pushed a commit that referenced this issue Sep 5, 2023
…oblem (GH-108929) (#108954)

gh-108927: Fix test_import + test_importlib + test_unittest problem (GH-108929)
(cherry picked from commit 3f89b25)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@lordmauve
Copy link
Contributor

I am also seeing this in the 3.11.6 release and can confirm that #108929 fixes it.

@lordmauve
Copy link
Contributor

Actually when I run just ./python -m test test_import test_importlib test_unittest I see this in 3.11.5 and 3.11.4 as well. But 3.11.4's test suite is succeeding on our CI. 3.11.6 is not.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 4, 2023
…blem (pythonGH-108929)

(cherry picked from commit 3f89b25)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
vstinner pushed a commit that referenced this issue Oct 4, 2023
…oblem (GH-108929) (#110347)

gh-108927: Fix test_import + test_importlib + test_unittest problem (GH-108929)
(cherry picked from commit 3f89b25)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@iritkatriel iritkatriel added the tests Tests in the Lib/test dir label Nov 26, 2023
serhiy-storchaka added a commit that referenced this issue Dec 4, 2023
It breaks import machinery if the test module has submodules used in
other tests.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 4, 2023
…nGH-108952)

It breaks import machinery if the test module has submodules used in
other tests.
(cherry picked from commit e08b70f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 4, 2023
…nGH-108952)

It breaks import machinery if the test module has submodules used in
other tests.
(cherry picked from commit e08b70f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Dec 4, 2023
…08952) (ПР-112712)

It breaks import machinery if the test module has submodules used in
other tests.
(cherry picked from commit e08b70f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Dec 4, 2023
…08952) (ПР-112711)

It breaks import machinery if the test module has submodules used in
other tests.
(cherry picked from commit e08b70f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@vstinner
Copy link
Member

vstinner commented Dec 4, 2023

@serhiy-storchaka serhiy-storchaka closed this as completed Dec 4, 2023

Nice!

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Dec 5, 2023
serhiy-storchaka added a commit that referenced this issue Dec 6, 2023
…112765)

Co-authored-by: Victor Stinner <vstinner@python.org>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Dec 6, 2023
…allation (pythonGH-112765)

(cherry picked from commit f8c0198)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Dec 6, 2023
…allation (pythonGH-112765)

(cherry picked from commit f8c0198)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
serhiy-storchaka added a commit that referenced this issue Dec 6, 2023
…on (GH-112765) (GH-112784)

(cherry picked from commit f8c0198)

Co-authored-by: Victor Stinner <vstinner@python.org>
serhiy-storchaka added a commit that referenced this issue Dec 6, 2023
…on (GH-112765) (GH-112785)

(cherry picked from commit f8c0198)

Co-authored-by: Victor Stinner <vstinner@python.org>
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
…nGH-108952)

It breaks import machinery if the test module has submodules used in
other tests.
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants