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

Skip to content

Commit b1a1619

Browse files
Issue #29000: Fixed bytes formatting of octals with zero padding in alternate
form.
1 parent af56e0e commit b1a1619

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

Lib/test/test_format.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def test_common_format(self):
114114
testcommon("%o", 100000000000, "1351035564000")
115115
testcommon("%d", 10, "10")
116116
testcommon("%d", 100000000000, "100000000000")
117+
117118
big = 123456789012345678901234567890
118119
testcommon("%d", big, "123456789012345678901234567890")
119120
testcommon("%d", -big, "-123456789012345678901234567890")
@@ -133,6 +134,7 @@ def test_common_format(self):
133134
testcommon("%.31d", big, "0123456789012345678901234567890")
134135
testcommon("%32.31d", big, " 0123456789012345678901234567890")
135136
testcommon("%d", float(big), "123456________________________", 6)
137+
136138
big = 0x1234567890abcdef12345 # 21 hex digits
137139
testcommon("%x", big, "1234567890abcdef12345")
138140
testcommon("%x", -big, "-1234567890abcdef12345")
@@ -156,19 +158,26 @@ def test_common_format(self):
156158
testcommon("%#X", big, "0X1234567890ABCDEF12345")
157159
testcommon("%#x", big, "0x1234567890abcdef12345")
158160
testcommon("%#x", -big, "-0x1234567890abcdef12345")
161+
testcommon("%#27x", big, " 0x1234567890abcdef12345")
162+
testcommon("%#-27x", big, "0x1234567890abcdef12345 ")
163+
testcommon("%#027x", big, "0x00001234567890abcdef12345")
164+
testcommon("%#.23x", big, "0x001234567890abcdef12345")
159165
testcommon("%#.23x", -big, "-0x001234567890abcdef12345")
166+
testcommon("%#27.23x", big, " 0x001234567890abcdef12345")
167+
testcommon("%#-27.23x", big, "0x001234567890abcdef12345 ")
168+
testcommon("%#027.23x", big, "0x00001234567890abcdef12345")
160169
testcommon("%#+.23x", big, "+0x001234567890abcdef12345")
161170
testcommon("%# .23x", big, " 0x001234567890abcdef12345")
162171
testcommon("%#+.23X", big, "+0X001234567890ABCDEF12345")
163-
testcommon("%#-+.23X", big, "+0X001234567890ABCDEF12345")
164-
testcommon("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
165-
testcommon("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
166-
testcommon("%#+27.23X", big, " +0X001234567890ABCDEF12345")
167172
# next one gets two leading zeroes from precision, and another from the
168173
# 0 flag and the width
169174
testcommon("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
175+
testcommon("%# 027.23X", big, " 0X0001234567890ABCDEF12345")
170176
# same, except no 0 flag
171177
testcommon("%#+27.23X", big, " +0X001234567890ABCDEF12345")
178+
testcommon("%#-+27.23x", big, "+0x001234567890abcdef12345 ")
179+
testcommon("%#- 27.23x", big, " 0x001234567890abcdef12345 ")
180+
172181
big = 0o12345670123456701234567012345670 # 32 octal digits
173182
testcommon("%o", big, "12345670123456701234567012345670")
174183
testcommon("%o", -big, "-12345670123456701234567012345670")
@@ -191,13 +200,21 @@ def test_common_format(self):
191200
testcommon("%o", big, "12345670123456701234567012345670")
192201
testcommon("%#o", big, "0o12345670123456701234567012345670")
193202
testcommon("%#o", -big, "-0o12345670123456701234567012345670")
203+
testcommon("%#38o", big, " 0o12345670123456701234567012345670")
204+
testcommon("%#-38o", big, "0o12345670123456701234567012345670 ")
205+
testcommon("%#038o", big, "0o000012345670123456701234567012345670")
206+
testcommon("%#.34o", big, "0o0012345670123456701234567012345670")
194207
testcommon("%#.34o", -big, "-0o0012345670123456701234567012345670")
208+
testcommon("%#38.34o", big, " 0o0012345670123456701234567012345670")
209+
testcommon("%#-38.34o", big, "0o0012345670123456701234567012345670 ")
210+
testcommon("%#038.34o", big, "0o000012345670123456701234567012345670")
195211
testcommon("%#+.34o", big, "+0o0012345670123456701234567012345670")
196212
testcommon("%# .34o", big, " 0o0012345670123456701234567012345670")
197-
testcommon("%#+.34o", big, "+0o0012345670123456701234567012345670")
198-
testcommon("%#-+.34o", big, "+0o0012345670123456701234567012345670")
199-
testcommon("%#-+37.34o", big, "+0o0012345670123456701234567012345670")
200-
testcommon("%#+37.34o", big, "+0o0012345670123456701234567012345670")
213+
testcommon("%#+38.34o", big, " +0o0012345670123456701234567012345670")
214+
testcommon("%#-+38.34o", big, "+0o0012345670123456701234567012345670 ")
215+
testcommon("%#- 38.34o", big, " 0o0012345670123456701234567012345670 ")
216+
testcommon("%#+038.34o", big, "+0o00012345670123456701234567012345670")
217+
testcommon("%# 038.34o", big, " 0o00012345670123456701234567012345670")
201218
# next one gets one leading zero from precision
202219
testcommon("%.33o", big, "012345670123456701234567012345670")
203220
# base marker added in spite of leading zero (different to Python 2)
@@ -208,6 +225,7 @@ def test_common_format(self):
208225
testcommon("%035.33o", big, "00012345670123456701234567012345670")
209226
# base marker shouldn't change the size
210227
testcommon("%0#35.33o", big, "0o012345670123456701234567012345670")
228+
211229
# Some small ints, in both Python int and flavors).
212230
testcommon("%d", 42, "42")
213231
testcommon("%d", -42, "-42")

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #29000: Fixed bytes formatting of octals with zero padding in alternate
14+
form.
15+
1316
- Issue #28512: Fixed setting the offset attribute of SyntaxError by
1417
PyErr_SyntaxLocationEx() and PyErr_SyntaxLocationObject().
1518

Objects/bytesobject.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ _PyBytes_Format(PyObject *format, PyObject *args)
882882
if (width > len)
883883
width--;
884884
}
885-
if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
885+
if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) {
886886
assert(pbuf[0] == '0');
887887
assert(pbuf[1] == c);
888888
if (fill != ' ') {
@@ -904,8 +904,7 @@ _PyBytes_Format(PyObject *format, PyObject *args)
904904
if (fill == ' ') {
905905
if (sign)
906906
*res++ = sign;
907-
if ((flags & F_ALT) &&
908-
(c == 'x' || c == 'X')) {
907+
if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) {
909908
assert(pbuf[0] == '0');
910909
assert(pbuf[1] == c);
911910
*res++ = *pbuf++;

0 commit comments

Comments
 (0)