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

gh-116303: Skip test module dependent tests if test modules are disabled #116307

Closed
wants to merge 40 commits into from

Conversation

erlend-aasland
Copy link
Contributor

@erlend-aasland erlend-aasland commented Mar 4, 2024

@encukou
Copy link
Member

encukou commented Mar 4, 2024

Would it be worth it to skip only the affected assertions, rather than the whole test method?

@erlend-aasland
Copy link
Contributor Author

Would it be worth it to skip only the affected assertions, rather than the whole test method?

Yes, I think so. I'll split them up.

@erlend-aasland erlend-aasland changed the title gh-116303: Skip some sqlite3 tests if testcapi is unavailable gh-116303: Skip test module dependent tests if test modules are disabled Mar 4, 2024
@erlend-aasland erlend-aasland marked this pull request as draft March 4, 2024 13:54
- Lib/test/support/__init__.py
- Lib/test/test_audit.py
- Lib/test/test_gdb/test_misc.py
- Lib/test/test_inspect/test_inspect.py
- Lib/test/test_pydoc/test_pydoc.py
@erlend-aasland
Copy link
Contributor Author

@encukou: I started fixing some of the remaining tests, but I won't be back at my laptop until tonight. Feel free to pick this up in the mean time.

@erlend-aasland
Copy link
Contributor Author

@serhiy-storchaka, would you like to join in on finishing this PR? There are still a lot of test failures1, but I'm slowly getting there. For example, test_importlib still fails; it might be because I introduced a new bug, it might be because we need more skips, or it might be because we uncovered latent test bugs. It is hard to tell right now2.

Footnotes

  1. when testing with --disable-test-modules only; unfortunately, they do not show up in CI nor in buildbots, since we do not test with this configure option (yet)

  2. the amount of tests annotated with docstrings and/or sprinkled with print()s do not make this job easier; ideally we should clean up the whole test suite so running python -m test -v generated usable output. test_threading, for example, is sometimes so verbose that it is hard to tell signal from noise.

Comment on lines +511 to 513
return unittest.skip("_testinternalcapi required")
config = _testinternalcapi.get_config()
return not bool(config['code_debug_ranges'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it return a boolean (which is not callable) or a function (which always true)? How this helper is supposed to be used?

@@ -108,7 +108,7 @@ def setUp(self):
)
finder = self.machinery.FileFinder(None)
self.spec = importlib.util.find_spec(self.name)
self.assertIsNotNone(self.spec)
assert self.spec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why skip this in optimized mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaraco requested to leave the assert's for importlib tests, so I reverted the unittest assert style changes. The code is shared with an external repo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - I only care about the retention of bare asserts in the tests for importlib.metadata and importlib.resources. Unfortunately, the tests for importlib.metadata are interspersed with other importlib tests, so it's not easy to know which are which without looking at the cpython branch of importlib_metadata. I do have plans to move importlib.metadata tests into their own package so they're clearly separated.

@vstinner
Copy link
Member

vstinner commented Mar 7, 2024

About the test_embed bug, it seems like importing _ctypes is enough to trigger the bug which looks like a memory corruption:

vstinner@WIN C:\victor\python\main>type bug.py
import subprocess
program = r"PCbuild\amd64\_testembed_d.exe"
cmd = [program, "test_repeated_init_exec", "import _ctypes"]
subprocess.run(cmd)

vstinner@WIN C:\victor\python\main>python bug.py
Running Debug|x64 interpreter...
--- Loop #1 ---
--- Loop #2 ---
--- Loop #3 ---
--- Loop #4 ---

vstinner@WIN C:\victor\python\main>python bug.py
Running Debug|x64 interpreter...
--- Loop #1 ---
--- Loop #2 ---
--- Loop #3 ---
--- Loop #4 ---

vstinner@WIN C:\victor\python\main>python bug.py
Running Debug|x64 interpreter...
--- Loop #1 ---
--- Loop #2 ---
--- Loop #3 ---
--- Loop #4 ---

vstinner@WIN C:\victor\python\main>python bug.py
Running Debug|x64 interpreter...
--- Loop #1 ---
--- Loop #2 ---
--- Loop #3 ---
--- Loop #4 ---

vstinner@WIN C:\victor\python\main>python bug.py
Running Debug|x64 interpreter...
--- Loop #1 ---
--- Loop #2 ---
Assertion failed: PyUnicode_CheckExact(ep->me_key), file C:\victor\python\main\Objects\dictobject.c, line 1101

@vstinner
Copy link
Member

vstinner commented Mar 7, 2024

About the test_embed bug, it seems like importing _ctypes is enough to trigger the bug which looks like a memory corruption

I can reproduce the bug on the main branch, without these changes. Use this patch to make the bug more likely:

diff --git a/Programs/_testembed.c b/Programs/_testembed.c
index 30998bf80f..e8f36ae442 100644
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -38,7 +38,7 @@ char **main_argv;
 /* Use path starting with "./" avoids a search along the PATH */
 #define PROGRAM_NAME L"./_testembed"

-#define INIT_LOOPS 4
+#define INIT_LOOPS 12

 // Ignore Py_DEPRECATED() compiler warnings: deprecated functions are
 // tested on purpose here.

Example of crash:

vstinner@WIN C:\victor\python\main>python bug.py    
Running Debug|x64 interpreter...
--- Loop #1 ---
--- Loop #2 ---
--- Loop #3 ---
--- Loop #4 ---
--- Loop #5 ---
--- Loop #6 ---
--- Loop #7 ---
--- Loop #8 ---
--- Loop #9 ---
--- Loop #10 ---
--- Loop #11 ---
--- Loop #12 ---

vstinner@WIN C:\victor\python\main>python bug.py
Running Debug|x64 interpreter...
--- Loop #1 ---
--- Loop #2 ---
--- Loop #3 ---
Assertion failed: PyUnicode_CheckExact(ep->me_key), file C:\victor\python\main\Objects\dictobject.c, line 1101

@vstinner
Copy link
Member

vstinner commented Mar 7, 2024

I can reproduce the bug on the main branch, without these changes.

I tried to dig into Python history: the bug exists since July 1st, 2023 at least. I failed to go further in the past with git bisect, around June 1st, 2023, the test fails at importing _ctypes at the second loop iteration!

vstinner@WIN C:\victor\python\main>python bug.py  
Running Debug|x64 interpreter...
   == Process #1 ===
--- Loop #1 ---
--- Loop #2 ---
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named '_ctypes'
=> exitcode 1

Latest version of my bug.py:

import subprocess
program = r"PCbuild\amd64\_testembed_d.exe"
cmd = [program, "test_repeated_init_exec", "import _ctypes"]

for i in range(1, 11):
    print(f"   == Process #{i} ===")
    proc = subprocess.run(cmd)
    exitcode = proc.returncode
    print(f"=> exitcode {exitcode}")
    if exitcode:
        break

@vstinner
Copy link
Member

vstinner commented Mar 7, 2024

I created #116467 for the test_embed crash: Initialize/Finalize Python multiple time and import _ctypes each time lead to a memory corruption.

@serhiy-storchaka
Copy link
Member

@vstinner, could you please open a separate issue for this and copy the results of researches there?

@erlend-aasland
Copy link
Contributor Author

Thanks for your help, everyone. I'm going to close this PR and split it up in multiple parts.

@erlend-aasland erlend-aasland deleted the sqlite/testcapi branch March 7, 2024 22:41
jaraco added a commit that referenced this pull request Mar 12, 2024
gh-116307: Create a new import helper 'isolated modules' and use that instead of 'Clean Import' to ensure that tests from importlib_resources don't leave modules in sys.modules.
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 12, 2024
…ythonGH-116680)

(cherry picked from commit a254807)

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
pythongh-116307: Create a new import helper 'isolated modules' and use that instead of 'Clean Import' to ensure that tests from importlib_resources don't leave modules in sys.modules.
pablogsal pushed a commit to pablogsal/cpython that referenced this pull request Mar 12, 2024
… tests (pythonGH-116680) (python#116684)

pythongh-116307: Proper fix for 'mod' leaking across importlib tests (pythonGH-116680)
(cherry picked from commit a254807)


pythongh-116307: Create a new import helper 'isolated modules' and use that instead of 'Clean Import' to ensure that tests from importlib_resources don't leave modules in sys.modules.

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
jaraco added a commit that referenced this pull request Mar 13, 2024
#116694)

[3.11] gh-116307: Proper fix for 'mod' leaking across importlib tests (GH-116680)
(cherry picked from commit a254807)


gh-116307: Create a new import helper 'isolated modules' and use that instead of 'Clean Import' to ensure that tests from importlib_resources don't leave modules in sys.modules.
jaraco added a commit to jaraco/cpython that referenced this pull request Mar 13, 2024
… tests (pythonGH-116680)

(cherry picked from commit a254807)

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
pythongh-116307: Create a new import helper 'isolated modules' and use that instead of 'Clean Import' to ensure that tests from importlib_resources don't leave modules in sys.modules.
vstinner pushed a commit to vstinner/cpython that referenced this pull request Mar 20, 2024
…ython#116680)

pythongh-116307: Create a new import helper 'isolated modules' and use that instead of 'Clean Import' to ensure that tests from importlib_resources don't leave modules in sys.modules.
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
…ython#116680)

pythongh-116307: Create a new import helper 'isolated modules' and use that instead of 'Clean Import' to ensure that tests from importlib_resources don't leave modules in sys.modules.
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
…ython#116680)

pythongh-116307: Create a new import helper 'isolated modules' and use that instead of 'Clean Import' to ensure that tests from importlib_resources don't leave modules in sys.modules.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting merge needs backport to 3.11 only security fixes needs backport to 3.12 bug and security fixes skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants