Commit 99e88d36 authored by Stefan Behnel's avatar Stefan Behnel

avoid None check in __Pyx_PyDict_GetItem() when possible

parent 9789f14e
......@@ -2685,6 +2685,8 @@ class IndexNode(ExprNode):
elif is_slice and base_type in (bytes_type, str_type, unicode_type, list_type, tuple_type):
self.type = base_type
else:
if base_type is dict_type:
self.base = self.base.as_none_safe_node("'NoneType' object is unsubscriptable")
self.type = py_object_type
else:
if base_type.is_ptr or base_type.is_array:
......
......@@ -236,15 +236,10 @@ static CYTHON_INLINE int __Pyx_IterFinish(void) {
}
/////////////// DictGetItem.proto ///////////////
//@requires: RaiseNoneIndexingError
#if PY_MAJOR_VERSION >= 3
static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
PyObject *value;
if (unlikely(d == Py_None)) {
__Pyx_RaiseNoneIndexingError();
return NULL;
}
value = PyDict_GetItemWithError(d, key);
if (unlikely(!value)) {
if (!PyErr_Occurred())
......
# mode: run
# tag: dict, getitem
cimport cython
def test(dict d, index):
"""
>>> d = { 1: 10 }
......@@ -25,11 +30,6 @@ def test(dict d, index):
"""
return d[index]
cdef class Subscriptable:
def __getitem__(self, key):
return key
def getitem_in_condition(dict d, key, expected_result):
"""
>>> d = dict(a=1, b=2)
......@@ -37,3 +37,20 @@ def getitem_in_condition(dict d, key, expected_result):
True
"""
return d[key] is expected_result or d[key] == expected_result
@cython.test_fail_if_path_exists('//NoneCheckNode')
def getitem_not_none(dict d not None, key):
"""
>>> d = { 1: 10 }
>>> test(d, 1)
10
>>> test(d, 2)
Traceback (most recent call last):
KeyError: 2
>>> test(d, (1,2))
Traceback (most recent call last):
KeyError: (1, 2)
"""
return d[key]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment