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-92154: Expose PyCode_GetCode in the C API #92168

Merged
merged 5 commits into from
May 3, 2022

Conversation

Fidget-Spinner
Copy link
Member

@Fidget-Spinner Fidget-Spinner commented May 2, 2022

Fixes #92154.

PyCode_GetCode(PyObject *co)
{
if (!PyCode_Check(co)) {
PyErr_BadInternalCall();
Copy link
Member Author

Choose a reason for hiding this comment

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

I forgot if this or a TypeError is preferred for the C API (or no error setting at all?)

Copy link
Member

Choose a reason for hiding this comment

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

Use PyCode_GetCode(PyCodeObject *co) and then you won't need the check.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would like to, but co_code was previously PyObject*, and I want this to be a drop-in replacement as far as possible.

Copy link
Member

Choose a reason for hiding this comment

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

PyErr_BadInternalCall() is fine, but I would even suggest replacing a runtime check with an assertion. The caller is responsible to pass the right type.

@Fidget-Spinner
Copy link
Member Author

Also I frankly have no clue if we need to update stable_abi.toml. I'm not sure when it counts as stable ABI and when it doesn't.

Doc/c-api/code.rst Outdated Show resolved Hide resolved
Doc/whatsnew/3.11.rst Show resolved Hide resolved
Doc/whatsnew/3.11.rst Outdated Show resolved Hide resolved
Doc/whatsnew/3.11.rst Show resolved Hide resolved
PyCode_GetCode(PyObject *co)
{
if (!PyCode_Check(co)) {
PyErr_BadInternalCall();
Copy link
Member

Choose a reason for hiding this comment

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

PyErr_BadInternalCall() is fine, but I would even suggest replacing a runtime check with an assertion. The caller is responsible to pass the right type.

@vstinner
Copy link
Member

vstinner commented May 3, 2022

Also I frankly have no clue if we need to update stable_abi.toml. I'm not sure when it counts as stable ABI and when it doesn't.

Include/cpython/ is the API excluded from the limited C API and so excluded from the stable ABI: https://devguide.python.org/c-api/

Please don't add this function to the stable ABI yet. Let's wait for one Python release, and then see if it's stable or not.

Doc/c-api/code.rst Outdated Show resolved Hide resolved
@vstinner
Copy link
Member

vstinner commented May 3, 2022

I prepared PR python/pythoncapi-compat#34 to add the function to pythoncapi-compat.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[C-API] Add a function to access PyCodeObject.co_code in C
4 participants