From 23e116ae8cf4059ecbeba3bd89c4960bdb625131 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 9 May 2020 01:39:35 +0900 Subject: [PATCH 1/3] bpo-40566: Apply PEP 573 to abc module --- .../2020-05-09-01-39-16.bpo-40566.wlcjW_.rst | 1 + Modules/_abc.c | 27 +++++++++---------- 2 files changed, 13 insertions(+), 15 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-05-09-01-39-16.bpo-40566.wlcjW_.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-05-09-01-39-16.bpo-40566.wlcjW_.rst b/Misc/NEWS.d/next/Core and Builtins/2020-05-09-01-39-16.bpo-40566.wlcjW_.rst new file mode 100644 index 00000000000000..92a5e3ce632172 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-05-09-01-39-16.bpo-40566.wlcjW_.rst @@ -0,0 +1 @@ +Apply :pep:`573` to :mod:`abc`. diff --git a/Modules/_abc.c b/Modules/_abc.c index 7c040ef80ba3da..af11ba969db044 100644 --- a/Modules/_abc.c +++ b/Modules/_abc.c @@ -21,16 +21,9 @@ _Py_IDENTIFIER(__subclasshook__); typedef struct { PyTypeObject *_abc_data_type; + unsigned long long abc_invalidation_counter; } _abcmodule_state; -/* A global counter that is incremented each time a class is - registered as a virtual subclass of anything. It forces the - negative cache to be cleared before its next use. - Note: this counter is private. Use `abc.get_cache_token()` for - external code. */ -// FIXME: PEP 573: Move abc_invalidation_counter into _abcmodule_state. -static unsigned long long abc_invalidation_counter = 0; - static inline _abcmodule_state* get_abc_state(PyObject *module) { @@ -88,7 +81,8 @@ abc_data_new(PyTypeObject *type, PyObject *args, PyObject *kwds) self->_abc_registry = NULL; self->_abc_cache = NULL; self->_abc_negative_cache = NULL; - self->_abc_negative_cache_version = abc_invalidation_counter; + _abcmodule_state * state = PyType_GetModuleState(type); + self->_abc_negative_cache_version = state->abc_invalidation_counter; return (PyObject *) self; } @@ -495,7 +489,7 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass) Py_DECREF(impl); /* Invalidate negative cache */ - abc_invalidation_counter++; + get_abc_state(module)->abc_invalidation_counter++; Py_INCREF(subclass); return subclass; @@ -540,7 +534,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self, } subtype = (PyObject *)Py_TYPE(instance); if (subtype == subclass) { - if (impl->_abc_negative_cache_version == abc_invalidation_counter) { + if (impl->_abc_negative_cache_version == get_abc_state(module)->abc_invalidation_counter) { incache = _in_weak_set(impl->_abc_negative_cache, subclass); if (incache < 0) { goto end; @@ -629,15 +623,16 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self, goto end; } + _abcmodule_state *state = get_abc_state(module); /* 2. Check negative cache; may have to invalidate. */ - if (impl->_abc_negative_cache_version < abc_invalidation_counter) { + if (impl->_abc_negative_cache_version < state->abc_invalidation_counter) { /* Invalidate the negative cache. */ if (impl->_abc_negative_cache != NULL && PySet_Clear(impl->_abc_negative_cache) < 0) { goto end; } - impl->_abc_negative_cache_version = abc_invalidation_counter; + impl->_abc_negative_cache_version = state->abc_invalidation_counter; } else { incache = _in_weak_set(impl->_abc_negative_cache, subclass); @@ -830,7 +825,8 @@ static PyObject * _abc_get_cache_token_impl(PyObject *module) /*[clinic end generated code: output=c7d87841e033dacc input=70413d1c423ad9f9]*/ { - return PyLong_FromUnsignedLongLong(abc_invalidation_counter); + _abcmodule_state *state = get_abc_state(module); + return PyLong_FromUnsignedLongLong(state->abc_invalidation_counter); } static struct PyMethodDef _abcmodule_methods[] = { @@ -849,7 +845,8 @@ static int _abcmodule_exec(PyObject *module) { _abcmodule_state *state = get_abc_state(module); - state->_abc_data_type = (PyTypeObject *)PyType_FromSpec(&_abc_data_type_spec); + state->abc_invalidation_counter = 0; + state->_abc_data_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &_abc_data_type_spec, NULL); if (state->_abc_data_type == NULL) { return -1; } From eeeb23ebb7883a2c22484e3ec4489bb99d7c6376 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 9 May 2020 02:41:51 +0900 Subject: [PATCH 2/3] bpo-40566: Apply tiran's review --- Modules/_abc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Modules/_abc.c b/Modules/_abc.c index af11ba969db044..cd426c25dc849b 100644 --- a/Modules/_abc.c +++ b/Modules/_abc.c @@ -74,14 +74,14 @@ static PyObject * abc_data_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { _abc_data *self = (_abc_data *) type->tp_alloc(type, 0); - if (self == NULL) { + _abcmodule_state *state = PyType_GetModuleState(type); + if (self == NULL || state == NULL) { return NULL; } self->_abc_registry = NULL; self->_abc_cache = NULL; self->_abc_negative_cache = NULL; - _abcmodule_state * state = PyType_GetModuleState(type); self->_abc_negative_cache_version = state->abc_invalidation_counter; return (PyObject *) self; } @@ -606,6 +606,7 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self, } PyObject *ok, *subclasses = NULL, *result = NULL; + _abcmodule_state *state = NULL; Py_ssize_t pos; int incache; _abc_data *impl = _get_impl(module, self); @@ -623,7 +624,7 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self, goto end; } - _abcmodule_state *state = get_abc_state(module); + state = get_abc_state(module); /* 2. Check negative cache; may have to invalidate. */ if (impl->_abc_negative_cache_version < state->abc_invalidation_counter) { /* Invalidate the negative cache. */ From b439a7311bf8b65d596630d7c5a065e4c8e11a37 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 9 May 2020 03:26:32 +0900 Subject: [PATCH 3/3] bpo-40566: Update --- Modules/_abc.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Modules/_abc.c b/Modules/_abc.c index cd426c25dc849b..434bc454175b56 100644 --- a/Modules/_abc.c +++ b/Modules/_abc.c @@ -74,8 +74,14 @@ static PyObject * abc_data_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { _abc_data *self = (_abc_data *) type->tp_alloc(type, 0); - _abcmodule_state *state = PyType_GetModuleState(type); - if (self == NULL || state == NULL) { + _abcmodule_state *state = NULL; + if (self == NULL) { + return NULL; + } + + state = PyType_GetModuleState(type); + if (state == NULL) { + Py_DECREF(self); return NULL; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy