@@ -21,16 +21,9 @@ _Py_IDENTIFIER(__subclasshook__);
21
21
22
22
typedef struct {
23
23
PyTypeObject * _abc_data_type ;
24
+ unsigned long long abc_invalidation_counter ;
24
25
} _abcmodule_state ;
25
26
26
- /* A global counter that is incremented each time a class is
27
- registered as a virtual subclass of anything. It forces the
28
- negative cache to be cleared before its next use.
29
- Note: this counter is private. Use `abc.get_cache_token()` for
30
- external code. */
31
- // FIXME: PEP 573: Move abc_invalidation_counter into _abcmodule_state.
32
- static unsigned long long abc_invalidation_counter = 0 ;
33
-
34
27
static inline _abcmodule_state *
35
28
get_abc_state (PyObject * module )
36
29
{
@@ -81,14 +74,21 @@ static PyObject *
81
74
abc_data_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
82
75
{
83
76
_abc_data * self = (_abc_data * ) type -> tp_alloc (type , 0 );
77
+ _abcmodule_state * state = NULL ;
84
78
if (self == NULL ) {
85
79
return NULL ;
86
80
}
87
81
82
+ state = PyType_GetModuleState (type );
83
+ if (state == NULL ) {
84
+ Py_DECREF (self );
85
+ return NULL ;
86
+ }
87
+
88
88
self -> _abc_registry = NULL ;
89
89
self -> _abc_cache = NULL ;
90
90
self -> _abc_negative_cache = NULL ;
91
- self -> _abc_negative_cache_version = abc_invalidation_counter ;
91
+ self -> _abc_negative_cache_version = state -> abc_invalidation_counter ;
92
92
return (PyObject * ) self ;
93
93
}
94
94
@@ -495,7 +495,7 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass)
495
495
Py_DECREF (impl );
496
496
497
497
/* Invalidate negative cache */
498
- abc_invalidation_counter ++ ;
498
+ get_abc_state ( module ) -> abc_invalidation_counter ++ ;
499
499
500
500
Py_INCREF (subclass );
501
501
return subclass ;
@@ -540,7 +540,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
540
540
}
541
541
subtype = (PyObject * )Py_TYPE (instance );
542
542
if (subtype == subclass ) {
543
- if (impl -> _abc_negative_cache_version == abc_invalidation_counter ) {
543
+ if (impl -> _abc_negative_cache_version == get_abc_state ( module ) -> abc_invalidation_counter ) {
544
544
incache = _in_weak_set (impl -> _abc_negative_cache , subclass );
545
545
if (incache < 0 ) {
546
546
goto end ;
@@ -612,6 +612,7 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
612
612
}
613
613
614
614
PyObject * ok , * subclasses = NULL , * result = NULL ;
615
+ _abcmodule_state * state = NULL ;
615
616
Py_ssize_t pos ;
616
617
int incache ;
617
618
_abc_data * impl = _get_impl (module , self );
@@ -629,15 +630,16 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
629
630
goto end ;
630
631
}
631
632
633
+ state = get_abc_state (module );
632
634
/* 2. Check negative cache; may have to invalidate. */
633
- if (impl -> _abc_negative_cache_version < abc_invalidation_counter ) {
635
+ if (impl -> _abc_negative_cache_version < state -> abc_invalidation_counter ) {
634
636
/* Invalidate the negative cache. */
635
637
if (impl -> _abc_negative_cache != NULL &&
636
638
PySet_Clear (impl -> _abc_negative_cache ) < 0 )
637
639
{
638
640
goto end ;
639
641
}
640
- impl -> _abc_negative_cache_version = abc_invalidation_counter ;
642
+ impl -> _abc_negative_cache_version = state -> abc_invalidation_counter ;
641
643
}
642
644
else {
643
645
incache = _in_weak_set (impl -> _abc_negative_cache , subclass );
@@ -830,7 +832,8 @@ static PyObject *
830
832
_abc_get_cache_token_impl (PyObject * module )
831
833
/*[clinic end generated code: output=c7d87841e033dacc input=70413d1c423ad9f9]*/
832
834
{
833
- return PyLong_FromUnsignedLongLong (abc_invalidation_counter );
835
+ _abcmodule_state * state = get_abc_state (module );
836
+ return PyLong_FromUnsignedLongLong (state -> abc_invalidation_counter );
834
837
}
835
838
836
839
static struct PyMethodDef _abcmodule_methods [] = {
@@ -849,7 +852,8 @@ static int
849
852
_abcmodule_exec (PyObject * module )
850
853
{
851
854
_abcmodule_state * state = get_abc_state (module );
852
- state -> _abc_data_type = (PyTypeObject * )PyType_FromSpec (& _abc_data_type_spec );
855
+ state -> abc_invalidation_counter = 0 ;
856
+ state -> _abc_data_type = (PyTypeObject * )PyType_FromModuleAndSpec (module , & _abc_data_type_spec , NULL );
853
857
if (state -> _abc_data_type == NULL ) {
854
858
return -1 ;
855
859
}
0 commit comments