make the types of None and Ellipsis callable · pythoncapi/cpython@c4607ae · GitHub
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

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

0 commit comments

Comments
 (0)