Author: Armin Rigo <[email protected]> Branch: hpy Changeset: r98111:635f3981943c Date: 2019-11-18 12:53 +0100 http://bitbucket.org/pypy/pypy/changeset/635f3981943c/
Log: update pyhandle/hpy 56a54e1 diff --git a/pypy/module/hpy_universal/_vendored/test/conftest.py b/pypy/module/hpy_universal/_vendored/test/conftest.py new file mode 100644 --- /dev/null +++ b/pypy/module/hpy_universal/_vendored/test/conftest.py @@ -0,0 +1,26 @@ +import pytest +from .support import ExtensionCompiler + +def pytest_addoption(parser): + parser.addoption( + "--correct", action="store_true", + help="Test against headers installed through hpy_devel" + ) + [email protected](scope='session') +def hpy_include_dir(request): + if request.config.getoption('--correct'): + from hpy_devel import get_include + return get_include() + else: + import os + THIS_DIR = os.path.dirname(__file__) + return os.path.join(THIS_DIR, '../hpy-api/hpy_devel/include') + [email protected](params=['cpython', 'universal']) +def abimode(request): + return request.param + [email protected] +def compiler(tmpdir, abimode, hpy_include_dir): + return ExtensionCompiler(tmpdir, abimode, hpy_include_dir) diff --git a/pypy/module/hpy_universal/_vendored/test/support.py b/pypy/module/hpy_universal/_vendored/test/support.py --- a/pypy/module/hpy_universal/_vendored/test/support.py +++ b/pypy/module/hpy_universal/_vendored/test/support.py @@ -78,7 +78,8 @@ # ext = get_extension(str(filename), name, include_dirs=[self.include_dir], - extra_compile_args=['-Wfatal-errors']) + extra_compile_args=['-Wfatal-errors', '-g', '-Og'], + extra_link_args=['-g']) so_filename = c_compile(str(self.tmpdir), ext, compiler_verbose=False, universal_mode=self.universal_mode) return so_filename @@ -196,13 +197,15 @@ objects = compiler.compile(ext.sources, output_dir=tmpdir, macros=[('HPY_UNIVERSAL_ABI', None)], - include_dirs=include_dirs) + include_dirs=include_dirs, + extra_preargs=ext.extra_compile_args) filename = ext.name + '.hpy.so' compiler.link(compiler.SHARED_LIBRARY, objects, filename, - tmpdir + tmpdir, + extra_preargs=ext.extra_link_args, # export_symbols=... ) return os.path.join(tmpdir, filename) diff --git a/pypy/module/hpy_universal/_vendored/test/test_basic.py b/pypy/module/hpy_universal/_vendored/test/test_basic.py new file mode 100644 --- /dev/null +++ b/pypy/module/hpy_universal/_vendored/test/test_basic.py @@ -0,0 +1,160 @@ +""" +NOTE: this tests are also meant to be run as PyPy "applevel" tests. + +This means that global imports will NOT be visible inside the test +functions. In particular, you have to "import pytest" inside the test in order +to be able to use e.g. pytest.raises (which on PyPy will be implemented by a +"fake pytest module") +""" +from .support import HPyTest + + +class TestBasic(HPyTest): + + def test_empty_module(self): + import sys + mod = self.make_module(""" + @INIT + """) + assert type(mod) is type(sys) + + def test_different_name(self): + mod = self.make_module(""" + @INIT + """, name="foo") + assert mod.__name__ == "foo" + + def test_noop_function(self): + mod = self.make_module(""" + HPy_METH_NOARGS(f) + static HPy f_impl(HPyContext ctx, HPy self) + { + return HPy_Dup(ctx, ctx->h_None); + } + @EXPORT f METH_NOARGS + @INIT + """) + assert mod.f() is None + + def test_self_is_module(self): + mod = self.make_module(""" + HPy_METH_NOARGS(f) + static HPy f_impl(HPyContext ctx, HPy self) + { + return HPy_Dup(ctx, self); + } + @EXPORT f METH_NOARGS + @INIT + """) + assert mod.f() is mod + + def test_identity_function(self): + mod = self.make_module(""" + HPy_METH_O(f) + static HPy f_impl(HPyContext ctx, HPy self, HPy arg) + { + return HPy_Dup(ctx, arg); + } + @EXPORT f METH_O + @INIT + """) + x = object() + assert mod.f(x) is x + + def test_long_aslong(self): + mod = self.make_module(""" + HPy_METH_O(f) + static HPy f_impl(HPyContext ctx, HPy self, HPy arg) + { + long a = HPyLong_AsLong(ctx, arg); + return HPyLong_FromLong(ctx, a * 2); + } + @EXPORT f METH_O + @INIT + """) + assert mod.f(45) == 90 + + def test_wrong_number_of_arguments(self): + import pytest + mod = self.make_module(""" + HPy_METH_NOARGS(f_noargs) + static HPy f_noargs_impl(HPyContext ctx, HPy self) + { + return HPy_Dup(ctx, ctx->h_None); + } + HPy_METH_O(f_o) + static HPy f_o_impl(HPyContext ctx, HPy self, HPy arg) + { + return HPy_Dup(ctx, ctx->h_None); + } + @EXPORT f_noargs METH_NOARGS + @EXPORT f_o METH_O + @INIT + """) + with pytest.raises(TypeError): + mod.f_noargs(1) + with pytest.raises(TypeError): + mod.f_o() + with pytest.raises(TypeError): + mod.f_o(1, 2) + + def test_many_int_arguments(self): + mod = self.make_module(""" + HPy_METH_VARARGS(f) + static HPy f_impl(HPyContext ctx, HPy self, + HPy *args, HPy_ssize_t nargs) + { + long a, b, c, d, e; + if (!HPyArg_Parse(ctx, args, nargs, "lllll", + &a, &b, &c, &d, &e)) + return HPy_NULL; + return HPyLong_FromLong(ctx, + 10000*a + 1000*b + 100*c + 10*d + e); + } + @EXPORT f METH_VARARGS + @INIT + """) + assert mod.f(4, 5, 6, 7, 8) == 45678 + + def test_close(self): + mod = self.make_module(""" + HPy_METH_O(f) + static HPy f_impl(HPyContext ctx, HPy self, HPy arg) + { + HPy one = HPyLong_FromLong(ctx, 1); + if (HPy_IsNull(one)) + return HPy_NULL; + HPy res = HPyNumber_Add(ctx, arg, one); + HPy_Close(ctx, one); + return res; + } + @EXPORT f METH_O + @INIT + """) + assert mod.f(41.5) == 42.5 + + def test_string(self): + mod = self.make_module(""" + HPy_METH_NOARGS(f) + static HPy f_impl(HPyContext ctx, HPy self) + { + return HPyUnicode_FromString(ctx, "foobar"); + } + @EXPORT f METH_NOARGS + @INIT + """) + assert mod.f() == "foobar" + + def test_bool(self): + mod = self.make_module(""" + HPy_METH_O(f) + static HPy f_impl(HPyContext ctx, HPy self, HPy arg) + { + int cond = HPyLong_AsLong(ctx, arg) > 5; + return HPy_Dup(ctx, cond ? ctx->h_True : ctx->h_False); + } + @EXPORT f METH_O + @INIT + """) + assert mod.f(4) is False + assert mod.f(6) is True diff --git a/pypy/module/hpy_universal/_vendored/test/test_cpy_compat.py b/pypy/module/hpy_universal/_vendored/test/test_cpy_compat.py new file mode 100644 --- /dev/null +++ b/pypy/module/hpy_universal/_vendored/test/test_cpy_compat.py @@ -0,0 +1,105 @@ +from .support import HPyTest + + +class TestCPythonCompatibility(HPyTest): + + def test_frompyobject(self): + mod = self.make_module(""" + #include <Python.h> + HPy_METH_NOARGS(f) + static HPy f_impl(HPyContext ctx, HPy self) + { + PyObject *o = PyList_New(0); + + Py_ssize_t initial_refcount = o->ob_refcnt; + HPy h = HPy_FromPyObject(ctx, o); + Py_ssize_t final_refcount = o->ob_refcnt; + + PyList_Append(o, PyLong_FromSsize_t(final_refcount - + initial_refcount)); + Py_DECREF(o); + return h; + } + @EXPORT f METH_NOARGS + @INIT + """) + x = mod.f() + assert x == [+1] + + def test_hpy_close(self): + mod = self.make_module(""" + #include <Python.h> + HPy_METH_NOARGS(f) + static HPy f_impl(HPyContext ctx, HPy self) + { + PyObject *o = PyList_New(0); + + HPy h = HPy_FromPyObject(ctx, o); + Py_ssize_t initial_refcount = o->ob_refcnt; + HPy_Close(ctx, h); + Py_ssize_t final_refcount = o->ob_refcnt; + + Py_DECREF(o); + return HPyLong_FromLong(ctx, (long)(final_refcount - + initial_refcount)); + } + @EXPORT f METH_NOARGS + @INIT + """) + assert mod.f() == -1 + + def test_hpy_dup(self): + mod = self.make_module(""" + #include <Python.h> + HPy_METH_NOARGS(f) + static HPy f_impl(HPyContext ctx, HPy self) + { + PyObject *o = PyList_New(0); + + HPy h = HPy_FromPyObject(ctx, o); + Py_ssize_t initial_refcount = o->ob_refcnt; + HPy h2 = HPy_Dup(ctx, h); + Py_ssize_t final_refcount = o->ob_refcnt; + + HPy_Close(ctx, h); + HPy_Close(ctx, h2); + Py_DECREF(o); + return HPyLong_FromLong(ctx, (long)(final_refcount - + initial_refcount)); + } + @EXPORT f METH_NOARGS + @INIT + """) + assert mod.f() == +1 + + def test_many_handles(self): + mod = self.make_module(""" + #include <Python.h> + #define NUM_HANDLES 10000 + + HPy_METH_NOARGS(f) + static HPy f_impl(HPyContext ctx, HPy self) + { + PyObject *o = PyList_New(0); + + Py_ssize_t result = -42; + HPy handles[NUM_HANDLES]; + int i; + Py_ssize_t initial_refcount = o->ob_refcnt; + for (i = 0; i < NUM_HANDLES; i++) + handles[i] = HPy_FromPyObject(ctx, o); + for (i = 0; i < NUM_HANDLES; i++) + if (HPy_IsNull(handles[i])) + goto error; + for (i = 0; i < NUM_HANDLES; i++) + HPy_Close(ctx, handles[i]); + Py_ssize_t final_refcount = o->ob_refcnt; + result = final_refcount - initial_refcount; + + error: + return HPyLong_FromLong(ctx, (long)result); + } + @EXPORT f METH_NOARGS + @INIT + """) + assert mod.f() == 0 diff --git a/pypy/module/hpy_universal/_vendored/test/test_importing.py b/pypy/module/hpy_universal/_vendored/test/test_importing.py new file mode 100644 --- /dev/null +++ b/pypy/module/hpy_universal/_vendored/test/test_importing.py @@ -0,0 +1,30 @@ +import pytest +from .support import HPyTest + +# this function should probably goes somewhere into hpy_universal and/or and +# hpy package and/or an import hook, or whatever. I do not want to think about +# this now. +def import_module_properly(mod): + raise NotImplementedError("fix me eventually") + +# this was moved from support.py, where it did not belong +## class HPyLoader(ExtensionFileLoader): +## def create_module(self, spec): +## import hpy_universal +## return hpy_universal.load_from_spec(spec) + + +class TestImporting(HPyTest): + + @pytest.mark.xfail + def test_importing_attributes(self): + import sys + modname = 'mytest' + so_filename = self.compile_module(""" + @INIT + """, name=modname) + mod = import_module_properly(so_filename, modname) + assert mod in sys.modules + assert mod.__loader__.name == 'mytest' + assert mod.__spec__.loader is mod.__loader__ + assert mod.__file__ diff --git a/pypy/module/hpy_universal/_vendored/test/test_support.py b/pypy/module/hpy_universal/_vendored/test/test_support.py new file mode 100644 --- /dev/null +++ b/pypy/module/hpy_universal/_vendored/test/test_support.py @@ -0,0 +1,16 @@ +from . import support + + +def test_expand_template(): + expanded = support.expand_template(""" + @EXPORT test_f METH_O + some more C stuff + @INIT + """, name='mytest') + methods = '{"test_f", test_f, METH_O, NULL},' + init_code = support.INIT_TEMPLATE % {'methods': methods, 'name': 'mytest'} + assert expanded.rstrip() == f"""#include <hpy.h> + + some more C stuff +{init_code} +""".rstrip() _______________________________________________ pypy-commit mailing list [email protected] https://mail.python.org/mailman/listinfo/pypy-commit
