Skip to content

bpo-39573: Convert Py_REFCNT and Py_SIZE to functions - #20429

Merged
vstinner merged 2 commits into
python:masterfrom
vstinner:py_refnct_func
May 27, 2020
Merged

vstinner merged 2 commits into
python:masterfrom
vstinner:py_refnct_func

Conversation

@vstinner

@vstinner vstinner commented May 26, 2020 •

Copy link
Copy Markdown
Member

Convert Py_REFCNT() and Py_SIZE() macros to static inline functions.
They cannot be used as l-value anymore: use Py_SET_REFCNT() and
Py_SET_SIZE() to set an object reference count and size.

Replace &Py_SIZE(self) with &((PyVarObject*)self)->ob_size
in arraymodule.c.

This change is backward incompatible on purpose, to prepare the C API
for a fully opaque PyObject structure.

https://bugs.python.org/issue39573

Convert Py_REFCNT() and Py_SIZE() macros to static inline functions.
They cannot be used as l-value anymore: use Py_SET_REFCNT() and
Py_SET_SIZE() to set an object reference count and size.

Replace &Py_SIZE(self) with &((PyVarObject*)self)->ob_size
in arraymodule.c.

This change is backward incompatible on purpose, to prepare the C API
for an opaque PyObject structure.

@corona10 corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Objects/tupleobject.c:85:21: error: expression is not assignable
        Py_SIZE(op) = size;
        ~~~~~~~~~~~ ^

@corona10 corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I left comment ;)

@bedevere-bot

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@vstinner

Copy link
Copy Markdown
Member Author

Objects/tupleobject.c:85:21: error: expression is not assignable

Oh, Py_TRACE_REFS! Thanks for testing for me. It's now fixed.

@corona10 corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@vstinner

Copy link
Copy Markdown
Member Author

This PR breaks Cython. I proposed cython/cython#3639 to use Py_SET_SIZE(): PR for the Cython master branch (then I will have to backport it to the 0.29.x branch). Maybe we should wait for a new Cython release compatible with this PR, before merging this PR.

@vstinner

Copy link
Copy Markdown
Member Author

This PR breaks Cython. I proposed cython/cython#3639 to use Py_SET_SIZE(): PR for the Cython master branch (then I will have to backport it to the 0.29.x branch).

My fix landed in 0.29.x and master branches of Cython, great!

I asked cython/cython#3639 (comment) if a Cython 0.29.20 version will be released with the fix.

In the meanwhile, I think that it's fine to break Cython for a few days/weeks since we are at the beginning of the Python 3.10 dev cycle. So I merge my PR to be able to move on other C API issues.

@vstinner
vstinner merged commit fe2978b into python:master May 27, 2020
@vstinner
vstinner deleted the py_refnct_func branch May 27, 2020 12:55
tacaswell added a commit to tacaswell/numpy that referenced this pull request May 29, 2020
In python/cpython#20290 CPython changed
`Py_TYPE` from a macro to an inline function.  This requires a code
change to us `Py_SET_TYPE` instead when using `Py_TYPE()` as a lvalue
in c code.

In python/cpython#20429 CPython changed
`Py_SIZE` from a macro to an inline function.  This requires a code
change to us `Py_SET_SIZE` instead of using `Py_SIZE` as a lvalue in c
code.
charris pushed a commit to charris/numpy that referenced this pull request May 29, 2020
In python/cpython#20290 CPython changed
`Py_TYPE` from a macro to an inline function.  This requires a code
change to us `Py_SET_TYPE` instead when using `Py_TYPE()` as a lvalue
in c code.

In python/cpython#20429 CPython changed
`Py_SIZE` from a macro to an inline function.  This requires a code
change to us `Py_SET_SIZE` instead of using `Py_SIZE` as a lvalue in c
code.
cielavenir pushed a commit to cielavenir/numpy that referenced this pull request Aug 9, 2023
In python/cpython#20290 CPython changed
`Py_TYPE` from a macro to an inline function.  This requires a code
change to us `Py_SET_TYPE` instead when using `Py_TYPE()` as a lvalue
in c code.

In python/cpython#20429 CPython changed
`Py_SIZE` from a macro to an inline function.  This requires a code
change to us `Py_SET_SIZE` instead of using `Py_SIZE` as a lvalue in c
code.
cielavenir pushed a commit to cielavenir/numpy that referenced this pull request Aug 22, 2023
In python/cpython#20290 CPython changed
`Py_TYPE` from a macro to an inline function.  This requires a code
change to us `Py_SET_TYPE` instead when using `Py_TYPE()` as a lvalue
in c code.

In python/cpython#20429 CPython changed
`Py_SIZE` from a macro to an inline function.  This requires a code
change to us `Py_SET_SIZE` instead of using `Py_SIZE` as a lvalue in c
code.
cielavenir pushed a commit to cielavenir/numpy that referenced this pull request Nov 5, 2025
In python/cpython#20290 CPython changed
`Py_TYPE` from a macro to an inline function.  This requires a code
change to us `Py_SET_TYPE` instead when using `Py_TYPE()` as a lvalue
in c code.

In python/cpython#20429 CPython changed
`Py_SIZE` from a macro to an inline function.  This requires a code
change to us `Py_SET_SIZE` instead of using `Py_SIZE` as a lvalue in c
code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants