merge getattr() optimisation into builtin function support by overriding its signature
authorStefan Behnel <scoder@users.berlios.de>
Wed, 3 Nov 2010 20:11:54 +0000 (21:11 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Wed, 3 Nov 2010 20:11:54 +0000 (21:11 +0100)
Cython/Compiler/Builtin.py
Cython/Compiler/Optimize.py

index 2f6b2dee8830089d19e4ccfe49a7310260c6d785..63609c2ba83d66fe813662ab3618e3d7e99ca8b3 100644 (file)
@@ -22,8 +22,8 @@ builtin_function_table = [
     #('eval',      "",     "",      ""),
     #('execfile',  "",     "",      ""),
     #('filter',    "",     "",      ""),
-    #('getattr',    "OO",   "O",     "PyObject_GetAttr"),   # optimised later on
-    ('getattr3',   "OOO",  "O",     "__Pyx_GetAttr3",       "getattr"),
+    ('getattr',    "OO",   "O",     "PyObject_GetAttr"),   # for 3 arguments, see code further down
+    ('getattr3',   "OOO",  "O",     "__Pyx_GetAttr3",       "getattr"), # Pyrex compatibility
     ('hasattr',    "OO",   "b",     "PyObject_HasAttr"),
     ('hash',       "O",    "l",     "PyObject_Hash"),
     #('hex',       "",     "",      ""),
@@ -400,6 +400,16 @@ def init_builtin_funcs():
     for desc in builtin_function_table:
         declare_builtin_func(*desc)
 
+    # getattr with 3 args
+    PyObject_GetAttr3_func_type = PyrexTypes.CFuncType(
+        PyrexTypes.py_object_type, [
+            PyrexTypes.CFuncTypeArg("object", PyrexTypes.py_object_type, None),
+            PyrexTypes.CFuncTypeArg("attr_name", PyrexTypes.py_object_type, None),
+            PyrexTypes.CFuncTypeArg("default", PyrexTypes.py_object_type, None),
+            ])
+    builtin_scope.declare_builtin_cfunction('getattr', PyObject_GetAttr3_func_type,
+                                            '__Pyx_GetAttr3', 'getattr', getattr3_utility_code)
+
 builtin_types = {}
 
 def init_builtin_types():
index 04f741f3b46a8ce15f00bc2d4aae1b39a5ec2a88..e97aea8888053a378c8cbdd6a78ffb2413a352ca 100644 (file)
@@ -1819,39 +1819,6 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
 
     ### builtin functions
 
-    PyObject_GetAttr2_func_type = PyrexTypes.CFuncType(
-        PyrexTypes.py_object_type, [
-            PyrexTypes.CFuncTypeArg("object", PyrexTypes.py_object_type, None),
-            PyrexTypes.CFuncTypeArg("attr_name", PyrexTypes.py_object_type, None),
-            ])
-
-    PyObject_GetAttr3_func_type = PyrexTypes.CFuncType(
-        PyrexTypes.py_object_type, [
-            PyrexTypes.CFuncTypeArg("object", PyrexTypes.py_object_type, None),
-            PyrexTypes.CFuncTypeArg("attr_name", PyrexTypes.py_object_type, None),
-            PyrexTypes.CFuncTypeArg("default", PyrexTypes.py_object_type, None),
-            ])
-
-    def _handle_simple_function_getattr(self, node, pos_args):
-        """Replace 2/3 argument forms of getattr() by C-API calls.
-        """
-        if len(pos_args) == 2:
-            return ExprNodes.PythonCapiCallNode(
-                node.pos, "PyObject_GetAttr", self.PyObject_GetAttr2_func_type,
-                args = pos_args,
-                may_return_none = True,
-                is_temp = node.is_temp)
-        elif len(pos_args) == 3:
-            return ExprNodes.PythonCapiCallNode(
-                node.pos, "__Pyx_GetAttr3", self.PyObject_GetAttr3_func_type,
-                args = pos_args,
-                may_return_none = True,
-                is_temp = node.is_temp,
-                utility_code = Builtin.getattr3_utility_code)
-        else:
-            self._error_wrong_arg_count('getattr', node, pos_args, '2 or 3')
-        return node
-
     Pyx_strlen_func_type = PyrexTypes.CFuncType(
         PyrexTypes.c_size_t_type, [
             PyrexTypes.CFuncTypeArg("bytes", PyrexTypes.c_char_ptr_type, None)