[Python-checkins] cpython: make the types of None and Ellipsis callable

benjamin.peterson python-checkins at python.org
Sat Jul 30 01:20:44 CEST 2011


http://hg.python.org/cpython/rev/84c3be27b4c7
changeset:   71614:84c3be27b4c7
parent:      71611:a6afd26caa8a
user:        Benjamin Peterson <benjamin at python.org>
date:        Fri Jul 29 18:19:43 2011 -0500
summary:
  make the types of None and Ellipsis callable

files:
  Lib/test/test_builtin.py |   7 +++++
  Misc/NEWS                |   3 ++
  Objects/object.c         |  34 ++++++++++++++++++++++++++++
  Objects/sliceobject.c    |  29 +++++++++++++++++++++++
  4 files changed, 73 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1343,6 +1343,13 @@
         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):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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
diff --git a/Objects/object.c b/Objects/object.c
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1277,6 +1277,16 @@
     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 @@
     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 = {
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -17,6 +17,17 @@
 #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)
 {
     return PyUnicode_FromString("Ellipsis");
@@ -43,6 +54,24 @@
     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 = {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list