Skip to content

Commit 102d120

Browse files
committed
[Bug #536241] string.zfill() produces mangled output for a Unicode string.
Walter Doerwald provided a patch, which I've modified in two ways: 1) (Uncontroversial) Removed code to make module work in earlier versions of Python without the unicode() built-in 2) (Poss. controversial) Instead of making string.zfill take the repr() of non-string objects, take the str(). Should a warning be added to this branch of the code so that the automatic str() can be deprecated? 2.2.2 bugfix candidate, assuming the repr()->str() change is deemed OK.
1 parent c6c9c4a commit 102d120

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Lib/string.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def rfind(s, *args):
190190
_float = float
191191
_int = int
192192
_long = long
193-
_StringType = type('')
193+
_StringTypes = (str, unicode)
194194

195195
# Convert string to float
196196
def atof(s):
@@ -276,14 +276,14 @@ def zfill(x, width):
276276
of the specified width. The string x is never truncated.
277277
278278
"""
279-
if type(x) == type(''): s = x
280-
else: s = `x`
281-
n = len(s)
282-
if n >= width: return s
279+
if not isinstance(x, _StringTypes):
280+
x = str(x)
281+
n = len(x)
282+
if n >= width: return x
283283
sign = ''
284-
if s[0] in ('-', '+'):
285-
sign, s = s[0], s[1:]
286-
return sign + '0'*(width-n) + s
284+
if x[0] in '-+':
285+
sign, x = x[0], x[1:]
286+
return sign + '0'*(width-n) + x
287287

288288
# Expand tabs in a string.
289289
# Doesn't take non-printing chars into account, but does understand \n.

0 commit comments

Comments
 (0)