Cython has moved to github.

cython-devel

view Cython/Compiler/Builtin.py @ 1484:4bbc6166052f

optional arguments for list.sort()
author Robert Bradshaw <robertwb@math.washington.edu>
date Sat Dec 13 10:55:30 2008 -0800 (3 years ago)
parents 4105074b6c2e
children eebf2dc3fd55
line source
1 #
2 # Pyrex - Builtin Definitions
3 #
5 from Symtab import BuiltinScope, StructOrUnionScope
6 from Cython.Utils import UtilityCode
7 from TypeSlots import Signature
8 import PyrexTypes
9 import Naming
11 builtin_function_table = [
12 # name, args, return, C API func, py equiv = "*"
13 ('abs', "O", "O", "PyNumber_Absolute"),
14 #('chr', "", "", ""),
15 #('cmp', "", "", "", ""), # int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
16 #('compile', "", "", ""), # PyObject* Py_CompileString( char *str, char *filename, int start)
17 ('delattr', "OO", "r", "PyObject_DelAttr"),
18 ('dir', "O", "O", "PyObject_Dir"),
19 ('divmod', "OO", "O", "PyNumber_Divmod"),
20 ('exec', "OOO", "O", "__Pyx_PyRun"),
21 #('eval', "", "", ""),
22 #('execfile', "", "", ""),
23 #('filter', "", "", ""),
24 ('getattr', "OO", "O", "PyObject_GetAttr"),
25 ('getattr3', "OOO", "O", "__Pyx_GetAttr3", "getattr"),
26 ('hasattr', "OO", "b", "PyObject_HasAttr"),
27 ('hash', "O", "l", "PyObject_Hash"),
28 #('hex', "", "", ""),
29 #('id', "", "", ""),
30 #('input', "", "", ""),
31 ('intern', "s", "O", "__Pyx_InternFromString"),
32 ('isinstance', "OO", "b", "PyObject_IsInstance"),
33 ('issubclass', "OO", "b", "PyObject_IsSubclass"),
34 ('iter', "O", "O", "PyObject_GetIter"),
35 ('len', "O", "Z", "PyObject_Length"),
36 ('locals', "", "O", "__pyx_locals"),
37 #('map', "", "", ""),
38 #('max', "", "", ""),
39 #('min', "", "", ""),
40 #('oct', "", "", ""),
41 # Not worth doing open, when second argument would become mandatory
42 #('open', "ss", "O", "PyFile_FromString"),
43 #('ord', "", "", ""),
44 ('pow', "OOO", "O", "PyNumber_Power"),
45 #('range', "", "", ""),
46 #('raw_input', "", "", ""),
47 #('reduce', "", "", ""),
48 ('reload', "O", "O", "PyImport_ReloadModule"),
49 ('repr', "O", "O", "PyObject_Repr"),
50 #('round', "", "", ""),
51 ('setattr', "OOO", "r", "PyObject_SetAttr"),
52 #('sum', "", "", ""),
53 #('unichr', "", "", ""),
54 #('unicode', "", "", ""),
55 #('vars', "", "", ""),
56 #('zip', "", "", ""),
57 # Can't do these easily until we have builtin type entries.
58 #('typecheck', "OO", "i", "PyObject_TypeCheck", False),
59 #('issubtype', "OO", "i", "PyType_IsSubtype", False),
61 # Put in namespace append optimization.
62 ('__Pyx_PyObject_Append', "OO", "O", "__Pyx_PyObject_Append"),
63 ]
65 # Builtin types
66 # bool
67 # buffer
68 # classmethod
69 # dict
70 # enumerate
71 # file
72 # float
73 # int
74 # list
75 # long
76 # object
77 # property
78 # slice
79 # staticmethod
80 # super
81 # str
82 # tuple
83 # type
84 # xrange
86 builtin_types_table = [
88 ("type", "PyType_Type", []),
90 ("bool", "PyBool_Type", []),
91 ("int", "PyInt_Type", []),
92 ("long", "PyLong_Type", []),
93 ("float", "PyFloat_Type", []),
94 ("complex", "PyComplex_Type", []),
96 ("bytes", "PyBytes_Type", []),
97 ("str", "PyString_Type", []),
98 ("unicode", "PyUnicode_Type", []),
100 ("tuple", "PyTuple_Type", []),
102 ("list", "PyList_Type", [("append", "OO", "i", "PyList_Append"),
103 ("insert", "OZO", "i", "PyList_Insert"),
104 # ("sort", "O", "i", "PyList_Sort"), # has optional arguments
105 ("reverse","O", "i", "PyList_Reverse")]),
107 ("dict", "PyDict_Type", [("items", "O", "O", "PyDict_Items"),
108 ("keys", "O", "O", "PyDict_Keys"),
109 ("values","O", "O", "PyDict_Values"),
110 ("copy", "O", "O", "PyDict_Copy")]),
112 ("slice", "PySlice_Type", []),
113 ("file", "PyFile_Type", []),
115 ("set", "PySet_Type", [("clear", "O", "i", "PySet_Clear"),
116 ("discard", "OO", "i", "PySet_Discard"),
117 ("add", "OO", "i", "PySet_Add"),
118 ("pop", "O", "O", "PySet_Pop")]),
119 ("frozenset", "PyFrozenSet_Type", []),
120 ]
124 builtin_structs_table = [
125 ('Py_buffer', 'Py_buffer',
126 [("buf", PyrexTypes.c_void_ptr_type),
127 ("obj", PyrexTypes.py_object_type),
128 ("len", PyrexTypes.c_py_ssize_t_type),
129 ("itemsize", PyrexTypes.c_py_ssize_t_type),
130 ("readonly", PyrexTypes.c_bint_type),
131 ("ndim", PyrexTypes.c_int_type),
132 ("format", PyrexTypes.c_char_ptr_type),
133 ("shape", PyrexTypes.c_py_ssize_t_ptr_type),
134 ("strides", PyrexTypes.c_py_ssize_t_ptr_type),
135 ("suboffsets", PyrexTypes.c_py_ssize_t_ptr_type),
136 ("internal", PyrexTypes.c_void_ptr_type),
137 ])
138 ]
140 getattr3_utility_code = UtilityCode(
141 proto = """
142 static PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
143 """,
144 impl = """
145 static PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
146 PyObject *r = PyObject_GetAttr(o, n);
147 if (!r) {
148 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
149 goto bad;
150 PyErr_Clear();
151 r = d;
152 Py_INCREF(d);
153 }
154 return r;
155 bad:
156 return 0;
157 }
158 """)
160 pyexec_utility_code = UtilityCode(
161 proto = """
162 static PyObject* __Pyx_PyRun(PyObject*, PyObject*, PyObject*);
163 """,
164 impl = """
165 static PyObject* __Pyx_PyRun(PyObject* o, PyObject* globals, PyObject* locals) {
166 PyObject* result;
167 PyObject* s = 0;
169 if (!locals && !globals) {
170 globals = PyModule_GetDict(%s);""" % Naming.module_cname + """
171 if (!globals)
172 goto bad;
173 locals = globals;
174 } else if (!locals) {
175 locals = globals;
176 } else if (!globals) {
177 globals = locals;
178 }
180 if (PyUnicode_Check(o)) {
181 s = PyUnicode_AsUTF8String(o);
182 if (!s) goto bad;
183 o = s;
184 } else if (!PyString_Check(o)) {
185 /* FIXME: support file objects and code objects */
186 PyErr_SetString(PyExc_TypeError,
187 "exec currently requires a string as code input.");
188 goto bad;
189 }
191 result = PyRun_String(
192 PyString_AS_STRING(o), Py_file_input, globals, locals);
194 Py_XDECREF(s);
195 return result;
196 bad:
197 Py_XDECREF(s);
198 return 0;
199 }
200 """)
202 intern_utility_code = UtilityCode(
203 proto = """
204 #if PY_MAJOR_VERSION >= 3
205 # define __Pyx_InternFromString(s) PyUnicode_InternFromString(s)
206 #else
207 # define __Pyx_InternFromString(s) PyString_InternFromString(s)
208 #endif
209 """)
211 def put_py23_set_init_utility_code(code, pos):
212 code.putln("#if PY_VERSION_HEX < 0x02040000")
213 code.putln(code.error_goto_if_neg("__Pyx_Py23SetsImport()", pos))
214 code.putln("#endif")
216 py23_set_utility_code = UtilityCode(
217 proto = """
218 #if PY_VERSION_HEX < 0x02050000
219 #ifndef PyAnySet_CheckExact
221 #define PyAnySet_CheckExact(ob) \\
222 ((ob)->ob_type == &PySet_Type || \\
223 (ob)->ob_type == &PyFrozenSet_Type)
225 #define PySet_New(iterable) \\
226 PyObject_CallFunctionObjArgs((PyObject *)&PySet_Type, (iterable), NULL)
228 #define Pyx_PyFrozenSet_New(iterable) \\
229 PyObject_CallFunctionObjArgs((PyObject *)&PyFrozenSet_Type, (iterable), NULL)
231 #define PySet_Size(anyset) \\
232 PyObject_Size((anyset))
234 #define PySet_Contains(anyset, key) \\
235 PySequence_Contains((anyset), (key))
237 #define PySet_Pop(set) \\
238 PyObject_CallMethod(set, "pop", NULL)
240 static INLINE int PySet_Clear(PyObject *set) {
241 PyObject *ret = PyObject_CallMethod(set, "clear", NULL);
242 if (!ret) return -1;
243 Py_DECREF(ret); return 0;
244 }
246 static INLINE int PySet_Discard(PyObject *set, PyObject *key) {
247 PyObject *ret = PyObject_CallMethod(set, "discard", "O", key);
248 if (!ret) return -1;
249 Py_DECREF(ret); return 0;
250 }
252 static INLINE int PySet_Add(PyObject *set, PyObject *key) {
253 PyObject *ret = PyObject_CallMethod(set, "add", "O", key);
254 if (!ret) return -1;
255 Py_DECREF(ret); return 0;
256 }
258 #endif /* PyAnySet_CheckExact (<= Py2.4) */
260 #if PY_VERSION_HEX < 0x02040000
261 #ifndef Py_SETOBJECT_H
262 #define Py_SETOBJECT_H
264 static PyTypeObject *__Pyx_PySet_Type = NULL;
265 static PyTypeObject *__Pyx_PyFrozenSet_Type = NULL;
267 #define PySet_Type (*__Pyx_PySet_Type)
268 #define PyFrozenSet_Type (*__Pyx_PyFrozenSet_Type)
270 #define PyAnySet_Check(ob) \\
271 (PyAnySet_CheckExact(ob) || \\
272 PyType_IsSubtype((ob)->ob_type, &PySet_Type) || \\
273 PyType_IsSubtype((ob)->ob_type, &PyFrozenSet_Type))
275 #define PyFrozenSet_CheckExact(ob) ((ob)->ob_type == &PyFrozenSet_Type)
277 static int __Pyx_Py23SetsImport(void) {
278 PyObject *sets=0, *Set=0, *ImmutableSet=0;
280 sets = PyImport_ImportModule("sets");
281 if (!sets) goto bad;
282 Set = PyObject_GetAttrString(sets, "Set");
283 if (!Set) goto bad;
284 ImmutableSet = PyObject_GetAttrString(sets, "ImmutableSet");
285 if (!ImmutableSet) goto bad;
286 Py_DECREF(sets);
288 __Pyx_PySet_Type = (PyTypeObject*) Set;
289 __Pyx_PyFrozenSet_Type = (PyTypeObject*) ImmutableSet;
291 return 0;
293 bad:
294 Py_XDECREF(sets);
295 Py_XDECREF(Set);
296 Py_XDECREF(ImmutableSet);
297 return -1;
298 }
300 #else
301 static int __Pyx_Py23SetsImport(void) { return 0; }
302 #endif /* !Py_SETOBJECT_H */
303 #endif /* < Py2.4 */
304 #endif /* < Py2.5 */
305 """,
306 init = put_py23_set_init_utility_code,
307 cleanup = """
308 #if PY_VERSION_HEX < 0x02040000
309 Py_XDECREF(__Pyx_PySet_Type); __Pyx_PySet_Type = NULL;
310 Py_XDECREF(__Pyx_PyFrozenSet_Type); __Pyx_PyFrozenSet_Type = NULL;
311 #endif /* < Py2.4 */
312 """)
314 builtin_utility_code = {
315 'exec' : pyexec_utility_code,
316 'getattr3' : getattr3_utility_code,
317 'intern' : intern_utility_code,
318 'set' : py23_set_utility_code,
319 'frozenset' : py23_set_utility_code,
320 }
322 builtin_scope = BuiltinScope()
324 def declare_builtin_func(name, args, ret, cname, py_equiv = "*"):
325 sig = Signature(args, ret)
326 type = sig.function_type()
327 utility = builtin_utility_code.get(name)
328 builtin_scope.declare_builtin_cfunction(name, type, cname, py_equiv, utility)
330 def init_builtin_funcs():
331 for desc in builtin_function_table:
332 declare_builtin_func(*desc)
334 def init_builtin_types():
335 for name, cname, funcs in builtin_types_table:
336 utility = builtin_utility_code.get(name)
337 the_type = builtin_scope.declare_builtin_type(name, cname, utility)
338 for name, args, ret, cname in funcs:
339 sig = Signature(args, ret)
340 the_type.scope.declare_cfunction(name, sig.function_type(), None, cname)
342 def init_builtin_structs():
343 for name, cname, attribute_types in builtin_structs_table:
344 scope = StructOrUnionScope(name)
345 for attribute_name, attribute_type in attribute_types:
346 scope.declare_var(
347 attribute_name, attribute_type, None, attribute_name)
348 builtin_scope.declare_struct_or_union(
349 name, "struct", scope, 1, None, cname = cname)
351 def init_builtins():
352 init_builtin_funcs()
353 init_builtin_types()
354 init_builtin_structs()
355 global list_type, tuple_type, dict_type, set_type, unicode_type, type_type
356 type_type = builtin_scope.lookup('type').type
357 list_type = builtin_scope.lookup('list').type
358 tuple_type = builtin_scope.lookup('tuple').type
359 dict_type = builtin_scope.lookup('dict').type
360 set_type = builtin_scope.lookup('set').type
361 unicode_type = builtin_scope.lookup('unicode').type
363 init_builtins()