diff options
author | Victor Stinner <vstinner@python.org> | 2020-11-05 14:02:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-05 14:02:12 (GMT) |
commit | 53a03aafd5812018a3821a2e83063fd3d6cd2576 (patch) | |
tree | c38adba5e60f412a36ea31736fb6265b2f754a40 /Include | |
parent | 80449f243b13311d660eab3a751648029bcdd833 (diff) | |
download | cpython-53a03aafd5812018a3821a2e83063fd3d6cd2576.zip cpython-53a03aafd5812018a3821a2e83063fd3d6cd2576.tar.gz cpython-53a03aafd5812018a3821a2e83063fd3d6cd2576.tar.bz2 |
bpo-42262: Add Py_NewRef() and Py_XNewRef() (GH-23152)
Added Py_NewRef() and Py_XNewRef() functions to increment the reference
count of an object and return the object.
Diffstat (limited to 'Include')
-rw-r--r-- | Include/boolobject.h | 4 | ||||
-rw-r--r-- | Include/object.h | 30 |
2 files changed, 29 insertions, 5 deletions
diff --git a/Include/boolobject.h b/Include/boolobject.h index bb8044a..6673d72 100644 --- a/Include/boolobject.h +++ b/Include/boolobject.h @@ -22,8 +22,8 @@ PyAPI_DATA(struct _longobject) _Py_FalseStruct, _Py_TrueStruct; #define Py_True ((PyObject *) &_Py_TrueStruct) /* Macros for returning Py_True or Py_False, respectively */ -#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True -#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False +#define Py_RETURN_TRUE return Py_NewRef(Py_True) +#define Py_RETURN_FALSE return Py_NewRef(Py_False) /* Function to return a bool from a C long */ PyAPI_FUNC(PyObject *) PyBool_FromLong(long); diff --git a/Include/object.h b/Include/object.h index 6ee4ee7..835d9de 100644 --- a/Include/object.h +++ b/Include/object.h @@ -526,6 +526,31 @@ they can have object code that is not dependent on Python compilation flags. PyAPI_FUNC(void) Py_IncRef(PyObject *); PyAPI_FUNC(void) Py_DecRef(PyObject *); +// Increment the reference count of the object and return the object. +PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj); + +// Similar to Py_NewRef() but the object can be NULL. +PyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj); + +static inline PyObject* _Py_NewRef(PyObject *obj) +{ + Py_INCREF(obj); + return obj; +} + +static inline PyObject* _Py_XNewRef(PyObject *obj) +{ + Py_XINCREF(obj); + return obj; +} + +// Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI. +// Names overriden with macros by static inline functions for best +// performances. +#define Py_NewRef(obj) _Py_NewRef(obj) +#define Py_XNewRef(obj) _Py_XNewRef(obj) + + /* _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'). @@ -536,7 +561,7 @@ PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */ #define Py_None (&_Py_NoneStruct) /* Macro for returning Py_None from a function */ -#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None +#define Py_RETURN_NONE return Py_NewRef(Py_None) /* Py_NotImplemented is a singleton used to signal that an operation is @@ -546,8 +571,7 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ #define Py_NotImplemented (&_Py_NotImplementedStruct) /* Macro for returning Py_NotImplemented from a function */ -#define Py_RETURN_NOTIMPLEMENTED \ - return Py_INCREF(Py_NotImplemented), Py_NotImplemented +#define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented) /* Rich comparison opcodes */ #define Py_LT 0 |