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_sys.test_intern() fails if run more than once in the same process #108963

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

Comments

@vstinner
Copy link
Member

vstinner commented Sep 5, 2023

Bug report

Example:

$ ./python -m test test_sys test_sys -m test_intern -v
(...)
0:00:00 load avg: 1.60 Run tests sequentially
0:00:00 load avg: 1.60 [1/2] test_sys
test_intern (test.test_sys.SysModuleTest.test_intern) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
0:00:00 load avg: 1.60 [2/2] test_sys
test_intern (test.test_sys.SysModuleTest.test_intern) ... FAIL

======================================================================
FAIL: test_intern (test.test_sys.SysModuleTest.test_intern)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/vstinner/python/3.12/Lib/test/test_sys.py", line 687, in test_intern
    self.assertTrue(sys.intern(s) is s)
AssertionError: False is not true

----------------------------------------------------------------------
(...)

Result: FAILURE

Linked PRs

@vstinner vstinner added type-bug An unexpected behavior, bug, or error tests Tests in the Lib/test dir labels Sep 5, 2023
@vstinner
Copy link
Member Author

vstinner commented Sep 6, 2023

@serhiy-storchaka: Oh, that another victim of regrtest trying to be nice :-) test_sys pass when run multiple times in the same process using this change:

diff --git a/Lib/test/libregrtest/runtest.py b/Lib/test/libregrtest/runtest.py
index 16ae04191d..249e331434 100644
--- a/Lib/test/libregrtest/runtest.py
+++ b/Lib/test/libregrtest/runtest.py
@@ -436,7 +436,7 @@ def _load_run_test(result: TestResult, ns: Namespace) -> None:
     module_name = abs_module_name(result.test_name, ns.testdir)
 
     # Remove the module from sys.module to reload it if it was already imported
-    sys.modules.pop(module_name, None)
+    #sys.modules.pop(module_name, None)
 
     test_mod = importlib.import_module(module_name)
 

The interesting part is that ./python -m test -R 3:3 test_sys doesn't fail since it doesn't remove the test module between two test iterations ;-)

@serhiy-storchaka
Copy link
Member

For reference, the module level counter was added in aee3486 to prevent test_intern failing when it is rerun in verbose mode. But if the module is reloaded, the counter is reset.

One of solutions is to use the random module. Possibility of collisions in negligible.

Other solution -- use timer in the hope that the computer is not fast enough to finish the whole test several times in the time resolution unit.

We can also try to use id()s of some objects (self, globals(), locals()), but there is greater chance that all of them are allocated at exactly same address after reloading the module.

test_subinterp_intern_dynamically_allocated fails for the same reason.

@aisk
Copy link
Contributor

aisk commented Sep 16, 2023

Trying to change to using random.random() and it looks like it works well: #109491

C:\cpython>python -m test test_sys test_sys
Running Debug|x64 interpreter...
0:00:00 Run 2 tests sequentially
0:00:00 [1/2] test_sys
0:00:04 [2/2] test_sys

== Tests result: SUCCESS ==

All 2 tests OK.

Total duration: 8.9 sec
Total tests: run=148 skipped=12
Total test files: run=2/2
Result: SUCCESS

@vstinner vstinner changed the title sys.test_intern() fails if run more than once in the same process test_sys.test_intern() fails if run more than once in the same process Sep 18, 2023
@vstinner vstinner added needs backport to 3.12 bug and security fixes and removed needs backport to 3.12 bug and security fixes labels Oct 2, 2023
@vstinner vstinner closed this as completed Oct 2, 2023
vstinner pushed a commit to vstinner/cpython that referenced this issue Oct 2, 2023
Yhg1s pushed a commit that referenced this issue Oct 2, 2023
…n test … (#110216)

gh-108963: using random to generate unique string in sys.intern test (#109491)

(cherry picked from commit 44b1e4e)

Co-authored-by: AN Long <[email protected]>
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 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

3 participants