Author: Antonio Cuni <[email protected]>
Branch: hpy-ctypespace
Changeset: r98140:a8819c541390
Date: 2019-11-22 12:06 +0100
http://bitbucket.org/pypy/pypy/changeset/a8819c541390/

Log:    Introduce rffi.constcharp2str, and improve the repr of low-level
        array types.

        Previously, you got annotation errors like this, which were VERY
        confusing:

         * UnionError:
            - SomePtr(ll_ptrtype=<* Array of Char >)
            - SomePtr(ll_ptrtype=<* Array of Char >)

        Now you get the much more meaningful:

         * UnionError:
            - SomePtr(ll_ptrtype=<* Array of Char {'nolength': True} >)
            - SomePtr(ll_ptrtype=<* Array of Char {'render_as_const': True,
        'nolength': True} >)

diff --git a/rpython/rtyper/lltypesystem/lltype.py 
b/rpython/rtyper/lltypesystem/lltype.py
--- a/rpython/rtyper/lltypesystem/lltype.py
+++ b/rpython/rtyper/lltypesystem/lltype.py
@@ -467,12 +467,16 @@
     _str_fields = saferecursive(_str_fields, '...')
 
     def __str__(self):
-        return "%s of %s " % (self.__class__.__name__,
-                               self._str_fields(),)
+        hints = str(self._hints) if self._hints else ''
+        return "%s of %s %s " % (self.__class__.__name__,
+                                 self._str_fields(),
+                                 hints)
 
     def _short_name(self):
-        return "%s %s" % (self.__class__.__name__,
-                          self.OF._short_name(),)
+        hints = str(self._hints) if self._hints else ''
+        return "%s %s %s" % (self.__class__.__name__,
+                             self.OF._short_name(),
+                             hints)
     _short_name = saferecursive(_short_name, '...')
 
     def _container_example(self):
diff --git a/rpython/rtyper/lltypesystem/rffi.py 
b/rpython/rtyper/lltypesystem/rffi.py
--- a/rpython/rtyper/lltypesystem/rffi.py
+++ b/rpython/rtyper/lltypesystem/rffi.py
@@ -1024,6 +1024,14 @@
  ) = make_string_mappings(unicode)
 
 
+def constcharp2str(cp):
+    """
+    Like charp2str, but takes a CONST_CCHARP instead
+    """
+    cp = cast(CCHARP, cp)
+    return charp2str(cp)
+constcharp2str._annenforceargs_ = [lltype.SomePtr(CONST_CCHARP)]
+
 @not_rpython
 def _deprecated_get_nonmovingbuffer(*args):
     raise Exception(
diff --git a/rpython/rtyper/lltypesystem/test/test_rffi.py 
b/rpython/rtyper/lltypesystem/test/test_rffi.py
--- a/rpython/rtyper/lltypesystem/test/test_rffi.py
+++ b/rpython/rtyper/lltypesystem/test/test_rffi.py
@@ -142,6 +142,33 @@
         xf = self.compile(f, [], backendopt=False)
         assert xf() == 3
 
+    def test_constcharp2str(self):
+        c_source = py.code.Source("""
+        const char *z(void)
+        {
+            return "hello world";
+        }
+        """)
+        eci = ExternalCompilationInfo(separate_module_sources=[c_source],
+                                     post_include_bits=['const char 
*z(void);'])
+        z = llexternal('z', [], CONST_CCHARP, compilation_info=eci)
+
+        def f():
+            l_buf = lltype.malloc(CCHARP.TO, 5, flavor='raw')
+            l_buf[0] = 'A'
+            l_buf[1] = 'B'
+            l_buf[2] = 'C'
+            l_buf[3] = '\x00'
+            l_buf[4] = 'E'
+            l_constbuf = cast(CONST_CCHARP, l_buf)
+            res = constcharp2str(l_constbuf)
+            lltype.free(l_buf, flavor='raw')
+            return len(res)
+
+        assert f() == 3
+        xf = self.compile(f, [], backendopt=False)
+        assert xf() == 3
+
     def test_stringstar(self):
         c_source = """
         #include <string.h>
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to