This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: string formatter %x problem with indirectly given long
Type: Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, facundobatista, ggenellina, kenjinoguchi
Priority: normal Keywords:

Created on 2007-06-21 23:33 by kenjinoguchi, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg32378 - (view) Author: Kenji Noguchi (kenjinoguchi) Date: 2007-06-21 23:33
"%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,

msg32379 - (view) Author: Gabriel Genellina (ggenellina) Date: 2007-06-25 08:38
Patch #1742669 tries to fix this problem and is a bit more generic.
msg63377 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2008-03-08 01:41
This issue has been resolved in issue1742669 .

$ ./python.exe 
Python 2.6a1+ (trunk:61230M, Mar  4 2008, 10:56:31) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class X:
...   def __int__(self): return 0x80000000
... 
>>> "%08x" % X()
'80000000'
msg63378 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-03-08 01:52
Already fixed. Thanks Kenji for the report, Gabriel for the patch in the
other issue, and Alexander for noticing this "duplicateness".
History
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45119
2008-03-08 01:52:07facundobatistasetstatus: open -> closed
resolution: duplicate
messages: + msg63378
2008-03-08 01:41:11belopolskysetnosy: + facundobatista, belopolsky
messages: + msg63377
title: string formatter %x problem with indirectly given long -> string formatter %x problem with indirectly given long
2007-06-21 23:33:01kenjinoguchicreate