summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@dropbox.com>2016-08-18 16:22:23 (GMT)
committerGuido van Rossum <guido@dropbox.com>2016-08-18 16:22:23 (GMT)
commit97c1adf3935234da716d3289b85f72dcd67e90c2 (patch)
tree0af6f9f258cf26ee9e59db463cc89d04c45bc0b8 /Objects
parent0a6996d87d19a524c2a11dd315d96d12083c47d4 (diff)
downloadcpython-97c1adf3935234da716d3289b85f72dcd67e90c2.zip
cpython-97c1adf3935234da716d3289b85f72dcd67e90c2.tar.gz
cpython-97c1adf3935234da716d3289b85f72dcd67e90c2.tar.bz2
Anti-registration of various ABC methods.
- Issue #25958: Support "anti-registration" of special methods from various ABCs, like __hash__, __iter__ or __len__. All these (and several more) can be set to None in an implementation class and the behavior will be as if the method is not defined at all. (Previously, this mechanism existed only for __hash__, to make mutable classes unhashable.) Code contributed by Andrew Barnert and Ivan Levkivskyi.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/enumobject.c12
-rw-r--r--Objects/typeobject.c14
2 files changed, 24 insertions, 2 deletions
diff --git a/Objects/enumobject.c b/Objects/enumobject.c
index c458cfe..dae166d 100644
--- a/Objects/enumobject.c
+++ b/Objects/enumobject.c
@@ -250,6 +250,13 @@ reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
reversed_meth = _PyObject_LookupSpecial(seq, &PyId___reversed__);
+ if (reversed_meth == Py_None) {
+ Py_DECREF(reversed_meth);
+ PyErr_Format(PyExc_TypeError,
+ "'%.200s' object is not reversible",
+ Py_TYPE(seq)->tp_name);
+ return NULL;
+ }
if (reversed_meth != NULL) {
PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL);
Py_DECREF(reversed_meth);
@@ -259,8 +266,9 @@ reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
if (!PySequence_Check(seq)) {
- PyErr_SetString(PyExc_TypeError,
- "argument to reversed() must be a sequence");
+ PyErr_Format(PyExc_TypeError,
+ "'%.200s' object is not reversible",
+ Py_TYPE(seq)->tp_name);
return NULL;
}
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index b1686d2..1b4c261 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -5856,6 +5856,13 @@ slot_sq_contains(PyObject *self, PyObject *value)
_Py_IDENTIFIER(__contains__);
func = lookup_maybe(self, &PyId___contains__);
+ if (func == Py_None) {
+ Py_DECREF(func);
+ PyErr_Format(PyExc_TypeError,
+ "'%.200s' object is not a container",
+ Py_TYPE(self)->tp_name);
+ return -1;
+ }
if (func != NULL) {
args = PyTuple_Pack(1, value);
if (args == NULL)
@@ -6241,6 +6248,13 @@ slot_tp_iter(PyObject *self)
_Py_IDENTIFIER(__iter__);
func = lookup_method(self, &PyId___iter__);
+ if (func == Py_None) {
+ Py_DECREF(func);
+ PyErr_Format(PyExc_TypeError,
+ "'%.200s' object is not iterable",
+ Py_TYPE(self)->tp_name);
+ return NULL;
+ }
if (func != NULL) {
PyObject *args;
args = res = PyTuple_New(0);