Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: Bug in Python? Date: Sat, 27 Feb 2016 03:55:32 -0500 Lines: 38 Message-ID: References: <56D0CCE9.2000301@mail.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Vte78S24o2jSBBrv560X0g8HCgjAV2viA9Q+fD4btHiA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; '(python': 0.05; 'cpython': 0.05; 'method.': 0.05; '[],': 0.07; 'builtin': 0.07; 'called.': 0.09; 'dict': 0.09; 'modifies': 0.09; 'overridden': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'python': 0.10; 'jan': 0.11; '"to': 0.16; '*list*': 0.16; '2016': 0.16; '_heapq': 0.16; 'expands': 0.16; 'heap': 0.16; 'heap,': 0.16; 'macro': 0.16; 'mylist': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; "type's": 0.16; 'wrote:': 0.16; 'skip:` 10': 0.18; 'skip:` 20': 0.18; 'transform': 0.18; 'extension': 0.20; 'doc': 0.22; 'latter': 0.22; 'code.': 0.23; 'seems': 0.23; 'feb': 0.23; 'implemented': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'fri,': 0.27; 'function': 0.28; 'initialized': 0.29; 'mind,': 0.29; 'array': 0.29; 'class.': 0.30; 'normally': 0.30; 'somebody': 0.30; 'says': 0.32; 'case,': 0.34; 'this?': 0.34; 'list': 0.34; 'could': 0.35; 'generic': 0.35; 'instance': 0.35; 'skip:* 20': 0.35; 'sometimes': 0.35; 'but': 0.36; 'list,': 0.36; '(and': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'display': 0.37; 'method': 0.37; 'say': 0.37; 'received:org': 0.37; 'loss': 0.38; 'means': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'easy': 0.60; 'back': 0.62; 'linked': 0.63; 'received:96': 0.63; 'between': 0.65; 'binding': 0.66; 'note:': 0.66; '26,': 0.72; 'obvious': 0.76; 'forth': 0.79; 'dict.': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-96-227-207-81.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103564 On 2/26/2016 6:07 PM, eryk sun wrote: > On Fri, Feb 26, 2016 at 4:08 PM, Sven R. Kunze 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. In other words, when that doc says *list*, it means a *list*. "To create a heap, use a list initialized to [], or you can transform a populated list into a heap via function heapify()." Note: when the doc says 'dict' rather than 'dictionary' or 'mapping', it is pretty obvious it means builtin dict. It may sometimes say 'dict or dict subclass'. Ditto for 'str' versus 'string' or 'text'. However, 'list' (Python builtin) and generic 'list are easy to confuse. In this case, 'initialized to []' is a hint. However, the doc could be made clearer. How about "A heap must be an instance of *list* (and not a subclass thereof). To create a heap, start with [] or transform an existing list into a heap via function heapify()." *list* means to display it blue, linked to the list class. -- Terry Jan Reedy