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

patrick.maupin python-checkins at python.org
Mon Mar 5 04:23:05 CET 2007


Author: patrick.maupin
Date: Mon Mar  5 04:23:01 2007
New Revision: 54132

Modified:
   sandbox/trunk/pep3101/test_simpleformat.py
   sandbox/trunk/pep3101/unicodeformat.c
Log:
Added tests for name_mapper and made them pass

Modified: sandbox/trunk/pep3101/test_simpleformat.py
==============================================================================
--- sandbox/trunk/pep3101/test_simpleformat.py	(original)
+++ sandbox/trunk/pep3101/test_simpleformat.py	Mon Mar  5 04:23:01 2007
@@ -3,6 +3,10 @@
 
 import pep3101
 
+# Variables used for testing name mapper
+global1 = 10
+global2 = 20
+global3 = 30
 
 # The test implementation does not allow an argument
 # index or keyword name to be used more than once. The
@@ -253,6 +257,21 @@
         self.formatRaises(ValueError, "{{ {{{0}}", True)
         self.formatRaises(ValueError, "{0}}", True)
 
+    def test_name_mapper(self):
+        mydict = dict(foo=1, bar=2)
+        dict2 = mydict, dict(foobar=3)
+        foo = 27
+        global3 = 50
+        self.formatRaises(ValueError, "{foo}")
+        self.formatRaises(ValueError, "{foo} {foobar}", _dict=mydict)
+        self.formatEquals("1", "{foo}", _dict=mydict)
+        self.formatEquals("1 2", "{foo} {bar}", _dict=mydict)
+        self.formatRaises(ValueError, "{foo} {bar} {global3}", _dict=mydict)
+        self.formatEquals("1 2 3", "{foo} {bar} {foobar}", _dict=dict2)
+        self.formatEquals("27 2", "{foo} {bar}", _dict=mydict, foo=foo)
+        self.assertEquals(
+            pep3101.format("{foo} {global3} {global1}"),
+            "27 50 10")
 
 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 04:23:01 2007
@@ -294,7 +294,8 @@
 {
     PyObject *locals, *globals, *result;
     int gotglobals, gotlocals;
-    if ((fs->args != NULL))
+
+    if (!PyTuple_GET_SIZE(fs->args))
         return (int)SetError(fs, "no keywords parameters available");
     locals = PyEval_GetLocals();
     gotlocals = locals != NULL;
@@ -309,7 +310,7 @@
     }
     if (gotglobals) {
         Py_INCREF(globals);
-        PyTuple_SET_ITEM(result, 0, globals);
+        PyTuple_SET_ITEM(result, gotlocals, globals);
     }
     fs->keywords_tuple = result;
     return 1;
@@ -1971,7 +1972,7 @@
 
     if (fs.num_args != 1) {
         PyErr_SetString(PyExc_TypeError,
-                "Function self plus one positional argument");
+                "Function expects self plus one positional argument");
         return 0;
     }
     fs.do_markup = single_field_markup;


More information about the Python-checkins mailing list