diff options
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/boolobject.c | 2 | ||||
-rw-r--r-- | Objects/complexobject.c | 4 | ||||
-rw-r--r-- | Objects/floatobject.c | 4 | ||||
-rw-r--r-- | Objects/intobject.c | 6 | ||||
-rw-r--r-- | Objects/longobject.c | 4 | ||||
-rw-r--r-- | Objects/object.c | 4 | ||||
-rw-r--r-- | Objects/setobject.c | 4 | ||||
-rw-r--r-- | Objects/typeobject.c | 25 | ||||
-rw-r--r-- | Objects/weakrefobject.c | 4 |
9 files changed, 32 insertions, 25 deletions
diff --git a/Objects/boolobject.c b/Objects/boolobject.c index a0d6cd5..cd19481 100644 --- a/Objects/boolobject.c +++ b/Objects/boolobject.c @@ -112,7 +112,7 @@ static PyNumberMethods bool_as_number = { 0, /* nb_negative */ 0, /* nb_positive */ 0, /* nb_absolute */ - 0, /* nb_nonzero */ + 0, /* nb_bool */ 0, /* nb_invert */ 0, /* nb_lshift */ 0, /* nb_rshift */ diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 2713e3e..da47f81 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -568,7 +568,7 @@ complex_abs(PyComplexObject *v) } static int -complex_nonzero(PyComplexObject *v) +complex_bool(PyComplexObject *v) { return v->cval.real != 0.0 || v->cval.imag != 0.0; } @@ -938,7 +938,7 @@ static PyNumberMethods complex_as_number = { (unaryfunc)complex_neg, /* nb_negative */ (unaryfunc)complex_pos, /* nb_positive */ (unaryfunc)complex_abs, /* nb_absolute */ - (inquiry)complex_nonzero, /* nb_nonzero */ + (inquiry)complex_bool, /* nb_bool */ 0, /* nb_invert */ 0, /* nb_lshift */ 0, /* nb_rshift */ diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 40126b2..aab7612 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -837,7 +837,7 @@ float_abs(PyFloatObject *v) } static int -float_nonzero(PyFloatObject *v) +float_bool(PyFloatObject *v) { return v->ob_fval != 0.0; } @@ -1087,7 +1087,7 @@ static PyNumberMethods float_as_number = { (unaryfunc)float_neg, /*nb_negative*/ (unaryfunc)float_pos, /*nb_positive*/ (unaryfunc)float_abs, /*nb_absolute*/ - (inquiry)float_nonzero, /*nb_nonzero*/ + (inquiry)float_bool, /*nb_bool*/ 0, /*nb_invert*/ 0, /*nb_lshift*/ 0, /*nb_rshift*/ diff --git a/Objects/intobject.c b/Objects/intobject.c index e64d0b1..b420c12 100644 --- a/Objects/intobject.c +++ b/Objects/intobject.c @@ -780,7 +780,7 @@ int_abs(PyIntObject *v) } static int -int_nonzero(PyIntObject *v) +int_bool(PyIntObject *v) { return v->ob_ival != 0; } @@ -1034,14 +1034,14 @@ static PyNumberMethods int_as_number = { (unaryfunc)int_neg, /*nb_negative*/ (unaryfunc)int_pos, /*nb_positive*/ (unaryfunc)int_abs, /*nb_absolute*/ - (inquiry)int_nonzero, /*nb_nonzero*/ + (inquiry)int_bool, /*nb_bool*/ (unaryfunc)int_invert, /*nb_invert*/ (binaryfunc)int_lshift, /*nb_lshift*/ (binaryfunc)int_rshift, /*nb_rshift*/ (binaryfunc)int_and, /*nb_and*/ (binaryfunc)int_xor, /*nb_xor*/ (binaryfunc)int_or, /*nb_or*/ - 0, /*nb_coerce*/ + 0, /*nb_coerce*/ (unaryfunc)int_int, /*nb_int*/ (unaryfunc)int_long, /*nb_long*/ (unaryfunc)int_float, /*nb_float*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index 87082cb..566e6a7 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -2886,7 +2886,7 @@ long_abs(PyLongObject *v) } static int -long_nonzero(PyLongObject *v) +long_bool(PyLongObject *v) { return ABS(v->ob_size) != 0; } @@ -3307,7 +3307,7 @@ static PyNumberMethods long_as_number = { (unaryfunc) long_neg, /*nb_negative*/ (unaryfunc) long_pos, /*tp_positive*/ (unaryfunc) long_abs, /*tp_absolute*/ - (inquiry) long_nonzero, /*tp_nonzero*/ + (inquiry) long_bool, /*tp_bool*/ (unaryfunc) long_invert, /*nb_invert*/ long_lshift, /*nb_lshift*/ (binaryfunc) long_rshift, /*nb_rshift*/ diff --git a/Objects/object.c b/Objects/object.c index e202e9b..4d9f414 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1246,8 +1246,8 @@ PyObject_IsTrue(PyObject *v) if (v == Py_None) return 0; else if (v->ob_type->tp_as_number != NULL && - v->ob_type->tp_as_number->nb_nonzero != NULL) - res = (*v->ob_type->tp_as_number->nb_nonzero)(v); + v->ob_type->tp_as_number->nb_bool != NULL) + res = (*v->ob_type->tp_as_number->nb_bool)(v); else if (v->ob_type->tp_as_mapping != NULL && v->ob_type->tp_as_mapping->mp_length != NULL) res = (*v->ob_type->tp_as_mapping->mp_length)(v); diff --git a/Objects/setobject.c b/Objects/setobject.c index 05549e6..d489711 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1784,7 +1784,7 @@ static PyNumberMethods set_as_number = { 0, /*nb_negative*/ 0, /*nb_positive*/ 0, /*nb_absolute*/ - 0, /*nb_nonzero*/ + 0, /*nb_bool*/ 0, /*nb_invert*/ 0, /*nb_lshift*/ 0, /*nb_rshift*/ @@ -1894,7 +1894,7 @@ static PyNumberMethods frozenset_as_number = { 0, /*nb_negative*/ 0, /*nb_positive*/ 0, /*nb_absolute*/ - 0, /*nb_nonzero*/ + 0, /*nb_bool*/ 0, /*nb_invert*/ 0, /*nb_lshift*/ 0, /*nb_rshift*/ diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 074fad9..ea1d1a2 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2928,7 +2928,7 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base) COPYNUM(nb_negative); COPYNUM(nb_positive); COPYNUM(nb_absolute); - COPYNUM(nb_nonzero); + COPYNUM(nb_bool); COPYNUM(nb_invert); COPYNUM(nb_lshift); COPYNUM(nb_rshift); @@ -4206,32 +4206,39 @@ SLOT0(slot_nb_positive, "__pos__") SLOT0(slot_nb_absolute, "__abs__") static int -slot_nb_nonzero(PyObject *self) +slot_nb_bool(PyObject *self) { PyObject *func, *args; - static PyObject *nonzero_str, *len_str; + static PyObject *bool_str, *len_str; int result = -1; + int from_len = 0; - func = lookup_maybe(self, "__nonzero__", &nonzero_str); + func = lookup_maybe(self, "__bool__", &bool_str); if (func == NULL) { if (PyErr_Occurred()) return -1; func = lookup_maybe(self, "__len__", &len_str); if (func == NULL) return PyErr_Occurred() ? -1 : 1; + from_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)) + if (from_len) { + /* enforced by slot_nb_len */ result = PyObject_IsTrue(temp); + } + else if (PyBool_Check(temp)) { + result = PyObject_IsTrue(temp); + } else { PyErr_Format(PyExc_TypeError, - "__nonzero__ should return " - "bool or int, returned %s", - temp->ob_type->tp_name); + "__bool__ should return " + "bool, returned %s", + temp->ob_type->tp_name); result = -1; } Py_DECREF(temp); @@ -4887,7 +4894,7 @@ static slotdef slotdefs[] = { 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, + UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, "x != 0"), UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 206a455..e05788f 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -499,7 +499,7 @@ WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor) WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr) static int -proxy_nonzero(PyWeakReference *proxy) +proxy_bool(PyWeakReference *proxy) { PyObject *o = PyWeakref_GET_OBJECT(proxy); if (!proxy_checkref(proxy)) @@ -596,7 +596,7 @@ static PyNumberMethods proxy_as_number = { proxy_neg, /*nb_negative*/ proxy_pos, /*nb_positive*/ proxy_abs, /*nb_absolute*/ - (inquiry)proxy_nonzero, /*nb_nonzero*/ + (inquiry)proxy_bool, /*nb_bool*/ proxy_invert, /*nb_invert*/ proxy_lshift, /*nb_lshift*/ proxy_rshift, /*nb_rshift*/ |