cython-devel

changeset 1169:30b7a6c2c7c1

isinstance uses PyObject_TypeCheck when second argument is a type
author Robert Bradshaw <robertwb@math.washington.edu>
date Mon Sep 22 23:45:08 2008 -0700 (3 years ago)
parents d8031111e77c
children 30eb7436ba25
files Cython/Compiler/Builtin.py Cython/Compiler/Main.py Cython/Compiler/Optimize.py Cython/Compiler/Symtab.py Cython/Includes/python_object.pxd
line diff
1.1 --- a/Cython/Compiler/Builtin.py Mon Sep 22 22:08:56 2008 -0700 1.2 +++ b/Cython/Compiler/Builtin.py Mon Sep 22 23:45:08 2008 -0700 1.3 @@ -181,7 +181,8 @@ 1.4 init_builtin_funcs() 1.5 init_builtin_types() 1.6 init_builtin_structs() 1.7 - global list_type, tuple_type, dict_type, unicode_type 1.8 + global list_type, tuple_type, dict_type, unicode_type, type_type 1.9 + type_type = builtin_scope.lookup('type').type 1.10 list_type = builtin_scope.lookup('list').type 1.11 tuple_type = builtin_scope.lookup('tuple').type 1.12 dict_type = builtin_scope.lookup('dict').type
2.1 --- a/Cython/Compiler/Main.py Mon Sep 22 22:08:56 2008 -0700 2.2 +++ b/Cython/Compiler/Main.py Mon Sep 22 23:45:08 2008 -0700 2.3 @@ -81,7 +81,7 @@ 2.4 from ParseTreeTransforms import CreateClosureClasses, MarkClosureVisitor, DecoratorTransform 2.5 from ParseTreeTransforms import InterpretCompilerDirectives 2.6 from AutoDocTransforms import EmbedSignature 2.7 - from Optimize import FlattenInListTransform, SwitchTransform, OptimizeRefcounting 2.8 + from Optimize import FlattenInListTransform, SwitchTransform, FinalOptimizePhase 2.9 from Buffer import IntroduceBufferAuxiliaryVars 2.10 from ModuleNode import check_c_classes 2.11 2.12 @@ -106,7 +106,7 @@ 2.13 _check_c_classes, 2.14 AnalyseExpressionsTransform(self), 2.15 SwitchTransform(), 2.16 - OptimizeRefcounting(self), 2.17 + FinalOptimizePhase(self), 2.18 # SpecialFunctions(self), 2.19 # CreateClosureClasses(context), 2.20 ]
3.1 --- a/Cython/Compiler/Optimize.py Mon Sep 22 22:08:56 2008 -0700 3.2 +++ b/Cython/Compiler/Optimize.py Mon Sep 22 23:45:08 2008 -0700 3.3 @@ -140,7 +140,15 @@ 3.4 return node 3.5 3.6 3.7 -class OptimizeRefcounting(Visitor.CythonTransform): 3.8 +class FinalOptimizePhase(Visitor.CythonTransform): 3.9 + """ 3.10 + This visitor handles several commuting optimizations, and is run 3.11 + just before the C code generation phase. 3.12 + 3.13 + The optimizations currently implemented in this class are: 3.14 + - Eliminate None assignment and refcounting for first assignment. 3.15 + - isinstance -> typecheck for cdef types 3.16 + """ 3.17 def visit_SingleAssignmentNode(self, node): 3.18 if node.first: 3.19 lhs = node.lhs 3.20 @@ -152,3 +160,22 @@ 3.21 lhs.skip_assignment_decref = True 3.22 return node 3.23 3.24 + def visit_SimpleCallNode(self, node): 3.25 + self.visitchildren(node) 3.26 + if node.function.type.is_cfunction: 3.27 + if node.function.name == 'isinstance': 3.28 + type_arg = node.args[1] 3.29 + if type_arg.type.is_builtin_type and type_arg.type.name == 'type': 3.30 + object_module = self.context.find_module('python_object') 3.31 + node.function.entry = object_module.lookup('PyObject_TypeCheck') 3.32 + node.function.type = node.function.entry.type 3.33 + PyTypeObjectPtr = PyrexTypes.CPtrType(object_module.lookup('PyTypeObject').type) 3.34 + node.args[1] = ExprNodes.CastNode(node.args[1], PyTypeObjectPtr) 3.35 + # Remove when result_code stuff is put in its proper place... 3.36 + node.function.result_code = node.function.entry.cname 3.37 + node.args[1].result_code = node.args[1].arg.result_as(PyTypeObjectPtr) 3.38 + if node.is_temp: 3.39 + node.allocate_temp(None, node.result_code) 3.40 + else: 3.41 + node.allocate_temp(None) 3.42 + return node
4.1 --- a/Cython/Compiler/Symtab.py Mon Sep 22 22:08:56 2008 -0700 4.2 +++ b/Cython/Compiler/Symtab.py Mon Sep 22 23:45:08 2008 -0700 4.3 @@ -6,8 +6,7 @@ 4.4 from Cython import Utils 4.5 from Errors import warning, error, InternalError 4.6 from StringEncoding import EncodedString 4.7 -import Options 4.8 -import Naming 4.9 +import Options, Naming 4.10 import PyrexTypes 4.11 from PyrexTypes import py_object_type 4.12 import TypeSlots 4.13 @@ -711,7 +710,7 @@ 4.14 entry = self.declare_type(name, type, None, visibility='extern') 4.15 4.16 var_entry = Entry(name = entry.name, 4.17 - type = py_object_type, 4.18 + type = self.lookup('type').type, # make sure "type" is the first type declared... 4.19 pos = entry.pos, 4.20 cname = "((PyObject*)%s)" % entry.type.typeptr_cname) 4.21 var_entry.is_variable = 1 4.22 @@ -1110,8 +1109,9 @@ 4.23 # variable entry attached to it. For the variable entry, 4.24 # we use a read-only C global variable whose name is an 4.25 # expression that refers to the type object. 4.26 + import Builtin 4.27 var_entry = Entry(name = entry.name, 4.28 - type = py_object_type, 4.29 + type = Builtin.type_type, 4.30 pos = entry.pos, 4.31 cname = "((PyObject*)%s)" % entry.type.typeptr_cname) 4.32 var_entry.is_variable = 1
5.1 --- a/Cython/Includes/python_object.pxd Mon Sep 22 22:08:56 2008 -0700 5.2 +++ b/Cython/Includes/python_object.pxd Mon Sep 22 23:45:08 2008 -0700 5.3 @@ -233,7 +233,7 @@ 5.4 # pointer of type PyTypeObject*, except when the incremented 5.5 # reference count is needed. 5.6 5.7 - bint PyObject_TypeCheck(object o, object type) # object o, PyTypeObject *type) 5.8 + bint PyObject_TypeCheck(object o, PyTypeObject *type) 5.9 # Return true if the object o is of type type or a subtype of 5.10 # type. Both parameters must be non-NULL. 5.11