[Python-checkins] r64424 - in python/trunk: Include/object.h Lib/test/test_sys.py Misc/NEWS Objects/intobject.c Objects/longobject.c Objects/typeobject.c Python/bltinmodule.c
Eric Smith
eric at trueblade.com
Tue Jun 24 14:50:39 CEST 2008
Note that in
http://mail.python.org/pipermail/python-dev/2008-February/077062.html,
Guido advocates not adding the __bin__ machinery, which is what lead to
the simple implementation of bin() just calling PyNumber_ToBase and
relying on __index__.
I don't think __bin__ should be added to 2.6. I don't see the point in
adding a feature in 2.6 that's implemented so differently in 3.0. It's
just asking for porting hassles.
Instead, I think the approach used in 3.0 (r64451) should be used
instead. That is, if this feature exist at all. I'm -0 on adding
bin(), etc. to floats.
Eric.
raymond.hettinger wrote:
> Author: raymond.hettinger
> Date: Fri Jun 20 06:18:15 2008
> New Revision: 64424
>
> Log:
> Make bin() implementation parallel oct() and hex() so that int/long subclasses can override or so that other classes can support.
>
> Modified:
> python/trunk/Include/object.h
> python/trunk/Lib/test/test_sys.py
> python/trunk/Misc/NEWS
> python/trunk/Objects/intobject.c
> python/trunk/Objects/longobject.c
> python/trunk/Objects/typeobject.c
> python/trunk/Python/bltinmodule.c
>
> Modified: python/trunk/Include/object.h
> ==============================================================================
> --- python/trunk/Include/object.h (original)
> +++ python/trunk/Include/object.h Fri Jun 20 06:18:15 2008
> @@ -267,6 +267,9 @@
>
> /* Added in release 2.5 */
> unaryfunc nb_index;
> +
> + /* Added in release 2.6 */
> + unaryfunc nb_bin;
> } PyNumberMethods;
>
> typedef struct {
>
> Modified: python/trunk/Lib/test/test_sys.py
> ==============================================================================
> --- python/trunk/Lib/test/test_sys.py (original)
> +++ python/trunk/Lib/test/test_sys.py Fri Jun 20 06:18:15 2008
> @@ -523,7 +523,7 @@
> 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
>
> Modified: python/trunk/Misc/NEWS
> ==============================================================================
> --- python/trunk/Misc/NEWS (original)
> +++ python/trunk/Misc/NEWS Fri Jun 20 06:18:15 2008
> @@ -4,6 +4,15 @@
>
> (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?
> ================================
>
>
> Modified: python/trunk/Objects/intobject.c
> ==============================================================================
> --- python/trunk/Objects/intobject.c (original)
> +++ python/trunk/Objects/intobject.c Fri Jun 20 06:18:15 2008
> @@ -934,6 +934,12 @@
> }
>
> 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 @@
> 0, /* nb_inplace_floor_divide */
> 0, /* nb_inplace_true_divide */
> (unaryfunc)int_int, /* nb_index */
> + (unaryfunc)int_bin, /* nb_bin */
> };
>
> PyTypeObject PyInt_Type = {
>
> Modified: python/trunk/Objects/longobject.c
> ==============================================================================
> --- python/trunk/Objects/longobject.c (original)
> +++ python/trunk/Objects/longobject.c Fri Jun 20 06:18:15 2008
> @@ -3301,6 +3301,12 @@
> }
>
> 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 @@
> 0, /* nb_inplace_floor_divide */
> 0, /* nb_inplace_true_divide */
> long_long, /* nb_index */
> + long_bin, /* nb_bin */
> };
>
> PyTypeObject PyLong_Type = {
>
> Modified: python/trunk/Objects/typeobject.c
> ==============================================================================
> --- python/trunk/Objects/typeobject.c (original)
> +++ python/trunk/Objects/typeobject.c Fri Jun 20 06:18:15 2008
> @@ -3743,6 +3743,7 @@
> 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 @@
> 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 @@
> "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,
>
> Modified: python/trunk/Python/bltinmodule.c
> ==============================================================================
> --- python/trunk/Python/bltinmodule.c (original)
> +++ python/trunk/Python/bltinmodule.c Fri Jun 20 06:18:15 2008
> @@ -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,
> _______________________________________________
> Python-checkins mailing list
> Python-checkins at python.org
> http://mail.python.org/mailman/listinfo/python-checkins
>
More information about the Python-checkins
mailing list