Skip to content

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

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:cfunc_macros
May 5, 2022
Merged

gh-89653: PEP 670: Convert PyCFunction macros to functions#92302
vstinner merged 1 commit into
python:mainfrom
vstinner:cfunc_macros

Conversation

@vstinner

@vstinner vstinner commented May 4, 2022

Copy link
Copy Markdown
Member

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

vstinner commented May 4, 2022

Copy link
Copy Markdown
Member Author

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

encukou commented May 4, 2022

Copy link
Copy Markdown
Member

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

vstinner commented May 5, 2022

Copy link
Copy Markdown
Member Author

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

encukou commented May 23, 2022

Copy link
Copy Markdown
Member

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