summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_new.py10
-rw-r--r--Objects/classobject.c6
2 files changed, 16 insertions, 0 deletions
diff --git a/Lib/test/test_new.py b/Lib/test/test_new.py
index f022f7e..4aab1e2 100644
--- a/Lib/test/test_new.py
+++ b/Lib/test/test_new.py
@@ -47,6 +47,16 @@ im()
verify(c.get_yolks() == 1 and c.get_more_yolks() == 4,
'Broken call of hand-crafted instance method')
+im = new.instancemethod(break_yolks, c)
+im()
+verify(c.get_yolks() == -1)
+try:
+ new.instancemethod(break_yolks, None)
+except TypeError:
+ pass
+else:
+ raise TestFailed, "dangerous instance method creation allowed"
+
# It's unclear what the semantics should be for a code object compiled at
# module scope, but bound and run in a function. In CPython, `c' is global
# (by accident?) while in Jython, `c' is local. The intent of the test
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 506faab..68505f1 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -2208,6 +2208,12 @@ instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
}
if (self == Py_None)
self = NULL;
+ if (self == NULL && classObj == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "unbound methods must have non-NULL im_class");
+ return NULL;
+ }
+
return PyMethod_New(func, self, classObj);
}