Author: Amaury Forgeot d'Arc <[email protected]>
Branch: const-correctness
Changeset: r66930:82959e78dad2
Date: 2013-09-10 15:23 +0200
http://bitbucket.org/pypy/pypy/changeset/82959e78dad2/

Log:    Add a remove_const() function. No need to manually cast from "char*"
        to "const char**"

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
@@ -980,6 +980,20 @@
         raise TypeError("invalid cast_opaque_ptr(): %r -> %r" %
                         (CURTYPE, PTRTYPE))
 
+def remove_const(T):
+    "The same type as T, but with any top level const-qualifier removed."
+    if isinstance(T, Ptr):
+        return Ptr(remove_const(T.TO))
+    if not T._hints.get('render_as_const'):
+        return T
+    hints = T._hints.copy()
+    del hints['render_as_const']
+    T2 = object.__new__(type(T))
+    T2.__dict__.update(T.__dict__)
+    T2._hints = T2._hints.copy()
+    del T2._hints['render_as_const']
+    return T2
+
 def direct_fieldptr(structptr, fieldname):
     """Get a pointer to a field in the struct.  The resulting
     pointer is actually of type Ptr(FixedSizeArray(FIELD, 1)).
@@ -1248,7 +1262,7 @@
             if len(args) != len(self._T.ARGS):
                 raise TypeError,"calling %r with wrong argument number: %r" % 
(self._T, args)
             for i, a, ARG in zip(range(len(self._T.ARGS)), args, self._T.ARGS):
-                if typeOf(a) != ARG:
+                if typeOf(a) != remove_const(ARG):
                     # ARG could be Void
                     if ARG == Void:
                         try:
diff --git a/rpython/rtyper/lltypesystem/test/test_lltype.py 
b/rpython/rtyper/lltypesystem/test/test_lltype.py
--- a/rpython/rtyper/lltypesystem/test/test_lltype.py
+++ b/rpython/rtyper/lltypesystem/test/test_lltype.py
@@ -210,6 +210,17 @@
     assert p1b == cast_pointer(Ptr(S1bis), p3)
     py.test.raises(RuntimeError, "cast_pointer(Ptr(S1), p3)")
 
+def test_remove_const():
+    T = Ptr(GcArray(('v', Signed), hints={'render_as_const': True, 'y': 2}))
+    T2 = remove_const(T)
+    assert T2 != T
+    assert T2 == Ptr(GcArray(('v', Signed), hints={'y': 2}))
+    S = Ptr(Struct("s2", ('a', Signed), hints={'render_as_const': True}))
+    S2 = remove_const(S)
+    assert S2 != S
+    assert S2 == Ptr(Struct("s2", ('a', Signed)))
+    
+
 def test_best_effort_gced_parent_detection():
     py.test.skip("test not relevant any more")
     S2 = Struct("s2", ('a', Signed))
diff --git a/rpython/translator/c/test/test_lltyped.py 
b/rpython/translator/c/test/test_lltyped.py
--- a/rpython/translator/c/test/test_lltyped.py
+++ b/rpython/translator/c/test/test_lltyped.py
@@ -954,7 +954,7 @@
             s = malloc(rffi.CCHARP.TO, 2, flavor='raw')
             s[0] = '9'
             s[1] = '\0'
-            res = atoi(rffi.cast(rffi.CONST_CCHARP, s))
+            res = atoi(s)
             free(s, flavor='raw')
             return res
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to