[pypy-dev] pow and complex object

Ben Young ben at transversal.com
Mon Jul 5 10:27:45 CEST 2004


Hi, I have a patch for string formatting where you want to left pad with 
zeros. Could someone apply it for me if it looks reasonable?

Cheers,

Ben
---

Index: objspace/std/stringobject.py
===================================================================
--- objspace/std/stringobject.py        (revision 5431)
+++ objspace/std/stringobject.py        (working copy)
@@ -991,9 +991,16 @@
             else:
                 width = None
                 prec = None
+                lpadchar = ' '
+                padzeros = False
+                seennum = False
                 if c in '-0123456789':
                     j = i
                     while format[j] in '-0123456789':
+                        if format[j] in '0123456789' and not seennum:
+                            seennum = True
+                            if format[j] == '0':
+                                padzeros = True
                         j += 1
                     if format[i:j] != '-':
                         width = int(format[i:j])
@@ -1050,6 +1057,10 @@
                 else:
                     raise ValueError, "unsupported format character '%s' (%x) 
at index %d" % (
                             c, ord(c), i)
+
+                if c in 'xdfg' and padzeros:
+                    lpadchar = '0'
+
                 if prec is not None:
                     pieces[-1] = pieces[-1][:prec]
                 if width is not None:
@@ -1059,7 +1070,7 @@
                         p = p + ' '*d
                     else:
                         d = max(width - len(p), 0)
-                        p = ' '*d + p
+                        p = lpadchar*d + p
                     pieces[-1] = p
             state = 0
             start = i+1




More information about the Pypy-dev mailing list