Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4607ae

Browse files
committed
make the types of None and Ellipsis callable
1 parent 4f921c2 commit c4607ae

4 files changed

Lines changed: 73 additions & 0 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,13 @@ def test_bytearray_translate(self):
13431343
self.assertRaises(ValueError, x.translate, b"1", 1)
13441344
self.assertRaises(TypeError, x.translate, b"1"*256, 1)
13451345

1346+
def test_construct_singletons(self):
1347+
for const in None, Ellipsis:
1348+
tp = type(const)
1349+
self.assertIs(tp(), const)
1350+
self.assertRaises(TypeError, tp, 1, 2)
1351+
self.assertRaises(TypeError, tp, a=1, b=2)
1352+
13461353
class TestSorted(unittest.TestCase):
13471354

13481355
def test_basic(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Make type(None) and type(Ellipsis) callable. They return the respective
14+
singleton instances.
15+
1316
- Forbid summing bytes in sum().
1417

1518
- Verify the types of AST strings and identifiers provided by the user before

Objects/object.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,16 @@ none_dealloc(PyObject* ignore)
12771277
Py_FatalError("deallocating None");
12781278
}
12791279

1280+
static PyObject *
1281+
none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1282+
{
1283+
if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
1284+
PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1285+
return NULL;
1286+
}
1287+
Py_RETURN_NONE;
1288+
}
1289+
12801290
static int
12811291
none_bool(PyObject *v)
12821292
{
@@ -1335,6 +1345,30 @@ static PyTypeObject PyNone_Type = {
13351345
0, /*tp_as_sequence*/
13361346
0, /*tp_as_mapping*/
13371347
0, /*tp_hash */
1348+
0, /*tp_call */
1349+
0, /*tp_str */
1350+
0, /*tp_getattro */
1351+
0, /*tp_setattro */
1352+
0, /*tp_as_buffer */
1353+
Py_TPFLAGS_DEFAULT, /*tp_flags */
1354+
0, /*tp_doc */
1355+
0, /*tp_traverse */
1356+
0, /*tp_clear */
1357+
0, /*tp_richcompare */
1358+
0, /*tp_weaklistoffset */
1359+
0, /*tp_iter */
1360+
0, /*tp_iternext */
1361+
0, /*tp_methods */
1362+
0, /*tp_members */
1363+
0, /*tp_getset */
1364+
0, /*tp_base */
1365+
0, /*tp_dict */
1366+
0, /*tp_descr_get */
1367+
0, /*tp_descr_set */
1368+
0, /*tp_dictoffset */
1369+
0, /*tp_init */
1370+
0, /*tp_alloc */
1371+
none_new, /*tp_new */
13381372
};
13391373

13401374
PyObject _Py_NoneStruct = {

Objects/sliceobject.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ this type and there is exactly one in existence.
1616
#include "Python.h"
1717
#include "structmember.h"
1818

19+
static PyObject *
20+
ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
21+
{
22+
if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
23+
PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments");
24+
return NULL;
25+
}
26+
Py_INCREF(Py_Ellipsis);
27+
return Py_Ellipsis;
28+
}
29+
1930
static PyObject *
2031
ellipsis_repr(PyObject *op)
2132
{
@@ -43,6 +54,24 @@ PyTypeObject PyEllipsis_Type = {
4354
0, /* tp_setattro */
4455
0, /* tp_as_buffer */
4556
Py_TPFLAGS_DEFAULT, /* tp_flags */
57+
0, /* tp_doc */
58+
0, /* tp_traverse */
59+
0, /* tp_clear */
60+
0, /* tp_richcompare */
61+
0, /* tp_weaklistoffset */
62+
0, /* tp_iter */
63+
0, /* tp_iternext */
64+
0, /* tp_methods */
65+
0, /* tp_members */
66+
0, /* tp_getset */
67+
0, /* tp_base */
68+
0, /* tp_dict */
69+
0, /* tp_descr_get */
70+
0, /* tp_descr_set */
71+
0, /* tp_dictoffset */
72+
0, /* tp_init */
73+
0, /* tp_alloc */
74+
ellipsis_new, /* tp_new */
4675
};
4776

4877
PyObject _Py_EllipsisObject = {

0 commit comments

Comments
 (0)