Mercurial > cpython-lru_cache-weakref
diff Python/bltinmodule.c @ 47779:edc701d00b62 legacy-trunk
Make bin() implementation parallel oct() and hex() so that int/long subclasses can override or so that other classes can support.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 20 Jun 2008 04:18:15 +0000 |
| parents | 1c964ed35ef9 |
| children | 3ada46f2321d |
line wrap: on
line diff
--- a/Python/bltinmodule.c Fri Jun 20 02:47:03 2008 +0000 +++ b/Python/bltinmodule.c Fri Jun 20 04:18:15 2008 +0000 @@ -211,7 +211,24 @@ static PyObject * builtin_bin(PyObject *self, PyObject *v) { - return PyNumber_ToBase(v, 2); + PyNumberMethods *nb; + PyObject *res; + + if ((nb = v->ob_type->tp_as_number) == NULL || + nb->nb_hex == NULL) { + PyErr_SetString(PyExc_TypeError, + "bin() argument can't be converted to hex"); + return NULL; + } + res = (*nb->nb_bin)(v); + if (res && !PyString_Check(res)) { + PyErr_Format(PyExc_TypeError, + "__bin__ returned non-string (type %.200s)", + res->ob_type->tp_name); + Py_DECREF(res); + return NULL; + } + return res; } PyDoc_STRVAR(bin_doc,
