Author: Armin Rigo <[email protected]>
Branch:
Changeset: r76373:7941975d4a8a
Date: 2015-03-14 17:43 +0100
http://bitbucket.org/pypy/pypy/changeset/7941975d4a8a/
Log: Add str2chararray(), copying a string into some already-allocated
'char[]' buffer
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
@@ -794,6 +794,12 @@
else:
lltype.free(cp, flavor='raw', track_allocation=False)
+ # str -> already-existing char[maxsize]
+ def str2chararray(s, array, maxsize):
+ ll_s = llstrtype(s)
+ copy_string_to_raw(ll_s, array, 0, min(len(s), maxsize))
+ str2chararray._annenforceargs_ = [strtype, None, int]
+
# char* -> str
# doesn't free char*
def charp2str(cp):
@@ -944,19 +950,19 @@
return (str2charp, free_charp, charp2str,
get_nonmovingbuffer, free_nonmovingbuffer,
alloc_buffer, str_from_buffer, keep_buffer_alive_until_here,
- charp2strn, charpsize2str,
+ charp2strn, charpsize2str, str2chararray,
)
(str2charp, free_charp, charp2str,
get_nonmovingbuffer, free_nonmovingbuffer,
alloc_buffer, str_from_buffer, keep_buffer_alive_until_here,
- charp2strn, charpsize2str,
+ charp2strn, charpsize2str, str2chararray,
) = make_string_mappings(str)
(unicode2wcharp, free_wcharp, wcharp2unicode,
get_nonmoving_unicodebuffer, free_nonmoving_unicodebuffer,
alloc_unicodebuffer, unicode_from_buffer, keep_unicodebuffer_alive_until_here,
- wcharp2unicoden, wcharpsize2unicode,
+ wcharp2unicoden, wcharpsize2unicode, unicode2wchararray,
) = make_string_mappings(unicode)
# char**
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
@@ -676,6 +676,23 @@
assert interpret(f, [], backendopt=True) == 43
+ def test_str2chararray(self):
+ eci = ExternalCompilationInfo(includes=['string.h'])
+ strlen = llexternal('strlen', [CCHARP], SIZE_T,
+ compilation_info=eci)
+ def f():
+ raw = str2charp("XxxZy")
+ str2chararray("abcdef", raw, 4)
+ assert raw[0] == 'a'
+ assert raw[1] == 'b'
+ assert raw[2] == 'c'
+ assert raw[3] == 'd'
+ assert raw[4] == 'y'
+ lltype.free(raw, flavor='raw')
+ return 0
+
+ assert interpret(f, []) == 0
+
def test_around_extcall(self):
if sys.platform == "win32":
py.test.skip('No pipes on windows')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit