This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Incorrect error messages in bisect
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: bup, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-09-29 09:29 by bup, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg326672 - (view) Author: Dan Snider (bup) * Date: 2018-09-29 09:29
internal_bisect_left and internal_bisect_right use PySequence_Size when a "hi" argument wasn't provided, which causes this silly error message:

    >>> bisect.bisect_right(dict.fromkeys(range(10)), 5)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: object of type 'dict' has no len()

They could use PyObject_Size and let PySequence_GetItem in the loop catch the error:

    >>> bisect.bisect_right(dict.fromkeys(range(10)), 5, 0, 10)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'dict' object does not support indexing

Since that actually makes sense and is more efficient / less verbose than adding a PySequence_Check.
msg326675 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-09-29 12:19
It was fixed in issue32500. This is a new feature and it was not backported.

>>> bisect.bisect_right(dict.fromkeys(range(10)), 5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: dict is not a sequence
>>> bisect.bisect_right(dict.fromkeys(range(10)), 5, 0, 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: dict is not a sequence
msg326688 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-09-29 17:09
Dan, thanks for the suggestion.  Changing to PyObject_Size() would have been the right thing to do if the error message had not been fixed.  But since the new error message "TypeError: dict is not a sequence" is more helpful than "TypeError: 'dict' object does not support indexing", we should leave the code as-is.
History
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 79023
2018-09-29 17:34:52serhiy.storchakasetresolution: not a bug -> out of date
2018-09-29 17:09:03rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg326688

stage: resolved
2018-09-29 12:19:03serhiy.storchakasetnosy: + rhettinger, serhiy.storchaka
messages: + msg326675
2018-09-29 09:29:49bupcreate