Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
5c34508
Initial work on C implementation of ABCMeta
Jul 23, 2017
cb7ffcf
Basic implementation of ABCMeta.__new__
Jul 25, 2017
b83ee80
Bare-bone implementation of register and subclass checks
Jul 25, 2017
181e83f
Fix mock failure and silence compiler warnings
Jul 26, 2017
c084a7f
Provide nicer dump of registry
Jul 26, 2017
a192d5d
Add better docstrings
Jul 26, 2017
35a2472
Expose the internal cavhes and registry (backward compatibility)
Jul 27, 2017
4812450
Merge remote-tracking branch 'upstream/master' into c-abc
Sep 3, 2017
b9038e2
Merge remote-tracking branch 'upstream/master' into c-abc
Jan 20, 2018
bbee578
Add _abc to Setup.dist
Jan 20, 2018
a3464fd
Fix _abc in Setup.dist
Jan 20, 2018
947bf7d
Update a comment
Jan 20, 2018
7ffc59e
Settle the .py version
Jan 20, 2018
41287a7
Fix some TODOs and refleaks
Jan 21, 2018
569cc44
Fix some more refleaks; use weak refs in registry
Jan 21, 2018
34665a8
Some more fixes; add some caching
Jan 21, 2018
576acac
Fix a crash due to erroneous DECREF
Jan 21, 2018
30098b4
Simplify some code; reorganize TODOs
Jan 22, 2018
51ede5d
Finish caches; add more comments
Jan 22, 2018
5263e1a
Use Py_RETURN_TRUE/FALSE and fix refleak
Jan 24, 2018
1f7aee9
Use Py_RETURN_NONE
Jan 24, 2018
11fea70
Use _PyObject_IsAbstract()
Jan 24, 2018
7ff3fbb
Use _PySet_NextEntry
Jan 24, 2018
9b4eb2f
Minor review comments
Jan 25, 2018
ab20a33
Sketch the new API
Jan 25, 2018
39f2692
Merge remote-tracking branch 'upstream/master' into c-abc
Jan 25, 2018
493d0ec
Use _PyObject_LookupAttr
Jan 25, 2018
2fe2c54
Some more progress
Jan 26, 2018
9476af6
Implement weakref callbacks and guarded iteration
Jan 26, 2018
ab68cdb
Fix some errors
Jan 26, 2018
3eb0a60
Fix two review comments
Jan 26, 2018
ed36b76
More fixes, test_abc passes
Jan 27, 2018
25fc5b9
Merge remote-tracking branch 'upstream/master' into c-abc
Jan 27, 2018
b2f75b9
Fix some remaining problems
Jan 27, 2018
cdb5cdf
Update TODO
Jan 27, 2018
a1a3a52
Add missing statics
Jan 27, 2018
a66b08c
Build on Windows
Jan 27, 2018
86af9ae
Refactor __abstractmethods__ calculation.
Jan 27, 2018
b22232a
Refactor via _abc_impl
Jan 27, 2018
e51c5ca
Fix some refleaks
Jan 27, 2018
bac7a43
Add docs (required by some tests) and initialization
Jan 27, 2018
4571649
Merge remote-tracking branch 'upstream/master' into c-abc
Jan 27, 2018
0d7513b
Outdated comment and detection of intrusions
Jan 27, 2018
c429f49
Minor fixes
Jan 27, 2018
357b56d
Few more refleaks
Jan 27, 2018
cd80fcb
Restore unwanted changes
Jan 28, 2018
34e13c3
Fix(?) some more refleaks
Jan 28, 2018
c5633b6
Reset caches between runs
Jan 28, 2018
3cbbc12
Fix abuse of borrowed reference
Jan 28, 2018
23bcb07
Fix remaining refleaks
Jan 28, 2018
0aab479
Updare TODO, switch to common result agreement, few more checks
Jan 28, 2018
86e0660
Fix typos
Jan 28, 2018
c55e482
Remove irrelevant TODOs, add few comments
Jan 28, 2018
5f9526a
Use Py_ssize_t for iterating
Jan 28, 2018
8174b61
Use c99 designated initializer.
Jan 28, 2018
bb8d623
Fix gset_new() and abc_data_new()
Jan 28, 2018
ef59e54
Massive refactoring...
Jan 28, 2018
22699fe
fixup
Jan 28, 2018
95cbf34
Review comments
Jan 28, 2018
4d596cc
More refactoring
Jan 28, 2018
fa3cba3
Typos and minor fixes
Jan 28, 2018
6f18293
Minor fixes and code style
Jan 28, 2018
9100891
Remove some unreachable code
Jan 28, 2018
36c5643
Review comments
Jan 28, 2018
dd2abda
Split the class into two separate implementations
Jan 28, 2018
99d950c
Add version independent (Py vs C) cache clearing
Jan 28, 2018
3762d49
Test the six-like tricky type.__new__(metaclass) with ABCMeta
Jan 28, 2018
404d1ce
Test both versions
Jan 28, 2018
0dc5fae
Add comment about testing
Jan 28, 2018
287b26a
Always DECREF after PyTuplr_Pack
Jan 28, 2018
ef34364
Use PySet_New() instead of calling copy() method.
Jan 29, 2018
d4d78a1
Copy set before iterating, and remove guarded set
Jan 29, 2018
f58822e
Add NULL check after PyWeakref_GetObject
Jan 30, 2018
3b74bdc
Add fast path for looking register
Jan 30, 2018
db1c852
Check negative cache version before cache lookup
Jan 30, 2018
6e62be7
Merge pull request #5 from methane/c-abc-no-guard-set
Feb 11, 2018
5384726
Fix nits pointed by pppery.
Feb 12, 2018
a48eecc
Create set lazily
Feb 14, 2018
16a8db1
Explicitly set NULL
Feb 14, 2018
5ad3ea8
Merge pull request #6 from methane/c-abc-lazyset
Feb 14, 2018
86a9b8d
Check PyObject_IsTrue() error
Feb 14, 2018
36c2013
Strip TODO comments.
Feb 14, 2018
09c5370
Add NEWS entry
Feb 14, 2018
207d8e9
Fix _reset_caches
Feb 15, 2018
a15377b
Add default: Py_UNREACHABLE()
Feb 15, 2018
d31da13
Rephrase NEWS entry
Feb 15, 2018
eaff1cb
Merge remote-tracking branch 'upstream/master' into c-abc
Feb 16, 2018
48de70e
Factor out Python version to a separate file
Feb 16, 2018
3bd0666
Factor out Python version to a separate file
Feb 16, 2018
702347a
Remove extra whitespace
Feb 16, 2018
e0c978b
Add more details to NEWS
Feb 16, 2018
b370dfe
Fix an import in refleak test
Feb 16, 2018
a1ae0a7
Restart tests
Feb 16, 2018
4746211
Make order of subclass checks in Python version stable and consistent…
Feb 16, 2018
001b416
Convert _abc to Argument Clinic
Feb 17, 2018
fc528df
Merge remote-tracking branch 'upstream/master' into c-abc
Feb 17, 2018
289c414
Regenerate clinic
Feb 17, 2018
ac0c639
Switch from Python invalidation counter to C long long
Feb 17, 2018
079e3be
The rest of the comments
Feb 17, 2018
9c49e5a
Merge remote-tracking branch 'upstream/master' into c-abc
Feb 17, 2018
4146588
Regenerate clinics.
Feb 17, 2018
c133605
Two more comments
Feb 17, 2018
f82e04d
Few more comments by Serhiy; add Whats New item
Feb 18, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expose the internal cavhes and registry (backward compatibility)
  • Loading branch information
ilevkivskyi committed Jul 27, 2017
commit 35a24728420b6d3ca04d1f9323d87d0f34de5896
71 changes: 58 additions & 13 deletions Modules/_abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
/* In particular use capitals like PyList_GET_SIZE */
/* Think (ask) about inlining some calls, like __subclasses__ */
/* Use PyId instead of string attrs */
/* Work on INCREF/DECREF */
/* Be sure to return NULL where exception possible */
/* Use macros */

#include "Python.h"
#include "structmember.h"
Expand Down Expand Up @@ -45,9 +48,15 @@ abcmeta_traverse(PyObject *self, visitproc visit, void *arg)
static int
abcmeta_clear(abc *tp)
{
PySet_Clear(tp->abc_registry);
PySet_Clear(tp->abc_cache);
PySet_Clear(tp->abc_negative_cache);
if (tp->abc_registry) { /* This may be because _abc_registry is writeable (and was shared by typing) */
PySet_Clear(tp->abc_registry);
}
if (tp->abc_cache) {
PySet_Clear(tp->abc_cache);
}
if (tp->abc_negative_cache) {
PySet_Clear(tp->abc_negative_cache);
}
return PyType_Type.tp_clear((PyObject *)tp);
}

Expand All @@ -58,7 +67,6 @@ abcmeta_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *ns, *bases, *items, *abstracts, *is_abstract, *base_abstracts;
PyObject *key, *value, *item, *iter;
Py_ssize_t pos = 0;

result = (abc *)PyType_Type.tp_new(type, args, kwds);
if (!result) {
return NULL;
Expand Down Expand Up @@ -141,21 +149,30 @@ static PyObject *
abcmeta_register(abc *self, PyObject *args)
{
PyObject *subclass = NULL;
int result;
if (!PyArg_UnpackTuple(args, "register", 1, 1, &subclass)) {
return NULL;
}
if (!PyType_Check(subclass)) {
PyErr_SetString(PyExc_TypeError, "Can only register classes");
return NULL;
}
if (PyObject_IsSubclass(subclass, (PyObject *)self)) { /* TODO: Check for error here */
result = PyObject_IsSubclass(subclass, (PyObject *)self);
if (result > 0) {
Py_INCREF(subclass);
return subclass;
}
if (PyObject_IsSubclass((PyObject *)self, subclass)) { /* TODO: Check for error here */
if (result < 0) {
return NULL;
}
result = PyObject_IsSubclass((PyObject *)self, subclass);
if (result > 0) {
PyErr_SetString(PyExc_RuntimeError, "Refusing to create an inheritance cycle");
return NULL;
}
if (result < 0) {
return NULL;
}
if (PySet_Add(self->abc_registry, subclass) < 0) {
return NULL;
}
Expand All @@ -170,13 +187,17 @@ abcmeta_subclasscheck(abc *self, PyObject *args); /* Forward */
static PyObject *
abcmeta_instancecheck(abc *self, PyObject *args)
{
PyObject *subclass, *instance = NULL;
if (!PyArg_UnpackTuple(args, "__isinstance__", 1, 1, &instance)) {
PyObject *result, *subclass, *instance = NULL;
if (!PyArg_UnpackTuple(args, "__instancecheck__", 1, 1, &instance)) {
return NULL;
}
subclass = (PyObject *)Py_TYPE(instance);
/* TODO: Use cache */
if (abcmeta_subclasscheck(self, PyTuple_Pack(1, subclass)) == Py_True) { /* TODO: Refactor to avoid packing */
result = abcmeta_subclasscheck(self, PyTuple_Pack(1, subclass));
if (!result) {
return NULL;
}
if (result == Py_True) { /* TODO: Refactor to avoid packing */
return Py_True;
}
subclass = PyObject_GetAttrString(instance, "__class__");
Expand All @@ -189,14 +210,18 @@ abcmeta_subclasscheck(abc *self, PyObject *args)
PyObject *subclasses, *subclass = NULL;
PyObject *ok, *mro, *iter, *key;
Py_ssize_t pos;
if (!PyArg_UnpackTuple(args, "__issubclass__", 1, 1, &subclass)) {
int result;
if (!PyArg_UnpackTuple(args, "__subclasscheck__", 1, 1, &subclass)) {
return NULL;
}
/* TODO: clear the registry from dead refs from time to time
on iteration here (have a counter for this) */
/* TODO: Reset caches every n-th succes/failure correspondingly
so that they don't grow too large */
ok = PyObject_CallMethod((PyObject *)self, "__subclasshook__", "O", subclass);
if (!ok) {
return NULL;
}
if (ok == Py_True) {
Py_INCREF(Py_True);
return Py_True;
Expand All @@ -214,19 +239,27 @@ abcmeta_subclasscheck(abc *self, PyObject *args)
}
iter = PyObject_GetIter(self->abc_registry);
while ((key = PyIter_Next(iter))) {
if (PyObject_IsSubclass(subclass, key)) {
result = PyObject_IsSubclass(subclass, key);
if (result > 0) {
Py_INCREF(Py_True);
return Py_True;
}
if (result < 0) {
return NULL;
}
Py_DECREF(key);
}
Py_DECREF(iter);
subclasses = PyObject_CallMethod((PyObject *)self, "__subclasses__", NULL);
for (pos = 0; pos < PyList_GET_SIZE(subclasses); pos++) {

ghost Jan 25, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily a list; __subclasses__ could be overridden

if (PyObject_IsSubclass(subclass, PyList_GET_ITEM(subclasses, pos))) {
result = PyObject_IsSubclass(subclass, PyList_GET_ITEM(subclasses, pos));
if (result > 0) {
Py_INCREF(Py_True);
return Py_True;
}
if (result < 0) {
return NULL;
}
}
Py_INCREF(Py_False);
return Py_False;
Expand Down Expand Up @@ -342,6 +375,18 @@ static PyMethodDef abcmeta_methods[] = {
{NULL, NULL},
};

static PyMemberDef abc_members[] = {
{"_abc_registry", T_OBJECT, offsetof(abc, abc_registry), 0, /* Maybe make these READONLY */
PyDoc_STR("ABC registry (private).")},
{"_abc_cache", T_OBJECT, offsetof(abc, abc_cache), 0,
PyDoc_STR("ABC positive cache (private).")},
{"_abc_negative_cache", T_OBJECT, offsetof(abc, abc_negative_cache), 0,
PyDoc_STR("ABC negative cache (private).")},
{"_abc_negative_cache_version", T_OBJECT, offsetof(abc, abc_negative_cache), 0,
PyDoc_STR("ABC negative cache version (private).")},
{NULL}
};

PyDoc_STRVAR(abcmeta_doc,
"Metaclass for defining Abstract Base Classes (ABCs).\n\
\n\
Expand Down Expand Up @@ -385,7 +430,7 @@ PyTypeObject ABCMeta = {
0, /* tp_iter */
0, /* tp_iternext */
abcmeta_methods, /* tp_methods */
0, /* tp_members */
abc_members, /* tp_members */
0, /* tp_getset */
DEFERRED_ADDRESS(&PyType_Type), /* tp_base */
0, /* tp_dict */
Expand Down