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

chore: use new PyCode API on Python 3.12 #4916

Merged
merged 4 commits into from
Nov 5, 2023
Merged

chore: use new PyCode API on Python 3.12 #4916

merged 4 commits into from
Nov 5, 2023

Conversation

cyyever
Copy link
Contributor

@cyyever cyyever commented Nov 2, 2023

This PR replaces PyObject_GetAttrString with PyCode_GetVarnames which is found by a warning of casting ''_object'' to "PyCodeObject". It also fixes an object leak which was found by code review.

Suggested changelog entry:

* Use new PyCode API on Python 3.12+

@cyyever cyyever marked this pull request as draft November 2, 2023 15:47
@rwgk
Copy link
Collaborator

rwgk commented Nov 2, 2023

Looks good, GHA pass, but I see you marked it as draft. Is there more you want to do here?

@cyyever
Copy link
Contributor Author

cyyever commented Nov 3, 2023

Looks good, GHA pass, but I see you marked it as draft. Is there more you want to do here?

I am checking more possible replacements of PyCode like this.

@cyyever cyyever marked this pull request as ready for review November 3, 2023 03:06
@cyyever
Copy link
Contributor Author

cyyever commented Nov 4, 2023

@rwgk Nothing to add, can review again?

@cyyever cyyever changed the title Use PyCode API Use newer PyCode API and other fixes Nov 4, 2023
PyObject *self_arg = PyTuple_GET_ITEM(co_varnames, 0);
Py_DECREF(co_varnames);
PyObject *self_caller = dict_getitem(locals, self_arg);
Py_DECREF(locals);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Surprised we missed this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Unfortunately this turns out to be incorrect.

I only discovered that when trying to deploy this PR at Google, but in retrospect it's totally clear:

https://docs.python.org/3/c-api/reflection.html#c.PyEval_GetLocals

Return value: Borrowed reference. Part of the Stable ABI.

I'll take care of removing this line again. (I'll report the full error message there.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's #4927 now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I didn't notice the CPython API convention.

@rwgk
Copy link
Collaborator

rwgk commented Nov 5, 2023

Thank you @cyyever!

@rwgk rwgk merged commit f260693 into pybind:master Nov 5, 2023
84 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Nov 5, 2023
@cyyever cyyever deleted the api branch November 6, 2023 00:35
@henryiii henryiii changed the title Use newer PyCode API and other fixes chore: use new PyCode API on Python 3.12 Nov 15, 2023
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Mar 27, 2024
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.

4 participants