]> granicus.if.org Git - python/commitdiff
Fix bug discovered by John W. Shipman -- when the width of a format
authorGuido van Rossum <guido@python.org>
Mon, 7 Jun 1999 15:12:32 +0000 (15:12 +0000)
committerGuido van Rossum <guido@python.org>
Mon, 7 Jun 1999 15:12:32 +0000 (15:12 +0000)
specifier came from an int expression instead of a constant in the
format, a negative width was truncated to zero instead of taken to
mean the same as that negative constant plugged into the format.  E.g.
"(%*s)" % (-5, "foo") yielded "(foo)" while "(%-5s)" yields "(foo  )".
Now both yield the latter -- like sprintf() in C.

Objects/stringobject.c

index 1f1a41b21bdd5e9e629fdca6bb2a8b90ff32da7b..eecb0060b582ed76bfc5e3f2266f7203858c5e53 100644 (file)
@@ -832,8 +832,10 @@ PyString_Format(format, args)
                                        goto error;
                                }
                                width = PyInt_AsLong(v);
-                               if (width < 0)
-                                       width = 0;
+                               if (width < 0) {
+                                       flags |= F_LJUST;
+                                       width = -width;
+                               }
                                if (--fmtcnt >= 0)
                                        c = *fmt++;
                        }