[ python-Bugs-1741218 ] string formatter %x problem with indirectly given long

SourceForge.net noreply at sourceforge.net
Mon Jun 25 10:38:32 CEST 2007


Bugs item #1741218, was opened at 2007-06-21 20:33
Message generated for change (Comment added) made by gagenellina
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1741218&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Kenji Noguchi (kenjinoguchi)
Assigned to: Nobody/Anonymous (nobody)
Summary: string formatter %x problem with indirectly given long 

Initial Comment:
"%x" % v fails if the v is a instance, and its __int__ returns larger than 0x80000000 on 32bit OS.

I've reported the problem at Python ML.
http://mail.python.org/pipermail/python-list/2007-June/446103.html

"%x" % int(v)
"%x" % 0x80000000L

above two work fine because string formatter processes a long value exceptionally. It looks inconsistent. So I made this patch. 

--- 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,



----------------------------------------------------------------------

Comment By: Gabriel Genellina (gagenellina)
Date: 2007-06-25 05:38

Message:
Logged In: YES 
user_id=479790
Originator: NO

Patch #1742669 tries to fix this problem and is a bit more generic.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1741218&group_id=5470


More information about the Python-bugs-list mailing list