GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-39573: Convert Py_TYPE() and Py_SIZE() back to macros by vstinner · Pull Request #23366 · python/cpython

bpo-39573: Convert Py_TYPE() and Py_SIZE() back to macros #23366

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.

Sign up for GitHub

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

Merged
vstinner merged 1 commit into python:master from vstinner:revert_py_type
Nov 18, 2020

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-39573: Convert Py_TYPE() and Py_SIZE() back to macros #23366

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
741ed78
bpo-39573: Convert Py_TYPE() and Py_SIZE() back to macros
vstinner Nov 18, 2020
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

8 changes: 2 additions & 6 deletions 8 Doc/c-api/structures.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ the definition of all other Python objects.

Return a :term:`borrowed reference`.

.. versionchanged:: 3.10
:c:func:`Py_TYPE()` is changed to the inline static function.
Use :c:func:`Py_SET_TYPE()` to set an object type.
The :c:func:`Py_SET_TYPE` function must be used to set an object type.


.. c:function:: int Py_IS_TYPE(PyObject *o, PyTypeObject *type)
Expand Down Expand Up @@ -108,9 +106,7 @@ the definition of all other Python objects.

Get the size of the Python object *o*.

.. versionchanged:: 3.10
:c:func:`Py_SIZE()` is changed to the inline static function.
Use :c:func:`Py_SET_SIZE()` to set an object size.
The :c:func:`Py_SET_SIZE` function must be used to set an object size.


.. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size)
Expand Down
11 changes: 0 additions & 11 deletions 11 Doc/whatsnew/3.10.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -489,17 +489,6 @@ Porting to Python 3.10
<arg-parsing>` and the :pep:`353`.
(Contributed by Victor Stinner in :issue:`40943`.)

* Since :c:func:`Py_TYPE()` is changed to the inline static function,
``Py_TYPE(obj) = new_type`` must be replaced with ``Py_SET_TYPE(obj, new_type)``:
see :c:func:`Py_SET_TYPE()` (available since Python 3.9). For backward
compatibility, this macro can be used::

#if PY_VERSION_HEX < 0x030900A4
# define Py_SET_TYPE(obj, type) ((Py_TYPE(obj) = (type)), (void)0)
#endif

(Contributed by Dong-hee Na in :issue:`39573`.)

* Since :c:func:`Py_REFCNT()` is changed to the inline static function,
Comment thread Outdated

@hroncok hroncok Nov 18, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Also this one.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Nov 18, 2020

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I didn't see any bug report about Py_REFCNT() becoming a static inline function, so I prefer to only change Py_TYPE() and Py_SIZE().

Setting manually the reference counter of an object is a very rare use case. I'm only aware of Cython doing that, and I already updated Cython to use Py_SET_REFCNT() internally.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@hroncok hroncok Nov 18, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

OK.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Nov 18, 2020

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Note: I'm open to reverting Py_REFCNT() as well if the Py_REFCNT() change breaks too many third party Python projects.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@hroncok hroncok Nov 18, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ok, I've made a mistake here and I've meant to point out Py_SIZE is not removed. See #23375

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Nov 18, 2020

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Oh sorry, I misunderstood your comment! Thanks for the PR, it's already merged ;-)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
``Py_REFCNT(obj) = new_refcnt`` must be replaced with ``Py_SET_REFCNT(obj, new_refcnt)``:
see :c:func:`Py_SET_REFCNT()` (available since Python 3.9). For backward
Expand Down
13 changes: 4 additions & 9 deletions 13 Include/object.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,11 @@ static inline Py_ssize_t _Py_REFCNT(const PyObject *ob) {
#define Py_REFCNT(ob) _Py_REFCNT(_PyObject_CAST_CONST(ob))


static inline Py_ssize_t _Py_SIZE(const PyVarObject *ob) {
return ob->ob_size;
}
#define Py_SIZE(ob) _Py_SIZE(_PyVarObject_CAST_CONST(ob))

// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
#define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type)

static inline PyTypeObject* _Py_TYPE(const PyObject *ob) {
return ob->ob_type;
}
#define Py_TYPE(ob) _Py_TYPE(_PyObject_CAST_CONST(ob))
// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size)


static inline int _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
Expand Down
4 changes: 4 additions & 0 deletions 4 Misc/NEWS.d/next/C API/2020-11-18-15-21-59.bpo-39573.VB3G2y.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Convert :c:func:`Py_TYPE` and :c:func:`Py_SIZE` back to macros to allow
using them as an l-value. Many third party C extension modules rely on the
ability of using Py_TYPE() and Py_SIZE() to set an object type and size:
``Py_TYPE(obj) = type;`` and ``Py_SIZE(obj) = size;``.
25 changes: 25 additions & 0 deletions 25 Modules/_testcapimodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5612,6 +5612,30 @@ pynumber_tobase(PyObject *module, PyObject *args)

static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);


static PyObject*
test_set_type_size(PyObject* self, PyObject* ignored)
{
PyObject *obj = PyList_New(0);
if (obj == NULL) {
return NULL;
}

// Ensure that following tests don't modify the object,
// to ensure that Py_DECREF() will not crash.
assert(Py_TYPE(obj) == &PyList_Type);
assert(Py_SIZE(obj) == 0);

// bpo-39573: Check that Py_TYPE() and Py_SIZE() can be used
// as l-values to set an object type and size.
Py_TYPE(obj) = &PyList_Type;
Py_SIZE(obj) = 0;

Py_DECREF(obj);
Py_RETURN_NONE;
}


static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
{"raise_memoryerror", raise_memoryerror, METH_NOARGS},
Expand Down Expand Up @@ -5883,6 +5907,7 @@ static PyMethodDef TestMethods[] = {
{"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS},
{"pynumber_tobase", pynumber_tobase, METH_VARARGS},
{"without_gc", without_gc, METH_O},
{"test_set_type_size", test_set_type_size, METH_NOARGS},
{NULL, NULL} /* sentinel */
};

Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.