@@ -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
{
@@ -88,7 +81,8 @@ abc_data_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
88
81
self -> _abc_registry = NULL ;
89
82
self -> _abc_cache = NULL ;
90
83
self -> _abc_negative_cache = NULL ;
91
- self -> _abc_negative_cache_version = abc_invalidation_counter ;
84
+ _abcmodule_state * state = PyType_GetModuleState (type );
85
+ self -> _abc_negative_cache_version = state -> abc_invalidation_counter ;
92
86
return (PyObject * ) self ;
93
87
}
94
88
@@ -495,7 +489,7 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass)
495
489
Py_DECREF (impl );
496
490
497
491
/* Invalidate negative cache */
498
- abc_invalidation_counter ++ ;
492
+ get_abc_state ( module ) -> abc_invalidation_counter ++ ;
499
493
500
494
Py_INCREF (subclass );
501
495
return subclass ;
@@ -540,7 +534,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
540
534
}
541
535
subtype = (PyObject * )Py_TYPE (instance );
542
536
if (subtype == subclass ) {
543
- if (impl -> _abc_negative_cache_version == abc_invalidation_counter ) {
537
+ if (impl -> _abc_negative_cache_version == get_abc_state ( module ) -> abc_invalidation_counter ) {
544
538
incache = _in_weak_set (impl -> _abc_negative_cache , subclass );
545
539
if (incache < 0 ) {
546
540
goto end ;
@@ -629,15 +623,16 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
629
623
goto end ;
630
624
}
631
625
626
+ _abcmodule_state * state = get_abc_state (module );
632
627
/* 2. Check negative cache; may have to invalidate. */
633
- if (impl -> _abc_negative_cache_version < abc_invalidation_counter ) {
628
+ if (impl -> _abc_negative_cache_version < state -> abc_invalidation_counter ) {
634
629
/* Invalidate the negative cache. */
635
630
if (impl -> _abc_negative_cache != NULL &&
636
631
PySet_Clear (impl -> _abc_negative_cache ) < 0 )
637
632
{
638
633
goto end ;
639
634
}
640
- impl -> _abc_negative_cache_version = abc_invalidation_counter ;
635
+ impl -> _abc_negative_cache_version = state -> abc_invalidation_counter ;
641
636
}
642
637
else {
643
638
incache = _in_weak_set (impl -> _abc_negative_cache , subclass );
@@ -830,7 +825,8 @@ static PyObject *
830
825
_abc_get_cache_token_impl (PyObject * module )
831
826
/*[clinic end generated code: output=c7d87841e033dacc input=70413d1c423ad9f9]*/
832
827
{
833
- return PyLong_FromUnsignedLongLong (abc_invalidation_counter );
828
+ _abcmodule_state * state = get_abc_state (module );
829
+ return PyLong_FromUnsignedLongLong (state -> abc_invalidation_counter );
834
830
}
835
831
836
832
static struct PyMethodDef _abcmodule_methods [] = {
@@ -849,7 +845,8 @@ static int
849
845
_abcmodule_exec (PyObject * module )
850
846
{
851
847
_abcmodule_state * state = get_abc_state (module );
852
- state -> _abc_data_type = (PyTypeObject * )PyType_FromSpec (& _abc_data_type_spec );
848
+ state -> abc_invalidation_counter = 0 ;
849
+ state -> _abc_data_type = (PyTypeObject * )PyType_FromModuleAndSpec (module , & _abc_data_type_spec , NULL );
853
850
if (state -> _abc_data_type == NULL ) {
854
851
return -1 ;
855
852
}
0 commit comments