Skip to content

test_sys.test_intern() fails if run more than once in the same process #108963

Closed

Description

@vstinner

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    testsTests in the Lib/test dir
    on Sep 5, 2023
  2. vstinner commented on Sep 6, 2023

    @vstinner
    MemberAuthor

    @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 ;-)

  3. serhiy-storchaka commented on Sep 6, 2023

    @serhiy-storchaka
    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.

  4. aisk commented on Sep 16, 2023

    @aisk
    Member

    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
    
  5. 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[/+] on Sep 18, 2023
  6. added a commit that references this issue on Oct 2, 2023
  7. added a commit that references this issue on Oct 2, 2023
  8. added a commit that references this issue on Oct 2, 2023
  9. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions