]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Make bin() implementation parallel oct() and hex() so that int/long subclasses can...
authorRaymond Hettinger <python@rcn.com>
Fri, 20 Jun 2008 04:18:15 +0000 (04:18 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 20 Jun 2008 04:18:15 +0000 (04:18 +0000)
Include/object.h
Lib/test/test_sys.py
Misc/NEWS
Objects/intobject.c
Objects/longobject.c
Objects/typeobject.c
Python/bltinmodule.c

index 59f3b9e1b100c6815585243d3112ae25c3277115..709174fdf5b55c2d7b0c36488821082e4d3066db 100644 (file)
@@ -267,6 +267,9 @@ typedef struct {
 
        /* Added in release 2.5 */
        unaryfunc nb_index;
+
+       /* Added in release 2.6 */
+       unaryfunc nb_bin;
 } PyNumberMethods;
 
 typedef struct {
index f1fb61549e24da6faac5fcab3528cdb193d39827..c98117ac5b61ba90b9448b550c9ce1f240a9e5fc 100644 (file)
@@ -523,7 +523,7 @@ class SizeofTest(unittest.TestCase):
         len_typeobject = p + 2*l + 15*p + l + 4*p + l + 9*p +\
                          l + 11*p + self.align(4)
         self.check_sizeof(class_newstyle,
-                          h + len_typeobject + 41*p + 10*p + 3*p + 6*p)
+                          h + len_typeobject + 42*p + 10*p + 3*p + 6*p)
 
     def test_specialtypes(self):
         i = self.i
index 9bd4f3cb88e4f1de2ace6cbe2c4b9718d7080753..9e02d763b86d29d0cf12e6500d854e98f3bb3f57 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -4,6 +4,15 @@ Python News
 
 (editors: check NEWS.help for information about editing NEWS using ReST.)
 
+What's New in Python 2.6 beta 2?
+================================
+
+Core and Builtins
+-----------------
+
+- Make bin() implementation parallel oct() and hex().
+
+
 What's New in Python 2.6 beta 1?
 ================================
 
index f98aee0e2e1544d1e59cbfc3ab5b1c8a8a7216db..5210ee8db7ce545e6feede0ddbf0618cad5e45e9 100644 (file)
@@ -933,6 +933,12 @@ int_float(PyIntObject *v)
        return PyFloat_FromDouble((double)(v -> ob_ival));
 }
 
+static PyObject *
+int_bin(PyObject *v)
+{
+        return PyNumber_ToBase(v, 2);
+}
+
 static PyObject *
 int_oct(PyIntObject *v)
 {
@@ -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 = {
index c65d0c00abe8037ea85552d941dcb7414d2bb726..b603dda269f44f7e78aaf1553accf0c4084ed9c5 100644 (file)
@@ -3300,6 +3300,12 @@ long_float(PyObject *v)
        return PyFloat_FromDouble(result);
 }
 
+static PyObject *
+long_bin(PyObject *v)
+{
+        return PyNumber_ToBase(v, 2);
+}
+
 static PyObject *
 long_oct(PyObject *v)
 {
@@ -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 = {
index e0ae55b057ca92038351e36eba9e4f50479bc55f..093fd20a82880b502ca471fc8441e3355ef56a7f 100644 (file)
@@ -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,
index e18eb2a95a90ac356af65d8ae295cbcf9bb77cbd..7647523b580feca999f76aa72453e1460443eb55 100644 (file)
@@ -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,