1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
from .support import HPyTest
class TestNumber(HPyTest):
def test_bool_from_bool_and_long(self):
import pytest
mod = self.make_module("""
HPyDef_METH(from_bool, "from_bool", HPyFunc_O)
static HPy from_bool_impl(HPyContext *ctx, HPy self, HPy arg)
{
int32_t x = HPyLong_AsInt32_t(ctx, arg);
if (x == -1 && HPyErr_Occurred(ctx))
return HPy_NULL;
if (x != 0 && x != 1) {
HPyErr_SetString(ctx, ctx->h_ValueError,
"value must be 0 or 1");
return HPy_NULL;
}
return HPyBool_FromBool(ctx, (x ? true : false));
}
HPyDef_METH(from_long, "from_long", HPyFunc_O)
static HPy from_long_impl(HPyContext *ctx, HPy self, HPy arg)
{
long x = HPyLong_AsLong(ctx, arg);
if (x == -1 && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyBool_FromLong(ctx, x);
}
@EXPORT(from_bool)
@EXPORT(from_long)
@INIT
""")
assert mod.from_bool(0) is False
assert mod.from_bool(1) is True
with pytest.raises(ValueError):
mod.from_bool(2)
assert mod.from_long(0) is False
assert mod.from_long(42) is True
def test_unary(self):
import pytest
import operator
for c_name, op in [
('Negative', operator.neg),
('Positive', operator.pos),
('Absolute', abs),
('Invert', operator.invert),
('Index', operator.index),
('Long', int),
('Float', float),
]:
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
return HPy_%s(ctx, arg);
}
@EXPORT(f)
@INIT
""" % (c_name,), name='number_'+c_name)
assert mod.f(-5) == op(-5)
assert mod.f(6) == op(6)
try:
res = op(4.75)
except Exception as e:
with pytest.raises(e.__class__):
mod.f(4.75)
else:
assert mod.f(4.75) == res
def test_binary(self):
import operator
for c_name, op in [
('Add', operator.add),
('Subtract', operator.sub),
('Multiply', operator.mul),
('FloorDivide', operator.floordiv),
('TrueDivide', operator.truediv),
('Remainder', operator.mod),
('Divmod', divmod),
('Lshift', operator.lshift),
('Rshift', operator.rshift),
('And', operator.and_),
('Xor', operator.xor),
('Or', operator.or_),
]:
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{
HPy a, b;
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b))
return HPy_NULL;
return HPy_%s(ctx, a, b);
}
@EXPORT(f)
@INIT
""" % (c_name,), name='number_'+c_name)
assert mod.f(5, 4) == op(5, 4)
assert mod.f(6, 3) == op(6, 3)
def test_power(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{
HPy a, b, c;
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OOO", &a, &b, &c))
return HPy_NULL;
return HPy_Power(ctx, a, b, c);
}
@EXPORT(f)
@INIT
""")
assert mod.f(4, 5, None) == 4 ** 5
assert mod.f(4, 5, 7) == pow(4, 5, 7)
def test_matmul(self):
class Mat:
def __matmul__(self, other):
return ('matmul', self, other)
m1 = Mat()
m2 = Mat()
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{
HPy a, b;
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b))
return HPy_NULL;
return HPy_MatrixMultiply(ctx, a, b);
}
@EXPORT(f)
@INIT
""")
assert mod.f(m1, m2) == m1.__matmul__(m2)
def test_inplace_binary(self):
import operator
for c_name, py_name in [
('Add', '__iadd__'),
('Subtract', '__isub__'),
('Multiply', '__imul__'),
('FloorDivide', '__ifloordiv__'),
('TrueDivide', '__itruediv__'),
('Remainder', '__imod__'),
('Lshift', '__ilshift__'),
('Rshift', '__irshift__'),
('And', '__iand__'),
('Xor', '__ixor__'),
('Or', '__ior__'),
]:
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{
HPy a, b;
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b))
return HPy_NULL;
return HPy_InPlace%s(ctx, a, b);
}
@EXPORT(f)
@INIT
""" % (c_name,), name='number_'+c_name)
class A:
def mymethod(self, b):
return (py_name, b)
setattr(A, py_name, A.mymethod)
assert mod.f(A(), 12.34) == A().mymethod(12.34)
def test_inplace_power(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{
HPy a, b, c;
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OOO", &a, &b, &c))
return HPy_NULL;
return HPy_InPlacePower(ctx, a, b, c);
}
@EXPORT(f)
@INIT
""")
class A:
def __ipow__(self, b):
return ('ipow', b)
# the behavior of PyNumber_InPlacePower is weird: if __ipow__ is
# defined, the 3rd arg is always ignored, even if the doc say the
# opposite
assert mod.f(A(), 5, None) == A().__ipow__(5)
assert mod.f(A(), 7, 'hello') == A().__ipow__(7)
assert mod.f(4, 5, 7) == pow(4, 5, 7)
def test_inplace_matmul(self):
class Mat:
def __imatmul__(self, other):
return ('imatmul', self, other)
m1 = Mat()
m2 = Mat()
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{
HPy a, b;
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b))
return HPy_NULL;
return HPy_InPlaceMatrixMultiply(ctx, a, b);
}
@EXPORT(f)
@INIT
""")
assert mod.f(m1, m2) == m1.__imatmul__(m2)
def test_number_check(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
int cond = HPyNumber_Check(ctx, arg);
return HPyLong_FromLong(ctx, cond);
}
@EXPORT(f)
@INIT
""")
assert mod.f("foo") == 0
assert mod.f(42) == 1
assert mod.f(42.1) == 1
|