]> granicus.if.org Git - python/commitdiff
also make NotImplementedType callable
authorBenjamin Peterson <benjamin@python.org>
Fri, 29 Jul 2011 23:27:44 +0000 (18:27 -0500)
committerBenjamin Peterson <benjamin@python.org>
Fri, 29 Jul 2011 23:27:44 +0000 (18:27 -0500)
Lib/test/test_builtin.py
Misc/NEWS
Objects/object.c

index 145ae69237f77ecca5dc5caa549a9ac520812453..4e33c234096895c7297c798d0d3149ef51c25efa 100644 (file)
@@ -1344,7 +1344,7 @@ class BuiltinTest(unittest.TestCase):
         self.assertRaises(TypeError, x.translate, b"1"*256, 1)
 
     def test_construct_singletons(self):
-        for const in None, Ellipsis:
+        for const in None, Ellipsis, NotImplemented:
             tp = type(const)
             self.assertIs(tp(), const)
             self.assertRaises(TypeError, tp, 1, 2)
index d0f203a8ca32d2c8fd92f0e1fe761affff4cc910..cdbb09b845d0eb6869709370f27d27ce2cd71cb2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,8 +10,8 @@ What's New in Python 3.3 Alpha 1?
 Core and Builtins
 -----------------
 
-- Make type(None) and type(Ellipsis) callable. They return the respective
-  singleton instances.
+- Make type(None), type(Ellipsis), and type(NotImplemented) callable. They
+  return the respective singleton instances.
 
 - Forbid summing bytes in sum().
 
index f2f4363d6a6304b3a0c8b914383a183337cfea35..cf10f3cb1b24a2237be5e066fbc51407ccfe47be 100644 (file)
@@ -1385,6 +1385,17 @@ NotImplemented_repr(PyObject *op)
     return PyUnicode_FromString("NotImplemented");
 }
 
+static PyObject *
+notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+    if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
+        PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
+        return NULL;
+    }
+    Py_INCREF(Py_NotImplemented);
+    return Py_NotImplemented;
+}
+
 static PyTypeObject PyNotImplemented_Type = {
     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     "NotImplementedType",
@@ -1400,6 +1411,30 @@ static PyTypeObject PyNotImplemented_Type = {
     0,                  /*tp_as_sequence*/
     0,                  /*tp_as_mapping*/
     0,                  /*tp_hash */
+    0,                  /*tp_call */
+    0,                  /*tp_str */
+    0,                  /*tp_getattro */
+    0,                  /*tp_setattro */
+    0,                  /*tp_as_buffer */
+    Py_TPFLAGS_DEFAULT, /*tp_flags */
+    0,                  /*tp_doc */
+    0,                  /*tp_traverse */
+    0,                  /*tp_clear */
+    0,                  /*tp_richcompare */
+    0,                  /*tp_weaklistoffset */
+    0,                  /*tp_iter */
+    0,                  /*tp_iternext */
+    0,                  /*tp_methods */
+    0,                  /*tp_members */
+    0,                  /*tp_getset */
+    0,                  /*tp_base */
+    0,                  /*tp_dict */
+    0,                  /*tp_descr_get */
+    0,                  /*tp_descr_set */
+    0,                  /*tp_dictoffset */
+    0,                  /*tp_init */
+    0,                  /*tp_alloc */
+    notimplemented_new, /*tp_new */
 };
 
 PyObject _Py_NotImplementedStruct = {