Skip to content

Commit 23e116a

Browse files
committed
bpo-40566: Apply PEP 573 to abc module
1 parent 81a5fc3 commit 23e116a

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Apply :pep:`573` to :mod:`abc`.

Modules/_abc.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,9 @@ _Py_IDENTIFIER(__subclasshook__);
2121

2222
typedef struct {
2323
PyTypeObject *_abc_data_type;
24+
unsigned long long abc_invalidation_counter;
2425
} _abcmodule_state;
2526

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-
3427
static inline _abcmodule_state*
3528
get_abc_state(PyObject *module)
3629
{
@@ -88,7 +81,8 @@ abc_data_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
8881
self->_abc_registry = NULL;
8982
self->_abc_cache = NULL;
9083
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;
9286
return (PyObject *) self;
9387
}
9488

@@ -495,7 +489,7 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass)
495489
Py_DECREF(impl);
496490

497491
/* Invalidate negative cache */
498-
abc_invalidation_counter++;
492+
get_abc_state(module)->abc_invalidation_counter++;
499493

500494
Py_INCREF(subclass);
501495
return subclass;
@@ -540,7 +534,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
540534
}
541535
subtype = (PyObject *)Py_TYPE(instance);
542536
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) {
544538
incache = _in_weak_set(impl->_abc_negative_cache, subclass);
545539
if (incache < 0) {
546540
goto end;
@@ -629,15 +623,16 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
629623
goto end;
630624
}
631625

626+
_abcmodule_state *state = get_abc_state(module);
632627
/* 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) {
634629
/* Invalidate the negative cache. */
635630
if (impl->_abc_negative_cache != NULL &&
636631
PySet_Clear(impl->_abc_negative_cache) < 0)
637632
{
638633
goto end;
639634
}
640-
impl->_abc_negative_cache_version = abc_invalidation_counter;
635+
impl->_abc_negative_cache_version = state->abc_invalidation_counter;
641636
}
642637
else {
643638
incache = _in_weak_set(impl->_abc_negative_cache, subclass);
@@ -830,7 +825,8 @@ static PyObject *
830825
_abc_get_cache_token_impl(PyObject *module)
831826
/*[clinic end generated code: output=c7d87841e033dacc input=70413d1c423ad9f9]*/
832827
{
833-
return PyLong_FromUnsignedLongLong(abc_invalidation_counter);
828+
_abcmodule_state *state = get_abc_state(module);
829+
return PyLong_FromUnsignedLongLong(state->abc_invalidation_counter);
834830
}
835831

836832
static struct PyMethodDef _abcmodule_methods[] = {
@@ -849,7 +845,8 @@ static int
849845
_abcmodule_exec(PyObject *module)
850846
{
851847
_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);
853850
if (state->_abc_data_type == NULL) {
854851
return -1;
855852
}

0 commit comments

Comments
 (0)