Author: Antonio Cuni <[email protected]>
Branch: hpy
Changeset: r98115:84c205b54510
Date: 2019-11-18 17:40 +0100
http://bitbucket.org/pypy/pypy/changeset/84c205b54510/
Log: update to pyhandle/hpy c8ddac3
diff --git a/pypy/module/hpy_universal/_vendored/include/cpython/hpy.h
b/pypy/module/hpy_universal/_vendored/include/cpython/hpy.h
--- a/pypy/module/hpy_universal/_vendored/include/cpython/hpy.h
+++ b/pypy/module/hpy_universal/_vendored/include/cpython/hpy.h
@@ -37,23 +37,30 @@
HPy h_None;
HPy h_True;
HPy h_False;
+ HPy h_ValueError;
} *HPyContext;
/* XXX! should be defined only once, not once for every .c! */
-static struct _HPyContext_s _global_ctx = {
- .h_None = _py2h(Py_None),
- .h_True = _py2h(Py_True),
- .h_False = _py2h(Py_False),
-};
+static struct _HPyContext_s _global_ctx;
+
+#define HPy_NULL ((HPy){NULL})
+#define HPy_IsNull(x) ((x)._o == NULL)
HPyAPI_FUNC(HPyContext)
_HPyGetContext(void) {
- return &_global_ctx;
+ HPyContext ctx = &_global_ctx;
+ if (HPy_IsNull(ctx->h_None)) {
+ // XXX: we need to find a better way to check whether the ctx is
+ // initialized or not
+ ctx->h_None = _py2h(Py_None);
+ ctx->h_True = _py2h(Py_True);
+ ctx->h_False = _py2h(Py_False);
+ ctx->h_ValueError = _py2h(PyExc_ValueError);
+ }
+ return ctx;
}
-#define HPy_NULL ((HPy){NULL})
-#define HPy_IsNull(x) ((x)._o == NULL)
HPyAPI_FUNC(HPy)
HPyNone_Get(HPyContext ctx)
@@ -197,5 +204,11 @@
return result;
}
+HPyAPI_FUNC(void)
+HPyErr_SetString(HPyContext ctx, HPy type, const char *message)
+{
+ PyErr_SetString(_h2py(type), message);
+}
+
#endif /* !HPy_CPYTHON_H */
diff --git
a/pypy/module/hpy_universal/_vendored/include/universal/autogen_ctx.h
b/pypy/module/hpy_universal/_vendored/include/universal/autogen_ctx.h
--- a/pypy/module/hpy_universal/_vendored/include/universal/autogen_ctx.h
+++ b/pypy/module/hpy_universal/_vendored/include/universal/autogen_ctx.h
@@ -13,6 +13,7 @@
HPy h_None;
HPy h_True;
HPy h_False;
+ HPy h_ValueError;
HPy (*ctx_Module_Create)(HPyContext ctx, HPyModuleDef *def);
HPy (*ctx_Dup)(HPyContext ctx, HPy h);
void (*ctx_Close)(HPyContext ctx, HPy h);
@@ -21,6 +22,7 @@
int (*ctx_Arg_Parse)(HPyContext ctx, HPy *args, HPy_ssize_t nargs, const
char *fmt, va_list _vl);
HPy (*ctx_Number_Add)(HPyContext ctx, HPy x, HPy y);
HPy (*ctx_Unicode_FromString)(HPyContext ctx, const char *utf8);
+ void (*ctx_Err_SetString)(HPyContext ctx, HPy type, const char *message);
HPy (*ctx_FromPyObject)(HPyContext ctx, struct _object *obj);
struct _object *(*ctx_AsPyObject)(HPyContext ctx, HPy h);
struct _object *(*ctx_CallRealFunctionFromTrampoline)(HPyContext ctx,
struct _object *self, struct _object *args, void *func, int ml_flags);
diff --git
a/pypy/module/hpy_universal/_vendored/include/universal/autogen_func.h
b/pypy/module/hpy_universal/_vendored/include/universal/autogen_func.h
--- a/pypy/module/hpy_universal/_vendored/include/universal/autogen_func.h
+++ b/pypy/module/hpy_universal/_vendored/include/universal/autogen_func.h
@@ -40,6 +40,10 @@
return ctx->ctx_Unicode_FromString ( ctx, utf8 );
}
+static inline void HPyErr_SetString(HPyContext ctx, HPy type, const char
*message) {
+ ctx->ctx_Err_SetString ( ctx, type, message );
+}
+
static inline HPy HPy_FromPyObject(HPyContext ctx, struct _object *obj) {
return ctx->ctx_FromPyObject ( ctx, obj );
}
diff --git a/pypy/module/hpy_universal/_vendored/test/test_basic.py
b/pypy/module/hpy_universal/_vendored/test/test_basic.py
--- a/pypy/module/hpy_universal/_vendored/test/test_basic.py
+++ b/pypy/module/hpy_universal/_vendored/test/test_basic.py
@@ -158,3 +158,26 @@
""")
assert mod.f(4) is False
assert mod.f(6) is True
+
+ def test_exception(self):
+ import pytest
+ mod = self.make_module("""
+ HPy_METH_O(f)
+ static HPy f_impl(HPyContext ctx, HPy self, HPy arg)
+ {
+ long x = HPyLong_AsLong(ctx, arg);
+ if (x < 5) {
+ return HPyLong_FromLong(ctx, -x);
+ }
+ else {
+ HPyErr_SetString(ctx, ctx->h_ValueError, "hello world");
+ return HPy_NULL;
+ }
+ }
+ @EXPORT f METH_O
+ @INIT
+ """)
+ assert mod.f(-10) == 10
+ with pytest.raises(ValueError) as exc:
+ mod.f(20)
+ assert str(exc.value) == 'hello world'
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit