Skip to content

Commit 7232ffe

Browse files
committed
Export GraalPy functions for refount/type/size
1 parent 9001498 commit 7232ffe

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

graalpython/com.oracle.graal.python.cext/include/object.h

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ check by comparing the reference count field to the immortality reference count.
172172
*/
173173
struct _object {
174174
_PyObject_HEAD_EXTRA
175+
175176
#if (defined(__GNUC__) || defined(__clang__)) \
176177
&& !(defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L)
177178
// On C99 and older, anonymous union is a GCC and clang extension
@@ -210,28 +211,22 @@ typedef struct {
210211

211212
// Test if the 'x' object is the 'y' object, the same as "x is y" in Python.
212213
PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
213-
#if 0 // GraalPy change
214-
#define Py_Is(x, y) ((x) == (y))
215-
#endif // GraalPy change
214+
// GraalPy change: call function
215+
#define Py_Is(x, y) (Py_Is(x, y))
216216

217-
218-
PyAPI_FUNC(Py_ssize_t) GraalPyPrivate_REFCNT(PyObject *ob);
219-
static inline Py_ssize_t Py_REFCNT(PyObject *ob) {
220-
return GraalPyPrivate_REFCNT(ob);
221-
}
217+
// GraalPy change: backported declaration from CPython 3.14
218+
// Py_REFCNT() implementation for the stable ABI
219+
PyAPI_FUNC(Py_ssize_t) Py_REFCNT(PyObject *ob);
222220
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
223221
# define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST(ob))
224222
#endif
225223

226224

225+
// GraalPy public API, mainly for non-C languages that can't use macros
226+
PyAPI_FUNC(PyTypeObject*) GraalPy_TYPE(PyObject *ob);
227227
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
228-
PyAPI_FUNC(PyTypeObject*) GraalPyPrivate_TYPE(PyObject *ob);
229228
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
230-
#if defined(GRAALVM_PYTHON) && defined(NDEBUG)
231-
return (pointer_to_stub(ob)->ob_type);
232-
#else
233-
return GraalPyPrivate_TYPE(ob);
234-
#endif
229+
return GraalPy_TYPE(ob);
235230
}
236231
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
237232
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
@@ -240,12 +235,13 @@ static inline PyTypeObject* Py_TYPE(PyObject *ob) {
240235
PyAPI_DATA(PyTypeObject) PyLong_Type;
241236
PyAPI_DATA(PyTypeObject) PyBool_Type;
242237

238+
// GraalPy public API, mainly for non-C languages that can't use macros
239+
PyAPI_FUNC(Py_ssize_t) GraalPy_SIZE(PyObject *ob);
243240
// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
244-
PyAPI_FUNC(Py_ssize_t) GraalPyPrivate_SIZE(PyObject *ob);
245241
static inline Py_ssize_t Py_SIZE(PyObject *ob) {
246242
assert(Py_TYPE(ob) != &PyLong_Type);
247243
assert(Py_TYPE(ob) != &PyBool_Type);
248-
return GraalPyPrivate_SIZE(ob);
244+
return GraalPy_SIZE(ob);
249245
}
250246
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
251247
# define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob))
@@ -273,22 +269,19 @@ static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
273269
#endif
274270

275271

276-
PyAPI_FUNC(void) GraalPyPrivate_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt);
272+
// GraalPy change: backported declaration from 3.14
273+
// Py_SET_REFCNT() implementation for stable ABI
274+
PyAPI_FUNC(void) _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt);
275+
277276
static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
278-
// This immortal check is for code that is unaware of immortal objects.
279-
// The runtime tracks these objects and we should avoid as much
280-
// as possible having extensions inadvertently change the refcnt
281-
// of an immortalized object.
282-
if (_Py_IsImmortal(ob)) {
283-
return;
284-
}
285-
GraalPyPrivate_SET_REFCNT(ob, refcnt);
277+
_Py_SetRefcnt(ob, refcnt);
286278
}
287279
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
288280
# define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt))
289281
#endif
290282

291283

284+
// GraalPy public API, mainly for non-C languages that can't use macros
292285
PyAPI_FUNC(void) GraalPy_SET_TYPE(PyObject *ob, PyTypeObject *type);
293286
static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
294287
GraalPy_SET_TYPE(ob, type);
@@ -297,7 +290,7 @@ static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
297290
# define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)
298291
#endif
299292

300-
293+
// GraalPy public API, mainly for non-C languages that can't use macros
301294
PyAPI_FUNC(void) GraalPy_SET_SIZE(PyVarObject *ob, Py_ssize_t size);
302295
static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
303296
GraalPy_SET_SIZE(ob, size);

graalpython/com.oracle.graal.python.cext/src/object.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,7 +2842,8 @@ int Py_IsFalse(PyObject *x)
28422842
}
28432843

28442844
// GraalPy additions
2845-
Py_ssize_t GraalPyPrivate_REFCNT(PyObject *obj) {
2845+
#undef Py_REFCNT
2846+
Py_ssize_t Py_REFCNT(PyObject *obj) {
28462847
Py_ssize_t res;
28472848
if (points_to_py_handle_space(obj))
28482849
{
@@ -2861,12 +2862,19 @@ Py_ssize_t GraalPyPrivate_REFCNT(PyObject *obj) {
28612862
return res;
28622863
}
28632864

2864-
// alias, currently used in PyO3
2865+
// alias, currently used in PyO3, remove after updating to 3.14
28652866
PyAPI_FUNC(Py_ssize_t) _Py_REFCNT(PyObject *obj) {
2866-
return GraalPyPrivate_REFCNT(obj);
2867+
return Py_REFCNT(obj);
28672868
}
28682869

2869-
void GraalPyPrivate_SET_REFCNT(PyObject* obj, Py_ssize_t cnt) {
2870+
void _Py_SetRefcnt(PyObject* obj, Py_ssize_t cnt) {
2871+
// This immortal check is for code that is unaware of immortal objects.
2872+
// The runtime tracks these objects and we should avoid as much
2873+
// as possible having extensions inadvertently change the refcnt
2874+
// of an immortalized object.
2875+
if (_Py_IsImmortal(obj)) {
2876+
return;
2877+
}
28702878
PyObject *dest;
28712879
if (points_to_py_handle_space(obj))
28722880
{
@@ -2885,7 +2893,7 @@ void GraalPyPrivate_SET_REFCNT(PyObject* obj, Py_ssize_t cnt) {
28852893
dest->ob_refcnt = cnt;
28862894
}
28872895

2888-
PyTypeObject* GraalPyPrivate_TYPE(PyObject *a) {
2896+
PyTypeObject* GraalPy_TYPE(PyObject *a) {
28892897
PyTypeObject *res;
28902898
if (points_to_py_handle_space(a))
28912899
{
@@ -2906,10 +2914,10 @@ PyTypeObject* GraalPyPrivate_TYPE(PyObject *a) {
29062914

29072915
// alias, currently used in PyO3
29082916
PyAPI_FUNC(PyTypeObject*) _Py_TYPE(PyObject *obj) {
2909-
return GraalPyPrivate_TYPE(obj);
2917+
return GraalPy_TYPE(obj);
29102918
}
29112919

2912-
Py_ssize_t GraalPyPrivate_SIZE(PyObject *ob) {
2920+
Py_ssize_t GraalPy_SIZE(PyObject *ob) {
29132921
PyVarObject* a = (PyVarObject*)ob;
29142922
Py_ssize_t res;
29152923
if (points_to_py_handle_space(a))
@@ -2942,7 +2950,7 @@ Py_ssize_t GraalPyPrivate_SIZE(PyObject *ob) {
29422950

29432951
// alias, currently used in PyO3
29442952
PyAPI_FUNC(Py_ssize_t) _Py_SIZE(PyObject *obj) {
2945-
return GraalPyPrivate_SIZE(obj);
2953+
return GraalPy_SIZE(obj);
29462954
}
29472955

29482956
void

graalpython/lib-graalpython/patches/nanobind.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ index 0000000..b61486b
907907
+-U __Py_VaBuildValue_SizeT
908908
+-U GraalPyList_ITEMS
909909
+-U GraalPyTuple_ITEMS
910-
+-U GraalPyPrivate_SIZE
910+
+-U GraalPy_SIZE
911911
+-U GraalPyTuple_GET_ITEM
912912
+-U __Py_EllipsisObjectReference
913913
+-U __Py_FalseStructReference

0 commit comments

Comments
 (0)