Author: Antonio Cuni <[email protected]>
Branch: hpy
Changeset: r98116:120fbccc3422
Date: 2019-11-18 18:07 +0100
http://bitbucket.org/pypy/pypy/changeset/120fbccc3422/

Log:    (antocuni, arigo) WIP implementing test_exception

        this mostly works already. However, if you raise an exception which
        is raised inside an llhelper callback, ll2ctypes raises it and
        ctypes prints it to stderr (by invoking sys.excepthook). After the
        printing, it is correctly propagated and thus the test passes.

        Next commits will try to convince ll2ctypes to do the right thing
        and just propagate the exception

diff --git a/pypy/module/hpy_universal/handles.py 
b/pypy/module/hpy_universal/handles.py
--- a/pypy/module/hpy_universal/handles.py
+++ b/pypy/module/hpy_universal/handles.py
@@ -4,6 +4,7 @@
     ('None', lambda space: space.w_None),
     ('False', lambda space: space.w_False),
     ('True', lambda space: space.w_True),
+    ('ValueError', lambda space: space.w_ValueError),
     ]
 
 
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
@@ -7,6 +7,7 @@
 from pypy.interpreter.gateway import unwrap_spec
 from pypy.interpreter.error import raise_import_error
 from pypy.interpreter.module import Module
+from pypy.interpreter.error import OperationError
 
 from pypy.module.hpy_universal import llapi, handles, interp_extfunc
 from pypy.module.hpy_universal.state import State
@@ -80,13 +81,23 @@
 
 @apifunc([llapi.HPyContext, rffi.CCHARP], llapi.HPy, error=0)
 def HPyUnicode_FromString(space, ctx, utf8):
+    w_obj = _maybe_utf8_to_w(space, utf8)
+    return handles.new(space, w_obj)
+
+def _maybe_utf8_to_w(space, utf8):
+    # should this be a method of space?
     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)
+    return space.newtext(s, length)
+
+@apifunc([llapi.HPyContext, llapi.HPy, rffi.CCHARP], lltype.Void, error=None)
+def HPyErr_SetString(space, ctx, h_exc_type, utf8):
+   w_obj = _maybe_utf8_to_w(space, utf8)
+   w_exc_type = handles.deref(space, h_exc_type)
+   raise OperationError(w_exc_type, w_obj)
 
 
 def create_hpy_module(space, name, origin, lib, initfunc):
diff --git a/pypy/module/hpy_universal/llapi.py 
b/pypy/module/hpy_universal/llapi.py
--- a/pypy/module/hpy_universal/llapi.py
+++ b/pypy/module/hpy_universal/llapi.py
@@ -26,6 +26,7 @@
     ('h_None', HPy),
     ('h_True', HPy),
     ('h_False', HPy),
+    ('h_ValueError', HPy),
     ('ctx_Module_Create', rffi.VOIDP),
     ('ctx_Dup', rffi.VOIDP),
     ('ctx_Close', rffi.VOIDP),
@@ -34,6 +35,7 @@
     ('ctx_Arg_Parse', rffi.VOIDP),
     ('ctx_Number_Add', rffi.VOIDP),
     ('ctx_Unicode_FromString', rffi.VOIDP),
+    ('ctx_Err_SetString', rffi.VOIDP),
     ('ctx_FromPyObject', rffi.VOIDP),
     ('ctx_AsPyObject', rffi.VOIDP),
     ('ctx_CallRealFunctionFromTrampoline', rffi.VOIDP),
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
@@ -69,3 +69,6 @@
         #
         funcptr = interp_hpy.HPyUnicode_FromString.get_llhelper(space)
         self.ctx.c_ctx_Unicode_FromString = rffi.cast(rffi.VOIDP, funcptr)
+        #
+        funcptr = interp_hpy.HPyErr_SetString.get_llhelper(space)
+        self.ctx.c_ctx_Err_SetString = rffi.cast(rffi.VOIDP, funcptr)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to