Figured out how to use the Python/C API for some builtin functions (such as len)...
authorRobert Bradshaw <robertwb@math.washington.edu>
Fri, 23 Feb 2007 08:18:05 +0000 (00:18 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Fri, 23 Feb 2007 08:18:05 +0000 (00:18 -0800)
Cython/Compiler/ExprNodes.py
Cython/Compiler/PyrexTypes.py
Cython/Compiler/Symtab.py

index cdf8ff562ff664c8921708e84705d8a202b20e09..3fdbd4891fc126dec833b270e5ef43524ea30221 100644 (file)
@@ -1290,7 +1290,6 @@ class SliceNode(ExprNode):
                 self.result_code,
                 code.error_goto(self.pos)))
 
-
 class SimpleCallNode(ExprNode):
     #  Function call without keyword, * or ** args.
     #
@@ -1440,8 +1439,7 @@ class SimpleCallNode(ExprNode):
                         lhs,
                         rhs,
                         " && ".join(exc_checks),
-                        code.error_goto(self.pos)))
-    
+                        code.error_goto(self.pos)))    
 
 class GeneralCallNode(ExprNode):
     #  General Python function call, including keyword,
index c343e5f24d7ad2f666ecf3ecb44a309a869d26d3..aca569e9a06e8dce59ccca11a3f522781cb68c32 100644 (file)
@@ -484,7 +484,7 @@ class CFuncType(CType):
         self.has_varargs = has_varargs
         self.exception_value = exception_value
         self.exception_check = exception_check
-    
+        
     def __repr__(self):
         arg_reprs = map(repr, self.args)
         if self.has_varargs:
index f247021fc5e3fa45f6f96a1d8a6fcf9b1a7328f8..5368dffc46693344fbcd79f8a2d1d002f6ac0309 100644 (file)
@@ -6,9 +6,7 @@ import re
 from Errors import error, InternalError, warning
 import Options
 import Naming
-from PyrexTypes import c_int_type, \
-    py_object_type, c_char_array_type, \
-    CEnumType, CStructOrUnionType, PyExtensionType
+from PyrexTypes import *
 from TypeSlots import \
     pyfunction_signature, pymethod_signature, \
     get_special_method_signature, get_property_accessor_signature
@@ -447,12 +445,31 @@ class BuiltinScope(Scope):
     
     def __init__(self):
         Scope.__init__(self, "__builtin__", None, None)
+        for name, definition in self.builtin_functions.iteritems():
+            if len(definition) < 4: definition.append(None) # exception_value
+            if len(definition) < 5: definition.append(False) # exception_check
+            cname, type, arg_types, exception_value, exception_check = definition
+            function = CFuncType(type, [CFuncTypeArg("", t, None) for t in arg_types], False, exception_value, exception_check)
+            self.add_cfunction(name, function, None, cname, False)
     
     def declare_builtin(self, name, pos):
         entry = self.declare(name, name, py_object_type, pos)
         entry.is_builtin = 1
         return entry
-    
+        
+    builtin_functions = {
+      "hasattr": ["PyObject_HasAttrString", c_int_type, (py_object_type, c_char_ptr_type)],
+      "cmp":     ["PyObject_Compare", c_int_type, (py_object_type, py_object_type), None, True],
+      "repr":    ["PyObject_Repr", py_object_type, (py_object_type, ), 0],
+      "str":     ["PyObject_Str", py_object_type, (py_object_type, ), 0],
+      "unicode": ["PyObject_Unicode", py_object_type, (py_object_type, ), 0],
+      "isinstance": ["PyObject_IsInstance", c_int_type, (py_object_type, py_object_type), -1],
+      "hash":    ["PyObject_Hash", c_long_type, (py_object_type, ), -1, True],
+      "type":    ["PyObject_Type", py_object_type, (py_object_type, ), 0],
+      "len":     ["PyObject_Size", c_py_ssize_t_type, (py_object_type, ), -1],
+      "dir":     ["PyObject_Dir", py_object_type, (py_object_type, ), 0],
+      "iter":     ["PyObject_GetIter", py_object_type, (py_object_type, ), 0],
+    }
 
 class ModuleScope(Scope):
     # module_name          string             Python name of the module