From: Stefan Behnel Date: Wed, 9 Dec 2009 15:43:17 +0000 (+0100) Subject: call Py_TYPE(x) instead of type(x) X-Git-Tag: 0.12.1~48 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=22dda2e45742b90b43815833bb33fdfd8563a289;p=cython.git call Py_TYPE(x) instead of type(x) --- diff --git a/Cython/Compiler/Builtin.py b/Cython/Compiler/Builtin.py index 8d87af99..6f033d1d 100644 --- a/Cython/Compiler/Builtin.py +++ b/Cython/Compiler/Builtin.py @@ -50,7 +50,7 @@ builtin_function_table = [ #('round', "", "", ""), ('setattr', "OOO", "r", "PyObject_SetAttr"), #('sum', "", "", ""), - ('type', "O", "O", "PyObject_Type"), + #('type', "O", "O", "PyObject_Type"), #('unichr', "", "", ""), #('unicode', "", "", ""), #('vars', "", "", ""), diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index f8901731..d5b6b2f1 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -2615,7 +2615,7 @@ class SimpleCallNode(CallNode): func_type.opt_arg_cname(formal_arg.name), actual_arg.result_as(formal_arg.type))) exc_checks = [] - if self.type.is_pyobject: + if self.type.is_pyobject and self.is_temp: exc_checks.append("!%s" % self.result()) else: exc_val = func_type.exception_value diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index 1b1287d8..c6500bbe 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -843,20 +843,6 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform): return replace_in(arg) return node - Pyx_Type_func_type = PyrexTypes.CFuncType( - Builtin.type_type, [ - PyrexTypes.CFuncTypeArg("object", PyrexTypes.py_object_type, None) - ]) - - def _handle_simple_function_type(self, node, pos_args): - if len(pos_args) != 1: - return node - self._inject_capi_function( - node, "__Pyx_Type", - self.Pyx_Type_func_type, - pytype_utility_code) - return node - def _handle_simple_function_float(self, node, pos_args): if len(pos_args) == 0: return ExprNodes.FloatNode(node.pos, value='0.0') @@ -1188,6 +1174,20 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ) return node + Pyx_Type_func_type = PyrexTypes.CFuncType( + Builtin.type_type, [ + PyrexTypes.CFuncTypeArg("object", PyrexTypes.py_object_type, None) + ]) + + def _handle_simple_function_type(self, node, pos_args): + if len(pos_args) != 1: + return node + node = ExprNodes.PythonCapiCallNode( + node.pos, "Py_TYPE", self.Pyx_Type_func_type, + args = pos_args, + is_temp = False) + return ExprNodes.CastNode(node, PyrexTypes.py_object_type) + ### special methods Pyx_tp_new_func_type = PyrexTypes.CFuncType( @@ -1702,17 +1702,6 @@ bad: ) -pytype_utility_code = UtilityCode( -proto = """ -static INLINE PyObject* __Pyx_Type(PyObject* o) { - PyObject* type = (PyObject*) Py_TYPE(o); - Py_INCREF(type); - return type; -} -""" -) - - include_string_h_utility_code = UtilityCode( proto = """ #include diff --git a/tests/run/builtin_type.pyx b/tests/run/builtin_type.pyx new file mode 100644 index 00000000..2f92906a --- /dev/null +++ b/tests/run/builtin_type.pyx @@ -0,0 +1,40 @@ +cimport cython + +@cython.test_assert_path_exists( + '//PythonCapiCallNode/PythonCapiFunctionNode[@cname="Py_TYPE"]') +def get_type_of(a): + """ + >>> get_type_of(object()) is object + True + """ + return type(a) + +@cython.test_assert_path_exists( + '//PythonCapiCallNode/PythonCapiFunctionNode[@cname="Py_TYPE"]') +def get_type_through_local(a): + """ + >>> get_type_of(object()) is object + True + """ + t = type(a) + return t + +@cython.test_assert_path_exists( + '//PythonCapiCallNode/PythonCapiFunctionNode[@cname="Py_TYPE"]') +@cython.test_fail_if_path_exists( + '//PythonCapiCallNode/PythonCapiFunctionNode[@cname="__Pyx_Type"]', + '//NameNode[@name="type"]') +def test_type(a, t): + """ + >>> test_type(object(), object) + True + """ + return type(a) and type(a) is t and type(a) == t + +@cython.test_assert_path_exists('//NameNode[@name="type"]') +def type_type(): + """ + >>> type_type()(object()) is object + True + """ + return type