2018-02-18 13:41:58 +01:00
|
|
|
/* ABCMeta implementation */
|
2021-10-22 15:36:28 +02:00
|
|
|
#ifndef Py_BUILD_CORE_BUILTIN
|
|
|
|
# define Py_BUILD_CORE_MODULE 1
|
|
|
|
#endif
|
2018-02-18 13:41:58 +01:00
|
|
|
|
|
|
|
#include "Python.h"
|
2021-04-22 00:52:52 +02:00
|
|
|
#include "pycore_moduleobject.h" // _PyModule_GetState()
|
2022-02-08 21:39:07 +01:00
|
|
|
#include "pycore_object.h" // _PyType_GetSubclasses()
|
|
|
|
#include "pycore_runtime.h" // _Py_ID()
|
2023-07-22 17:04:34 +02:00
|
|
|
#include "pycore_setobject.h" // _PySet_NextEntry()
|
2023-06-21 15:44:25 +02:00
|
|
|
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
|
2018-02-18 13:41:58 +01:00
|
|
|
#include "clinic/_abc.c.h"
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
module _abc
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=964f5328e1aefcda]*/
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_abc__doc__,
|
|
|
|
"Module contains faster C implementation of abc.ABCMeta");
|
|
|
|
|
2020-03-30 16:35:38 +02:00
|
|
|
typedef struct {
|
|
|
|
PyTypeObject *_abc_data_type;
|
2024-04-12 00:13:25 +02:00
|
|
|
uint64_t abc_invalidation_counter;
|
2020-03-30 16:35:38 +02:00
|
|
|
} _abcmodule_state;
|
|
|
|
|
|
|
|
static inline _abcmodule_state*
|
|
|
|
get_abc_state(PyObject *module)
|
|
|
|
{
|
2021-04-22 00:52:52 +02:00
|
|
|
void *state = _PyModule_GetState(module);
|
2020-03-30 16:35:38 +02:00
|
|
|
assert(state != NULL);
|
|
|
|
return (_abcmodule_state *)state;
|
|
|
|
}
|
|
|
|
|
2024-04-12 00:13:25 +02:00
|
|
|
static inline uint64_t
|
|
|
|
get_invalidation_counter(_abcmodule_state *state)
|
|
|
|
{
|
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
return _Py_atomic_load_uint64(&state->abc_invalidation_counter);
|
|
|
|
#else
|
|
|
|
return state->abc_invalidation_counter;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
increment_invalidation_counter(_abcmodule_state *state)
|
|
|
|
{
|
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
_Py_atomic_add_uint64(&state->abc_invalidation_counter, 1);
|
|
|
|
#else
|
|
|
|
state->abc_invalidation_counter++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-02-18 13:41:58 +01:00
|
|
|
/* This object stores internal state for ABCs.
|
|
|
|
Note that we can use normal sets for caches,
|
|
|
|
since they are never iterated over. */
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
2024-04-12 00:13:25 +02:00
|
|
|
/* These sets of weak references are lazily created. Once created, they
|
|
|
|
will point to the same sets until the ABCMeta object is destroyed or
|
|
|
|
cleared, both of which will only happen while the object is visible to a
|
|
|
|
single thread. */
|
2018-02-18 13:41:58 +01:00
|
|
|
PyObject *_abc_registry;
|
2024-04-12 00:13:25 +02:00
|
|
|
PyObject *_abc_cache;
|
|
|
|
PyObject *_abc_negative_cache;
|
|
|
|
uint64_t _abc_negative_cache_version;
|
2018-02-18 13:41:58 +01:00
|
|
|
} _abc_data;
|
|
|
|
|
2024-04-12 00:13:25 +02:00
|
|
|
static inline uint64_t
|
|
|
|
get_cache_version(_abc_data *impl)
|
|
|
|
{
|
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
return _Py_atomic_load_uint64(&impl->_abc_negative_cache_version);
|
|
|
|
#else
|
|
|
|
return impl->_abc_negative_cache_version;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
set_cache_version(_abc_data *impl, uint64_t version)
|
|
|
|
{
|
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
_Py_atomic_store_uint64(&impl->_abc_negative_cache_version, version);
|
|
|
|
#else
|
|
|
|
impl->_abc_negative_cache_version = version;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:36:04 +02:00
|
|
|
static int
|
|
|
|
abc_data_traverse(_abc_data *self, visitproc visit, void *arg)
|
|
|
|
{
|
2020-05-27 11:03:38 +02:00
|
|
|
Py_VISIT(Py_TYPE(self));
|
2020-04-07 18:36:04 +02:00
|
|
|
Py_VISIT(self->_abc_registry);
|
|
|
|
Py_VISIT(self->_abc_cache);
|
|
|
|
Py_VISIT(self->_abc_negative_cache);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
abc_data_clear(_abc_data *self)
|
|
|
|
{
|
|
|
|
Py_CLEAR(self->_abc_registry);
|
|
|
|
Py_CLEAR(self->_abc_cache);
|
|
|
|
Py_CLEAR(self->_abc_negative_cache);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-18 13:41:58 +01:00
|
|
|
static void
|
|
|
|
abc_data_dealloc(_abc_data *self)
|
|
|
|
{
|
2022-07-27 17:03:38 +02:00
|
|
|
PyObject_GC_UnTrack(self);
|
2020-03-30 16:35:38 +02:00
|
|
|
PyTypeObject *tp = Py_TYPE(self);
|
2020-04-07 18:36:04 +02:00
|
|
|
(void)abc_data_clear(self);
|
2020-03-30 16:35:38 +02:00
|
|
|
tp->tp_free(self);
|
|
|
|
Py_DECREF(tp);
|
2018-02-18 13:41:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
abc_data_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
_abc_data *self = (_abc_data *) type->tp_alloc(type, 0);
|
2020-05-09 10:31:40 +02:00
|
|
|
_abcmodule_state *state = NULL;
|
2018-02-18 13:41:58 +01:00
|
|
|
if (self == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-02-24 21:16:29 +01:00
|
|
|
state = _PyType_GetModuleState(type);
|
2020-05-09 10:31:40 +02:00
|
|
|
if (state == NULL) {
|
|
|
|
Py_DECREF(self);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-18 13:41:58 +01:00
|
|
|
self->_abc_registry = NULL;
|
|
|
|
self->_abc_cache = NULL;
|
|
|
|
self->_abc_negative_cache = NULL;
|
2024-04-12 00:13:25 +02:00
|
|
|
self->_abc_negative_cache_version = get_invalidation_counter(state);
|
2018-02-18 13:41:58 +01:00
|
|
|
return (PyObject *) self;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(abc_data_doc,
|
|
|
|
"Internal state held by ABC machinery.");
|
|
|
|
|
2020-03-30 16:35:38 +02:00
|
|
|
static PyType_Slot _abc_data_type_spec_slots[] = {
|
|
|
|
{Py_tp_doc, (void *)abc_data_doc},
|
|
|
|
{Py_tp_new, abc_data_new},
|
|
|
|
{Py_tp_dealloc, abc_data_dealloc},
|
2020-04-07 18:36:04 +02:00
|
|
|
{Py_tp_traverse, abc_data_traverse},
|
|
|
|
{Py_tp_clear, abc_data_clear},
|
2020-03-30 16:35:38 +02:00
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyType_Spec _abc_data_type_spec = {
|
|
|
|
.name = "_abc._abc_data",
|
|
|
|
.basicsize = sizeof(_abc_data),
|
2020-04-07 18:36:04 +02:00
|
|
|
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
2020-03-30 16:35:38 +02:00
|
|
|
.slots = _abc_data_type_spec_slots,
|
2018-02-18 13:41:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static _abc_data *
|
2020-03-30 16:35:38 +02:00
|
|
|
_get_impl(PyObject *module, PyObject *self)
|
2018-02-18 13:41:58 +01:00
|
|
|
{
|
2020-03-30 16:35:38 +02:00
|
|
|
_abcmodule_state *state = get_abc_state(module);
|
2022-02-08 21:39:07 +01:00
|
|
|
PyObject *impl = PyObject_GetAttr(self, &_Py_ID(_abc_impl));
|
2018-02-18 13:41:58 +01:00
|
|
|
if (impl == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-03-30 16:35:38 +02:00
|
|
|
if (!Py_IS_TYPE(impl, state->_abc_data_type)) {
|
2018-02-18 13:41:58 +01:00
|
|
|
PyErr_SetString(PyExc_TypeError, "_abc_impl is set to a wrong type");
|
|
|
|
Py_DECREF(impl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (_abc_data *)impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2024-04-12 00:13:25 +02:00
|
|
|
_in_weak_set(_abc_data *impl, PyObject **pset, PyObject *obj)
|
2018-02-18 13:41:58 +01:00
|
|
|
{
|
2024-04-12 00:13:25 +02:00
|
|
|
PyObject *set;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(impl);
|
|
|
|
set = *pset;
|
|
|
|
Py_END_CRITICAL_SECTION();
|
2018-02-18 13:41:58 +01:00
|
|
|
if (set == NULL || PySet_GET_SIZE(set) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
PyObject *ref = PyWeakref_NewRef(obj, NULL);
|
|
|
|
if (ref == NULL) {
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int res = PySet_Contains(set, ref);
|
|
|
|
Py_DECREF(ref);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_destroy(PyObject *setweakref, PyObject *objweakref)
|
|
|
|
{
|
2023-06-21 15:44:25 +02:00
|
|
|
PyObject *set = _PyWeakref_GET_REF(setweakref);
|
|
|
|
if (set == NULL) {
|
2018-02-18 13:41:58 +01:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
if (PySet_Discard(set, objweakref) < 0) {
|
|
|
|
Py_DECREF(set);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(set);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef _destroy_def = {
|
|
|
|
"_destroy", (PyCFunction) _destroy, METH_O
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2024-04-12 00:13:25 +02:00
|
|
|
_add_to_weak_set(_abc_data *impl, PyObject **pset, PyObject *obj)
|
2018-02-18 13:41:58 +01:00
|
|
|
{
|
2024-04-12 00:13:25 +02:00
|
|
|
PyObject *set;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(impl);
|
|
|
|
set = *pset;
|
|
|
|
if (set == NULL) {
|
|
|
|
set = *pset = PySet_New(NULL);
|
|
|
|
}
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
if (set == NULL) {
|
|
|
|
return -1;
|
2018-02-18 13:41:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *ref, *wr;
|
|
|
|
PyObject *destroy_cb;
|
|
|
|
wr = PyWeakref_NewRef(set, NULL);
|
|
|
|
if (wr == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
destroy_cb = PyCFunction_NewEx(&_destroy_def, wr, NULL);
|
|
|
|
if (destroy_cb == NULL) {
|
|
|
|
Py_DECREF(wr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ref = PyWeakref_NewRef(obj, destroy_cb);
|
|
|
|
Py_DECREF(destroy_cb);
|
|
|
|
if (ref == NULL) {
|
|
|
|
Py_DECREF(wr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret = PySet_Add(set, ref);
|
|
|
|
Py_DECREF(wr);
|
|
|
|
Py_DECREF(ref);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_abc._reset_registry
|
|
|
|
|
|
|
|
self: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Internal ABC helper to reset registry of a given class.
|
|
|
|
|
|
|
|
Should be only used by refleak.py
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_abc__reset_registry(PyObject *module, PyObject *self)
|
|
|
|
/*[clinic end generated code: output=92d591a43566cc10 input=12a0b7eb339ac35c]*/
|
|
|
|
{
|
2020-03-30 16:35:38 +02:00
|
|
|
_abc_data *impl = _get_impl(module, self);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (impl == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-04-12 00:13:25 +02:00
|
|
|
PyObject *registry;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(impl);
|
|
|
|
registry = impl->_abc_registry;
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
if (registry != NULL && PySet_Clear(registry) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
Py_DECREF(impl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(impl);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_abc._reset_caches
|
|
|
|
|
|
|
|
self: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Internal ABC helper to reset both caches of a given class.
|
|
|
|
|
|
|
|
Should be only used by refleak.py
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_abc__reset_caches(PyObject *module, PyObject *self)
|
|
|
|
/*[clinic end generated code: output=f296f0d5c513f80c input=c0ac616fd8acfb6f]*/
|
|
|
|
{
|
2020-03-30 16:35:38 +02:00
|
|
|
_abc_data *impl = _get_impl(module, self);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (impl == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-04-12 00:13:25 +02:00
|
|
|
PyObject *cache, *negative_cache;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(impl);
|
|
|
|
cache = impl->_abc_cache;
|
|
|
|
negative_cache = impl->_abc_negative_cache;
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
if (cache != NULL && PySet_Clear(cache) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
Py_DECREF(impl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* also the second cache */
|
2024-04-12 00:13:25 +02:00
|
|
|
if (negative_cache != NULL && PySet_Clear(negative_cache) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
Py_DECREF(impl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(impl);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_abc._get_dump
|
|
|
|
|
|
|
|
self: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Internal ABC helper for cache and registry debugging.
|
|
|
|
|
|
|
|
Return shallow copies of registry, of both caches, and
|
|
|
|
negative cache version. Don't call this function directly,
|
|
|
|
instead use ABC._dump_registry() for a nice repr.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_abc__get_dump(PyObject *module, PyObject *self)
|
|
|
|
/*[clinic end generated code: output=9d9569a8e2c1c443 input=2c5deb1bfe9e3c79]*/
|
|
|
|
{
|
2020-03-30 16:35:38 +02:00
|
|
|
_abc_data *impl = _get_impl(module, self);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (impl == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-04-12 00:13:25 +02:00
|
|
|
PyObject *res;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(impl);
|
|
|
|
res = Py_BuildValue("NNNK",
|
|
|
|
PySet_New(impl->_abc_registry),
|
|
|
|
PySet_New(impl->_abc_cache),
|
|
|
|
PySet_New(impl->_abc_negative_cache),
|
|
|
|
get_cache_version(impl));
|
|
|
|
Py_END_CRITICAL_SECTION();
|
2018-02-18 13:41:58 +01:00
|
|
|
Py_DECREF(impl);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute set of abstract method names.
|
|
|
|
static int
|
|
|
|
compute_abstract_methods(PyObject *self)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
PyObject *abstracts = PyFrozenSet_New(NULL);
|
|
|
|
if (abstracts == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *ns = NULL, *items = NULL, *bases = NULL; // Py_XDECREF()ed on error.
|
|
|
|
|
|
|
|
/* Stage 1: direct abstract methods. */
|
2022-02-08 21:39:07 +01:00
|
|
|
ns = PyObject_GetAttr(self, &_Py_ID(__dict__));
|
2018-02-18 13:41:58 +01:00
|
|
|
if (!ns) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't use PyDict_Next(ns) even when ns is dict because
|
|
|
|
// _PyObject_IsAbstract() can mutate ns.
|
|
|
|
items = PyMapping_Items(ns);
|
|
|
|
if (!items) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
assert(PyList_Check(items));
|
|
|
|
for (Py_ssize_t pos = 0; pos < PyList_GET_SIZE(items); pos++) {
|
|
|
|
PyObject *it = PySequence_Fast(
|
|
|
|
PyList_GET_ITEM(items, pos),
|
|
|
|
"items() returned non-iterable");
|
|
|
|
if (!it) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (PySequence_Fast_GET_SIZE(it) != 2) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"items() returned item which size is not 2");
|
|
|
|
Py_DECREF(it);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// borrowed
|
|
|
|
PyObject *key = PySequence_Fast_GET_ITEM(it, 0);
|
|
|
|
PyObject *value = PySequence_Fast_GET_ITEM(it, 1);
|
|
|
|
// items or it may be cleared while accessing __abstractmethod__
|
|
|
|
// So we need to keep strong reference for key
|
|
|
|
Py_INCREF(key);
|
|
|
|
int is_abstract = _PyObject_IsAbstract(value);
|
|
|
|
if (is_abstract < 0 ||
|
|
|
|
(is_abstract && PySet_Add(abstracts, key) < 0)) {
|
|
|
|
Py_DECREF(it);
|
|
|
|
Py_DECREF(key);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(key);
|
|
|
|
Py_DECREF(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stage 2: inherited abstract methods. */
|
2022-02-08 21:39:07 +01:00
|
|
|
bases = PyObject_GetAttr(self, &_Py_ID(__bases__));
|
2018-02-18 13:41:58 +01:00
|
|
|
if (!bases) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!PyTuple_Check(bases)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "__bases__ is not tuple");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Py_ssize_t pos = 0; pos < PyTuple_GET_SIZE(bases); pos++) {
|
|
|
|
PyObject *item = PyTuple_GET_ITEM(bases, pos); // borrowed
|
|
|
|
PyObject *base_abstracts, *iter;
|
|
|
|
|
2023-07-12 07:57:10 +02:00
|
|
|
if (PyObject_GetOptionalAttr(item, &_Py_ID(__abstractmethods__),
|
2022-02-08 21:39:07 +01:00
|
|
|
&base_abstracts) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (base_abstracts == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(iter = PyObject_GetIter(base_abstracts))) {
|
|
|
|
Py_DECREF(base_abstracts);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(base_abstracts);
|
|
|
|
PyObject *key, *value;
|
|
|
|
while ((key = PyIter_Next(iter))) {
|
2023-07-12 07:57:10 +02:00
|
|
|
if (PyObject_GetOptionalAttr(self, key, &value) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
Py_DECREF(key);
|
|
|
|
Py_DECREF(iter);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (value == NULL) {
|
|
|
|
Py_DECREF(key);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_abstract = _PyObject_IsAbstract(value);
|
|
|
|
Py_DECREF(value);
|
|
|
|
if (is_abstract < 0 ||
|
|
|
|
(is_abstract && PySet_Add(abstracts, key) < 0))
|
|
|
|
{
|
|
|
|
Py_DECREF(key);
|
|
|
|
Py_DECREF(iter);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(key);
|
|
|
|
}
|
|
|
|
Py_DECREF(iter);
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 21:39:07 +01:00
|
|
|
if (PyObject_SetAttr(self, &_Py_ID(__abstractmethods__), abstracts) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
error:
|
|
|
|
Py_DECREF(abstracts);
|
|
|
|
Py_XDECREF(ns);
|
|
|
|
Py_XDECREF(items);
|
|
|
|
Py_XDECREF(bases);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-30 10:50:28 +02:00
|
|
|
#define COLLECTION_FLAGS (Py_TPFLAGS_SEQUENCE | Py_TPFLAGS_MAPPING)
|
|
|
|
|
2018-02-18 13:41:58 +01:00
|
|
|
/*[clinic input]
|
|
|
|
_abc._abc_init
|
|
|
|
|
|
|
|
self: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Internal ABC helper for class set-up. Should be never used outside abc module.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_abc__abc_init(PyObject *module, PyObject *self)
|
|
|
|
/*[clinic end generated code: output=594757375714cda1 input=8d7fe470ff77f029]*/
|
|
|
|
{
|
2020-03-30 16:35:38 +02:00
|
|
|
_abcmodule_state *state = get_abc_state(module);
|
2018-02-18 13:41:58 +01:00
|
|
|
PyObject *data;
|
|
|
|
if (compute_abstract_methods(self) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up inheritance registry. */
|
2020-03-30 16:35:38 +02:00
|
|
|
data = abc_data_new(state->_abc_data_type, NULL, NULL);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (data == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-02-08 21:39:07 +01:00
|
|
|
if (PyObject_SetAttr(self, &_Py_ID(_abc_impl), data) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
Py_DECREF(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(data);
|
2021-04-30 10:50:28 +02:00
|
|
|
/* If __abc_tpflags__ & COLLECTION_FLAGS is set, then set the corresponding bit(s)
|
|
|
|
* in the new class.
|
|
|
|
* Used by collections.abc.Sequence and collections.abc.Mapping to indicate
|
|
|
|
* their special status w.r.t. pattern matching. */
|
|
|
|
if (PyType_Check(self)) {
|
|
|
|
PyTypeObject *cls = (PyTypeObject *)self;
|
2023-05-02 04:34:43 +02:00
|
|
|
PyObject *dict = _PyType_GetDict(cls);
|
2024-04-12 00:13:25 +02:00
|
|
|
PyObject *flags = NULL;
|
|
|
|
if (PyDict_Pop(dict, &_Py_ID(__abc_tpflags__), &flags) < 0) {
|
|
|
|
return NULL;
|
2021-04-30 10:50:28 +02:00
|
|
|
}
|
2024-04-12 00:13:25 +02:00
|
|
|
if (flags == NULL || !PyLong_CheckExact(flags)) {
|
|
|
|
Py_XDECREF(flags);
|
|
|
|
Py_RETURN_NONE;
|
2021-04-30 10:50:28 +02:00
|
|
|
}
|
2022-01-21 23:29:10 +01:00
|
|
|
|
2024-04-12 00:13:25 +02:00
|
|
|
long val = PyLong_AsLong(flags);
|
|
|
|
Py_DECREF(flags);
|
|
|
|
if (val == -1 && PyErr_Occurred()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((val & COLLECTION_FLAGS) == COLLECTION_FLAGS) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "__abc_tpflags__ cannot be both Py_TPFLAGS_SEQUENCE and Py_TPFLAGS_MAPPING");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
_PyType_SetFlags((PyTypeObject *)self, 0, val & COLLECTION_FLAGS);
|
2021-06-25 17:20:43 +02:00
|
|
|
}
|
2024-04-12 00:13:25 +02:00
|
|
|
Py_RETURN_NONE;
|
2021-06-25 17:20:43 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 13:41:58 +01:00
|
|
|
/*[clinic input]
|
|
|
|
_abc._abc_register
|
|
|
|
|
|
|
|
self: object
|
|
|
|
subclass: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Internal ABC helper for subclasss registration. Should be never used outside abc module.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass)
|
|
|
|
/*[clinic end generated code: output=7851e7668c963524 input=ca589f8c3080e67f]*/
|
|
|
|
{
|
|
|
|
if (!PyType_Check(subclass)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Can only register classes");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
int result = PyObject_IsSubclass(subclass, self);
|
|
|
|
if (result > 0) {
|
2022-11-14 13:08:15 +01:00
|
|
|
return Py_NewRef(subclass); /* Already a subclass. */
|
2018-02-18 13:41:58 +01:00
|
|
|
}
|
|
|
|
if (result < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Subtle: test for cycles *after* testing for "already a subclass";
|
|
|
|
this means we allow X.register(X) and interpret it as a no-op. */
|
|
|
|
result = PyObject_IsSubclass(self, subclass);
|
|
|
|
if (result > 0) {
|
|
|
|
/* This would create a cycle, which is bad for the algorithm below. */
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Refusing to create an inheritance cycle");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (result < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-03-30 16:35:38 +02:00
|
|
|
_abc_data *impl = _get_impl(module, self);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (impl == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-04-12 00:13:25 +02:00
|
|
|
if (_add_to_weak_set(impl, &impl->_abc_registry, subclass) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
Py_DECREF(impl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(impl);
|
|
|
|
|
|
|
|
/* Invalidate negative cache */
|
2024-04-12 00:13:25 +02:00
|
|
|
increment_invalidation_counter(get_abc_state(module));
|
2018-02-18 13:41:58 +01:00
|
|
|
|
2024-04-12 00:13:25 +02:00
|
|
|
/* Set Py_TPFLAGS_SEQUENCE or Py_TPFLAGS_MAPPING flag */
|
2021-06-25 17:20:43 +02:00
|
|
|
if (PyType_Check(self)) {
|
2024-04-12 00:13:25 +02:00
|
|
|
unsigned long collection_flag =
|
|
|
|
PyType_GetFlags((PyTypeObject *)self) & COLLECTION_FLAGS;
|
2021-06-25 17:20:43 +02:00
|
|
|
if (collection_flag) {
|
2024-04-12 00:13:25 +02:00
|
|
|
_PyType_SetFlagsRecursive((PyTypeObject *)subclass,
|
|
|
|
COLLECTION_FLAGS,
|
|
|
|
collection_flag);
|
2021-06-25 17:20:43 +02:00
|
|
|
}
|
2021-04-30 10:50:28 +02:00
|
|
|
}
|
2022-11-14 13:08:15 +01:00
|
|
|
return Py_NewRef(subclass);
|
2018-02-18 13:41:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_abc._abc_instancecheck
|
|
|
|
|
|
|
|
self: object
|
|
|
|
instance: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Internal ABC helper for instance checks. Should be never used outside abc module.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
|
|
|
|
PyObject *instance)
|
|
|
|
/*[clinic end generated code: output=b8b5148f63b6b56f input=a4f4525679261084]*/
|
|
|
|
{
|
|
|
|
PyObject *subtype, *result = NULL, *subclass = NULL;
|
2020-03-30 16:35:38 +02:00
|
|
|
_abc_data *impl = _get_impl(module, self);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (impl == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-02-08 21:39:07 +01:00
|
|
|
subclass = PyObject_GetAttr(instance, &_Py_ID(__class__));
|
2018-02-18 13:41:58 +01:00
|
|
|
if (subclass == NULL) {
|
|
|
|
Py_DECREF(impl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Inline the cache checking. */
|
2024-04-12 00:13:25 +02:00
|
|
|
int incache = _in_weak_set(impl, &impl->_abc_cache, subclass);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (incache < 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (incache > 0) {
|
2022-11-14 13:08:15 +01:00
|
|
|
result = Py_NewRef(Py_True);
|
2018-02-18 13:41:58 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
subtype = (PyObject *)Py_TYPE(instance);
|
|
|
|
if (subtype == subclass) {
|
2024-04-12 00:13:25 +02:00
|
|
|
if (get_cache_version(impl) == get_invalidation_counter(get_abc_state(module))) {
|
|
|
|
incache = _in_weak_set(impl, &impl->_abc_negative_cache, subclass);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (incache < 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (incache > 0) {
|
2022-11-14 13:08:15 +01:00
|
|
|
result = Py_NewRef(Py_False);
|
2018-02-18 13:41:58 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Fall back to the subclass check. */
|
2022-02-08 21:39:07 +01:00
|
|
|
result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__),
|
|
|
|
subclass);
|
2018-02-18 13:41:58 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2022-02-08 21:39:07 +01:00
|
|
|
result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__),
|
|
|
|
subclass);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (result == NULL) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (PyObject_IsTrue(result)) {
|
|
|
|
case -1:
|
2022-11-23 14:57:50 +01:00
|
|
|
Py_SETREF(result, NULL);
|
2018-02-18 13:41:58 +01:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
Py_DECREF(result);
|
2022-02-08 21:39:07 +01:00
|
|
|
result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__),
|
|
|
|
subtype);
|
2018-02-18 13:41:58 +01:00
|
|
|
break;
|
|
|
|
case 1: // Nothing to do.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Py_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
Py_XDECREF(impl);
|
|
|
|
Py_XDECREF(subclass);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-05 23:39:32 +02:00
|
|
|
// Return -1 when exception occurred.
|
2018-02-18 13:41:58 +01:00
|
|
|
// Return 1 when result is set.
|
|
|
|
// Return 0 otherwise.
|
|
|
|
static int subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,
|
|
|
|
PyObject **result);
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_abc._abc_subclasscheck
|
|
|
|
|
|
|
|
self: object
|
|
|
|
subclass: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Internal ABC helper for subclasss checks. Should be never used outside abc module.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
|
|
|
|
PyObject *subclass)
|
|
|
|
/*[clinic end generated code: output=b56c9e4a530e3894 input=1d947243409d10b8]*/
|
|
|
|
{
|
2018-03-22 12:26:06 +01:00
|
|
|
if (!PyType_Check(subclass)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-22 13:52:42 +01:00
|
|
|
PyObject *ok, *subclasses = NULL, *result = NULL;
|
2020-05-09 10:31:40 +02:00
|
|
|
_abcmodule_state *state = NULL;
|
2018-02-18 13:41:58 +01:00
|
|
|
Py_ssize_t pos;
|
|
|
|
int incache;
|
2020-03-30 16:35:38 +02:00
|
|
|
_abc_data *impl = _get_impl(module, self);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (impl == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1. Check cache. */
|
2024-04-12 00:13:25 +02:00
|
|
|
incache = _in_weak_set(impl, &impl->_abc_cache, subclass);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (incache < 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (incache > 0) {
|
|
|
|
result = Py_True;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2020-05-09 10:31:40 +02:00
|
|
|
state = get_abc_state(module);
|
2018-02-18 13:41:58 +01:00
|
|
|
/* 2. Check negative cache; may have to invalidate. */
|
2024-04-12 00:13:25 +02:00
|
|
|
uint64_t invalidation_counter = get_invalidation_counter(state);
|
|
|
|
if (get_cache_version(impl) < invalidation_counter) {
|
2018-02-18 13:41:58 +01:00
|
|
|
/* Invalidate the negative cache. */
|
2024-04-12 00:13:25 +02:00
|
|
|
PyObject *negative_cache;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(impl);
|
|
|
|
negative_cache = impl->_abc_negative_cache;
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
if (negative_cache != NULL && PySet_Clear(negative_cache) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2024-04-12 00:13:25 +02:00
|
|
|
set_cache_version(impl, invalidation_counter);
|
2018-02-18 13:41:58 +01:00
|
|
|
}
|
|
|
|
else {
|
2024-04-12 00:13:25 +02:00
|
|
|
incache = _in_weak_set(impl, &impl->_abc_negative_cache, subclass);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (incache < 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (incache > 0) {
|
|
|
|
result = Py_False;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 3. Check the subclass hook. */
|
2022-02-08 21:39:07 +01:00
|
|
|
ok = PyObject_CallMethodOneArg(
|
|
|
|
(PyObject *)self, &_Py_ID(__subclasshook__), subclass);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (ok == NULL) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (ok == Py_True) {
|
|
|
|
Py_DECREF(ok);
|
2024-04-12 00:13:25 +02:00
|
|
|
if (_add_to_weak_set(impl, &impl->_abc_cache, subclass) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
result = Py_True;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (ok == Py_False) {
|
|
|
|
Py_DECREF(ok);
|
2024-04-12 00:13:25 +02:00
|
|
|
if (_add_to_weak_set(impl, &impl->_abc_negative_cache, subclass) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
result = Py_False;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (ok != Py_NotImplemented) {
|
|
|
|
Py_DECREF(ok);
|
|
|
|
PyErr_SetString(PyExc_AssertionError, "__subclasshook__ must return either"
|
|
|
|
" False, True, or NotImplemented");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
Py_DECREF(ok);
|
|
|
|
|
2018-03-22 13:52:42 +01:00
|
|
|
/* 4. Check if it's a direct subclass. */
|
2024-02-15 19:54:57 +01:00
|
|
|
if (PyType_IsSubtype((PyTypeObject *)subclass, (PyTypeObject *)self)) {
|
2024-04-12 00:13:25 +02:00
|
|
|
if (_add_to_weak_set(impl, &impl->_abc_cache, subclass) < 0) {
|
2018-03-22 13:52:42 +01:00
|
|
|
goto end;
|
2018-02-18 13:41:58 +01:00
|
|
|
}
|
2024-02-15 19:54:57 +01:00
|
|
|
result = Py_True;
|
|
|
|
goto end;
|
2018-02-18 13:41:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 5. Check if it's a subclass of a registered class (recursive). */
|
|
|
|
if (subclasscheck_check_registry(impl, subclass, &result)) {
|
2019-07-05 23:39:32 +02:00
|
|
|
// Exception occurred or result is set.
|
2018-02-18 13:41:58 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 6. Check if it's a subclass of a subclass (recursive). */
|
|
|
|
subclasses = PyObject_CallMethod(self, "__subclasses__", NULL);
|
2018-08-20 22:04:19 +02:00
|
|
|
if (subclasses == NULL) {
|
|
|
|
goto end;
|
|
|
|
}
|
2018-02-18 13:41:58 +01:00
|
|
|
if (!PyList_Check(subclasses)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "__subclasses__() must return a list");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
for (pos = 0; pos < PyList_GET_SIZE(subclasses); pos++) {
|
2024-04-12 00:13:25 +02:00
|
|
|
PyObject *scls = PyList_GetItemRef(subclasses, pos);
|
|
|
|
if (scls == NULL) {
|
|
|
|
goto end;
|
|
|
|
}
|
2018-02-18 13:41:58 +01:00
|
|
|
int r = PyObject_IsSubclass(subclass, scls);
|
|
|
|
Py_DECREF(scls);
|
|
|
|
if (r > 0) {
|
2024-04-12 00:13:25 +02:00
|
|
|
if (_add_to_weak_set(impl, &impl->_abc_cache, subclass) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
result = Py_True;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (r < 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No dice; update negative cache. */
|
2024-04-12 00:13:25 +02:00
|
|
|
if (_add_to_weak_set(impl, &impl->_abc_negative_cache, subclass) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
result = Py_False;
|
|
|
|
|
|
|
|
end:
|
2018-03-07 08:27:01 +01:00
|
|
|
Py_DECREF(impl);
|
2018-02-18 13:41:58 +01:00
|
|
|
Py_XDECREF(subclasses);
|
2022-11-14 13:08:15 +01:00
|
|
|
return Py_XNewRef(result);
|
2018-02-18 13:41:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,
|
|
|
|
PyObject **result)
|
|
|
|
{
|
|
|
|
// Fast path: check subclass is in weakref directly.
|
2024-04-12 00:13:25 +02:00
|
|
|
int ret = _in_weak_set(impl, &impl->_abc_registry, subclass);
|
2018-02-18 13:41:58 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
*result = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret > 0) {
|
|
|
|
*result = Py_True;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-04-12 00:13:25 +02:00
|
|
|
PyObject *registry_shared;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(impl);
|
|
|
|
registry_shared = impl->_abc_registry;
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
if (registry_shared == NULL) {
|
2018-02-18 13:41:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2024-04-12 00:13:25 +02:00
|
|
|
|
|
|
|
// Make a local copy of the registry to protect against concurrent
|
|
|
|
// modifications of _abc_registry.
|
2024-04-18 17:18:22 +02:00
|
|
|
PyObject *registry = PyFrozenSet_New(registry_shared);
|
2024-04-12 00:13:25 +02:00
|
|
|
if (registry == NULL) {
|
2018-12-07 11:11:30 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2018-02-18 13:41:58 +01:00
|
|
|
PyObject *key;
|
|
|
|
Py_ssize_t pos = 0;
|
|
|
|
Py_hash_t hash;
|
|
|
|
|
2024-04-12 00:13:25 +02:00
|
|
|
while (_PySet_NextEntry(registry, &pos, &key, &hash)) {
|
2023-06-21 15:44:25 +02:00
|
|
|
PyObject *rkey;
|
2024-04-12 00:13:25 +02:00
|
|
|
if (PyWeakref_GetRef(key, &rkey) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
// Someone inject non-weakref type in the registry.
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
2023-06-21 15:44:25 +02:00
|
|
|
|
|
|
|
if (rkey == NULL) {
|
2018-02-18 13:41:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int r = PyObject_IsSubclass(subclass, rkey);
|
|
|
|
Py_DECREF(rkey);
|
|
|
|
if (r < 0) {
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (r > 0) {
|
2024-04-12 00:13:25 +02:00
|
|
|
if (_add_to_weak_set(impl, &impl->_abc_cache, subclass) < 0) {
|
2018-02-18 13:41:58 +01:00
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*result = Py_True;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 00:13:25 +02:00
|
|
|
Py_DECREF(registry);
|
2018-02-18 13:41:58 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_abc.get_cache_token
|
|
|
|
|
|
|
|
Returns the current ABC cache token.
|
|
|
|
|
|
|
|
The token is an opaque object (supporting equality testing) identifying the
|
|
|
|
current version of the ABC cache for virtual subclasses. The token changes
|
|
|
|
with every call to register() on any ABC.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_abc_get_cache_token_impl(PyObject *module)
|
|
|
|
/*[clinic end generated code: output=c7d87841e033dacc input=70413d1c423ad9f9]*/
|
|
|
|
{
|
2020-05-09 10:31:40 +02:00
|
|
|
_abcmodule_state *state = get_abc_state(module);
|
2024-04-12 00:13:25 +02:00
|
|
|
return PyLong_FromUnsignedLongLong(get_invalidation_counter(state));
|
2018-02-18 13:41:58 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 16:35:38 +02:00
|
|
|
static struct PyMethodDef _abcmodule_methods[] = {
|
2018-02-18 13:41:58 +01:00
|
|
|
_ABC_GET_CACHE_TOKEN_METHODDEF
|
|
|
|
_ABC__ABC_INIT_METHODDEF
|
|
|
|
_ABC__RESET_REGISTRY_METHODDEF
|
|
|
|
_ABC__RESET_CACHES_METHODDEF
|
|
|
|
_ABC__GET_DUMP_METHODDEF
|
|
|
|
_ABC__ABC_REGISTER_METHODDEF
|
|
|
|
_ABC__ABC_INSTANCECHECK_METHODDEF
|
|
|
|
_ABC__ABC_SUBCLASSCHECK_METHODDEF
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
2020-02-17 14:50:35 +01:00
|
|
|
static int
|
2020-03-30 16:35:38 +02:00
|
|
|
_abcmodule_exec(PyObject *module)
|
2020-02-17 14:50:35 +01:00
|
|
|
{
|
2020-03-30 16:35:38 +02:00
|
|
|
_abcmodule_state *state = get_abc_state(module);
|
2020-05-09 10:31:40 +02:00
|
|
|
state->abc_invalidation_counter = 0;
|
|
|
|
state->_abc_data_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &_abc_data_type_spec, NULL);
|
2020-03-30 16:35:38 +02:00
|
|
|
if (state->_abc_data_type == NULL) {
|
2020-02-17 14:50:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2020-03-30 16:35:38 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_abcmodule_traverse(PyObject *module, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
_abcmodule_state *state = get_abc_state(module);
|
|
|
|
Py_VISIT(state->_abc_data_type);
|
2020-02-17 14:50:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:35:38 +02:00
|
|
|
static int
|
|
|
|
_abcmodule_clear(PyObject *module)
|
|
|
|
{
|
|
|
|
_abcmodule_state *state = get_abc_state(module);
|
|
|
|
Py_CLEAR(state->_abc_data_type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_abcmodule_free(void *module)
|
|
|
|
{
|
|
|
|
_abcmodule_clear((PyObject *)module);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyModuleDef_Slot _abcmodule_slots[] = {
|
|
|
|
{Py_mod_exec, _abcmodule_exec},
|
2023-05-05 23:11:27 +02:00
|
|
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
2024-05-03 17:30:55 +02:00
|
|
|
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
|
2020-02-17 14:50:35 +01:00
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
2018-02-18 13:41:58 +01:00
|
|
|
static struct PyModuleDef _abcmodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
2020-10-26 23:19:22 +01:00
|
|
|
.m_name = "_abc",
|
|
|
|
.m_doc = _abc__doc__,
|
|
|
|
.m_size = sizeof(_abcmodule_state),
|
|
|
|
.m_methods = _abcmodule_methods,
|
|
|
|
.m_slots = _abcmodule_slots,
|
|
|
|
.m_traverse = _abcmodule_traverse,
|
|
|
|
.m_clear = _abcmodule_clear,
|
|
|
|
.m_free = _abcmodule_free,
|
2018-02-18 13:41:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__abc(void)
|
|
|
|
{
|
2020-02-17 14:50:35 +01:00
|
|
|
return PyModuleDef_Init(&_abcmodule);
|
2018-02-18 13:41:58 +01:00
|
|
|
}
|