A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/cython/cython/issues/1807 below:

__Pyx_GetItemInt_Fast should prefer mapping over sequence protocol · Issue #1807 · cython/cython · GitHub

The Cython code obj[i] where i is a cdef int is translated to a call to __Pyx_GetItemInt_Fast.

This will use the sequence protocol to get obj[i]. More precisely, Py_TYPE(obj)->tp_as_sequence->sq_item(obj, i) will be called.

However, Python always tries the mapping protocol first. Because of this, it can happen that obj[i] in Cython behaves differently from Python. There is nothing in the Python docs that says that the mapping and sequence protocols should agree, so this is a bug.

Simple repro:

def get(obj, int i):
    return obj[i]

class D(dict):
    pass 

This gives:

>>> d = D([(-1, "hello")]); d
{-1: 'hello'}
>>> d[-1]
'hello' 
>>> get(d, -1)
KeyError: 0

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4