[Python-checkins] r54077 - sandbox/trunk/pep3101/test_simpleformat.py sandbox/trunk/pep3101/unicodeformat.c

patrick.maupin python-checkins at python.org
Fri Mar 2 07:28:11 CET 2007


Author: patrick.maupin
Date: Fri Mar  2 07:28:09 2007
New Revision: 54077

Modified:
   sandbox/trunk/pep3101/test_simpleformat.py
   sandbox/trunk/pep3101/unicodeformat.c
Log:
Better error detection and exception reporting for bad/missing
arguments and object index/attribute errors.


Modified: sandbox/trunk/pep3101/test_simpleformat.py
==============================================================================
--- sandbox/trunk/pep3101/test_simpleformat.py	(original)
+++ sandbox/trunk/pep3101/test_simpleformat.py	Fri Mar  2 07:28:09 2007
@@ -47,8 +47,8 @@
 
    def test_missingargs(self):
        #self.formatRaises(None, "Doesn't use all {0} args", 42, 24)
-       self.formatRaises(IndexError, "There is no {4} arg", 42, 24)
-       self.formatRaises(KeyError, "There question is {when}", who=True)
+       self.formatRaises(ValueError, "There is no {4} arg", 42, 24)
+       self.formatRaises(ValueError, "There question is {when}", who=True)
 
    def test_attributes(self):
        class Container(object):

Modified: sandbox/trunk/pep3101/unicodeformat.c
==============================================================================
--- sandbox/trunk/pep3101/unicodeformat.c	(original)
+++ sandbox/trunk/pep3101/unicodeformat.c	Fri Mar  2 07:28:09 2007
@@ -121,7 +121,7 @@
     /* current position and end of the 'self' string passed to FormatMethod */
     SubString fmtstr;
     /* Used for error reporting */
-    CH_TYPE *fmtstart;
+    CH_TYPE *fmtstart, *fieldstart;
     /* Output string we are constructing, including current and end pointers*/
     SubStringObj outstr;
     /* Field Specifier, after the colon in {1:{2}}
@@ -185,11 +185,18 @@
     check_fmtstr returns True if we still have characters
     left in the format string.
 */
+static void *
+check_fmtstr_helper(FmtState *fs)
+{
+    fs->fmtstr.ptr = fs->fieldstart;
+    return SetError(fs, "Unterminated replacement field");
+}
+
 Py_LOCAL_INLINE(int)
 check_fmtstr(FmtState *fs)
 {
     return (fs->fmtstr.ptr < fs->fmtstr.end) ||
-           SetError(fs, "Unexpected end of format_string");
+        check_fmtstr_helper(fs);
 }
 
 /************************************************************************/
@@ -478,13 +485,14 @@
     index = 0;  /* Just to shut up the compiler warning */
     if (!check_fmtstr(fs))
         return NULL;
+    isargument=1;
     isnumeric = (CH_TYPE_ISDECIMAL(*fs->fmtstr.ptr));
     myobj = isnumeric ? fs->args : fs->keywords;
     if (myobj == NULL)
-        return SetError(fs, "Keyword not specified");
+        goto ERROR;
     Py_INCREF(myobj);
 
-    for (isindex=1, expectclose=0, isargument=1;;) {
+    for (isindex=1, expectclose=0;;) {
         if (!check_fmtstr(fs))
             break;
         if (!isindex) {
@@ -521,6 +529,8 @@
         }
         Py_DECREF(myobj);
         myobj = newobj;
+        if (myobj == NULL)
+            break;
         if (expectclose)
             if  ((!check_fmtstr(fs)) || (*fs->fmtstr.ptr++ != ']')) {
                 SetError(fs, "Expected ]");
@@ -539,7 +549,16 @@
            break;
         }
     }
-    Py_DECREF(myobj);
+ERROR:
+    if ((myobj == NULL) && isargument)
+    {
+       PyErr_Clear();
+       fs->fmtstr.ptr = fs->fieldstart;
+       return SetError(fs, isnumeric
+            ? "Not enough positional arguments"
+            : "Keyword argument not found");
+    }
+    Py_XDECREF(myobj);
     return NULL;
 }
 
@@ -1087,7 +1106,7 @@
             fmtstr.ptr++;
             count--;
         }
-        fmtstr.ptr++;
+        fs->fieldstart = fmtstr.ptr++;
         count = total - count;
         total -= count;
         doubled = (total > 1) && (*fmtstr.ptr == c);


More information about the Python-checkins mailing list