@@ -78,6 +78,19 @@ PyStackRef_IsNull(_PyStackRef ref)
7878 return ref .index == 0 ;
7979}
8080
81+ static inline bool
82+ PyStackRef_IsError (_PyStackRef ref )
83+ {
84+ return ref .index == 2 ;
85+ }
86+
87+ static inline bool
88+ PyStackRef_IsValid (_PyStackRef ref )
89+ {
90+ /* Invalid values are ERROR and NULL */
91+ return !PyStackRef_IsError (ref ) && !PyStackRef_IsNull (ref );
92+ }
93+
8194static inline int
8295PyStackRef_IsTrue (_PyStackRef ref )
8396{
@@ -105,6 +118,7 @@ PyStackRef_IsTaggedInt(_PyStackRef ref)
105118static inline PyObject *
106119_PyStackRef_AsPyObjectBorrow (_PyStackRef ref , const char * filename , int linenumber )
107120{
121+ assert (!PyStackRef_IsError (ref ));
108122 assert (!PyStackRef_IsTaggedInt (ref ));
109123 _Py_stackref_record_borrow (ref , filename , linenumber );
110124 return _Py_stackref_get_object (ref );
@@ -156,6 +170,7 @@ _PyStackRef_CLOSE(_PyStackRef ref, const char *filename, int linenumber)
156170static inline void
157171_PyStackRef_XCLOSE (_PyStackRef ref , const char * filename , int linenumber )
158172{
173+ assert (!PyStackRef_IsError (ref ));
159174 if (PyStackRef_IsNull (ref )) {
160175 return ;
161176 }
@@ -166,6 +181,7 @@ _PyStackRef_XCLOSE(_PyStackRef ref, const char *filename, int linenumber)
166181static inline _PyStackRef
167182_PyStackRef_DUP (_PyStackRef ref , const char * filename , int linenumber )
168183{
184+ assert (!PyStackRef_IsError (ref ));
169185 if (PyStackRef_IsTaggedInt (ref )) {
170186 return ref ;
171187 }
@@ -246,6 +262,8 @@ PyStackRef_IsNullOrInt(_PyStackRef ref);
246262#define Py_TAG_REFCNT 1
247263#define Py_TAG_BITS 3
248264
265+ static const _PyStackRef PyStackRef_ERROR = { .bits = Py_TAG_INVALID };
266+
249267static inline bool
250268PyStackRef_IsError (_PyStackRef ref )
251269{
@@ -299,7 +317,6 @@ PyStackRef_IncrementTaggedIntNoOverflow(_PyStackRef ref)
299317
300318
301319static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED };
302- static const _PyStackRef PyStackRef_ERROR = { .bits = Py_TAG_INVALID };
303320
304321#define PyStackRef_IsNull (stackref ) ((stackref).bits == PyStackRef_NULL.bits)
305322#define PyStackRef_True ((_PyStackRef){.bits = ((uintptr_t)&_Py_TrueStruct) | Py_TAG_DEFERRED })
@@ -481,7 +498,6 @@ PyStackRef_AsStrongReference(_PyStackRef stackref)
481498
482499#define PyStackRef_NULL_BITS Py_TAG_REFCNT
483500static const _PyStackRef PyStackRef_NULL = { .bits = PyStackRef_NULL_BITS };
484- static const _PyStackRef PyStackRef_ERROR = { .bits = Py_TAG_INVALID };
485501
486502#define PyStackRef_IsNull (ref ) ((ref).bits == PyStackRef_NULL_BITS)
487503#define PyStackRef_True ((_PyStackRef){.bits = ((uintptr_t)&_Py_TrueStruct) | Py_TAG_REFCNT })
0 commit comments