[pypy-svn] r36101 - in pypy/dist/pypy/rlib/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Tue Jan 2 15:18:41 CET 2007


Author: arigo
Date: Tue Jan  2 15:18:37 2007
New Revision: 36101

Modified:
   pypy/dist/pypy/rlib/rctypes/rctypesobject.py
   pypy/dist/pypy/rlib/rctypes/test/test_rctypesobject.py
Log:
- support for rc_char_p containing None.
- a copyfrom() that is easier to support for llmemory.


Modified: pypy/dist/pypy/rlib/rctypes/rctypesobject.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rctypesobject.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rctypesobject.py	Tue Jan  2 15:18:37 2007
@@ -75,12 +75,6 @@
                 return cls(memblock.addr, memblock)
             cls.allocate = staticmethod(allocate1)
 
-            def copyfrom1(self, srcbox):
-                assert isinstance(srcbox, cls)
-                llmemory.raw_memcopy(srcbox.addr, self.addr, cls.rawsize)
-                self._copykeepalives(0, srcbox)
-            cls.copyfrom = copyfrom1
-
             if hasattr(cls, 'llvalue2value') and not hasattr(cls, 'get_value'):
                 def get_value(self):
                     ptr = self.ll_ref(cls.CDATATYPE)
@@ -96,6 +90,18 @@
                     keepalive_until_here(self)
                 cls.set_value = set_value
 
+            if hasattr(cls, 'get_value') and hasattr(cls, 'set_value'):
+                def copyfrom1(self, srcbox):
+                    assert isinstance(srcbox, cls)
+                    self.set_value(srcbox.get_value())
+                    self._copykeepalives(0, srcbox)
+            else:
+                def copyfrom1(self, srcbox):
+                    assert isinstance(srcbox, cls)
+                    llmemory.raw_memcopy(srcbox.addr, self.addr, cls.rawsize)
+                    self._copykeepalives(0, srcbox)
+            cls.copyfrom = copyfrom1
+
     def sameaddr(self, otherbox):
         return self.addr == otherbox.addr
 
@@ -199,22 +205,29 @@
     num_keepalives = 1
 
     def llvalue2value(p):
-        length = strlen(p)
-        return charp2string(p, length)
+        if p:
+            length = strlen(p)
+            return charp2string(p, length)
+        else:
+            return None
     llvalue2value = staticmethod(llvalue2value)
 
     #def get_value(self): added by __initclass__() above
 
     def set_value(self, string):
-        n = len(string)
-        rawsize = RCTypesCharP.ITEMOFS * (n + 1)
-        targetmemblock = AllocatedRawMemBlock(0, rawsize, zero=False)
-        targetaddr = targetmemblock.addr
-        a = targetaddr
-        for i in range(n):
-            a.char[0] = string[i]
-            a += RCTypesCharP.ITEMOFS
-        a.char[0] = '\x00'
+        if string is not None:
+            n = len(string)
+            rawsize = RCTypesCharP.ITEMOFS * (n + 1)
+            targetmemblock = AllocatedRawMemBlock(0, rawsize, zero=False)
+            targetaddr = targetmemblock.addr
+            a = targetaddr
+            for i in range(n):
+                a.char[0] = string[i]
+                a += RCTypesCharP.ITEMOFS
+            a.char[0] = '\x00'
+        else:
+            targetmemblock = None
+            targetaddr = llmemory.NULL
         ptr = self.ll_ref(RCTypesCharP.CDATATYPE)
         ptr[0] = llmemory.cast_adr_to_ptr(targetaddr, RCTypesCharP.LLTYPE)
         keepalive_until_here(self)

Modified: pypy/dist/pypy/rlib/rctypes/test/test_rctypesobject.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/test/test_rctypesobject.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/test/test_rctypesobject.py	Tue Jan  2 15:18:37 2007
@@ -158,6 +158,18 @@
         res = self.do(func)
         assert res == 1
 
+    def test_char_p_None(self):
+        def func():
+            p = rc_char_p.allocate()
+            assert p.get_value() is None
+            p.set_value("")
+            assert p.get_value() == ""
+            p.set_value("abc")
+            assert p.get_value() == "abc"
+            p.set_value(None)
+            assert p.get_value() is None
+        self.do(func)
+
     def test_char_array(self):
         def func():
             a = RFixedArray(rc_char, 10).allocate()


More information about the pypy-svn mailing list