msg289781 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
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 "", line 1, in OverflowError: cannot fit 'int' into an index-sized integer >>> bool(range(1<<1000)) Traceback (most recent call last): File "", line 1, in 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 . |
|
|
msg290139 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2017-03-24 20:49 |
https://docs.python.org/3/library/functions.html#bool refers to https://docs.python.org/3/library/stdtypes.html#truth 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 (vstinner) *  |
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) *  |
Date: 2017-03-27 14:06 |
This issue depends on . Tests are failed until the patch of is merged. |
|
|
msg292064 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2017-04-21 16:34 |
Hum, I dislike this change since it's non-obvious what/who is raising the OverflowError. If an object calls a function in __len__() and the function raises OverflowError, should we consider that object is "true"? In temptation to guess, I prefer to not guess but passthrough the exception. If you want to support bool(range(1<<1000)), we need to get the result of __len__() as a Python object rather than a C Py_ssize_t. Maybe, if __len__() raises an OverflowError: call again the len(), but using the "__len__" method instead of the slot? |
|
|
msg292121 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2017-04-22 15:18 |
I had similar doubts about this patch and needed opinions of other core developers. > Maybe, if __len__() raises an OverflowError: call again the len(), but using the "__len__" method instead of the slot? Following patch implements this idea. I don't like it because it is too complicated. I think that we should either document that raising an OverflowError by __len__() is normal and interpreted as true in Boolean context, or document that __len__() should return a value not larger than sys.maxsize, otherwise len() and bool() can raise an OverflowError (see ). |
|
|
msg292140 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2017-04-22 22:05 |
If someone wants to return a value larger than maxsize and support bool(): it is already possible right now by defining a __bool__ method no? If yes, I suggest to only document this CPython implementation detail (consequence of slots, for efficiency) and suggest to use __bool__ for such corner case. I also dislike retrying to call "__len__" method instead of the slot. It can have annoying side effects. |
|
|
msg292156 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2017-04-23 07:14 |
This was documented in . |
|
|