Title: Avoid raising OverflowError in bool()
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.7
Status: open Resolution:
Dependencies: 29839 Superseder:
Assigned To: Nosy List: haypo, mark.dickinson, rhettinger, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2017-03-17 20:27 by serhiy.storchaka, last changed 2017-03-27 14:06 by serhiy.storchaka.

File name Uploaded Description Edit
bool-overflow.diff serhiy.storchaka, 2017-03-17 20:35
Messages (4)
msg289781 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-17 20:27
For now bool() raises OverflowError if __bool__ is not defined and __len__ returns large value.

>>> class A:
...     def __len__(self):
...         return 1 << 1000
>>> bool(A())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: cannot fit 'int' into an index-sized integer
>>> bool(range(1<<1000))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C ssize_t

Proposed patch makes bool() returning True if len() raises OverflowError.

This is an alternate solution of issue28876.
msg290139 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-03-24 20:49 refers to

The latter says that values are true ("All other values are considered true") unless one of certain conditions holds.  For user-defined classes, the condition is that the class defines a __bool__() or __len__() method and that the first of those methods returns the bool False or integer zero.

I easily interpret this as meaning that bool(x) (should) *always* return True or False.  In particular, for user classes, any exception in user-coded __bool__ or __len__ (should be) included in "does not return integer zero or bool value False".  This would mean that 'True' would truly be the default return for Bool().

There is currently an unstated exception for raised Exceptions.  This issue proposes an exception to the exception for OverflowErrors (once negative lengths consistently raise ValueErrors and never OverflowErrors).  While this sensible in itself, I am completely happy with the added complication.  I would like to either reconsider the exception for Exceptions or make it explicit.

Patch has new text and What's New entry.  Added logic in object.c looks correct.
msg290606 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2017-03-27 13:03
Serhiy: Can you please create a pull request? It would be easier to review.
msg290615 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-27 14:06
This issue depends on issue29839. Tests are failed until the patch of issue29839 is merged.
Date User Action Args
2017-03-27 14:06:27serhiy.storchakasetmessages: + msg290615
2017-03-27 13:03:41hayposetnosy: + haypo
messages: + msg290606
2017-03-24 20:49:53terry.reedysetnosy: + terry.reedy
messages: + msg290139
2017-03-17 20:35:34serhiy.storchakasetfiles: + bool-overflow.diff
keywords: + patch
dependencies: + Avoid raising OverflowError in len() when __len__() returns negative large value
2017-03-17 20:27:26serhiy.storchakacreate