Cython has moved to github.

cython-devel

view Cython/Compiler/Builtin.py @ 1187:897597836a72

[Cython] PATCH: population of builtin types for 'isinstance' optimization
author "Lisandro Dalcin" <dalcinl@gmail.com>
date Tue Sep 30 11:14:06 2008 -0700 (3 years ago)
parents 3152381ccf89
children ea91c6d40b4d
line source
1 #
2 # Pyrex - Builtin Definitions
3 #
5 from Symtab import BuiltinScope, StructOrUnionScope
6 from TypeSlots import Signature
7 import PyrexTypes
9 builtin_function_table = [
10 # name, args, return, C API func, py equiv = "*"
11 ('abs', "O", "O", "PyNumber_Absolute"),
12 #('chr', "", "", ""),
13 #('cmp', "", "", "", ""), # int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
14 #('compile', "", "", ""), # PyObject* Py_CompileString( char *str, char *filename, int start)
15 ('delattr', "OO", "r", "PyObject_DelAttr"),
16 ('dir', "O", "O", "PyObject_Dir"),
17 ('divmod', "OO", "O", "PyNumber_Divmod"),
18 #('eval', "", "", ""),
19 #('execfile', "", "", ""),
20 #('filter', "", "", ""),
21 ('getattr', "OO", "O", "PyObject_GetAttr"),
22 ('getattr3', "OOO", "O", "__Pyx_GetAttr3", "getattr"),
23 ('hasattr', "OO", "b", "PyObject_HasAttr"),
24 ('hash', "O", "l", "PyObject_Hash"),
25 #('hex', "", "", ""),
26 #('id', "", "", ""),
27 #('input', "", "", ""),
28 ('intern', "s", "O", "__Pyx_InternFromString"),
29 ('isinstance', "OO", "b", "PyObject_IsInstance"),
30 ('issubclass', "OO", "b", "PyObject_IsSubclass"),
31 ('iter', "O", "O", "PyObject_GetIter"),
32 ('len', "O", "Z", "PyObject_Length"),
33 ('locals', "", "O", "__pyx_locals"),
34 #('map', "", "", ""),
35 #('max', "", "", ""),
36 #('min', "", "", ""),
37 #('oct', "", "", ""),
38 # Not worth doing open, when second argument would become mandatory
39 #('open', "ss", "O", "PyFile_FromString"),
40 #('ord', "", "", ""),
41 ('pow', "OOO", "O", "PyNumber_Power"),
42 #('range', "", "", ""),
43 #('raw_input', "", "", ""),
44 #('reduce', "", "", ""),
45 ('reload', "O", "O", "PyImport_ReloadModule"),
46 ('repr', "O", "O", "PyObject_Repr"),
47 #('round', "", "", ""),
48 ('setattr', "OOO", "r", "PyObject_SetAttr"),
49 #('sum', "", "", ""),
50 #('unichr', "", "", ""),
51 #('unicode', "", "", ""),
52 #('vars', "", "", ""),
53 #('zip', "", "", ""),
54 # Can't do these easily until we have builtin type entries.
55 #('typecheck', "OO", "i", "PyObject_TypeCheck", False),
56 #('issubtype', "OO", "i", "PyType_IsSubtype", False),
58 # Put in namespace append optimization.
59 ('__Pyx_PyObject_Append', "OO", "O", "__Pyx_PyObject_Append"),
60 ]
62 # Builtin types
63 # bool
64 # buffer
65 # classmethod
66 # dict
67 # enumerate
68 # file
69 # float
70 # int
71 # list
72 # long
73 # object
74 # property
75 # slice
76 # staticmethod
77 # super
78 # str
79 # tuple
80 # type
81 # xrange
83 builtin_types_table = [
85 ("type", "PyType_Type", []),
87 ("bool", "PyBool_Type", []),
88 ("int", "PyInt_Type", []),
89 ("long", "PyLong_Type", []),
90 ("float", "PyFloat_Type", []),
91 ("complex", "PyComplex_Type", []),
93 ("bytes", "PyBytes_Type", []),
94 ("str", "PyString_Type", []),
95 ("unicode", "PyUnicode_Type", []),
97 ("tuple", "PyTuple_Type", []),
99 ("list", "PyList_Type", [("append", "OO", "i", "PyList_Append"),
100 ("insert", "OiO", "i", "PyList_Insert"),
101 ("sort", "O", "i", "PyList_Sort"),
102 ("reverse","O", "i", "PyList_Reverse")]),
104 ("dict", "PyDict_Type", [("items", "O", "O", "PyDict_Items"),
105 ("keys", "O", "O", "PyDict_Keys"),
106 ("values","O", "O", "PyDict_Values")]),
108 ("set", "PySet_Type", []),
109 ("frozenset", "PyFrozenSet_Type", []),
111 ("slice", "PySlice_Type", []),
112 ("file", "PyFile_Type", []),
114 ]
116 builtin_structs_table = [
117 ('Py_buffer', 'Py_buffer',
118 [("buf", PyrexTypes.c_void_ptr_type),
119 ("obj", PyrexTypes.py_object_type),
120 ("len", PyrexTypes.c_py_ssize_t_type),
121 ("itemsize", PyrexTypes.c_py_ssize_t_type),
122 ("readonly", PyrexTypes.c_bint_type),
123 ("ndim", PyrexTypes.c_int_type),
124 ("format", PyrexTypes.c_char_ptr_type),
125 ("shape", PyrexTypes.c_py_ssize_t_ptr_type),
126 ("strides", PyrexTypes.c_py_ssize_t_ptr_type),
127 ("suboffsets", PyrexTypes.c_py_ssize_t_ptr_type),
128 ("internal", PyrexTypes.c_void_ptr_type),
129 ])
130 ]
132 getattr3_utility_code = ["""
133 static PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
134 ""","""
135 static PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
136 PyObject *r = PyObject_GetAttr(o, n);
137 if (!r) {
138 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
139 goto bad;
140 PyErr_Clear();
141 r = d;
142 Py_INCREF(d);
143 }
144 return r;
145 bad:
146 return 0;
147 }
148 """]
150 intern_utility_code = ["""
151 #if PY_MAJOR_VERSION >= 3
152 # define __Pyx_InternFromString(s) PyUnicode_InternFromString(s)
153 #else
154 # define __Pyx_InternFromString(s) PyString_InternFromString(s)
155 #endif
156 ""","""
157 """]
159 builtin_utility_code = {
160 'getattr3': getattr3_utility_code,
161 'intern' : intern_utility_code,
162 }
164 builtin_scope = BuiltinScope()
166 def declare_builtin_func(name, args, ret, cname, py_equiv = "*"):
167 sig = Signature(args, ret)
168 type = sig.function_type()
169 utility = builtin_utility_code.get(name)
170 builtin_scope.declare_builtin_cfunction(name, type, cname, py_equiv, utility)
172 def init_builtin_funcs():
173 for desc in builtin_function_table:
174 declare_builtin_func(*desc)
176 def init_builtin_types():
177 for name, cname, funcs in builtin_types_table:
178 the_type = builtin_scope.declare_builtin_type(name, cname)
179 for name, args, ret, cname in funcs:
180 sig = Signature(args, ret)
181 the_type.scope.declare_cfunction(name, sig.function_type(), None, cname)
183 def init_builtin_structs():
184 for name, cname, attribute_types in builtin_structs_table:
185 scope = StructOrUnionScope(name)
186 for attribute_name, attribute_type in attribute_types:
187 scope.declare_var(
188 attribute_name, attribute_type, None, attribute_name)
189 builtin_scope.declare_struct_or_union(
190 name, "struct", scope, 1, None, cname = cname)
192 def init_builtins():
193 init_builtin_funcs()
194 init_builtin_types()
195 init_builtin_structs()
196 global list_type, tuple_type, dict_type, unicode_type, type_type
197 type_type = builtin_scope.lookup('type').type
198 list_type = builtin_scope.lookup('list').type
199 tuple_type = builtin_scope.lookup('tuple').type
200 dict_type = builtin_scope.lookup('dict').type
201 unicode_type = builtin_scope.lookup('unicode').type
203 init_builtins()