Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38fd5b6

Browse files
committed
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Note a curious extension to the std C rules: x, X and o formatting can never produce a sign character in C, so the '+' and ' ' flags are meaningless for them. But unbounded ints *can* produce a sign character under these conversions (no fixed- width bitstring is wide enough to hold all negative values in 2's-comp form). So these flags become meaningful in Python when formatting a Python long which is too big to fit in a C long. This required shuffling around existing code, which hacked x and X conversions to death when both the '#' and '0' flags were specified: the hacks weren't strong enough to deal with the simultaneous possibility of the ' ' or '+' flags too, since signs were always meaningless before for x and X conversions. Isomorphic shuffling was required in unicodeobject.c. Also added dozens of non-trivial new unbounded-int test cases to test_format.py.
1 parent 31575ce commit 38fd5b6

4 files changed

Lines changed: 409 additions & 75 deletions

File tree

Include/stringobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ extern DL_IMPORT(void) PyString_Concat(PyObject **, PyObject *);
5959
extern DL_IMPORT(void) PyString_ConcatAndDel(PyObject **, PyObject *);
6060
extern DL_IMPORT(int) _PyString_Resize(PyObject **, int);
6161
extern DL_IMPORT(PyObject *) PyString_Format(PyObject *, PyObject *);
62+
extern DL_IMPORT(PyObject *) _PyString_FormatLong(PyObject*, int, int,
63+
int, char**, int*);
6264

6365
#ifdef INTERN_STRINGS
6466
extern DL_IMPORT(void) PyString_InternInPlace(PyObject **);

Lib/test/test_format.py

Lines changed: 136 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,36 @@
66
# they crash python)
77
# test on unicode strings as well
88

9+
overflowok = 1
10+
911
def testformat(formatstr, args, output=None):
10-
if verbose:
11-
if output:
12-
print "%s %% %s =? %s ..." %\
13-
(repr(formatstr), repr(args), repr(output)),
14-
else:
15-
print "%s %% %s works? ..." % (repr(formatstr), repr(args)),
16-
try:
17-
result = formatstr % args
18-
except OverflowError:
19-
if verbose:
20-
print 'overflow (this is fine)'
21-
else:
22-
if output and result != output:
23-
if verbose:
24-
print 'no'
25-
print "%s %% %s == %s != %s" %\
26-
(repr(formatstr), repr(args), repr(result), repr(output))
27-
else:
28-
if verbose:
29-
print 'yes'
12+
if verbose:
13+
if output:
14+
print "%s %% %s =? %s ..." %\
15+
(repr(formatstr), repr(args), repr(output)),
16+
else:
17+
print "%s %% %s works? ..." % (repr(formatstr), repr(args)),
18+
try:
19+
result = formatstr % args
20+
except OverflowError:
21+
if not overflowok:
22+
raise
23+
if verbose:
24+
print 'overflow (this is fine)'
25+
else:
26+
if output and result != output:
27+
if verbose:
28+
print 'no'
29+
print "%s %% %s == %s != %s" %\
30+
(repr(formatstr), repr(args), repr(result), repr(output))
31+
else:
32+
if verbose:
33+
print 'yes'
3034

3135
def testboth(formatstr, *args):
32-
testformat(formatstr, *args)
33-
testformat(unicode(formatstr), *args)
34-
36+
testformat(formatstr, *args)
37+
testformat(unicode(formatstr), *args)
38+
3539

3640
testboth("%.1d", (1,), "1")
3741
testboth("%.*d", (sys.maxint,1)) # expect overflow
@@ -50,3 +54,112 @@ def testboth(formatstr, *args):
5054
# test some ridiculously large precision, expect overflow
5155
testboth('%12.*f', (123456, 1.0))
5256

57+
# Formatting of long integers. Overflow is not ok
58+
overflowok = 0
59+
testboth("%x", 10L, "a")
60+
testboth("%x", 100000000000L, "174876e800")
61+
testboth("%o", 10L, "12")
62+
testboth("%o", 100000000000L, "1351035564000")
63+
testboth("%d", 10L, "10")
64+
testboth("%d", 100000000000L, "100000000000")
65+
66+
# Make sure big is too big to fit in a 64-bit int, else the unbounded
67+
# int formatting will be sidestepped on some machines. That's vital,
68+
# because bitwise (x, X, o) formats of regular Python ints never
69+
# produce a sign ("+" or "-").
70+
71+
big = 123456789012345678901234567890L
72+
testboth("%d", big, "123456789012345678901234567890")
73+
testboth("%d", -big, "-123456789012345678901234567890")
74+
testboth("%5d", -big, "-123456789012345678901234567890")
75+
testboth("%31d", -big, "-123456789012345678901234567890")
76+
testboth("%32d", -big, " -123456789012345678901234567890")
77+
testboth("%-32d", -big, "-123456789012345678901234567890 ")
78+
testboth("%032d", -big, "-0123456789012345678901234567890")
79+
testboth("%-032d", -big, "-123456789012345678901234567890 ")
80+
testboth("%034d", -big, "-000123456789012345678901234567890")
81+
testboth("%034d", big, "0000123456789012345678901234567890")
82+
testboth("%0+34d", big, "+000123456789012345678901234567890")
83+
testboth("%+34d", big, " +123456789012345678901234567890")
84+
testboth("%34d", big, " 123456789012345678901234567890")
85+
testboth("%.2d", big, "123456789012345678901234567890")
86+
testboth("%.30d", big, "123456789012345678901234567890")
87+
testboth("%.31d", big, "0123456789012345678901234567890")
88+
testboth("%32.31d", big, " 0123456789012345678901234567890")
89+
90+
big = 0x1234567890abcdef12345L # 21 hex digits
91+
testboth("%x", big, "1234567890abcdef12345")
92+
testboth("%x", -big, "-1234567890abcdef12345")
93+
testboth("%5x", -big, "-1234567890abcdef12345")
94+
testboth("%22x", -big, "-1234567890abcdef12345")
95+
testboth("%23x", -big, " -1234567890abcdef12345")
96+
testboth("%-23x", -big, "-1234567890abcdef12345 ")
97+
testboth("%023x", -big, "-01234567890abcdef12345")
98+
testboth("%-023x", -big, "-1234567890abcdef12345 ")
99+
testboth("%025x", -big, "-0001234567890abcdef12345")
100+
testboth("%025x", big, "00001234567890abcdef12345")
101+
testboth("%0+25x", big, "+0001234567890abcdef12345")
102+
testboth("%+25x", big, " +1234567890abcdef12345")
103+
testboth("%25x", big, " 1234567890abcdef12345")
104+
testboth("%.2x", big, "1234567890abcdef12345")
105+
testboth("%.21x", big, "1234567890abcdef12345")
106+
testboth("%.22x", big, "01234567890abcdef12345")
107+
testboth("%23.22x", big, " 01234567890abcdef12345")
108+
testboth("%-23.22x", big, "01234567890abcdef12345 ")
109+
testboth("%X", big, "1234567890ABCDEF12345")
110+
testboth("%#X", big, "0X1234567890ABCDEF12345")
111+
testboth("%#x", big, "0x1234567890abcdef12345")
112+
testboth("%#x", -big, "-0x1234567890abcdef12345")
113+
testboth("%#.23x", -big, "-0x001234567890abcdef12345")
114+
testboth("%#+.23x", big, "+0x001234567890abcdef12345")
115+
testboth("%# .23x", big, " 0x001234567890abcdef12345")
116+
testboth("%#+.23X", big, "+0X001234567890ABCDEF12345")
117+
testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345")
118+
testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
119+
testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
120+
testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
121+
# next one gets two leading zeroes from precision, and another from the
122+
# 0 flag and the width
123+
testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
124+
# same, except no 0 flag
125+
testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
126+
127+
big = 012345670123456701234567012345670L # 32 octal digits
128+
testboth("%o", big, "12345670123456701234567012345670")
129+
testboth("%o", -big, "-12345670123456701234567012345670")
130+
testboth("%5o", -big, "-12345670123456701234567012345670")
131+
testboth("%33o", -big, "-12345670123456701234567012345670")
132+
testboth("%34o", -big, " -12345670123456701234567012345670")
133+
testboth("%-34o", -big, "-12345670123456701234567012345670 ")
134+
testboth("%034o", -big, "-012345670123456701234567012345670")
135+
testboth("%-034o", -big, "-12345670123456701234567012345670 ")
136+
testboth("%036o", -big, "-00012345670123456701234567012345670")
137+
testboth("%036o", big, "000012345670123456701234567012345670")
138+
testboth("%0+36o", big, "+00012345670123456701234567012345670")
139+
testboth("%+36o", big, " +12345670123456701234567012345670")
140+
testboth("%36o", big, " 12345670123456701234567012345670")
141+
testboth("%.2o", big, "12345670123456701234567012345670")
142+
testboth("%.32o", big, "12345670123456701234567012345670")
143+
testboth("%.33o", big, "012345670123456701234567012345670")
144+
testboth("%34.33o", big, " 012345670123456701234567012345670")
145+
testboth("%-34.33o", big, "012345670123456701234567012345670 ")
146+
testboth("%o", big, "12345670123456701234567012345670")
147+
testboth("%#o", big, "012345670123456701234567012345670")
148+
testboth("%#o", -big, "-012345670123456701234567012345670")
149+
testboth("%#.34o", -big, "-0012345670123456701234567012345670")
150+
testboth("%#+.34o", big, "+0012345670123456701234567012345670")
151+
testboth("%# .34o", big, " 0012345670123456701234567012345670")
152+
testboth("%#+.34o", big, "+0012345670123456701234567012345670")
153+
testboth("%#-+.34o", big, "+0012345670123456701234567012345670")
154+
testboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ")
155+
testboth("%#+37.34o", big, " +0012345670123456701234567012345670")
156+
# next one gets one leading zero from precision
157+
testboth("%.33o", big, "012345670123456701234567012345670")
158+
# base marker shouldn't change that, since "0" is redundant
159+
testboth("%#.33o", big, "012345670123456701234567012345670")
160+
# but reduce precision, and base marker should add a zero
161+
testboth("%#.32o", big, "012345670123456701234567012345670")
162+
# one leading zero from precision, and another from "0" flag & width
163+
testboth("%034.33o", big, "0012345670123456701234567012345670")
164+
# base marker shouldn't change that
165+
testboth("%0#34.33o", big, "0012345670123456701234567012345670")

0 commit comments

Comments
 (0)