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

Skip to content

Commit 068325e

Browse files
committed
Apply the second version of SF patch http://www.python.org/sf/536241
Add a method zfill to str, unicode and UserString and change Lib/string.py accordingly. This activates the zfill version in unicodeobject.c that was commented out and implements the same in stringobject.c. It also adds the test for unicode support in Lib/string.py back in and uses repr() instead() of str() (as it was before Lib/string.py 1.62)
1 parent b384c72 commit 068325e

8 files changed

Lines changed: 83 additions & 13 deletions

File tree

Doc/lib/libstdtypes.tex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,12 @@ \subsubsection{String Methods \label{string-methods}}
694694
Return a copy of the string converted to uppercase.
695695
\end{methoddesc}
696696

697+
\begin{methoddesc}[string]{zfill}{width}
698+
Return the numeric string left filled with zeros in a string
699+
of length \var{width}. The original string is returned if
700+
\var{width} is less than \code{len(\var{s})}.
701+
\end{methoddesc}
702+
697703

698704
\subsubsection{String Formatting Operations \label{typesseq-strings}}
699705

Lib/UserString.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def title(self): return self.__class__(self.data.title())
128128
def translate(self, *args):
129129
return self.__class__(self.data.translate(*args))
130130
def upper(self): return self.__class__(self.data.upper())
131+
def zfill(self, width): return self.__class__(self.data.zfill(width))
131132

132133
class MutableString(UserString):
133134
"""mutable string objects

Lib/string.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ def rfind(s, *args):
190190
_float = float
191191
_int = int
192192
_long = long
193-
_StringTypes = (str, unicode)
193+
try:
194+
_StringTypes = (str, unicode)
195+
except NameError:
196+
_StringTypes = (str,)
194197

195198
# Convert string to float
196199
def atof(s):
@@ -277,13 +280,8 @@ def zfill(x, width):
277280
278281
"""
279282
if not isinstance(x, _StringTypes):
280-
x = str(x)
281-
n = len(x)
282-
if n >= width: return x
283-
sign = ''
284-
if x[0] in '-+':
285-
sign, x = x[0], x[1:]
286-
return sign + '0'*(width-n) + x
283+
x = repr(x)
284+
return x.zfill(width)
287285

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

Lib/test/string_tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ def run_method_tests(test):
227227
test('endswith', 'ab', 0, 'ab', 0, 1)
228228
test('endswith', 'ab', 0, 'ab', 0, 0)
229229

230+
test('zfill', '123', '123', 2)
231+
test('zfill', '123', '123', 3)
232+
test('zfill', '123', '0123', 4)
233+
test('zfill', '+123', '+123', 3)
234+
test('zfill', '+123', '+123', 4)
235+
test('zfill', '+123', '+0123', 5)
236+
test('zfill', '-123', '-123', 3)
237+
test('zfill', '-123', '-123', 4)
238+
test('zfill', '-123', '-0123', 5)
239+
test('zfill', '', '000', 3)
230240
test('zfill', '34', '34', 1)
231241
test('zfill', '34', '0034', 4)
232242

Lib/test/test_unicode.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,18 @@ def __getitem__(self, i): return self.seq[i]
206206
test('capwords', u'abc\tdef\nghi', u'Abc Def Ghi')
207207
test('capwords', u'abc\t def \nghi', u'Abc Def Ghi')
208208

209-
verify(string.zfill(u'34', 1) == u'34')
210-
verify(string.zfill(u'34', 5) == u'00034')
209+
test('zfill', u'123', u'123', 2)
210+
test('zfill', u'123', u'123', 3)
211+
test('zfill', u'123', u'0123', 4)
212+
test('zfill', u'+123', u'+123', 3)
213+
test('zfill', u'+123', u'+123', 4)
214+
test('zfill', u'+123', u'+0123', 5)
215+
test('zfill', u'-123', u'-123', 3)
216+
test('zfill', u'-123', u'-123', 4)
217+
test('zfill', u'-123', u'-0123', 5)
218+
test('zfill', u'', u'000', 3)
219+
test('zfill', u'34', u'34', 1)
220+
test('zfill', u'34', u'00034', 5)
211221

212222
# Comparisons:
213223
print 'Testing Unicode comparisons...',

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Type/class unification and new-style classes
66

77
Core and builtins
88

9+
- A method zfill() was added to str and unicode, that fills a numeric
10+
string to the left with zeros. For example,
11+
"+123".zfill(6) -> "+00123".
12+
913
- Complex numbers supported divmod() and the // and % operators, but
1014
these make no sense. Since this was documented, they're being
1115
deprecated now.

Objects/stringobject.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,6 +2381,45 @@ string_center(PyStringObject *self, PyObject *args)
23812381
return pad(self, left, marg - left, ' ');
23822382
}
23832383

2384+
static char zfill__doc__[] =
2385+
"S.zfill(width) -> string\n"
2386+
"\n"
2387+
"Pad a numeric string S with zeros on the left, to fill a field\n"
2388+
"of the specified width. The string S is never truncated.";
2389+
2390+
static PyObject *
2391+
string_zfill(PyStringObject *self, PyObject *args)
2392+
{
2393+
int fill;
2394+
PyObject *s;
2395+
const char *p;
2396+
2397+
int width;
2398+
if (!PyArg_ParseTuple(args, "i:zfill", &width))
2399+
return NULL;
2400+
2401+
if (PyString_GET_SIZE(self) >= width) {
2402+
Py_INCREF(self);
2403+
return (PyObject*) self;
2404+
}
2405+
2406+
fill = width - PyString_GET_SIZE(self);
2407+
2408+
s = pad(self, fill, 0, '0');
2409+
2410+
if (s == NULL)
2411+
return NULL;
2412+
2413+
p = PyString_AS_STRING(s);
2414+
if (p[fill] == '+' || p[fill] == '-') {
2415+
/* move sign to beginning of string */
2416+
p[0] = p[fill];
2417+
p[fill] = '0';
2418+
}
2419+
2420+
return (PyObject*) s;
2421+
}
2422+
23842423
static char isspace__doc__[] =
23852424
"S.isspace() -> bool\n"
23862425
"\n"
@@ -2728,6 +2767,7 @@ string_methods[] = {
27282767
{"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__},
27292768
{"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__},
27302769
{"center", (PyCFunction)string_center, METH_VARARGS, center__doc__},
2770+
{"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__},
27312771
{"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__},
27322772
{"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__},
27332773
{"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, expandtabs__doc__},

Objects/unicodeobject.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4824,7 +4824,6 @@ unicode_upper(PyUnicodeObject *self)
48244824
return fixup(self, fixupper);
48254825
}
48264826

4827-
#if 0
48284827
static char zfill__doc__[] =
48294828
"S.zfill(width) -> unicode\n\
48304829
\n\
@@ -4850,6 +4849,9 @@ unicode_zfill(PyUnicodeObject *self, PyObject *args)
48504849

48514850
u = pad(self, fill, 0, '0');
48524851

4852+
if (u == NULL)
4853+
return NULL;
4854+
48534855
if (u->str[fill] == '+' || u->str[fill] == '-') {
48544856
/* move sign to beginning of string */
48554857
u->str[0] = u->str[fill];
@@ -4858,7 +4860,6 @@ unicode_zfill(PyUnicodeObject *self, PyObject *args)
48584860

48594861
return (PyObject*) u;
48604862
}
4861-
#endif
48624863

48634864
#if 0
48644865
static PyObject*
@@ -4970,8 +4971,8 @@ static PyMethodDef unicode_methods[] = {
49704971
{"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__},
49714972
{"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__},
49724973
{"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__},
4973-
#if 0
49744974
{"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__},
4975+
#if 0
49754976
{"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__},
49764977
#endif
49774978

0 commit comments

Comments
 (0)