Commits

Armin Rigo committed 0336118

Improve the repr of instances and types of 'cdata'.

Comments (0)

Files changed (2)

ffi/backend_ctypes.py

     def _get_c_name(cls):
         return cls._reftypename.replace(' &', '')
 
+    @classmethod
+    def _fix_class(cls):
+        cls.__name__ = 'CData<%s>' % (cls._get_c_name(),)
+        cls.__module__ = 'ffi'
+
     def __repr__(self):
         return '<cdata %r>' % (self._get_c_name(),)
 
-    def _cast_to_address_of(self, Class):
-        raise TypeError("cannot cast %r to %r" % (
+    def _convert_to_address_of(self, Class):
+        raise TypeError("cannot convert %r to %r" % (
             self._get_c_name(), Class._reftypename.replace('&', '*')))
 
 
             _ctype = ctype
             _reftypename = '%s &' % name
 
-            def __init__(self, value=0):
-                if ctype(value).value != value:
+            def __init__(self, value):
+                if value is None:
+                    value = 0
+                elif ctype(value).value != value:
                     raise OverflowError("%r out of range: %d" %
                                         (name, value))
                 self._value = value
                 return x
             _export = staticmethod(_identity)
         #
+        CTypesInt._fix_class()
         return CTypesInt
 
     def new_pointer_type(self, bitem):
                 if init is None:
                     address = 0      # null pointer
                 elif isinstance(init, CTypesData):
-                    address = init._cast_to_address_of(bitem)
+                    address = init._convert_to_address_of(bitem)
                 else:
                     raise TypeError("%r expected, got %r" % (
                         CTypesPtr._get_c_name(), type(init).__name__))
             def __setitem__(self, index, value):
                 self._address[index] = bitem._import(value)
         #
+        CTypesPtr._fix_class()
         return CTypesPtr
 
     def new_array_type(self, bitem, length):
                     raise IndexError
                 self._blob[index] = bitem._import(value)
 
-            def _cast_to_address_of(self, bexpecteditem):
+            def _convert_to_address_of(self, bexpecteditem):
                 if bitem is bexpecteditem:
                     return ctypes.addressof(self._blob)
-                else:
-                    return CTypesData._cast_to_address_of(self, bexpecteditem)
+                return CTypesData._convert_to_address_of(self, bexpecteditem)
         #
+        CTypesArray._fix_class()
         return CTypesArray
 
 

testing/backend_tests.py

         ffi = FFI(backend=self.Backend())
         a = ffi.new("short int[10]")
         e = py.test.raises(TypeError, ffi.new, "long int *", a)
-        assert str(e.value) == "cannot cast 'short[10]' to 'long *'"
+        assert str(e.value) == "cannot convert 'short[10]' to 'long *'"
 
     def test_new_pointer_to_array(self):
         ffi = FFI(backend=self.Backend())
         assert p[3] == 106
         # keepalive: a
 
+    def test_repr(self):
+        ffi = FFI(backend=self.Backend())
+        p = ffi.new("unsigned short int")
+        assert repr(p) == "<cdata 'unsigned short'>"
+        assert repr(type(p)) == "<class 'ffi.CData<unsigned short>'>"
+        p = ffi.new("int*")
+        assert repr(p) == "<cdata 'int *'>"
+        assert repr(type(p)) == "<class 'ffi.CData<int *>'>"
+        p = ffi.new("int [2]")
+        assert repr(p) == "<cdata 'int[2]'>"
+        assert repr(type(p)) == "<class 'ffi.CData<int[2]>'>"
+
     def test_new_array_of_array(self):
         py.test.skip("in-progress")
         ffi = FFI(backend=self.Backend())