mirror of
https://github.com/python/cpython
synced 2026-09-29 09:40:29 +03:00
- patch #1600346 submitted by Tomer Filiba
- Renamed nb_nonzero slots to nb_bool - Renamed __nonzero__ methods to __bool__ - update core, lib, docs, and tests to match
This commit is contained in:
parent
dfc9d4f7aa
commit
4dafcc4ece
31 changed files with 118 additions and 82 deletions
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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*/
|
||||
|
|
|
|||
|
|
@ -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*/
|
||||
|
|
|
|||
|
|
@ -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*/
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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*/
|
||||
|
|
|
|||
|
|
@ -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, "<<"),
|
||||
|
|
|
|||
|
|
@ -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*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue