classification
Title: bisect insort C implementation ignores methods on list subclasses
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.7
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: amaury.forgeotdarc, georg.brandl, hdima, jek, rhettinger
Priority: low Keywords: patch

Created on 2008-09-22 18:53 by jek, last changed 2008-10-08 18:47 by georg.brandl. This issue is now closed.

Files
File name Uploaded Description Edit
test.py jek, 2008-09-22 18:53 test case
bisect.diff hdima, 2008-09-26 17:32 Patch
bisect2.patch hdima, 2008-09-26 19:48 Patch with PyList_CheckExact
Messages (6)
msg73589 - (view) Author: jason kirtland (jek) Date: 2008-09-22 18:53
The C implementation (only) of bisect does not invoke list subclass
methods when insorting.  Code like this will not trigger the assert:

  class Boom(list):
     def insert(self, index, item):
         assert False

  bisect.insort(Boom(), 123)

object-derived classes are OK.
msg73867 - (view) Author: Dmitry Vasiliev (hdima) Date: 2008-09-26 17:32
Actually it was an optimization. PyList_Insert() was used for list and
list-derived objects.

I've attached the patch which fix the issue and for me the new code
looks even cleaner than the original code.
msg73873 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-09-26 17:52
We could keep the optimization for the standard case:
What about simply replacing PyList_Check with PyList_CheckExact?

- most usages use plain lists, and will even run slightly faster
- list-derived objects get the desired behaviour.
msg73881 - (view) Author: Dmitry Vasiliev (hdima) Date: 2008-09-26 19:48
Good idea! Don't know why I didn't use it in the very first version. :-)

New patch attached.
msg74348 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-10-05 17:02
Don't think this is too late for Py3.0.
msg74543 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-10-08 18:47
OK, committed as r66856, should get merged to 3.0 soon.
History
Date User Action Args
2008-10-08 18:47:48georg.brandlsetstatus: open -> closed
messages: + msg74543
2008-10-05 17:02:41rhettingersetassignee: rhettinger -> georg.brandl
versions: + Python 3.0, Python 2.7, - Python 2.6, Python 2.5
messages: + msg74348
resolution: accepted
nosy: + georg.brandl
2008-09-26 19:48:35hdimasetfiles: + bisect2.patch
messages: + msg73881
2008-09-26 17:52:35amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg73873
2008-09-26 17:32:55hdimasetfiles: + bisect.diff
keywords: + patch
messages: + msg73867
nosy: + hdima
2008-09-23 12:41:51rhettingersetpriority: low
assignee: rhettinger
nosy: + rhettinger
2008-09-22 18:53:44jekcreate