| 1 | #include "parts.h" |
| 2 | #include "util.h" |
| 3 | |
| 4 | |
| 5 | static PyObject * |
| 6 | number_check(PyObject *Py_UNUSED(module), PyObject *obj) |
| 7 | { |
| 8 | NULLABLE(obj); |
| 9 | return PyLong_FromLong(PyNumber_Check(obj)); |
| 10 | } |
| 11 | |
| 12 | #define BINARYFUNC(funcsuffix, methsuffix) \ |
| 13 | static PyObject * \ |
| 14 | number_##methsuffix(PyObject *Py_UNUSED(module), PyObject *args) \ |
| 15 | { \ |
| 16 | PyObject *o1, *o2; \ |
| 17 | \ |
| 18 | if (!PyArg_ParseTuple(args, "OO", &o1, &o2)) { \ |
| 19 | return NULL; \ |
| 20 | } \ |
| 21 | \ |
| 22 | NULLABLE(o1); \ |
| 23 | NULLABLE(o2); \ |
| 24 | return PyNumber_##funcsuffix(o1, o2); \ |
| 25 | }; |
| 26 | |
| 27 | BINARYFUNC(Add, add) |
| 28 | BINARYFUNC(Subtract, subtract) |
| 29 | BINARYFUNC(Multiply, multiply) |
| 30 | BINARYFUNC(MatrixMultiply, matrixmultiply) |
| 31 | BINARYFUNC(FloorDivide, floordivide) |
| 32 | BINARYFUNC(TrueDivide, truedivide) |
| 33 | BINARYFUNC(Remainder, remainder) |
| 34 | BINARYFUNC(Divmod, divmod) |
| 35 | |
| 36 | #define TERNARYFUNC(funcsuffix, methsuffix) \ |
| 37 | static PyObject * \ |
| 38 | number_##methsuffix(PyObject *Py_UNUSED(module), PyObject *args) \ |
| 39 | { \ |
| 40 | PyObject *o1, *o2, *o3 = Py_None; \ |
| 41 | \ |
| 42 | if (!PyArg_ParseTuple(args, "OO|O", &o1, &o2, &o3)) { \ |
| 43 | return NULL; \ |
| 44 | } \ |
| 45 | \ |
| 46 | NULLABLE(o1); \ |
| 47 | NULLABLE(o2); \ |
| 48 | return PyNumber_##funcsuffix(o1, o2, o3); \ |
| 49 | }; |
| 50 | |
| 51 | TERNARYFUNC(Power, power) |
| 52 | |
| 53 | #define UNARYFUNC(funcsuffix, methsuffix) \ |
| 54 | static PyObject * \ |
| 55 | number_##methsuffix(PyObject *Py_UNUSED(module), PyObject *obj) \ |
| 56 | { \ |
| 57 | NULLABLE(obj); \ |
| 58 | return PyNumber_##funcsuffix(obj); \ |
| 59 | }; |
| 60 | |
| 61 | UNARYFUNC(Negative, negative) |
| 62 | UNARYFUNC(Positive, positive) |
| 63 | UNARYFUNC(Absolute, absolute) |
| 64 | UNARYFUNC(Invert, invert) |
| 65 | |
| 66 | BINARYFUNC(Lshift, lshift) |
| 67 | BINARYFUNC(Rshift, rshift) |
| 68 | BINARYFUNC(And, and) |
| 69 | BINARYFUNC(Xor, xor) |
| 70 | BINARYFUNC(Or, or) |
| 71 | |
| 72 | BINARYFUNC(InPlaceAdd, inplaceadd) |
| 73 | BINARYFUNC(InPlaceSubtract, inplacesubtract) |
| 74 | BINARYFUNC(InPlaceMultiply, inplacemultiply) |
| 75 | BINARYFUNC(InPlaceMatrixMultiply, inplacematrixmultiply) |
| 76 | BINARYFUNC(InPlaceFloorDivide, inplacefloordivide) |
| 77 | BINARYFUNC(InPlaceTrueDivide, inplacetruedivide) |
| 78 | BINARYFUNC(InPlaceRemainder, inplaceremainder) |
| 79 | |
| 80 | TERNARYFUNC(InPlacePower, inplacepower) |
| 81 | |
| 82 | BINARYFUNC(InPlaceLshift, inplacelshift) |
| 83 | BINARYFUNC(InPlaceRshift, inplacershift) |
| 84 | BINARYFUNC(InPlaceAnd, inplaceand) |
| 85 | BINARYFUNC(InPlaceXor, inplacexor) |
| 86 | BINARYFUNC(InPlaceOr, inplaceor) |
| 87 | |
| 88 | UNARYFUNC(Long, long) |
| 89 | UNARYFUNC(Float, float) |
| 90 | UNARYFUNC(Index, index) |
| 91 | |
| 92 | static PyObject * |
| 93 | number_tobase(PyObject *Py_UNUSED(module), PyObject *args) |
| 94 | { |
| 95 | PyObject *n; |
| 96 | int base; |
| 97 | |
| 98 | if (!PyArg_ParseTuple(args, "Oi", &n, &base)) { |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | NULLABLE(n); |
| 103 | return PyNumber_ToBase(n, base); |
| 104 | } |
| 105 | |
| 106 | static PyObject * |
| 107 | number_asssizet(PyObject *Py_UNUSED(module), PyObject *args) |
| 108 | { |
| 109 | PyObject *o, *exc; |
| 110 | Py_ssize_t ret; |
| 111 | |
| 112 | if (!PyArg_ParseTuple(args, "OO", &o, &exc)) { |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | NULLABLE(o); |
| 117 | NULLABLE(exc); |
| 118 | ret = PyNumber_AsSsize_t(o, exc); |
| 119 | |
| 120 | if (ret == (Py_ssize_t)(-1) && PyErr_Occurred()) { |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | return PyLong_FromSsize_t(ret); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static PyMethodDef test_methods[] = { |
| 129 | {"number_check", number_check, METH_O}, |
| 130 | {"number_add", number_add, METH_VARARGS}, |
| 131 | {"number_subtract", number_subtract, METH_VARARGS}, |
| 132 | {"number_multiply", number_multiply, METH_VARARGS}, |
| 133 | {"number_matrixmultiply", number_matrixmultiply, METH_VARARGS}, |
| 134 | {"number_floordivide", number_floordivide, METH_VARARGS}, |
| 135 | {"number_truedivide", number_truedivide, METH_VARARGS}, |
| 136 | {"number_remainder", number_remainder, METH_VARARGS}, |
| 137 | {"number_divmod", number_divmod, METH_VARARGS}, |
| 138 | {"number_power", number_power, METH_VARARGS}, |
| 139 | {"number_negative", number_negative, METH_O}, |
| 140 | {"number_positive", number_positive, METH_O}, |
| 141 | {"number_absolute", number_absolute, METH_O}, |
| 142 | {"number_invert", number_invert, METH_O}, |
| 143 | {"number_lshift", number_lshift, METH_VARARGS}, |
| 144 | {"number_rshift", number_rshift, METH_VARARGS}, |
| 145 | {"number_and", number_and, METH_VARARGS}, |
| 146 | {"number_xor", number_xor, METH_VARARGS}, |
| 147 | {"number_or", number_or, METH_VARARGS}, |
| 148 | {"number_inplaceadd", number_inplaceadd, METH_VARARGS}, |
| 149 | {"number_inplacesubtract", number_inplacesubtract, METH_VARARGS}, |
| 150 | {"number_inplacemultiply", number_inplacemultiply, METH_VARARGS}, |
| 151 | {"number_inplacematrixmultiply", number_inplacematrixmultiply, METH_VARARGS}, |
| 152 | {"number_inplacefloordivide", number_inplacefloordivide, METH_VARARGS}, |
| 153 | {"number_inplacetruedivide", number_inplacetruedivide, METH_VARARGS}, |
| 154 | {"number_inplaceremainder", number_inplaceremainder, METH_VARARGS}, |
| 155 | {"number_inplacepower", number_inplacepower, METH_VARARGS}, |
| 156 | {"number_inplacelshift", number_inplacelshift, METH_VARARGS}, |
| 157 | {"number_inplacershift", number_inplacershift, METH_VARARGS}, |
| 158 | {"number_inplaceand", number_inplaceand, METH_VARARGS}, |
| 159 | {"number_inplacexor", number_inplacexor, METH_VARARGS}, |
| 160 | {"number_inplaceor", number_inplaceor, METH_VARARGS}, |
| 161 | {"number_long", number_long, METH_O}, |
| 162 | {"number_float", number_float, METH_O}, |
| 163 | {"number_index", number_index, METH_O}, |
| 164 | {"number_tobase", number_tobase, METH_VARARGS}, |
| 165 | {"number_asssizet", number_asssizet, METH_VARARGS}, |
| 166 | {NULL}, |
| 167 | }; |
| 168 | |
| 169 | int |
| 170 | _PyTestCapi_Init_Numbers(PyObject *mod) |
| 171 | { |
| 172 | if (PyModule_AddFunctions(mod, test_methods) < 0) { |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |