summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-06-20 04:18:15 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-06-20 04:18:15 (GMT)
commite3ae655edfea3dd8ed32fcca63cb3eae861a58b7 (patch)
treeb1624452b23a4e614e103735dbe7e2a581b159bc /Python
parente0f124495b0c0d7826ef1e64a8db27ebe28ed085 (diff)
downloadcpython-e3ae655edfea3dd8ed32fcca63cb3eae861a58b7.zip
cpython-e3ae655edfea3dd8ed32fcca63cb3eae861a58b7.tar.gz
cpython-e3ae655edfea3dd8ed32fcca63cb3eae861a58b7.tar.bz2
Make bin() implementation parallel oct() and hex() so that int/long subclasses can override or so that other classes can support.
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c19
1 files changed, 18 insertions, 1 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index e18eb2a..7647523 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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,