Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 10 Lib/test/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,16 @@ def __len__(self):
except (Exception) as e_len:
self.assertEqual(str(e_bool), str(e_len))

def test_len_overflow(self):
self.assertIs(bool(range(1 << 1000)), True)
class A:
def __len__(self):
return length
length = 1 << 1000
self.assertIs(bool(A()), True)
length = -1 << 1000
self.assertRaises(ValueError, bool, A())

def test_blocked(self):
class A:
__bool__ = None
Expand Down
5 changes: 4 additions & 1 deletion 5 Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ What's New in Python 3.7.0 alpha 1?
Core and Builtins
-----------------

- bpo-12414: sys.getsizeof() on a code object now returns the sizes
- bpo-29840: bool() no longer raises OverflowError for objects without defined
__bool__ when __len__() returned a large integer or raised OverflowError.

- bpo-12414: sys.getsizeof() on a code object now returns the sizes
which includes the code struct and sizes of objects which it references.
Patch by Dong-hee Na.

Expand Down
29 changes: 19 additions & 10 deletions 29 Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1313,26 +1313,35 @@ PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
int
PyObject_IsTrue(PyObject *v)
{
Py_ssize_t res;
Py_ssize_t len;
if (v == Py_True)
return 1;
if (v == Py_False)
return 0;
if (v == Py_None)
return 0;
else if (v->ob_type->tp_as_number != NULL &&
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);
if (v->ob_type->tp_as_number != NULL &&
v->ob_type->tp_as_number->nb_bool != NULL)
return (*v->ob_type->tp_as_number->nb_bool)(v);

if (v->ob_type->tp_as_mapping != NULL &&
v->ob_type->tp_as_mapping->mp_length != NULL)
len = (*v->ob_type->tp_as_mapping->mp_length)(v);
else if (v->ob_type->tp_as_sequence != NULL &&
v->ob_type->tp_as_sequence->sq_length != NULL)
res = (*v->ob_type->tp_as_sequence->sq_length)(v);
len = (*v->ob_type->tp_as_sequence->sq_length)(v);
else
return 1;
/* if it is negative, it should be either -1 or -2 */
return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
if (len > 0)
return 1;
if (len == 0)
return 0;
assert(PyErr_Occurred());
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyErr_Clear();
return 1;
}
return -1;
}

/* equivalent of 'not v'
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.