Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-31333: Re-implement ABCMeta in C #5273

Merged
merged 103 commits into from
Feb 18, 2018
Merged
Show file tree
Hide file tree
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
ilevkivskyi Jul 23, 2017
cb7ffcf
Basic implementation of ABCMeta.__new__
ilevkivskyi Jul 25, 2017
b83ee80
Bare-bone implementation of register and subclass checks
ilevkivskyi Jul 25, 2017
181e83f
Fix mock failure and silence compiler warnings
ilevkivskyi Jul 26, 2017
c084a7f
Provide nicer dump of registry
ilevkivskyi Jul 26, 2017
a192d5d
Add better docstrings
ilevkivskyi Jul 26, 2017
35a2472
Expose the internal cavhes and registry (backward compatibility)
ilevkivskyi Jul 27, 2017
4812450
Merge remote-tracking branch 'upstream/master' into c-abc
ilevkivskyi 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
methane Jan 24, 2018
1f7aee9
Use Py_RETURN_NONE
methane Jan 24, 2018
11fea70
Use _PyObject_IsAbstract()
methane Jan 24, 2018
7ff3fbb
Use _PySet_NextEntry
methane 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
methane Jan 27, 2018
86af9ae
Refactor __abstractmethods__ calculation.
methane 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
methane 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
methane Jan 28, 2018
8174b61
Use c99 designated initializer.
methane Jan 28, 2018
bb8d623
Fix gset_new() and abc_data_new()
methane Jan 28, 2018
ef59e54
Massive refactoring...
methane Jan 28, 2018
22699fe
fixup
methane 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.
methane Jan 29, 2018
d4d78a1
Copy set before iterating, and remove guarded set
methane Jan 29, 2018
f58822e
Add NULL check after PyWeakref_GetObject
methane Jan 30, 2018
3b74bdc
Add fast path for looking register
methane Jan 30, 2018
db1c852
Check negative cache version before cache lookup
methane Jan 30, 2018
6e62be7
Merge pull request #5 from methane/c-abc-no-guard-set
ilevkivskyi Feb 11, 2018
5384726
Fix nits pointed by pppery.
methane Feb 12, 2018
a48eecc
Create set lazily
methane Feb 14, 2018
16a8db1
Explicitly set NULL
methane Feb 14, 2018
5ad3ea8
Merge pull request #6 from methane/c-abc-lazyset
ilevkivskyi Feb 14, 2018
86a9b8d
Check PyObject_IsTrue() error
methane Feb 14, 2018
36c2013
Strip TODO comments.
methane Feb 14, 2018
09c5370
Add NEWS entry
methane Feb 14, 2018
207d8e9
Fix _reset_caches
methane Feb 15, 2018
a15377b
Add default: Py_UNREACHABLE()
methane Feb 15, 2018
d31da13
Rephrase NEWS entry
methane 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
Convert _abc to Argument Clinic
  • Loading branch information
Ivan Levkivskyi committed Feb 17, 2018
commit 001b416685b5a0c24d56eeb41a11255bd29ee875
202 changes: 113 additions & 89 deletions Modules/_abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#include "Python.h"
#include "structmember.h"
#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");
Expand Down Expand Up @@ -156,19 +162,21 @@ _add_to_weak_set(PyObject **pset, PyObject *obj)
return ret;
}

/*[clinic input]
_abc._reset_registry

self: object
/

Internal ABC helper to reset registry of a given class.

PyDoc_STRVAR(_reset_registry_doc,
"Internal ABC helper to reset registry of a given class.\n\
\n\
Should be only used by refleak.py");
Should be only used by refleak.py
[clinic start generated code]*/

static PyObject *
_reset_registry(PyObject *m, PyObject *args)
_abc__reset_registry(PyObject *module, PyObject *self)
/*[clinic end generated code: output=92d591a43566cc10 input=12a0b7eb339ac35c]*/
{
PyObject *self;
if (!PyArg_UnpackTuple(args, "_reset_registry", 1, 1, &self)) {
return NULL;
}
_abc_data *impl = _get_impl(self);
if (impl == NULL) {
return NULL;
Expand All @@ -181,20 +189,22 @@ _reset_registry(PyObject *m, PyObject *args)
Py_RETURN_NONE;
}

PyDoc_STRVAR(_reset_caches_doc,
"Internal ABC helper to reset both caches of a given class.\n\
\n\
Should be only used by refleak.py");
/*[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 *
_reset_caches(PyObject *m, PyObject *args)
_abc__reset_caches(PyObject *module, PyObject *self)
/*[clinic end generated code: output=f296f0d5c513f80c input=c0ac616fd8acfb6f]*/
{
PyObject *self;
_abc_data *impl;
if (!PyArg_UnpackTuple(args, "_reset_caches", 1, 1, &self)) {
return NULL;
}
impl = _get_impl(self);
_abc_data *impl = _get_impl(self);
if (impl == NULL) {
return NULL;
}
Expand All @@ -212,22 +222,25 @@ _reset_caches(PyObject *m, PyObject *args)
Py_RETURN_NONE;
}

PyDoc_STRVAR(_get_dump_doc,
"Internal ABC helper for cache and registry debugging.\n\
\n\
Return shallow copies of registry, of both caches, and\n\
negative cache version. Don't call this function directly,\n\
instead use ABC._dump_registry() for a nice repr.");
/*[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 *
_get_dump(PyObject *m, PyObject *args)
_abc__get_dump(PyObject *module, PyObject *self)
/*[clinic end generated code: output=9d9569a8e2c1c443 input=2c5deb1bfe9e3c79]*/
{
PyObject *self, *registry, *cache, *negative_cache;
_abc_data *impl;
if (!PyArg_UnpackTuple(args, "_get_dump", 1, 1, &self)) {
return NULL;
}
impl = _get_impl(self);
PyObject *registry, *cache, *negative_cache;
_abc_data *impl = _get_impl(self);
if (impl == NULL) {
return NULL;
}
Expand Down Expand Up @@ -383,17 +396,20 @@ compute_abstract_methods(PyObject *self)
return ret;
}

PyDoc_STRVAR(_abc_init_doc,
"Internal ABC helper for class set-up. Should be never used outside abc module");
/*[clinic input]
_abc._abc_init

self: object
/

Internal ABC helper for class set-up. Should be never used outside abc module
Copy link
Member

Choose a reason for hiding this comment

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

Missed period at the end.

[clinic start generated code]*/

static PyObject *
_abc_init(PyObject *m, PyObject *args)
_abc__abc_init(PyObject *module, PyObject *self)
/*[clinic end generated code: output=594757375714cda1 input=6061a045416da18b]*/
{
PyObject *self, *data;
if (!PyArg_UnpackTuple(args, "_abc_init", 1, 1, &self)) {
return NULL;
}

PyObject *data;
if (compute_abstract_methods(self) < 0) {
return NULL;
}
Expand All @@ -411,16 +427,20 @@ _abc_init(PyObject *m, PyObject *args)
Py_RETURN_NONE;
}

PyDoc_STRVAR(_abc_register_doc,
"Internal ABC helper for subclasss registration. Should be never used outside abc module");
/*[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_register(PyObject *m, PyObject *args)
_abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass)
/*[clinic end generated code: output=7851e7668c963524 input=4b94c86e77e9e901]*/
{
PyObject *self, *subclass = NULL;
if (!PyArg_UnpackTuple(args, "_abc_register", 2, 2, &self, &subclass)) {
return NULL;
}
if (!PyType_Check(subclass)) {
PyErr_SetString(PyExc_TypeError, "Can only register classes");
return NULL;
Expand Down Expand Up @@ -470,18 +490,23 @@ _abc_register(PyObject *m, PyObject *args)
return subclass;
}

PyDoc_STRVAR(_abc_instancecheck_doc,
"Internal ABC helper for instance checks. Should be never used outside abc module");

/*[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_instancecheck(PyObject *m, PyObject *args)
_abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
PyObject *instance)
/*[clinic end generated code: output=b8b5148f63b6b56f input=5b15699272795a93]*/
{
PyObject *self, *result = NULL, *subclass = NULL,
*subtype, *instance = NULL;
if (!PyArg_UnpackTuple(args, "_abc_instancecheck", 2, 2, &self, &instance)) {
return NULL;
}

PyObject *subtype, *result = NULL, *subclass = NULL;
_abc_data *impl = _get_impl(self);
if (impl == NULL) {
return NULL;
Expand Down Expand Up @@ -555,20 +580,24 @@ _abc_instancecheck(PyObject *m, PyObject *args)
static int subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,
PyObject **result);

PyDoc_STRVAR(_abc_subclasscheck_doc,
"Internal ABC helper for subclasss checks. Should be never used outside abc module");
/*[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_subclasscheck(PyObject *m, PyObject *args)
_abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
PyObject *subclass)
/*[clinic end generated code: output=b56c9e4a530e3894 input=4c87faea511976a8]*/
{
PyObject *self, *subclasses = NULL, *subclass = NULL, *result = NULL;
PyObject *ok, *mro;
PyObject *ok, *mro, *subclasses = NULL, *result = NULL;
Py_ssize_t pos;
int incache;
if (!PyArg_UnpackTuple(args, "_abc_subclasscheck", 2, 2, &self, &subclass)) {
return NULL;
}

_abc_data *impl = _get_impl(self);
if (impl == NULL) {
return NULL;
Expand Down Expand Up @@ -768,38 +797,33 @@ subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,
return ret;
}

/*[clinic input]
_abc.get_cache_token

Returns the current ABC cache token.

PyDoc_STRVAR(_cache_token_doc,
"Returns the current ABC cache token.\n\
\n\
The token is an opaque object (supporting equality testing) identifying the\n\
current version of the ABC cache for virtual subclasses. The token changes\n\
with every call to ``register()`` on any ABC.");
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.
Copy link
Member

Choose a reason for hiding this comment

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

Double bacticks look not needed.

[clinic start generated code]*/

static PyObject *
get_cache_token(void)
_abc_get_cache_token_impl(PyObject *module)
/*[clinic end generated code: output=c7d87841e033dacc input=2a19dea381467239]*/
{
Py_INCREF(abc_invalidation_counter);
return abc_invalidation_counter;
}

static struct PyMethodDef module_functions[] = {
{"get_cache_token", (PyCFunction)get_cache_token, METH_NOARGS,
_cache_token_doc},
{"_abc_init", (PyCFunction)_abc_init, METH_VARARGS,
_abc_init_doc},
{"_reset_registry", (PyCFunction)_reset_registry, METH_VARARGS,
_reset_registry_doc},
{"_reset_caches", (PyCFunction)_reset_caches, METH_VARARGS,
_reset_caches_doc},
{"_get_dump", (PyCFunction)_get_dump, METH_VARARGS,
_get_dump_doc},
{"_abc_register", (PyCFunction)_abc_register, METH_VARARGS,
_abc_register_doc},
{"_abc_instancecheck", (PyCFunction)_abc_instancecheck, METH_VARARGS,
_abc_instancecheck_doc},
{"_abc_subclasscheck", (PyCFunction)_abc_subclasscheck, METH_VARARGS,
_abc_subclasscheck_doc},
_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 */
};

Expand Down
Loading