diff options
author | Benjamin Peterson <benjamin@python.org> | 2013-04-13 21:19:01 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2013-04-13 21:19:01 (GMT) |
commit | 214a7d267471b22cd458bcdac0ca74d22f2ad96a (patch) | |
tree | cbbb0ff7687599b4e481f4cc2e6de8c1d24a110e /Python | |
parent | c1ab0bd788eeaab6816d5ab2b4a691513c4c9228 (diff) | |
download | cpython-214a7d267471b22cd458bcdac0ca74d22f2ad96a.zip cpython-214a7d267471b22cd458bcdac0ca74d22f2ad96a.tar.gz cpython-214a7d267471b22cd458bcdac0ca74d22f2ad96a.tar.bz2 |
properly lookup the __round__ special method (closes #17722)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bltinmodule.c | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index a486b49..5291565 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1810,10 +1810,10 @@ For most object types, eval(repr(object)) == object."); static PyObject * builtin_round(PyObject *self, PyObject *args, PyObject *kwds) { - static PyObject *round_str = NULL; PyObject *ndigits = NULL; static char *kwlist[] = {"number", "ndigits", 0}; - PyObject *number, *round; + PyObject *number, *round, *result; + _Py_IDENTIFIER(__round__); if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:round", kwlist, &number, &ndigits)) @@ -1824,24 +1824,21 @@ builtin_round(PyObject *self, PyObject *args, PyObject *kwds) return NULL; } - if (round_str == NULL) { - round_str = PyUnicode_InternFromString("__round__"); - if (round_str == NULL) - return NULL; - } - - round = _PyType_Lookup(Py_TYPE(number), round_str); + round = _PyObject_LookupSpecial(number, &PyId___round__); if (round == NULL) { - PyErr_Format(PyExc_TypeError, - "type %.100s doesn't define __round__ method", - Py_TYPE(number)->tp_name); + if (!PyErr_Occurred()) + PyErr_Format(PyExc_TypeError, + "type %.100s doesn't define __round__ method", + Py_TYPE(number)->tp_name); return NULL; } if (ndigits == NULL) - return PyObject_CallFunction(round, "O", number); + result = PyObject_CallFunctionObjArgs(round, NULL); else - return PyObject_CallFunction(round, "OO", number, ndigits); + result = PyObject_CallFunctionObjArgs(round, ndigits, NULL); + Py_DECREF(round); + return result; } PyDoc_STRVAR(round_doc, |