]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
make the types of None and Ellipsis callable
authorBenjamin Peterson <benjamin@python.org>
Fri, 29 Jul 2011 23:19:43 +0000 (18:19 -0500)
committerBenjamin Peterson <benjamin@python.org>
Fri, 29 Jul 2011 23:19:43 +0000 (18:19 -0500)
Lib/test/test_builtin.py
Misc/NEWS
Objects/object.c
Objects/sliceobject.c

index aa9b4e2ed8d69221df61de2e25c8612f0c371572..145ae69237f77ecca5dc5caa549a9ac520812453 100644 (file)
@@ -1343,6 +1343,13 @@ class BuiltinTest(unittest.TestCase):
         self.assertRaises(ValueError, x.translate, b"1", 1)
         self.assertRaises(TypeError, x.translate, b"1"*256, 1)
 
+    def test_construct_singletons(self):
+        for const in None, Ellipsis:
+            tp = type(const)
+            self.assertIs(tp(), const)
+            self.assertRaises(TypeError, tp, 1, 2)
+            self.assertRaises(TypeError, tp, a=1, b=2)
+
 class TestSorted(unittest.TestCase):
 
     def test_basic(self):
index 0b205ad9a8b3b54f94ffef385529cccb6bfcb487..d1d13a5d6f89b93c5d0aa1868bead04f495907e7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ 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.
+
 - Forbid summing bytes in sum().
 
 - Verify the types of AST strings and identifiers provided by the user before
index 27d7089dcf826b380bef071879d5f738b653399f..f2f4363d6a6304b3a0c8b914383a183337cfea35 100644 (file)
@@ -1277,6 +1277,16 @@ none_dealloc(PyObject* ignore)
     Py_FatalError("deallocating None");
 }
 
+static PyObject *
+none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+    if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
+        PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
+        return NULL;
+    }
+    Py_RETURN_NONE;
+}
+
 static int
 none_bool(PyObject *v)
 {
@@ -1335,6 +1345,30 @@ static PyTypeObject PyNone_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 */
+    none_new,           /*tp_new */
 };
 
 PyObject _Py_NoneStruct = {
index 51c53a8a11c3c33778d1b4d4d8247e4b279d0387..53cc951a735cee904f98fdc56c88baffaecbe049 100644 (file)
@@ -16,6 +16,17 @@ this type and there is exactly one in existence.
 #include "Python.h"
 #include "structmember.h"
 
+static PyObject *
+ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+    if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
+        PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments");
+        return NULL;
+    }
+    Py_INCREF(Py_Ellipsis);
+    return Py_Ellipsis;
+}
+
 static PyObject *
 ellipsis_repr(PyObject *op)
 {
@@ -43,6 +54,24 @@ PyTypeObject PyEllipsis_Type = {
     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 */
+    ellipsis_new,                       /* tp_new */
 };
 
 PyObject _Py_EllipsisObject = {