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

Skip to content

Commit 18d7d7a

Browse files
committed
also make NotImplementedType callable
1 parent d83be99 commit 18d7d7a

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@ def test_bytearray_translate(self):
13441344
self.assertRaises(TypeError, x.translate, b"1"*256, 1)
13451345

13461346
def test_construct_singletons(self):
1347-
for const in None, Ellipsis:
1347+
for const in None, Ellipsis, NotImplemented:
13481348
tp = type(const)
13491349
self.assertIs(tp(), const)
13501350
self.assertRaises(TypeError, tp, 1, 2)

Misc/NEWS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ 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.
13+
- Make type(None), type(Ellipsis), and type(NotImplemented) callable. They
14+
return the respective singleton instances.
1515

1616
- Forbid summing bytes in sum().
1717

Objects/object.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,17 @@ NotImplemented_repr(PyObject *op)
13851385
return PyUnicode_FromString("NotImplemented");
13861386
}
13871387

1388+
static PyObject *
1389+
notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1390+
{
1391+
if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
1392+
PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1393+
return NULL;
1394+
}
1395+
Py_INCREF(Py_NotImplemented);
1396+
return Py_NotImplemented;
1397+
}
1398+
13881399
static PyTypeObject PyNotImplemented_Type = {
13891400
PyVarObject_HEAD_INIT(&PyType_Type, 0)
13901401
"NotImplementedType",
@@ -1400,6 +1411,30 @@ static PyTypeObject PyNotImplemented_Type = {
14001411
0, /*tp_as_sequence*/
14011412
0, /*tp_as_mapping*/
14021413
0, /*tp_hash */
1414+
0, /*tp_call */
1415+
0, /*tp_str */
1416+
0, /*tp_getattro */
1417+
0, /*tp_setattro */
1418+
0, /*tp_as_buffer */
1419+
Py_TPFLAGS_DEFAULT, /*tp_flags */
1420+
0, /*tp_doc */
1421+
0, /*tp_traverse */
1422+
0, /*tp_clear */
1423+
0, /*tp_richcompare */
1424+
0, /*tp_weaklistoffset */
1425+
0, /*tp_iter */
1426+
0, /*tp_iternext */
1427+
0, /*tp_methods */
1428+
0, /*tp_members */
1429+
0, /*tp_getset */
1430+
0, /*tp_base */
1431+
0, /*tp_dict */
1432+
0, /*tp_descr_get */
1433+
0, /*tp_descr_set */
1434+
0, /*tp_dictoffset */
1435+
0, /*tp_init */
1436+
0, /*tp_alloc */
1437+
notimplemented_new, /*tp_new */
14031438
};
14041439

14051440
PyObject _Py_NotImplementedStruct = {

0 commit comments

Comments
 (0)