Skip to content

[3.13] gh-128911: Add tests on the PyImport C API (#128915) - #128960

Merged
vstinner merged 1 commit into
python:3.13from
vstinner:test_capi_import13
Jan 17, 2025
Merged

vstinner merged 1 commit into
python:3.13from
vstinner:test_capi_import13

Conversation

@vstinner

@vstinner vstinner commented Jan 17, 2025 •

Copy link
Copy Markdown
Member
  • Add Modules/_testlimitedcapi/import.c
  • Add Lib/test/test_capi/test_import.py
  • Remove _testcapi.check_pyimport_addmodule(): tests already covered by newly added tests.

Co-authored-by: Serhiy Storchaka storchaka@gmail.com
(cherry picked from commit d95ba9f)

@vstinner

Copy link
Copy Markdown
Member Author

@serhiy-storchaka: In Python 3.13, non-string names lead to crashes. I had to remove these tests:

diff --git a/Lib/test/test_capi/test_import.py b/Lib/test/test_capi/test_import.py
index 94f96728d91..3abd1cd7470 100644
--- a/Lib/test/test_capi/test_import.py
+++ b/Lib/test/test_capi/test_import.py
@@ -70,8 +70,6 @@ def check_addmodule(self, add_module, accept_nonstr=False):
         names = ['nonexistent']
         if accept_nonstr:
             names.append(b'\xff')  # non-UTF-8
-            # PyImport_AddModuleObject() accepts non-string names
-            names.append(tuple(['hashable non-string']))
         for name in names:
             with self.subTest(name=name):
                 self.assertNotIn(name, sys.modules)
@@ -311,9 +309,6 @@ def test_executecodemoduleobject(self):
 
         code = compile('attr = 1', '<test>', 'exec')
         self.assertRaises(TypeError, execute_code_func, [], code, NULL, NULL)
-        nonstring = tuple(['hashable non-string'])
-        self.assertRaises(AttributeError, execute_code_func, nonstring, code, NULL, NULL)
-        sys.modules.pop(nonstring, None)
         # CRASHES execute_code_func(NULL, code, NULL, NULL)
         # CRASHES execute_code_func(name, NULL, NULL, NULL)
 
@vstinner vstinner added the needs backport to 3.12 only security fixes label Jan 17, 2025
* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
(cherry picked from commit d95ba9f)
@vstinner
vstinner merged commit 34ded1a into python:3.13 Jan 17, 2025
@vstinner
vstinner deleted the test_capi_import13 branch January 17, 2025 21:44
@miss-islington-app

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 34ded1a1a10204635cad27830fcbee2f8547e8ed 3.12
@vstinner

Copy link
Copy Markdown
Member Author

The change cannot be backported to 3.12: Modules/_testlimitedcapi.c doesn't exist in the 3.12 branch.

@vstinner vstinner removed the needs backport to 3.12 only security fixes label Jan 17, 2025
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Jan 18, 2025
…nGH-128915) (pythonGH-128960)

pythongh-128911: Add tests on the PyImport C API (pythonGH-128915)

* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.
(cherry picked from commit 34ded1a)

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
(cherry picked from commit d95ba9f)
@bedevere-app

bedevere-app Bot commented Jan 18, 2025

Copy link
Copy Markdown

GH-128989 is a backport of this pull request to the 3.12 branch.

vstinner added a commit that referenced this pull request Jan 19, 2025
) (#128989)

* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.
(cherry picked from commit 34ded1a)

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
(cherry picked from commit d95ba9f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant