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
|
#include "parts.h"
#include "util.h"
static PyObject *
number_check(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyNumber_Check(obj));
}
#define BINARYFUNC(funcsuffix, methsuffix) \
static PyObject * \
number_##methsuffix(PyObject *Py_UNUSED(module), PyObject *args) \
{ \
PyObject *o1, *o2; \
\
if (!PyArg_ParseTuple(args, "OO", &o1, &o2)) { \
return NULL; \
} \
\
NULLABLE(o1); \
NULLABLE(o2); \
return PyNumber_##funcsuffix(o1, o2); \
};
BINARYFUNC(Add, add)
BINARYFUNC(Subtract, subtract)
BINARYFUNC(Multiply, multiply)
BINARYFUNC(MatrixMultiply, matrixmultiply)
BINARYFUNC(FloorDivide, floordivide)
BINARYFUNC(TrueDivide, truedivide)
BINARYFUNC(Remainder, remainder)
BINARYFUNC(Divmod, divmod)
#define TERNARYFUNC(funcsuffix, methsuffix) \
static PyObject * \
number_##methsuffix(PyObject *Py_UNUSED(module), PyObject *args) \
{ \
PyObject *o1, *o2, *o3 = Py_None; \
\
if (!PyArg_ParseTuple(args, "OO|O", &o1, &o2, &o3)) { \
return NULL; \
} \
\
NULLABLE(o1); \
NULLABLE(o2); \
return PyNumber_##funcsuffix(o1, o2, o3); \
};
TERNARYFUNC(Power, power)
#define UNARYFUNC(funcsuffix, methsuffix) \
static PyObject * \
number_##methsuffix(PyObject *Py_UNUSED(module), PyObject *obj) \
{ \
NULLABLE(obj); \
return PyNumber_##funcsuffix(obj); \
};
UNARYFUNC(Negative, negative)
UNARYFUNC(Positive, positive)
UNARYFUNC(Absolute, absolute)
UNARYFUNC(Invert, invert)
BINARYFUNC(Lshift, lshift)
BINARYFUNC(Rshift, rshift)
BINARYFUNC(And, and)
BINARYFUNC(Xor, xor)
BINARYFUNC(Or, or)
BINARYFUNC(InPlaceAdd, inplaceadd)
BINARYFUNC(InPlaceSubtract, inplacesubtract)
BINARYFUNC(InPlaceMultiply, inplacemultiply)
BINARYFUNC(InPlaceMatrixMultiply, inplacematrixmultiply)
BINARYFUNC(InPlaceFloorDivide, inplacefloordivide)
BINARYFUNC(InPlaceTrueDivide, inplacetruedivide)
BINARYFUNC(InPlaceRemainder, inplaceremainder)
TERNARYFUNC(InPlacePower, inplacepower)
BINARYFUNC(InPlaceLshift, inplacelshift)
BINARYFUNC(InPlaceRshift, inplacershift)
BINARYFUNC(InPlaceAnd, inplaceand)
BINARYFUNC(InPlaceXor, inplacexor)
BINARYFUNC(InPlaceOr, inplaceor)
UNARYFUNC(Long, long)
UNARYFUNC(Float, float)
UNARYFUNC(Index, index)
static PyObject *
number_tobase(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *n;
int base;
if (!PyArg_ParseTuple(args, "Oi", &n, &base)) {
return NULL;
}
NULLABLE(n);
return PyNumber_ToBase(n, base);
}
static PyObject *
number_asssizet(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *o, *exc;
Py_ssize_t ret;
if (!PyArg_ParseTuple(args, "OO", &o, &exc)) {
return NULL;
}
NULLABLE(o);
NULLABLE(exc);
ret = PyNumber_AsSsize_t(o, exc);
if (ret == (Py_ssize_t)(-1) && PyErr_Occurred()) {
return NULL;
}
return PyLong_FromSsize_t(ret);
}
static PyMethodDef test_methods[] = {
{"number_check", number_check, METH_O},
{"number_add", number_add, METH_VARARGS},
{"number_subtract", number_subtract, METH_VARARGS},
{"number_multiply", number_multiply, METH_VARARGS},
{"number_matrixmultiply", number_matrixmultiply, METH_VARARGS},
{"number_floordivide", number_floordivide, METH_VARARGS},
{"number_truedivide", number_truedivide, METH_VARARGS},
{"number_remainder", number_remainder, METH_VARARGS},
{"number_divmod", number_divmod, METH_VARARGS},
{"number_power", number_power, METH_VARARGS},
{"number_negative", number_negative, METH_O},
{"number_positive", number_positive, METH_O},
{"number_absolute", number_absolute, METH_O},
{"number_invert", number_invert, METH_O},
{"number_lshift", number_lshift, METH_VARARGS},
{"number_rshift", number_rshift, METH_VARARGS},
{"number_and", number_and, METH_VARARGS},
{"number_xor", number_xor, METH_VARARGS},
{"number_or", number_or, METH_VARARGS},
{"number_inplaceadd", number_inplaceadd, METH_VARARGS},
{"number_inplacesubtract", number_inplacesubtract, METH_VARARGS},
{"number_inplacemultiply", number_inplacemultiply, METH_VARARGS},
{"number_inplacematrixmultiply", number_inplacematrixmultiply, METH_VARARGS},
{"number_inplacefloordivide", number_inplacefloordivide, METH_VARARGS},
{"number_inplacetruedivide", number_inplacetruedivide, METH_VARARGS},
{"number_inplaceremainder", number_inplaceremainder, METH_VARARGS},
{"number_inplacepower", number_inplacepower, METH_VARARGS},
{"number_inplacelshift", number_inplacelshift, METH_VARARGS},
{"number_inplacershift", number_inplacershift, METH_VARARGS},
{"number_inplaceand", number_inplaceand, METH_VARARGS},
{"number_inplacexor", number_inplacexor, METH_VARARGS},
{"number_inplaceor", number_inplaceor, METH_VARARGS},
{"number_long", number_long, METH_O},
{"number_float", number_float, METH_O},
{"number_index", number_index, METH_O},
{"number_tobase", number_tobase, METH_VARARGS},
{"number_asssizet", number_asssizet, METH_VARARGS},
{NULL},
};
int
_PyTestCapi_Init_Numbers(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}
return 0;
}
|