[Python-checkins] cpython: Cleanup unicodeobject.c

victor.stinner python-checkins at python.org
Tue Dec 4 10:07:45 CET 2012


http://hg.python.org/cpython/rev/b3fe2ef13470
changeset:   80719:b3fe2ef13470
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Tue Dec 04 09:30:24 2012 +0100
summary:
  Cleanup unicodeobject.c

 * Remove micro-optization:
   (errors == "surrogateescape" || strcmp(errors, "surrogateescape") == 0).
   Only use strcmp()
 * Initialize 'arg' members in unicode_format_arg() to help the compiler to
   diagnose real bugs and also make the code simpler to read

files:
  Objects/unicodeobject.c |  21 ++++++++++++---------
  1 files changed, 12 insertions(+), 9 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -3110,8 +3110,7 @@
         *surrogateescape = 0;
         return 0;
     }
-    if (errors == "surrogateescape"
-        || strcmp(errors, "surrogateescape") == 0) {
+    if (strcmp(errors, "surrogateescape") == 0) {
         *surrogateescape = 1;
         return 0;
     }
@@ -13438,7 +13437,6 @@
 
     PyObject *v;
 
-    arg->ch = FORMAT_READ(ctx);
     if (arg->ch == '(') {
         /* Get argument value from a dictionary. Example: "%(name)s". */
         Py_ssize_t keystart;
@@ -13487,7 +13485,6 @@
     }
 
     /* Parse flags. Example: "%+i" => flags=F_SIGN. */
-    arg->flags = 0;
     while (--ctx->fmtcnt >= 0) {
         arg->ch = FORMAT_READ(ctx);
         ctx->fmtpos++;
@@ -13502,7 +13499,6 @@
     }
 
     /* Parse width. Example: "%10s" => width=10 */
-    arg->width = -1;
     if (arg->ch == '*') {
         v = unicode_format_getnextarg(ctx);
         if (v == NULL)
@@ -13544,7 +13540,6 @@
     }
 
     /* Parse precision. Example: "%.3f" => prec=3 */
-    arg->prec = -1;
     if (arg->ch == '.') {
         arg->prec = 0;
         if (--ctx->fmtcnt >= 0) {
@@ -13613,9 +13608,12 @@
    - "e", "E", "f", "F", "g", "G": float
    - "c": int or str (1 character)
 
+   When possible, the output is written directly into the Unicode writer
+   (ctx->writer). A string is created when padding is required.
+
    Return 0 if the argument has been formatted into *p_str,
           1 if the argument has been written into ctx->writer,
-          -1 on error. */
+         -1 on error. */
 static int
 unicode_format_arg_format(struct unicode_formatter_t *ctx,
                           struct unicode_format_arg_t *arg,
@@ -13639,10 +13637,8 @@
     if (v == NULL)
         return -1;
 
-    arg->sign = 0;
 
     switch (arg->ch) {
-
     case 's':
     case 'r':
     case 'a':
@@ -13894,6 +13890,13 @@
     PyObject *str;
     int ret;
 
+    arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos);
+    arg.flags = 0;
+    arg.width = -1;
+    arg.prec = -1;
+    arg.sign = 0;
+    str = NULL;
+
     ret = unicode_format_arg_parse(ctx, &arg);
     if (ret == -1)
         return -1;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list