FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-105059: Remove anonymous union from PyObject by eduardo-elizondo · Pull Request #105275 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (3) .h  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions Include/Python.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
# define _SGI_MP_SOURCE
#endif

// stdlib.h, stdio.h, errno.h and string.h headers are not used by Python
// stdlib.h, stdio.h, and errno.h headers are not used by Python
// headers, but kept for backward compatibility. They are excluded from the
// limited C API of Python 3.11.
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
# include <stdlib.h>
# include <stdio.h> // FILE*
# include <errno.h> // errno
# include <string.h> // memcpy()
#endif
#ifndef MS_WINDOWS
# include <unistd.h>
Expand All @@ -34,6 +33,7 @@

#include <assert.h> // assert()
#include <wchar.h> // wchar_t
#include <string.h> // memcpy()

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It makes me sad to use memcpy() in the limited C API which should attempt to hide as many implementation details as possible. I don't get how the immortal object change interacts with the stable ABI. I would prefer to convert Py_INCREF/Py_DECREF to opaque function calls. It would avoid any C and C++ compatibility issue. At least, it would make the ABI a little bit more "forward compatible".

But that can be done later, since the priority is to unblock the next Python 3.12 beta release.

By the way, the nogil fork has also a completely different implementation which is also ABI incompatible unless these functions are implemented as opaque function calls.


#include "pyport.h"
#include "pymacro.h"
Expand Down
15 changes: 5 additions & 10 deletions Include/object.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ check by comparing the reference count field to the immortality reference count.
#define PyObject_HEAD_INIT(type) \
{ \
_PyObject_EXTRA_INIT \
{ _Py_IMMORTAL_REFCNT }, \
_Py_IMMORTAL_REFCNT, \
(type) \
},
#else
#define PyObject_HEAD_INIT(type) \
{ \
_PyObject_EXTRA_INIT \
{ 1 }, \
1, \
(type) \
},
#endif /* Py_BUILD_CORE */
Expand All @@ -165,12 +165,7 @@ check by comparing the reference count field to the immortality reference count.
*/
struct _object {
_PyObject_HEAD_EXTRA
union {
Py_ssize_t ob_refcnt;
#if SIZEOF_VOID_P > 4
PY_UINT32_T ob_refcnt_split[2];
#endif
};
Py_ssize_t ob_refcnt;
PyTypeObject *ob_type;
};

Expand Down Expand Up @@ -624,12 +619,12 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
// directly PyObject.ob_refcnt.
#if SIZEOF_VOID_P > 4
// Portable saturated add, branching on the carry flag and set low bits
PY_UINT32_T cur_refcnt = op->ob_refcnt_split[PY_BIG_ENDIAN];
PY_UINT32_T cur_refcnt = _Py_CAST(PY_UINT32_T, op->ob_refcnt);
PY_UINT32_T new_refcnt = cur_refcnt + 1;
if (new_refcnt == 0) {
return;
}
op->ob_refcnt_split[PY_BIG_ENDIAN] = new_refcnt;
memcpy(&op->ob_refcnt, &new_refcnt, sizeof(new_refcnt));

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Isn't this require the little-endian?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Maybe move the union here, use memcpy() and only use the bits that you need in the union?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The problem of the current implementation (without this change) is the usage of anonymous union in PyObject: it is not compatible with (strict) C99. A named union is fine with C89 and C99. It's more about finding the right syntax to get the most efficient code.

#else
// Explicitly check immortality against the immortal value
if (_Py_IsImmortal(op)) {
Expand Down
4 changes: 2 additions & 2 deletions Objects/object.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,7 @@ PyTypeObject _PyNone_Type = {

PyObject _Py_NoneStruct = {
_PyObject_EXTRA_INIT
{ _Py_IMMORTAL_REFCNT },
_Py_IMMORTAL_REFCNT,
&_PyNone_Type
};

Expand Down Expand Up @@ -1979,7 +1979,7 @@ PyTypeObject _PyNotImplemented_Type = {

PyObject _Py_NotImplementedStruct = {
_PyObject_EXTRA_INIT
{ _Py_IMMORTAL_REFCNT },
_Py_IMMORTAL_REFCNT,
&_PyNotImplemented_Type
};

Expand Down
2 changes: 1 addition & 1 deletion Objects/setobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2544,6 +2544,6 @@ static PyTypeObject _PySetDummy_Type = {

static PyObject _dummy_struct = {
_PyObject_EXTRA_INIT
{ _Py_IMMORTAL_REFCNT },
_Py_IMMORTAL_REFCNT,
&_PySetDummy_Type
};
2 changes: 1 addition & 1 deletion Objects/sliceobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ PyTypeObject PyEllipsis_Type = {

PyObject _Py_EllipsisObject = {
_PyObject_EXTRA_INIT
{ _Py_IMMORTAL_REFCNT },
_Py_IMMORTAL_REFCNT,
&PyEllipsis_Type
};

Expand Down

Back | FazBrowse Home | New Git URL