-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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-43977: Use tp_flags for collection matching #25723
Changes from 6 commits
6971b4d
b81bcf5
a87ae7a
52be43b
b0c7738
ddef5fb
6d448a7
b78d63e
8001e6e
0ff9a88
c0e75a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,3 @@ | ||||||||||||||
Use tp_flags on the class object to determine if the subject is a sequence | ||||||||||||||
or mapping when pattern matching. Avoids the need to import collections.abc | ||||||||||||||
when pattern matching. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just some markup to make this a bit richer:
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ PyDoc_STRVAR(_abc__doc__, | |
_Py_IDENTIFIER(__abstractmethods__); | ||
_Py_IDENTIFIER(__class__); | ||
_Py_IDENTIFIER(__dict__); | ||
_Py_IDENTIFIER(__flags__); | ||
_Py_IDENTIFIER(__bases__); | ||
_Py_IDENTIFIER(_abc_impl); | ||
_Py_IDENTIFIER(__subclasscheck__); | ||
|
@@ -417,6 +418,8 @@ compute_abstract_methods(PyObject *self) | |
return ret; | ||
} | ||
|
||
#define COLLECTION_FLAGS (Py_TPFLAGS_SEQUENCE | Py_TPFLAGS_MAPPING) | ||
|
||
/*[clinic input] | ||
_abc._abc_init | ||
|
||
|
@@ -446,6 +449,27 @@ _abc__abc_init(PyObject *module, PyObject *self) | |
return NULL; | ||
} | ||
Py_DECREF(data); | ||
if (PyType_Check(self)) { | ||
PyTypeObject *cls = (PyTypeObject *)self; | ||
PyObject *flags = _PyDict_GetItemIdWithError(cls->tp_dict, &PyId___flags__); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this whole mechanism makes me a bit uncomfortable. I'm not aware of any case where writing to But I can't really think of anything better, so I guess it's fine. Maybe we could just use a name like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, it is just a name. I like using a dunder name, as it is a special thing. |
||
if (flags == NULL) { | ||
if (PyErr_Occurred()) { | ||
return NULL; | ||
} | ||
} | ||
else { | ||
if (PyLong_CheckExact(flags)) { | ||
long val = PyLong_AsLong(flags); | ||
if (val == -1 && PyErr_Occurred()) { | ||
return NULL; | ||
} | ||
((PyTypeObject *)self)->tp_flags |= (val & COLLECTION_FLAGS); | ||
} | ||
if (_PyDict_DelItemId(cls->tp_dict, &PyId___flags__) < 0) { | ||
return NULL; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we deleting >>> class C:
... __flags__ = "Spam"
...
>>> C.__flags__
284160 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
} | ||
Py_RETURN_NONE; | ||
} | ||
|
||
|
@@ -499,6 +523,11 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass) | |
/* Invalidate negative cache */ | ||
get_abc_state(module)->abc_invalidation_counter++; | ||
|
||
if (PyType_Check(subclass) && PyType_Check(self) && | ||
!PyType_HasFeature((PyTypeObject *)subclass, Py_TPFLAGS_IMMUTABLETYPE)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
{ | ||
((PyTypeObject *)subclass)->tp_flags |= (((PyTypeObject *)self)->tp_flags & COLLECTION_FLAGS); | ||
} | ||
Py_INCREF(subclass); | ||
return subclass; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is public and can be relied upon, it needs to be added to the docs for collections.abc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be internal. I'll hide it.