Bug in Python?

eryk sun eryksun at gmail.com
Fri Feb 26 18:07:20 EST 2016


 On Fri, Feb 26, 2016 at 4:08 PM, Sven R. Kunze <srkunze at mail.de> wrote:
> Python sometimes seems not to hop back and forth between C and Python code.
> Can somebody explain this?

Normally a C extension would call PySequence_SetItem, which would call
the type's sq_ass_item, which for MyList is slot_sq_ass_item. The
latter function bridges the CPython and Python sides by binding and
calling the overridden __setitem__ method.  However, the _heapq
extension module uses `PyList_SET_ITEM(heap, 0, lastelt)`. This macro
expands to `((PyListObject *)(heap))->ob_item[0] = lastelt`. This
directly modifies the internal ob_item array of the list, so the
overridden __setitem__ method is never called. I presume it was
implemented like this with performance in mind, but I don't know
whether or not that justifies the loss of generality.


More information about the Python-list mailing list