Skip to content

gh-76595: Add tests for PyCapsule_Import()#154588

Open
serhiy-storchaka wants to merge 4 commits into
python:mainfrom
serhiy-storchaka:capsule-import-tests
Open

gh-76595: Add tests for PyCapsule_Import()#154588
serhiy-storchaka wants to merge 4 commits into
python:mainfrom
serhiy-storchaka:capsule-import-tests

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jul 24, 2026

Copy link
Copy Markdown
Member

PyCapsule_Import() was only exercised by the best-effort loop in the test_capsule C self-test.

Add _testcapi helpers to create a capsule with an arbitrary name and to call PyCapsule_Import() from Python, and tests covering the current behavior, including error cases and non-ASCII, non-UTF-8 and NULL names.

🤖 Generated with Claude Code

Add _testcapi helpers to create a capsule with an arbitrary name and to call PyCapsule_Import(), and Python tests covering the current behavior: only the first component of the dotted name is imported, the rest are attribute lookups.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) July 24, 2026 10:28
@@ -0,0 +1,63 @@
#include "parts.h"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems like the whole PyCapsule C API is part of the limited C API, so you can move this file to Modules/_testlimitedcapi/.

return NULL;
}
char *name_copy = NULL;
if (name != NULL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In which case name can be NULL? I expect that with the current code, name is always non-NULL.

It means that name_copy is always non-NULL when pointer is initialized, and so that the &dummy code path is never taken.

If name can be NULL, calling capsule_destructor() on &dummy seems to be wrong to me.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It can be NULL, the code supports this. Although it is not too useful. But the wrapper should not restrict what can be passed to the C API.

capsule_destructor() is a no-op if name is NULL. free() is not called on &dummy, it is called on PyCapsule_GetName(op).

Comment thread Lib/test/test_capi/test_capsule.py Outdated
Comment thread Lib/test/test_capi/test_capsule.py Outdated
Comment on lines +158 to +159
self.assertRaises(ImportError,
_testcapi.PyCapsule_Import, 'capsule_broken.capsule')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hum, I would prefer to test also the exception message since this case is a little bit special.

Suggested change
self.assertRaises(ImportError,
_testcapi.PyCapsule_Import, 'capsule_broken.capsule')
with self.assertRaises(ImportError) as cm:
_testcapi.PyCapsule_Import('capsule_broken.capsule')
self.assertEqual(str(cm.exception),
'PyCapsule_Import could not import '
'module "capsule_broken"')

Comment thread Misc/NEWS.d/next/Tests/2026-07-24-12-00-00.gh-issue-76595.KfF5xR.rst Outdated
serhiy-storchaka and others added 2 commits July 24, 2026 13:48
Co-authored-by: Victor Stinner <vstinner@python.org>
The PyCapsule C API is part of the limited C API.
Also assert the messages of the ImportError raised by PyCapsule_Import() and fix the check_import() helper after applying the review suggestion.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants