[pypy-svn] r42217 - in pypy/branch/pypy-string-formatting/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Fri Apr 20 21:17:24 CEST 2007


Author: arigo
Date: Fri Apr 20 21:17:23 2007
New Revision: 42217

Modified:
   pypy/branch/pypy-string-formatting/objspace/std/formatting.py
   pypy/branch/pypy-string-formatting/objspace/std/test/test_stringformat.py
Log:
(pedronis, arigo)

Support for %c.


Modified: pypy/branch/pypy-string-formatting/objspace/std/formatting.py
==============================================================================
--- pypy/branch/pypy-string-formatting/objspace/std/formatting.py	(original)
+++ pypy/branch/pypy-string-formatting/objspace/std/formatting.py	Fri Apr 20 21:17:23 2007
@@ -265,6 +265,32 @@
     def fmt_r(self, w_value):
         self.std_wp(self.space.str_w(self.space.repr(w_value)))
 
+    def fmt_c(self, w_value):
+        space = self.space
+        try:
+            n = space.int_w(w_value)
+        except OperationError, e1:
+            if not e1.match(space, space.w_TypeError):
+                raise
+            try:
+                s = space.str_w(w_value)
+            except OperationError, e2:
+                if not e2.match(space, space.w_TypeError):
+                    raise
+                s = ''      # something invalid to trigger the TypeError
+            if len(s) != 1:
+                raise OperationError(space.w_TypeError,
+                                     space.wrap("%c requires int or char"))
+        else:
+            try:
+                s = chr(n)
+            except ValueError:  # chr(out-of-range)
+                raise OperationError(space.w_OverflowError,
+                    space.wrap("character code not in range(256)"))
+        
+        self.prec = -1     # just because
+        self.std_wp(s)
+
     def fmt_d(self, w_value):
         "int formatting"
         r = int_num_helper(self.space, w_value)

Modified: pypy/branch/pypy-string-formatting/objspace/std/test/test_stringformat.py
==============================================================================
--- pypy/branch/pypy-string-formatting/objspace/std/test/test_stringformat.py	(original)
+++ pypy/branch/pypy-string-formatting/objspace/std/test/test_stringformat.py	Fri Apr 20 21:17:23 2007
@@ -129,6 +129,15 @@
     def test_incomplete_format(self):
         raises(ValueError, '%'.__mod__, ((23,),))
 
+    def test_format_char(self):
+        assert '%c' % 65 == 'A'
+        assert '%c' % 'e' == 'e'
+        raises(OverflowError, '%c'.__mod__, (256,))
+        raises(OverflowError, '%c'.__mod__, (-1,))
+        raises(TypeError, '%c'.__mod__, ("bla",))
+        raises(TypeError, '%c'.__mod__, ("",))
+        raises(TypeError, '%c'.__mod__, (['c'],))
+
 class AppTestWidthPrec:
     def test_width(self):
         assert "%3s" %'a' == '  a'



More information about the Pypy-commit mailing list