[pypy-svn] r76461 - in pypy/branch/reflex-support/pypy/module/cppyy: . test

wlav at codespeak.net wlav at codespeak.net
Tue Aug 3 23:49:18 CEST 2010


Author: wlav
Date: Tue Aug  3 23:49:16 2010
New Revision: 76461

Modified:
   pypy/branch/reflex-support/pypy/module/cppyy/converter.py
   pypy/branch/reflex-support/pypy/module/cppyy/test/test_datatypes.py
Log:
Enable passing int value to C char


Modified: pypy/branch/reflex-support/pypy/module/cppyy/converter.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/converter.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/converter.py	Tue Aug  3 23:49:16 2010
@@ -1,6 +1,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.rlib.rarithmetic import r_singlefloat
+from pypy.objspace.std.intobject import W_IntObject
 
 from pypy.module.cppyy import helper, capi
 
@@ -36,11 +37,23 @@
         return rffi.cast(rffi.VOIDP, x)
 
 class CharConverter(TypeConverter):
-    def convert_argument(self, space, w_obj):
-        arg = space.str_w(w_obj)
-        if len(arg) != 1:
+    def _from_space(self, space, w_value):
+        # allow int to pass to char and make sure that str is of length 1
+        if type(w_value) == W_IntObject:
+            try:
+                value = chr(space.c_int_w(w_value))     
+            except ValueError, e:
+                raise OperationError(space.w_TypeError, space.wrap(str(e)))
+        else:
+            value = space.str_w(w_value)
+
+        if len(value) != 1:  
             raise OperationError(space.w_TypeError,
-                                 space.wrap("char expecter, got string of size %d" % len(arg)))
+                                 space.wrap("char expecter, got string of size %d" % len(value)))
+        return value
+
+    def convert_argument(self, space, w_obj):
+        arg = self._from_space(space, w_obj)
         x = rffi.str2charp(arg)
         return rffi.cast(rffi.VOIDP, x)
 
@@ -52,8 +65,7 @@
     def to_memory(self, space, w_obj, w_value, offset):
         obj = space.interpclass_w(space.findattr(w_obj, space.wrap("_cppinstance")))
         fieldptr = lltype.direct_ptradd(obj.rawobject, offset)
-        print w_value
-        fieldptr[0] = space.str_w(w_value)
+        fieldptr[0] = self._from_space(space, w_value)
 
 class IntConverter(TypeConverter):
     def convert_argument(self, space, w_obj):

Modified: pypy/branch/reflex-support/pypy/module/cppyy/test/test_datatypes.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/test/test_datatypes.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/test/test_datatypes.py	Tue Aug  3 23:49:16 2010
@@ -59,17 +59,18 @@
 
         # char types through data members
         c.m_char = 'b';    assert c.get_char()  ==     'b'
-        #c.m_char = 40;     assert c.get_char()  == chr(40)
+        c.m_char = 40;     assert c.get_char()  == chr(40)
         c.set_char('c');   assert c.m_char      ==     'c'
-        #c.set_char(41);    assert c.m_char      == chr(41)
+        c.set_char(41);    assert c.m_char      == chr(41)
         c.m_uchar = 'd';   assert c.get_uchar() ==     'd'
-        #c.m_uchar = 42;    assert c.get_uchar() == chr(42)
+        c.m_uchar = 42;    assert c.get_uchar() == chr(42)
         c.set_uchar('e');  assert c.m_uchar     ==     'e'
-        #c.set_uchar(43);   assert c.m_uchar     == chr(43)
+        c.set_uchar(43);   assert c.m_uchar     == chr(43)
 
         raises(TypeError, 'c.set_char("string")')
-#        raises(TypeError, 'c.set_uchar(-1)')
+        raises(TypeError, 'c.set_char(500)')
         raises(TypeError, 'c.set_uchar("string")')
+#        raises(TypeError, 'c.set_uchar(-1)')
 
         """
         # integer types



More information about the Pypy-commit mailing list