Commit 3b67736b authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add and test more slots

parent 57450dd2
......@@ -507,41 +507,6 @@ PyObject* slot_tp_call(PyObject* self, PyObject* args, PyObject* kwds) noexcept
}
}
static int slot_nb_nonzero(PyObject* self) noexcept {
PyObject* func, *args;
static PyObject* nonzero_str, *len_str;
int result = -1;
int using_len = 0;
func = lookup_maybe(self, "__nonzero__", &nonzero_str);
if (func == NULL) {
if (PyErr_Occurred())
return -1;
func = lookup_maybe(self, "__len__", &len_str);
if (func == NULL)
return PyErr_Occurred() ? -1 : 1;
using_len = 1;
}
args = PyTuple_New(0);
if (args != NULL) {
PyObject* temp = PyObject_Call(func, args, NULL);
Py_DECREF(args);
if (temp != NULL) {
if (PyInt_CheckExact(temp) || PyBool_Check(temp))
result = PyObject_IsTrue(temp);
else {
PyErr_Format(PyExc_TypeError, "%s should return "
"bool or int, returned %s",
(using_len ? "__len__" : "__nonzero__"), temp->cls->tp_name);
result = -1;
}
Py_DECREF(temp);
}
}
Py_DECREF(func);
return result;
}
static const char* name_op[] = {
"__lt__", "__le__", "__eq__", "__ne__", "__gt__", "__ge__",
};
......@@ -844,13 +809,59 @@ static PyObject* slot_nb_power(PyObject* self, PyObject* other, PyObject* modulu
return Py_NotImplemented;
}
// SLOT0(slot_nb_invert, "__invert__")
SLOT0(slot_nb_negative, "__neg__")
SLOT0(slot_nb_positive, "__pos__")
SLOT0(slot_nb_absolute, "__abs__")
static int slot_nb_nonzero(PyObject* self) noexcept {
PyObject* func, *args;
static PyObject* nonzero_str, *len_str;
int result = -1;
int using_len = 0;
func = lookup_maybe(self, "__nonzero__", &nonzero_str);
if (func == NULL) {
if (PyErr_Occurred())
return -1;
func = lookup_maybe(self, "__len__", &len_str);
if (func == NULL)
return PyErr_Occurred() ? -1 : 1;
using_len = 1;
}
args = PyTuple_New(0);
if (args != NULL) {
PyObject* temp = PyObject_Call(func, args, NULL);
Py_DECREF(args);
if (temp != NULL) {
if (PyInt_CheckExact(temp) || PyBool_Check(temp))
result = PyObject_IsTrue(temp);
else {
PyErr_Format(PyExc_TypeError, "%s should return "
"bool or int, returned %s",
(using_len ? "__len__" : "__nonzero__"), temp->cls->tp_name);
result = -1;
}
Py_DECREF(temp);
}
}
Py_DECREF(func);
return result;
}
SLOT0(slot_nb_invert, "__invert__")
SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
static int slot_nb_coerce(PyObject** a, PyObject** b);
SLOT0(slot_nb_int, "__int__")
SLOT0(slot_nb_long, "__long__")
SLOT0(slot_nb_float, "__float__")
SLOT0(slot_nb_oct, "__oct__")
SLOT0(slot_nb_hex, "__hex__")
typedef wrapper_def slotdef;
......@@ -956,7 +967,11 @@ static slotdef slotdefs[] = {
RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, "divmod(y, x)"),
NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), //
UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), //
UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, "abs(x)"), //
UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred, "x != 0"), //
UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), //
BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), //
RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), //
BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), //
......@@ -967,6 +982,11 @@ static slotdef slotdefs[] = {
RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), //
BINSLOT("__or__", nb_or, slot_nb_or, "|"), //
RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), //
UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, "int(x)"), //
UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc, "long(x)"), //
UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, "float(x)"), //
UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc, "oct(x)"), //
UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, "hex(x)"), //
MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, "x.__len__() <==> len(x)"),
MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, wrap_binaryfunc, "x.__getitem__(y) <==> x[y]"),
......@@ -1132,8 +1152,8 @@ extern "C" int PyType_Ready(PyTypeObject* cls) {
SAVE(nb_nonzero);
SAVE(nb_add);
for (void** p = (void**)cls->tp_as_number; p < (void**)cls->tp_as_number + 1; p++) {
RELEASE_ASSERT(*p == NULL, "");
for (void** p = (void**)cls->tp_as_number; p < (void**)(cls->tp_as_number + 1); p++) {
// RELEASE_ASSERT(*p == NULL, "");
}
RESTORE(nb_nonzero);
......
......@@ -210,26 +210,38 @@ static PyTypeObject slots_tester_map= {
0, /* tp_free */
};
static int s_nonzero(slots_tester_object* self) {
printf("s_nonzero, %d\n", self->n);
return self->n != 0;
}
#define _PYSTON_STRINGIFY(N) #N
#define PYSTON_STRINGIFY(N) _PYSTON_STRINGIFY(N)
#define CREATE_FUNC(N) \
#define CREATE_UN(N, R) \
static PyObject* N(slots_tester_object* lhs) { \
printf(PYSTON_STRINGIFY(N) ", %d\n", lhs->n); \
Py_INCREF(R); \
return (PyObject*)R; \
}
#define CREATE_BIN(N) \
static PyObject* N(slots_tester_object* lhs, PyObject* rhs) { \
printf(PYSTON_STRINGIFY(N) ", %d %s\n", lhs->n, Py_TYPE(rhs)->tp_name); \
Py_INCREF(lhs); \
return (PyObject*)lhs; \
}
CREATE_FUNC(s_add);
CREATE_FUNC(s_subtract);
CREATE_FUNC(s_multiply);
CREATE_FUNC(s_divide);
CREATE_FUNC(s_remainder);
CREATE_FUNC(s_divmod);
CREATE_BIN(s_add);
CREATE_BIN(s_subtract);
CREATE_BIN(s_multiply);
CREATE_BIN(s_divide);
CREATE_BIN(s_remainder);
CREATE_BIN(s_divmod);
CREATE_UN(s_negative, lhs);
CREATE_UN(s_positive, lhs);
CREATE_UN(s_absolute, lhs);
static int s_nonzero(slots_tester_object* self) {
printf("s_nonzero, %d\n", self->n);
return self->n != 0;
}
CREATE_UN(s_invert, lhs);
static PyObject* s_power(slots_tester_object* lhs, PyObject* rhs, PyObject* mod) {
printf("s_power, %d %s %s\n", lhs->n, Py_TYPE(rhs)->tp_name, Py_TYPE(mod)->tp_name);
......@@ -237,13 +249,19 @@ static PyObject* s_power(slots_tester_object* lhs, PyObject* rhs, PyObject* mod)
return (PyObject*)lhs;
}
CREATE_FUNC(s_lshift);
CREATE_FUNC(s_rshift);
CREATE_FUNC(s_and);
CREATE_FUNC(s_xor);
CREATE_FUNC(s_or);
CREATE_BIN(s_lshift);
CREATE_BIN(s_rshift);
CREATE_BIN(s_and);
CREATE_BIN(s_xor);
CREATE_BIN(s_or);
CREATE_UN(s_int, Py_True);
CREATE_UN(s_long, Py_True);
CREATE_UN(s_float, PyFloat_FromDouble(1.0));
CREATE_UN(s_oct, PyString_FromString("oct"));
CREATE_UN(s_hex, PyString_FromString("hex"));
#undef CREATE_FUNC
#undef CREATE_BIN
static PyNumberMethods slots_tester_as_number = {
(binaryfunc)s_add, /* nb_add */
......@@ -253,22 +271,22 @@ static PyNumberMethods slots_tester_as_number = {
(binaryfunc)s_remainder, /* nb_remainder */
(binaryfunc)s_divmod, /* nb_divmod */
(ternaryfunc)s_power, /* nb_power */
0, /* nb_negative */
0, /* nb_positive */
0, /* nb_absolute */
(unaryfunc)s_negative, /* nb_negative */
(unaryfunc)s_positive, /* nb_positive */
(unaryfunc)s_absolute, /* nb_absolute */
(inquiry)s_nonzero, /* nb_nonzero */
0, /*nb_invert*/
(unaryfunc)s_invert, /*nb_invert*/
(binaryfunc)s_lshift, /*nb_lshift*/
(binaryfunc)s_rshift, /*nb_rshift*/
(binaryfunc)s_and, /*nb_and*/
(binaryfunc)s_xor, /*nb_xor*/
(binaryfunc)s_or, /*nb_or*/
0, /*nb_coerce*/
0, /*nb_int*/
0, /*nb_long*/
0, /*nb_float*/
0, /*nb_oct*/
0, /*nb_hex*/
(unaryfunc)s_int, /*nb_int*/
(unaryfunc)s_long, /*nb_long*/
(unaryfunc)s_float, /*nb_float*/
(unaryfunc)s_oct, /*nb_oct*/
(unaryfunc)s_hex, /*nb_hex*/
0, /*nb_inplace_add*/
0, /*nb_inplace_subtract*/
0, /*nb_inplace_multiply*/
......@@ -417,24 +435,37 @@ call_funcs(PyObject* _module, PyObject* args) {
printf("CHECKTYPES is not set!\n");
}
if (num->nb_nonzero) {
int n = num->nb_nonzero(obj);
printf("nb_nonzero exists and returned %d\n", n);
#define CHECK_UN(N) \
if (num->N) { \
PyObject* res = num->N(obj); \
printf(PYSTON_STRINGIFY(N) " exists and returned a %s\n", Py_TYPE(res)->tp_name); \
Py_DECREF(res); \
}
#define CHECK(N) \
#define CHECK_BIN(N) \
if (num->N) { \
PyObject* res = num->N(obj, obj); \
printf(PYSTON_STRINGIFY(N) " exists and returned a %s\n", Py_TYPE(res)->tp_name); \
Py_DECREF(res); \
}
CHECK(nb_add);
CHECK(nb_subtract);
CHECK(nb_multiply);
CHECK(nb_divide);
CHECK(nb_remainder);
CHECK(nb_divmod);
CHECK_BIN(nb_add);
CHECK_BIN(nb_subtract);
CHECK_BIN(nb_multiply);
CHECK_BIN(nb_divide);
CHECK_BIN(nb_remainder);
CHECK_BIN(nb_divmod);
CHECK_UN(nb_negative);
CHECK_UN(nb_positive);
CHECK_UN(nb_absolute);
if (num->nb_nonzero) {
int n = num->nb_nonzero(obj);
printf("nb_nonzero exists and returned %d\n", n);
}
CHECK_UN(nb_invert);
if (num->nb_power) {
PyObject* res = num->nb_power(obj, obj, Py_None);
......@@ -442,12 +473,18 @@ call_funcs(PyObject* _module, PyObject* args) {
Py_DECREF(res);
}
CHECK(nb_lshift);
CHECK(nb_rshift);
CHECK(nb_and);
CHECK(nb_xor);
CHECK(nb_or);
#undef CHECK
CHECK_BIN(nb_lshift);
CHECK_BIN(nb_rshift);
CHECK_BIN(nb_and);
CHECK_BIN(nb_xor);
CHECK_BIN(nb_or);
CHECK_UN(nb_int);
CHECK_UN(nb_long);
CHECK_UN(nb_float);
CHECK_UN(nb_oct);
CHECK_UN(nb_hex);
#undef CHECK_BIN
} else {
printf("tp_as_number doesnt exist\n");
......
......@@ -29,6 +29,15 @@ for i in xrange(3):
print t & 5
print t ^ 5
print t | 5
print +t
print -t
print abs(t)
print ~t
print int(t)
print long(t)
print float(t)
print hex(t)
print oct(t)
class C(object):
def __repr__(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment