You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid raising OverflowError in len() when __len__() returns negative large value #74025
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/serhiy-storchaka'closed_at=<Date2017-04-16.07:05:36.437>created_at=<Date2017-03-17.19:52:14.485>labels= ['interpreter-core', 'type-feature', '3.7']
title='Avoid raising OverflowError in len() when __len__() returns negative large value'updated_at=<Date2017-04-16.07:05:36.434>user='https://github.com/serhiy-storchaka'
I was going to say that this is an API change, but given that without this, folks would have to catch both exceptions and now only have to catch one of them, it isn't.
https://docs.python.org/3/library/functions.html#len * does not specify the exception. In such cases, we occasionally change exception in x.y.0 releases without prior notice other than News and What's New. Also, I think unnecessarily exposing a compile-switch dependent internal detail, as now, is almost a bug. So +1 to applying in 3.7
The doc does not specify that 'length' cannot be non-negative. Perhaps it should, so no-one will think that they can hijack '__len__' to return something that is not a length, as usually understood.
New changeset baf9f29 by Serhiy Storchaka in branch 'master': bpo-29839: Raise ValueError rather than OverflowError in len() for negative values. (#701) baf9f29
Activity
serhiy-storchaka commentedon Mar 17, 2017
For now len() raises ValueError if __len__() returns small negative integer and OverflowError if __len__() returns large negative integer.
Proposed patch makes it always raising ValueError.
warsaw commentedon Mar 17, 2017
I was going to say that this is an API change, but given that without this, folks would have to catch both exceptions and now only have to catch one of them, it isn't.
terryjreedy commentedon Mar 24, 2017
https://docs.python.org/3/library/functions.html#len * does not specify the exception. In such cases, we occasionally change exception in x.y.0 releases without prior notice other than News and What's New. Also, I think unnecessarily exposing a compile-switch dependent internal detail, as now, is almost a bug. So +1 to applying in 3.7
serhiy-storchaka commentedon Apr 16, 2017
New changeset baf9f29 by Serhiy Storchaka in branch 'master':
bpo-29839: Raise ValueError rather than OverflowError in len() for negative values. (#701)
baf9f29
serhiy-storchaka commentedon Apr 16, 2017
It does. https://docs.python.org/3/reference/datamodel.html#object.\_\_len__
.. method:: object.__len__(self)
Called to implement the built-in function :func:`len`. Should return the length
of the object, an integer ``>=`` 0.