string formatter %x and a class instance with __int__ cannot handle long
Kenji Noguchi
tokyo246 at gmail.com
Thu Jun 21 18:19:32 EDT 2007
I looked at python2.5.1 source code.
I noticed that, in Objects/stringobject.c around line 4684,
long type is exceptionally handled, which is hack, and
everything else falls to formatint. This explains why explicit
converting to long before formatting fixes the problem.
I made a patch but this is a hack on a hack.
I expect Python3000 won't have such problem as they unify
int and long.
Thanks
Kenji Noguchi
--- stringobject.c.org 2007-06-21 13:57:54.745877000 -0700
+++ stringobject.c 2007-06-21 13:59:19.576646000 -0700
@@ -4684,6 +4684,15 @@
case 'X':
if (c == 'i')
c = 'd';
+ /* try to convert objects to number*/
+ PyNumberMethods *nb;
+ if ((nb = v->ob_type->tp_as_number) &&
+ nb->nb_int) {
+ v = (*nb->nb_int) (v);
+ if(v == NULL)
+ goto error;
+ }
+
if (PyLong_Check(v)) {
int ilen;
temp = _PyString_FormatLong(v, flags,
2007/6/21, Kenji Noguchi <tokyo246 at gmail.com>:
> 2007/6/20, Gabriel Genellina <gagsl-py2 at yahoo.com.ar>:
> > It is a bug, at least for me, and I have half of a patch addressing it. As
> > a workaround, convert explicitely to long before formatting.
>
> I'm interested in your patch. What's the other half still missing?
More information about the Python-list
mailing list