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): ...@@ -2685,6 +2685,8 @@ class IndexNode(ExprNode):
elif is_slice and base_type in (bytes_type, str_type, unicode_type, list_type, tuple_type): elif is_slice and base_type in (bytes_type, str_type, unicode_type, list_type, tuple_type):
self.type = base_type self.type = base_type
else: else:
if base_type is dict_type:
self.base = self.base.as_none_safe_node("'NoneType' object is unsubscriptable")
self.type = py_object_type self.type = py_object_type
else: else:
if base_type.is_ptr or base_type.is_array: if base_type.is_ptr or base_type.is_array:
......
...@@ -236,15 +236,10 @@ static CYTHON_INLINE int __Pyx_IterFinish(void) { ...@@ -236,15 +236,10 @@ static CYTHON_INLINE int __Pyx_IterFinish(void) {
} }
/////////////// DictGetItem.proto /////////////// /////////////// DictGetItem.proto ///////////////
//@requires: RaiseNoneIndexingError
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) { static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
PyObject *value; PyObject *value;
if (unlikely(d == Py_None)) {
__Pyx_RaiseNoneIndexingError();
return NULL;
}
value = PyDict_GetItemWithError(d, key); value = PyDict_GetItemWithError(d, key);
if (unlikely(!value)) { if (unlikely(!value)) {
if (!PyErr_Occurred()) if (!PyErr_Occurred())
......
# mode: run
# tag: dict, getitem
cimport cython
def test(dict d, index): def test(dict d, index):
""" """
>>> d = { 1: 10 } >>> d = { 1: 10 }
...@@ -25,11 +30,6 @@ def test(dict d, index): ...@@ -25,11 +30,6 @@ def test(dict d, index):
""" """
return d[index] return d[index]
cdef class Subscriptable:
def __getitem__(self, key):
return key
def getitem_in_condition(dict d, key, expected_result): def getitem_in_condition(dict d, key, expected_result):
""" """
>>> d = dict(a=1, b=2) >>> d = dict(a=1, b=2)
...@@ -37,3 +37,20 @@ def getitem_in_condition(dict d, key, expected_result): ...@@ -37,3 +37,20 @@ def getitem_in_condition(dict d, key, expected_result):
True True
""" """
return d[key] is expected_result or d[key] == expected_result 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