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-89653: PEP 670: Convert PyCFunction macros to functions #92302

Merged
merged 1 commit into from
May 5, 2022
Merged

gh-89653: PEP 670: Convert PyCFunction macros to functions #92302

merged 1 commit into from
May 5, 2022

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented May 4, 2022

Convert the following macros to static inline functions:

  • PyCFunction_GET_CLASS()
  • PyCFunction_GET_FLAGS()
  • PyCFunction_GET_FUNCTION()
  • PyCFunction_GET_SELF()

Limited C API version 3.11 no longer casts arguments.

Export the PyCMethod_GetClass() function.

@vstinner
Copy link
Member Author

vstinner commented May 4, 2022

Export the PyCMethod_GetClass() function.

It seems like it was missed when PEP 573 was implemented in Python 3.9: cc @encukou @ericsnowcurrently @ncoghlan

@encukou
Copy link
Member

encukou commented May 4, 2022

Hm, PyCMethod_GetClass was left in by mistake from the initial implementation. I'd rather remove it than export it.
Per PEP-573, the API for this is PyCFunction_GET_CLASS. No PyCMethod_* functions were planned, all PyCFunction_* should work on PyCMethod as well.
(PyCMethod_Check* were added later, in 4c9ea09)

Convert the following macros to static inline functions:

* PyCFunction_GET_CLASS()
* PyCFunction_GET_FLAGS()
* PyCFunction_GET_FUNCTION()
* PyCFunction_GET_SELF()

Limited C API version 3.11 no longer casts arguments.
@vstinner
Copy link
Member Author

vstinner commented May 5, 2022

Hm, PyCMethod_GetClass was left in by mistake from the initial implementation. I'd rather remove it than export it.

Ah. In that change, I leave it unchanged in my PR which is unrelated.

Per PEP-573, the API for this is PyCFunction_GET_CLASS.

The problem with macros and static inline functions is that they are not usable in some programming languages, and they usually leak too many implementation details to be part of the limited C API. Since each PyCFunction macro or static inline function is paired with a regular function, to me, it looked consistent to expose PyCMethod_GetClass(). But it's up to you ;-)


I'm thinking about deprecating macros and static inline functions, and maybe later make them an alias of their related function. I already did for PyThreadState_GET() which now just calls PyThreadState_Get(). IMO these macros and static inline functions only exist for best performance for CPython internals, but the benefits of using them outside CPython is not significant (in terms of performance). They make sense in the internal C API, not for the regular public C API. But well, that's a whole different topic, I should run benchmarks and write a PEP ;-)

@encukou
Copy link
Member

encukou commented May 23, 2022

The problem with macros and static inline functions is that they are not usable in some programming languages, and they usually leak too many implementation details to be part of the limited C API. Since each PyCFunction macro or static inline function is paired with a regular function, to me, it looked consistent to expose PyCMethod_GetClass(). But it's up to you ;-)

Oh, I'm not insisting on this being a macro. But the function should be PyCFunction_GetClass rather than PyCMethod_*.
(The fact that the struct is different – that is, space for the class pointer isn't allocated – should be an implementation detail.)

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

Successfully merging this pull request may close these issues.

3 participants