| 1 |
from .support import HPyTest, make_hpy_abi_fixture |
| 2 |
|
| 3 |
class TestCPythonCompatibility(HPyTest): |
| 4 |
|
| 5 |
hpy_abi = make_hpy_abi_fixture('with hybrid', class_fixture=True) |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
def test_abi(self): |
| 17 |
mod = self.make_module(""" |
| 18 |
#include <Python.h> |
| 19 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 20 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 21 |
{ |
| 22 |
return HPyUnicode_FromString(ctx, HPY_ABI); |
| 23 |
} |
| 24 |
@EXPORT(f) |
| 25 |
@INIT |
| 26 |
""") |
| 27 |
hpy_abi = mod.f() |
| 28 |
expected = self.compiler.hpy_abi |
| 29 |
if expected in ('hybrid+debug', 'hybrid+trace'): |
| 30 |
expected = 'hybrid' |
| 31 |
assert hpy_abi == expected |
| 32 |
|
| 33 |
def test_frompyobject(self): |
| 34 |
mod = self.make_module(""" |
| 35 |
#include <Python.h> |
| 36 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 37 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 38 |
{ |
| 39 |
PyObject *o = PyList_New(0); |
| 40 |
Py_ssize_t initial_refcount = o->ob_refcnt; |
| 41 |
HPy h = HPy_FromPyObject(ctx, o); |
| 42 |
Py_ssize_t final_refcount = o->ob_refcnt; |
| 43 |
|
| 44 |
PyList_Append(o, PyLong_FromLong(1234)); |
| 45 |
PyList_Append(o, PyLong_FromSsize_t(final_refcount - |
| 46 |
initial_refcount)); |
| 47 |
Py_DECREF(o); |
| 48 |
return h; |
| 49 |
} |
| 50 |
@EXPORT(f) |
| 51 |
@INIT |
| 52 |
""") |
| 53 |
x = mod.f() |
| 54 |
assert x[0] == 1234 |
| 55 |
assert len(x) == 2 |
| 56 |
if self.supports_refcounts(): |
| 57 |
assert x == [1234, +1] |
| 58 |
|
| 59 |
def test_frompyobject_null(self): |
| 60 |
mod = self.make_module(""" |
| 61 |
#include <Python.h> |
| 62 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 63 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 64 |
{ |
| 65 |
HPy h = HPy_FromPyObject(ctx, NULL); |
| 66 |
if (HPy_IsNull(h)) { |
| 67 |
return HPy_Dup(ctx, ctx->h_True); |
| 68 |
} |
| 69 |
else { |
| 70 |
return HPy_Dup(ctx, ctx->h_False); |
| 71 |
} |
| 72 |
} |
| 73 |
@EXPORT(f) |
| 74 |
@INIT |
| 75 |
""") |
| 76 |
assert mod.f() |
| 77 |
|
| 78 |
def test_aspyobject(self): |
| 79 |
mod = self.make_module(""" |
| 80 |
#include <Python.h> |
| 81 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 82 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 83 |
{ |
| 84 |
PyObject *o = HPy_AsPyObject(ctx, arg); |
| 85 |
long val = PyLong_AsLong(o); |
| 86 |
Py_DecRef(o); |
| 87 |
return HPyLong_FromLong(ctx, val*2); |
| 88 |
} |
| 89 |
@EXPORT(f) |
| 90 |
@INIT |
| 91 |
""") |
| 92 |
assert mod.f(21) == 42 |
| 93 |
|
| 94 |
def test_aspyobject_null(self): |
| 95 |
mod = self.make_module(""" |
| 96 |
#include <Python.h> |
| 97 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 98 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 99 |
{ |
| 100 |
PyObject *o = HPy_AsPyObject(ctx, HPy_NULL); |
| 101 |
if (o == NULL) { |
| 102 |
return HPy_Dup(ctx, ctx->h_True); |
| 103 |
} |
| 104 |
else { |
| 105 |
return HPy_Dup(ctx, ctx->h_False); |
| 106 |
} |
| 107 |
} |
| 108 |
@EXPORT(f) |
| 109 |
@INIT |
| 110 |
""") |
| 111 |
assert mod.f() |
| 112 |
|
| 113 |
def test_aspyobject_custom_class(self): |
| 114 |
mod = self.make_module(""" |
| 115 |
#include <Python.h> |
| 116 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 117 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 118 |
{ |
| 119 |
PyObject *o = HPy_AsPyObject(ctx, arg); |
| 120 |
PyObject *o_res = PyObject_CallMethod(o, "foo", ""); |
| 121 |
HPy h_res = HPy_FromPyObject(ctx, o_res); |
| 122 |
Py_DecRef(o); |
| 123 |
Py_DecRef(o_res); |
| 124 |
return h_res; |
| 125 |
} |
| 126 |
@EXPORT(f) |
| 127 |
@INIT |
| 128 |
""") |
| 129 |
class MyClass: |
| 130 |
def foo(self): |
| 131 |
return 1234 |
| 132 |
obj = MyClass() |
| 133 |
assert mod.f(obj) == 1234 |
| 134 |
|
| 135 |
def test_hpy_close(self): |
| 136 |
mod = self.make_module(""" |
| 137 |
#include <Python.h> |
| 138 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 139 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 140 |
{ |
| 141 |
PyObject *o = PyList_New(0); |
| 142 |
|
| 143 |
HPy h = HPy_FromPyObject(ctx, o); |
| 144 |
Py_ssize_t initial_refcount = o->ob_refcnt; |
| 145 |
HPy_Close(ctx, h); |
| 146 |
Py_ssize_t final_refcount = o->ob_refcnt; |
| 147 |
|
| 148 |
Py_DECREF(o); |
| 149 |
return HPyLong_FromLong(ctx, (long)(final_refcount - |
| 150 |
initial_refcount)); |
| 151 |
} |
| 152 |
@EXPORT(f) |
| 153 |
@INIT |
| 154 |
""") |
| 155 |
x = mod.f() |
| 156 |
if self.supports_refcounts(): |
| 157 |
assert x == -1 |
| 158 |
|
| 159 |
def test_hpy_dup(self): |
| 160 |
mod = self.make_module(""" |
| 161 |
#include <Python.h> |
| 162 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 163 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 164 |
{ |
| 165 |
PyObject *o = PyList_New(0); |
| 166 |
|
| 167 |
HPy h = HPy_FromPyObject(ctx, o); |
| 168 |
Py_ssize_t initial_refcount = o->ob_refcnt; |
| 169 |
HPy h2 = HPy_Dup(ctx, h); |
| 170 |
Py_ssize_t final_refcount = o->ob_refcnt; |
| 171 |
|
| 172 |
HPy_Close(ctx, h); |
| 173 |
HPy_Close(ctx, h2); |
| 174 |
Py_DECREF(o); |
| 175 |
return HPyLong_FromLong(ctx, (long)(final_refcount - |
| 176 |
initial_refcount)); |
| 177 |
} |
| 178 |
@EXPORT(f) |
| 179 |
@INIT |
| 180 |
""") |
| 181 |
x = mod.f() |
| 182 |
if self.supports_refcounts(): |
| 183 |
assert x == +1 |
| 184 |
|
| 185 |
def test_many_handles(self): |
| 186 |
mod = self.make_module(""" |
| 187 |
#include <Python.h> |
| 188 |
#define NUM_HANDLES 10000 |
| 189 |
|
| 190 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 191 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 192 |
{ |
| 193 |
PyObject *o = PyList_New(0); |
| 194 |
Py_ssize_t final_refcount; |
| 195 |
|
| 196 |
Py_ssize_t result = -42; |
| 197 |
HPy handles[NUM_HANDLES]; |
| 198 |
int i; |
| 199 |
Py_ssize_t initial_refcount = o->ob_refcnt; |
| 200 |
for (i = 0; i < NUM_HANDLES; i++) |
| 201 |
handles[i] = HPy_FromPyObject(ctx, o); |
| 202 |
for (i = 0; i < NUM_HANDLES; i++) |
| 203 |
if (HPy_IsNull(handles[i])) |
| 204 |
goto error; |
| 205 |
for (i = 0; i < NUM_HANDLES; i++) |
| 206 |
HPy_Close(ctx, handles[i]); |
| 207 |
final_refcount = o->ob_refcnt; |
| 208 |
result = final_refcount - initial_refcount; |
| 209 |
|
| 210 |
error: |
| 211 |
return HPyLong_FromLong(ctx, (long)result); |
| 212 |
} |
| 213 |
@EXPORT(f) |
| 214 |
@INIT |
| 215 |
""") |
| 216 |
assert mod.f() == 0 |