Author: Maciej Fijalkowski <[email protected]>
Branch:
Changeset: r65812:19e81f8f9f39
Date: 2013-07-30 11:47 +0200
http://bitbucket.org/pypy/pypy/changeset/19e81f8f9f39/
Log: support const char* (test is a bit imperfect, but I fail to see when
it actually explodes)
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
@@ -649,6 +649,10 @@
# char *
CCHARP = lltype.Ptr(lltype.Array(lltype.Char, hints={'nolength': True}))
+# const char *
+CONST_CCHARP = lltype.Ptr(lltype.Array(lltype.Char, hints={'nolength': True,
+ 'render_as_const': True}))
+
# wchar_t *
CWCHARP = lltype.Ptr(lltype.Array(lltype.UniChar, hints={'nolength': True}))
diff --git a/rpython/translator/c/node.py b/rpython/translator/c/node.py
--- a/rpython/translator/c/node.py
+++ b/rpython/translator/c/node.py
@@ -358,6 +358,8 @@
self.fullptrtypename = 'void *@'
else:
self.fullptrtypename = self.itemtypename.replace('@', '*@')
+ if ARRAY._hints.get("render_as_const"):
+ self.fullptrtypename = 'const ' + self.fullptrtypename
def setup(self):
"""Array loops are forbidden by ForwardReference.become() because
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
@@ -1,5 +1,6 @@
import py
from rpython.rtyper.lltypesystem.lltype import *
+from rpython.rtyper.lltypesystem import rffi
from rpython.translator.c.test.test_genc import compile
from rpython.tool.sourcetools import func_with_new_name
@@ -314,14 +315,14 @@
from rpython.rtyper.lltypesystem.rstr import STR
from rpython.rtyper.lltypesystem import rffi, llmemory, lltype
P = lltype.Ptr(lltype.FixedSizeArray(lltype.Char, 1))
-
+
def f():
a = llstr("xyz")
b = (llmemory.cast_ptr_to_adr(a) + llmemory.offsetof(STR, 'chars')
+ llmemory.itemoffsetof(STR.chars, 0))
buf = rffi.cast(rffi.VOIDP, b)
return buf[2]
-
+
fn = self.getcompiled(f, [])
res = fn()
assert res == 'z'
@@ -941,3 +942,22 @@
assert fn(0) == 10
assert fn(1) == 10 + 521
assert fn(2) == 10 + 34
+
+ def test_const_char_star(self):
+ import os
+ from rpython.translator.tool.cbuild import ExternalCompilationInfo
+
+ eci = ExternalCompilationInfo(includes=["stdlib.h"])
+ atoi = rffi.llexternal('atoi', [rffi.CONST_CCHARP], rffi.INT,
+ compilation_info=eci)
+
+ def f(n):
+ s = malloc(rffi.CCHARP.TO, 2, flavor='raw')
+ s[0] = '9'
+ s[1] = '\0'
+ res = atoi(rffi.cast(rffi.CONST_CCHARP, s))
+ free(s, flavor='raw')
+ return res
+
+ fn = self.getcompiled(f, [int])
+ assert fn(0) == 9
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit