Skip to content

Commit 54b6f83

Browse files
committed
WIP: gh-115754: Get singletons via function calls
1 parent 729bfb3 commit 54b6f83

File tree

6 files changed

+25
-6
lines changed

6 files changed

+25
-6
lines changed

Include/boolobject.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ extern "C" {
1717
PyAPI_DATA(PyLongObject) _Py_FalseStruct;
1818
PyAPI_DATA(PyLongObject) _Py_TrueStruct;
1919

20-
/* Use these macros */
21-
#define Py_False _PyObject_CAST(&_Py_FalseStruct)
22-
#define Py_True _PyObject_CAST(&_Py_TrueStruct)
20+
PyAPI_FUNC(PyObject*) Py_GetFalse(void);
21+
PyAPI_FUNC(PyObject*) Py_GetTrue(void);
22+
#define Py_False Py_GetFalse()
23+
#define Py_True Py_GetTrue()
2324

2425
// Test if an object is the True singleton, the same as "x is True" in Python.
2526
PyAPI_FUNC(int) Py_IsTrue(PyObject *x);

Include/object.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,8 @@ _Py_NoneStruct is an object of undefined type which can be used in contexts
10701070
where NULL (nil) is not suitable (since NULL often means 'error').
10711071
*/
10721072
PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
1073-
#define Py_None (&_Py_NoneStruct)
1073+
PyAPI_FUNC(PyObject*) Py_GetNone(void);
1074+
#define Py_None Py_GetNone()
10741075

10751076
// Test if an object is the None singleton, the same as "x is None" in Python.
10761077
PyAPI_FUNC(int) Py_IsNone(PyObject *x);
@@ -1084,7 +1085,8 @@ Py_NotImplemented is a singleton used to signal that an operation is
10841085
not implemented for a given type combination.
10851086
*/
10861087
PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
1087-
#define Py_NotImplemented (&_Py_NotImplementedStruct)
1088+
PyAPI_FUNC(PyObject*) Py_GetNotImplemented(void);
1089+
#define Py_NotImplemented Py_GetNotImplemented()
10881090

10891091
/* Macro for returning Py_NotImplemented from a function */
10901092
#define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented

Include/sliceobject.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ extern "C" {
88

99
PyAPI_DATA(PyObject) _Py_EllipsisObject; /* Don't use this directly */
1010

11-
#define Py_Ellipsis (&_Py_EllipsisObject)
11+
PyAPI_FUNC(PyObject*) Py_GetEllipsis(void);
12+
#define Py_Ellipsis Py_GetEllipsis()
1213

1314
/* Slice object interface */
1415

Objects/longobject.c

+6
Original file line numberDiff line numberDiff line change
@@ -6605,3 +6605,9 @@ Py_ssize_t
66056605
PyUnstable_Long_CompactValue(const PyLongObject* op) {
66066606
return _PyLong_CompactValue(op);
66076607
}
6608+
6609+
PyObject* Py_GetFalse(void)
6610+
{ return _PyObject_CAST(&_Py_FalseStruct); }
6611+
6612+
PyObject* Py_GetTrue(void)
6613+
{ return _PyObject_CAST(&_Py_TrueStruct); }

Objects/object.c

+6
Original file line numberDiff line numberDiff line change
@@ -2970,3 +2970,9 @@ _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt)
29702970
{
29712971
Py_SET_REFCNT(ob, refcnt);
29722972
}
2973+
2974+
PyObject* Py_GetNone(void)
2975+
{ return &_Py_NoneStruct; }
2976+
2977+
PyObject* Py_GetNotImplemented(void)
2978+
{ return &_Py_NotImplementedStruct; }

Objects/sliceobject.c

+3
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,6 @@ PyTypeObject PySlice_Type = {
721721
0, /* tp_alloc */
722722
slice_new, /* tp_new */
723723
};
724+
725+
PyObject* Py_GetEllipsis(void)
726+
{ return &_Py_EllipsisObject; }

0 commit comments

Comments
 (0)