mirror of
https://github.com/python/cpython.git
synced 2025-07-28 13:44:43 +00:00
Make bin() implementation parallel oct() and hex() so that int/long subclasses can override or so that other classes can support.
This commit is contained in:
parent
e0f124495b
commit
e3ae655edf
7 changed files with 49 additions and 2 deletions
|
@ -211,7 +211,24 @@ Deprecated since release 2.3. Instead, use the extended call syntax:\n\
|
|||
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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue