[Python-checkins] r54134 - sandbox/trunk/pep3101/README.txt sandbox/trunk/pep3101/test_simpleformat.py sandbox/trunk/pep3101/unicodeformat.c

patrick.maupin python-checkins at python.org
Mon Mar 5 06:53:30 CET 2007


Author: patrick.maupin
Date: Mon Mar  5 06:53:26 2007
New Revision: 54134

Modified:
   sandbox/trunk/pep3101/README.txt
   sandbox/trunk/pep3101/test_simpleformat.py
   sandbox/trunk/pep3101/unicodeformat.c
Log:
More tests; fixed bugs in hook function and alternate syntax 2

Modified: sandbox/trunk/pep3101/README.txt
==============================================================================
--- sandbox/trunk/pep3101/README.txt	(original)
+++ sandbox/trunk/pep3101/README.txt	Mon Mar  5 06:53:26 2007
@@ -54,12 +54,11 @@
     - document differences between PEP and implementation
         (in pep_differences.txt)
     - Add docstrings to module
-    - Add keyword options and string metadata options
-      as described in pep_differences.
-    - Play with possible implementations for exposing
-      lowest level format specifier handler for use in
-      compatible template systems.
     - Should we have stricter checking on format strings?  For example
       type "s" doesn't allow a sign character.  Should specifying one
       be an error?
     - Test suite needs to check for specific exceptions.
+    - Add capability to control exception handling to pep_differences
+      and to code:  1) ability to dump exceptions into string, 2)
+      ability to re-do lower exceptions or "pile-on"
+

Modified: sandbox/trunk/pep3101/test_simpleformat.py
==============================================================================
--- sandbox/trunk/pep3101/test_simpleformat.py	(original)
+++ sandbox/trunk/pep3101/test_simpleformat.py	Mon Mar  5 06:53:26 2007
@@ -277,19 +277,53 @@
         mydict = dict(foo=1, foo2=1)
         bar = 3
         bar2 = 4
-        s = '{0} {1} {foo} {bar}'
+        result = ' a b 1 3'
+        s = ' {0} {1} {foo} {bar}'
         s2 = '{!useall}' + s
-        self.formatEquals('a b 1 3', s, 'a', 'b', bar=bar, _dict=mydict)
-        self.formatEquals('a b 1 3', s2, 'a', 'b', bar=bar, _dict=mydict)
-        self.formatEquals('a b 1 3', s, 'a', 'b', 3, bar=bar, _dict=mydict)
+        s3 = '{!useall}\r\n' + s
+        s4 = '{!useall}\n' + s
+        s5 = '{!useall}\r\n' + s
+        self.formatEquals(result, s, 'a', 'b', bar=bar, _dict=mydict)
+        self.formatEquals(result, s2, 'a', 'b', bar=bar, _dict=mydict)
+        self.formatEquals(result, s3, 'a', 'b', bar=bar, _dict=mydict)
+        self.formatEquals(result, s4, 'a', 'b', bar=bar, _dict=mydict)
+        self.formatEquals(result, s5, 'a', 'b', bar=bar, _dict=mydict)
+        self.formatEquals(result, s, 'a', 'b', 3, bar=bar, _dict=mydict)
         self.formatRaises(ValueError, s2, 'a', 'b', 3, bar=bar, _dict=mydict)
-        self.formatEquals('a b 1 3', s, 'a', 'b', bar=bar, _dict=mydict, sam=27)
+        self.formatEquals(result, s, 'a', 'b', bar=bar, _dict=mydict, sam=27)
         self.formatRaises(ValueError, s2, 'a', 'b', bar=bar, _dict=mydict, sam=27)
-        self.formatEquals('a b 1 3', s, 'a', 'b', bar=bar, foo=1)
-        self.formatEquals('a b 1 3', s, 'a', 'b', bar=bar, foo=1)
-        self.formatEquals('a b 1 3', s, 'a', 'b', bar=bar, foo=1, sam=27)
+        self.formatEquals(result, s, 'a', 'b', bar=bar, foo=1)
+        self.formatEquals(result, s, 'a', 'b', bar=bar, foo=1)
+        self.formatEquals(result, s, 'a', 'b', bar=bar, foo=1, sam=27)
         self.formatRaises(ValueError, s2, 'a', 'b', bar=bar, foo=1, sam=27)
 
+    def test_comments(self):
+        self.formatEquals('Hi there','''{#Comment}Hi{#
+           This is a comment}{#Another} th{#comment}ere{#}''')
+
+    def test_format_hook(self):
+        def hookfunc(obj, spec):
+            if obj > 100:
+                return None
+            return '_%s_' % (obj, spec)[obj==3]
+        self.formatEquals('_a_ _4_       123',
+                          '{!hook}{0:a} {1:b}{2:>10d}',
+                          3, 4, 123, _hook=hookfunc)
+        self.formatEquals('_ap_ _4_       123',
+                          '{0:ap} {1:bp}{2:>10d}',
+                          3, 4, 123, _hook=hookfunc)
+        self.formatRaises(ValueError, '{0:ap}')
+        self.formatEquals('_ap_', '{0:ap}', 3,  _hook=hookfunc)
+        self.formatRaises(ValueError, '{0:ap}', 123, _hook=hookfunc)
+
+    def test_alt_syntax(self):
+        self.formatEquals('{}1', '{!syntax1}{{}{0}', 1)
+        self.formatEquals('{  ${  1  $1  $${0}',
+                          '{!syntax2}{  $${  ${0}  $$${0}  $$$${0}',
+                           1)
+        self.formatEquals('1 {0} {\n0}',
+                          '{!syntax3}{0} { 0} {\n0}', 1)
+
 def test_main():
     test_support.run_unittest(FormatTest)
 

Modified: sandbox/trunk/pep3101/unicodeformat.c
==============================================================================
--- sandbox/trunk/pep3101/unicodeformat.c	(original)
+++ sandbox/trunk/pep3101/unicodeformat.c	Mon Mar  5 06:53:26 2007
@@ -399,7 +399,7 @@
 static int
 read_hook_parameter(FmtState *fs)
 {
-    if (fs->hookfunc != NULL)
+    if (fs->hookfunc == NULL)
         fs->hookfunc = read_parameter(fs, "_hook");
     return (fs->hookfunc != NULL) ||
        SetError(fs, "No _hook function supplied");
@@ -1740,7 +1740,7 @@
             switch (c = *ptr++) {
                 case '{':
                     if ((syntaxmode == 2) &&
-                        ((ptr == start) || (ptr[-2] != '$')))
+                        ((ptr == start+1) || (ptr[-2] != '$')))
                         continue;
                     break;
                 case '}':
@@ -1769,12 +1769,19 @@
                     else
                         count--;
                     break;
-                case 2:
-                    count -= 2;
-                    escape = !count || (ptr[-3] != '$');
+                case 2: {
+                    CH_TYPE *first_dollar = ptr - 2;
+                    int num_dollars;
+                    while ((first_dollar > start) &&
+                           (first_dollar[-1] == '$'))
+                        first_dollar--;
+                    num_dollars = ptr - 1 - first_dollar;
+                    count = first_dollar - start + (num_dollars / 2);
+                    escape = num_dollars & 1;
                     if (!escape)
                         ptr--;
                     break;
+                }
                 case 3:
                     switch (*ptr) {
                         case ' ':
@@ -1800,8 +1807,12 @@
         fs->fmtstr.ptr = ptr;
         if (count && !output_data(fs, start, count))
             return 0;
-        if (escape && !get_field_and_render(fs))
-            return 0;
+        if (escape) {
+            if (!get_field_and_render(fs))
+                return 0;
+            else
+                syntaxmode = fs->syntaxmode;
+        }
     }
     return 1;
 }


More information about the Python-checkins mailing list