Author: Armin Rigo <[email protected]>
Branch: hpy
Changeset: r98100:6009d087288b
Date: 2019-11-17 19:28 +0100
http://bitbucket.org/pypy/pypy/changeset/6009d087288b/

Log:    HPyUnicode_FromString()

diff --git a/pypy/module/hpy_universal/interp_hpy.py 
b/pypy/module/hpy_universal/interp_hpy.py
--- a/pypy/module/hpy_universal/interp_hpy.py
+++ b/pypy/module/hpy_universal/interp_hpy.py
@@ -2,6 +2,7 @@
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.rlib.rdynload import dlopen, dlsym, DLOpenError
 from rpython.rlib.objectmodel import specialize
+from rpython.rlib import rutf8
 
 from pypy.interpreter.gateway import unwrap_spec
 from pypy.interpreter.error import raise_import_error
@@ -81,6 +82,16 @@
     w_result = space.add(w_obj1, w_obj2)
     return handles.new(space, w_result)
 
+@apifunc([llapi.HPyContext, rffi.CCHARP], llapi.HPy, error=0)
+def HPyUnicode_FromString(space, ctx, utf8):
+    s = rffi.charp2str(utf8)
+    try:
+        length = rutf8.check_utf8(s, allow_surrogates=False)
+    except rutf8.CheckError:
+        raise   # XXX do something
+    w_obj = space.newtext(s, length)
+    return handles.new(space, w_obj)
+
 
 def create_hpy_module(space, name, origin, lib, initfunc):
     state = space.fromcache(State)
diff --git a/pypy/module/hpy_universal/state.py 
b/pypy/module/hpy_universal/state.py
--- a/pypy/module/hpy_universal/state.py
+++ b/pypy/module/hpy_universal/state.py
@@ -61,3 +61,6 @@
         #
         funcptr = interp_hpy.HPyNumber_Add.get_llhelper(space)
         self.ctx.c_ctx_Number_Add = rffi.cast(rffi.VOIDP, funcptr)
+        #
+        funcptr = interp_hpy.HPyUnicode_FromString.get_llhelper(space)
+        self.ctx.c_ctx_Unicode_FromString = rffi.cast(rffi.VOIDP, funcptr)
diff --git a/pypy/module/hpy_universal/test/test_basic.py 
b/pypy/module/hpy_universal/test/test_basic.py
--- a/pypy/module/hpy_universal/test/test_basic.py
+++ b/pypy/module/hpy_universal/test/test_basic.py
@@ -119,3 +119,15 @@
             @INIT
         """)
         assert mod.f(41.5) == 42.5
+
+    def test_string(self):
+        mod = self.make_module("""
+            HPy_FUNCTION(f)
+            static HPy f_impl(HPyContext ctx, HPy self, HPy args)
+            {
+                return HPyUnicode_FromString(ctx, "foobar");
+            }
+            @EXPORT f METH_NOARGS
+            @INIT
+        """)
+        assert mod.f() == "foobar"
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to