summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wouters <thomas@python.org>2001-05-23 12:31:25 (GMT)
committerThomas Wouters <thomas@python.org>2001-05-23 12:31:25 (GMT)
commit930874efdd5bbe3b90518e52f59acc0bd5e41180 (patch)
treedd97b3483e809f25dc49a72cb159fdd9a41c36c0
parent4a11bb3f86a5d5ca975f800cf1740b9253794ab7 (diff)
downloadcpython-930874efdd5bbe3b90518e52f59acc0bd5e41180.zip
cpython-930874efdd5bbe3b90518e52f59acc0bd5e41180.tar.gz
cpython-930874efdd5bbe3b90518e52f59acc0bd5e41180.tar.bz2
Backport of Tim's checkin 2.88:
A different approach to the problem reported in Patch #419651: Metrowerks on Mac adds 0x itself C std says %#x and %#X conversion of 0 do not add the 0x/0X base marker. Metrowerks apparently does. Mark Favas reported the same bug under a Compaq compiler on Tru64 Unix, but no other libc broken in this respect is known (known to be OK under MSVC and gcc). So just try the damn thing at runtime and see what the platform does. Note that we've always had bugs here, but never knew it before because a relevant test case didn't exist before 2.1.
-rw-r--r--Objects/unicodeobject.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index b623c20..76ca94c 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -4670,6 +4670,7 @@ formatint(Py_UNICODE *buf,
+ 1 + 1 = 24*/
char fmt[64]; /* plenty big enough! */
long x;
+ int use_native_c_format = 1;
x = PyInt_AsLong(v);
if (x == -1 && PyErr_Occurred())
@@ -4686,11 +4687,21 @@ formatint(Py_UNICODE *buf,
/* When converting 0 under %#x or %#X, C leaves off the base marker,
* but we want it (for consistency with other %#x conversions, and
* for consistency with Python's hex() function).
+ * BUG 28-Apr-2001 tim: At least two platform Cs (Metrowerks &
+ * Compaq Tru64) violate the std by converting 0 w/ leading 0x anyway.
+ * So add it only if the platform doesn't already.
*/
- if (x == 0 && (flags & F_ALT) && (type == 'x' || type == 'X'))
- sprintf(fmt, "0%c%%%s.%dl%c", type, "#", prec, type);
- else
- sprintf(fmt, "%%%s.%dl%c", (flags & F_ALT) ? "#" : "", prec, type);
+ if (x == 0 && (flags & F_ALT) && (type == 'x' || type == 'X')) {
+ /* Only way to know what the platform does is to try it. */
+ sprintf(fmt, type == 'x' ? "%#x" : "%#X", 0);
+ if (fmt[1] != (char)type) {
+ /* Supply our own leading 0x/0X -- needed under std C */
+ use_native_c_format = 0;
+ sprintf(fmt, "0%c%%#.%dl%c", type, prec, type);
+ }
+ }
+ if (use_native_c_format)
+ sprintf(fmt, "%%%s.%dl%c", (flags & F_ALT) ? "#" : "", prec, type);
return usprintf(buf, fmt, x);
}