@@ -526,6 +526,31 @@ they can have object code that is not dependent on Python compilation flags.
526526PyAPI_FUNC (void ) Py_IncRef (PyObject * );
527527PyAPI_FUNC (void ) Py_DecRef (PyObject * );
528528
529+ // Increment the reference count of the object and return the object.
530+ PyAPI_FUNC (PyObject * ) Py_NewRef (PyObject * obj );
531+
532+ // Similar to Py_NewRef() but the object can be NULL.
533+ PyAPI_FUNC (PyObject * ) Py_XNewRef (PyObject * obj );
534+
535+ static inline PyObject * _Py_NewRef (PyObject * obj )
536+ {
537+ Py_INCREF (obj );
538+ return obj ;
539+ }
540+
541+ static inline PyObject * _Py_XNewRef (PyObject * obj )
542+ {
543+ Py_XINCREF (obj );
544+ return obj ;
545+ }
546+
547+ // Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI.
548+ // Names overriden with macros by static inline functions for best
549+ // performances.
550+ #define Py_NewRef (obj ) _Py_NewRef(obj)
551+ #define Py_XNewRef (obj ) _Py_XNewRef(obj)
552+
553+
529554/*
530555_Py_NoneStruct is an object of undefined type which can be used in contexts
531556where NULL (nil) is not suitable (since NULL often means 'error').
@@ -536,7 +561,7 @@ PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
536561#define Py_None (&_Py_NoneStruct)
537562
538563/* Macro for returning Py_None from a function */
539- #define Py_RETURN_NONE return Py_INCREF (Py_None), Py_None
564+ #define Py_RETURN_NONE return Py_NewRef (Py_None)
540565
541566/*
542567Py_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 */
546571#define Py_NotImplemented (&_Py_NotImplementedStruct)
547572
548573/* Macro for returning Py_NotImplemented from a function */
549- #define Py_RETURN_NOTIMPLEMENTED \
550- return Py_INCREF(Py_NotImplemented), Py_NotImplemented
574+ #define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented)
551575
552576/* Rich comparison opcodes */
553577#define Py_LT 0
0 commit comments