mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
gh-115754: Add Py_GetConstant() function (#116883)
Add Py_GetConstant() and Py_GetConstantBorrowed() functions. In the limited C API version 3.13, getting Py_None, Py_False, Py_True, Py_Ellipsis and Py_NotImplemented singletons is now implemented as function calls at the stable ABI level to hide implementation details. Getting these constants still return borrowed references. Add _testlimitedcapi/object.c and test_capi/test_object.py to test Py_GetConstant() and Py_GetConstantBorrowed() functions.
This commit is contained in:
parent
5a76d1be8e
commit
8bea6c411d
22 changed files with 312 additions and 6 deletions
|
|
@ -18,8 +18,13 @@ PyAPI_DATA(PyLongObject) _Py_FalseStruct;
|
|||
PyAPI_DATA(PyLongObject) _Py_TrueStruct;
|
||||
|
||||
/* Use these macros */
|
||||
#define Py_False _PyObject_CAST(&_Py_FalseStruct)
|
||||
#define Py_True _PyObject_CAST(&_Py_TrueStruct)
|
||||
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000
|
||||
# define Py_False Py_GetConstantBorrowed(Py_CONSTANT_FALSE)
|
||||
# define Py_True Py_GetConstantBorrowed(Py_CONSTANT_TRUE)
|
||||
#else
|
||||
# define Py_False _PyObject_CAST(&_Py_FalseStruct)
|
||||
# define Py_True _PyObject_CAST(&_Py_TrueStruct)
|
||||
#endif
|
||||
|
||||
// Test if an object is the True singleton, the same as "x is True" in Python.
|
||||
PyAPI_FUNC(int) Py_IsTrue(PyObject *x);
|
||||
|
|
|
|||
|
|
@ -716,6 +716,8 @@ PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type;
|
|||
// Export for the stable ABI.
|
||||
PyAPI_DATA(int) _Py_SwappedOp[];
|
||||
|
||||
extern void _Py_GetConstant_Init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1068,12 +1068,34 @@ static inline PyObject* _Py_XNewRef(PyObject *obj)
|
|||
#endif
|
||||
|
||||
|
||||
#define Py_CONSTANT_NONE 0
|
||||
#define Py_CONSTANT_FALSE 1
|
||||
#define Py_CONSTANT_TRUE 2
|
||||
#define Py_CONSTANT_ELLIPSIS 3
|
||||
#define Py_CONSTANT_NOT_IMPLEMENTED 4
|
||||
#define Py_CONSTANT_ZERO 5
|
||||
#define Py_CONSTANT_ONE 6
|
||||
#define Py_CONSTANT_EMPTY_STR 7
|
||||
#define Py_CONSTANT_EMPTY_BYTES 8
|
||||
#define Py_CONSTANT_EMPTY_TUPLE 9
|
||||
|
||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
|
||||
PyAPI_FUNC(PyObject*) Py_GetConstant(unsigned int constant_id);
|
||||
PyAPI_FUNC(PyObject*) Py_GetConstantBorrowed(unsigned int constant_id);
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
_Py_NoneStruct is an object of undefined type which can be used in contexts
|
||||
where NULL (nil) is not suitable (since NULL often means 'error').
|
||||
*/
|
||||
PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
|
||||
#define Py_None (&_Py_NoneStruct)
|
||||
|
||||
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000
|
||||
# define Py_None Py_GetConstantBorrowed(Py_CONSTANT_NONE)
|
||||
#else
|
||||
# define Py_None (&_Py_NoneStruct)
|
||||
#endif
|
||||
|
||||
// Test if an object is the None singleton, the same as "x is None" in Python.
|
||||
PyAPI_FUNC(int) Py_IsNone(PyObject *x);
|
||||
|
|
@ -1087,7 +1109,12 @@ Py_NotImplemented is a singleton used to signal that an operation is
|
|||
not implemented for a given type combination.
|
||||
*/
|
||||
PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
|
||||
#define Py_NotImplemented (&_Py_NotImplementedStruct)
|
||||
|
||||
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000
|
||||
# define Py_NotImplemented Py_GetConstantBorrowed(Py_CONSTANT_NOT_IMPLEMENTED)
|
||||
#else
|
||||
# define Py_NotImplemented (&_Py_NotImplementedStruct)
|
||||
#endif
|
||||
|
||||
/* Macro for returning Py_NotImplemented from a function */
|
||||
#define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented
|
||||
|
|
|
|||
|
|
@ -8,7 +8,11 @@ extern "C" {
|
|||
|
||||
PyAPI_DATA(PyObject) _Py_EllipsisObject; /* Don't use this directly */
|
||||
|
||||
#define Py_Ellipsis (&_Py_EllipsisObject)
|
||||
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000
|
||||
# define Py_Ellipsis Py_GetConstantBorrowed(Py_CONSTANT_ELLIPSIS)
|
||||
#else
|
||||
# define Py_Ellipsis (&_Py_EllipsisObject)
|
||||
#endif
|
||||
|
||||
/* Slice object interface */
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue