summaryrefslogtreecommitdiffstats
path: root/Objects
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 /Objects
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 'Objects')
-rw-r--r--Objects/intobject.c7
-rw-r--r--Objects/longobject.c7
-rw-r--r--Objects/typeobject.c4
3 files changed, 18 insertions, 0 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c
index f98aee0..5210ee8 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -934,6 +934,12 @@ int_float(PyIntObject *v)
}
static PyObject *
+int_bin(PyObject *v)
+{
+ return PyNumber_ToBase(v, 2);
+}
+
+static PyObject *
int_oct(PyIntObject *v)
{
return _PyInt_Format(v, 8, 0);
@@ -1231,6 +1237,7 @@ static PyNumberMethods int_as_number = {
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */
(unaryfunc)int_int, /* nb_index */
+ (unaryfunc)int_bin, /* nb_bin */
};
PyTypeObject PyInt_Type = {
diff --git a/Objects/longobject.c b/Objects/longobject.c
index c65d0c0..b603dda 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -3301,6 +3301,12 @@ long_float(PyObject *v)
}
static PyObject *
+long_bin(PyObject *v)
+{
+ return PyNumber_ToBase(v, 2);
+}
+
+static PyObject *
long_oct(PyObject *v)
{
return _PyLong_Format(v, 8, 1, 0);
@@ -3540,6 +3546,7 @@ static PyNumberMethods long_as_number = {
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */
long_long, /* nb_index */
+ long_bin, /* nb_bin */
};
PyTypeObject PyLong_Type = {
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index e0ae55b..093fd20 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -3743,6 +3743,7 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
COPYNUM(nb_index);
}
+ COPYNUM(nb_hex);
}
if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
@@ -5135,6 +5136,7 @@ slot_nb_coerce(PyObject **a, PyObject **b)
SLOT0(slot_nb_int, "__int__")
SLOT0(slot_nb_long, "__long__")
SLOT0(slot_nb_float, "__float__")
+SLOT0(slot_nb_bin, "__bin__")
SLOT0(slot_nb_oct, "__oct__")
SLOT0(slot_nb_hex, "__hex__")
SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
@@ -5802,6 +5804,8 @@ static slotdef slotdefs[] = {
"long(x)"),
UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
"float(x)"),
+ UNSLOT("__bin__", nb_bin, slot_nb_bin, wrap_unaryfunc,
+ "bin(x)"),
UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
"oct(x)"),
UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,