From 78ed7c3a28f9d368172d5879debe198e77d0c220 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Fri, 23 Feb 2007 00:18:05 -0800 Subject: [PATCH] Figured out how to use the Python/C API for some builtin functions (such as len) to avoid python calling conventions. --- Cython/Compiler/ExprNodes.py | 4 +--- Cython/Compiler/PyrexTypes.py | 2 +- Cython/Compiler/Symtab.py | 25 +++++++++++++++++++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index cdf8ff56..3fdbd489 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -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, diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index c343e5f2..aca569e9 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -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: diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index f247021f..5368dffc 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -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 -- 2.26.2