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 macros to functions #99843

Merged
merged 2 commits into from
Nov 28, 2022
Merged

gh-89653: PEP 670: Convert macros to functions #99843

merged 2 commits into from
Nov 28, 2022

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Nov 28, 2022

Convert macros to static inline functions to avoid macro pitfalls,
like duplication of side effects:

  • DK_ENTRIES()
  • DK_UNICODE_ENTRIES()
  • PyCode_GetNumFree()
  • PyFloat_AS_DOUBLE()
  • PyInstanceMethod_GET_FUNCTION()
  • PyMemoryView_GET_BASE()
  • PyMemoryView_GET_BUFFER()
  • PyMethod_GET_FUNCTION()
  • PyMethod_GET_SELF()
  • PySet_GET_SIZE()
  • _PyHeapType_GET_MEMBERS()

Changes:

  • Add also assertions in some functions using "CAST" macro to check
    the type of the arguments when Python is built with assertions
    (debug build).
  • Remove an outdated comment in unicodeobject.h.
  • PyCode_GetNumFree() doesn't cast its argument: the replaced macro
    already required the exact type PyCodeObject*.
  • PyCode_GetNumFree() casts PyCode_GetNumFree.co_nfreevars from int
    to Py_ssize_t to be future proof, and because Py_ssize_t is
    commonly used in the C API.

@vstinner
Copy link
Member Author

cc @erlend-aasland

Convert macros to static inline functions to avoid macro pitfalls,
like duplication of side effects:

* DK_ENTRIES()
* DK_UNICODE_ENTRIES()
* PyCode_GetNumFree()
* PyFloat_AS_DOUBLE()
* PyInstanceMethod_GET_FUNCTION()
* PyMemoryView_GET_BASE()
* PyMemoryView_GET_BUFFER()
* PyMethod_GET_FUNCTION()
* PyMethod_GET_SELF()
* PySet_GET_SIZE()
* _PyHeapType_GET_MEMBERS()

Changes:

* Add also assertions in some functions using "CAST" macro to check
  the type of the arguments when Python is built with assertions
  (debug build).
* Remove an outdated comment in unicodeobject.h.
* PyCode_GetNumFree() doesn't cast its argument: the replaced macro
  already required the exact type PyCodeObject*.
* PyCode_GetNumFree() casts PyCode_GetNumFree.co_nfreevars from int
  to Py_ssize_t to be future proof, and because Py_ssize_t is
  commonly used in the C API.
@vstinner
Copy link
Member Author

I changed PyCode_GetNumFree() return type to Py_ssize_t.

@vstinner
Copy link
Member Author

See also PR #99844.

@vstinner vstinner merged commit 02f72b8 into python:main Nov 28, 2022
@vstinner vstinner deleted the static_inline branch November 28, 2022 15:40
@vstinner
Copy link
Member Author

Merged, thanks for the review @erlend-aasland.

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