From 0353b9561d683855bc25e139c05e1b9c8c86e292 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Fri, 8 Nov 2013 13:39:20 +0100 Subject: [PATCH] clean up some code redundancy --- Cython/Compiler/ExprNodes.py | 53 ++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 5c02a8c..8cd9ffb 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -3319,8 +3319,8 @@ class IndexNode(ExprNode): elif self.is_temp: if self.type.is_pyobject: + error_value = 'NULL' if self.index.type.is_int: - index_code = self.index.result() if self.base.type is list_type: function = "__Pyx_GetItemInt_List" elif self.base.type is tuple_type: @@ -3330,58 +3330,47 @@ class IndexNode(ExprNode): code.globalstate.use_utility_code( TempitaUtilityCode.load_cached("GetItemInt", "ObjectHandling.c")) else: - index_code = self.index.py_result() if self.base.type is dict_type: function = "__Pyx_PyDict_GetItem" code.globalstate.use_utility_code( UtilityCode.load_cached("DictGetItem", "ObjectHandling.c")) else: function = "PyObject_GetItem" - code.putln( - "%s = %s(%s, %s%s); if (!%s) %s" % ( - self.result(), - function, - self.base.py_result(), - index_code, - self.extra_index_params(code), - self.result(), - code.error_goto(self.pos))) - code.put_gotref(self.py_result()) elif self.type.is_unicode_char and self.base.type is unicode_type: assert self.index.type.is_int - index_code = self.index.result() function = "__Pyx_GetItemInt_Unicode" + error_value = '(Py_UCS4)-1' code.globalstate.use_utility_code( UtilityCode.load_cached("GetItemIntUnicode", "StringTools.c")) - code.putln( - "%s = %s(%s, %s%s); if (unlikely(%s == (Py_UCS4)-1)) %s;" % ( - self.result(), - function, - self.base.py_result(), - index_code, - self.extra_index_params(code), - self.result(), - code.error_goto(self.pos))) elif self.base.type is bytearray_type: assert self.index.type.is_int assert self.type.is_int - index_code = self.index.result() function = "__Pyx_GetItemInt_ByteArray" + error_value = '-1' code.globalstate.use_utility_code( UtilityCode.load_cached("GetItemIntByteArray", "StringTools.c")) - code.putln( - "%s = %s(%s, %s%s); if (unlikely(%s == -1)) %s;" % ( - self.result(), - function, - self.base.py_result(), - index_code, - self.extra_index_params(code), - self.result(), - code.error_goto(self.pos))) else: assert False, "unexpected type %s and base type %s for indexing" % ( self.type, self.base.type) + if self.index.type.is_int: + index_code = self.index.result() + else: + index_code = self.index.py_result() + + code.putln( + "%s = %s(%s, %s%s); if (unlikely(%s == %s)) %s;" % ( + self.result(), + function, + self.base.py_result(), + index_code, + self.extra_index_params(code), + self.result(), + error_value, + code.error_goto(self.pos))) + if self.type.is_pyobject: + code.put_gotref(self.py_result()) + def generate_setitem_code(self, value_code, code): if self.index.type.is_int: if self.base.type is bytearray_type: -- 2.7.4