Cython has moved to github.
cython-devel
view Cython/Compiler/Builtin.py @ 1881:dc7359d237b9
better error when user-declared type conflicts with builtin type (#170)
| author | Robert Bradshaw <robertwb@math.washington.edu> |
|---|---|
| date | Wed Mar 25 15:23:46 2009 -0700 (3 years ago) |
| parents | 249cf0073c3a |
| children | 52b9ed726ad5 |
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 #if PY_MAJOR_VERSION >= 3
185 } else if (!PyBytes_Check(o)) {
186 #else
187 } else if (!PyString_Check(o)) {
188 #endif
189 /* FIXME: support file objects and code objects */
190 PyErr_SetString(PyExc_TypeError,
191 "exec currently requires a string as code input.");
192 goto bad;
193 }
195 result = PyRun_String(
196 #if PY_MAJOR_VERSION >= 3
197 PyBytes_AS_STRING(o),
198 #else
199 PyString_AS_STRING(o),
200 #endif
201 Py_file_input, globals, locals);
203 Py_XDECREF(s);
204 return result;
205 bad:
206 Py_XDECREF(s);
207 return 0;
208 }
209 """)
211 intern_utility_code = UtilityCode(
212 proto = """
213 #if PY_MAJOR_VERSION >= 3
214 # define __Pyx_InternFromString(s) PyUnicode_InternFromString(s)
215 #else
216 # define __Pyx_InternFromString(s) PyString_InternFromString(s)
217 #endif
218 """)
220 def put_py23_set_init_utility_code(code, pos):
221 code.putln("#if PY_VERSION_HEX < 0x02040000")
222 code.putln(code.error_goto_if_neg("__Pyx_Py23SetsImport()", pos))
223 code.putln("#endif")
225 py23_set_utility_code = UtilityCode(
226 proto = """
227 #if PY_VERSION_HEX < 0x02050000
228 #ifndef PyAnySet_CheckExact
230 #define PyAnySet_CheckExact(ob) \\
231 ((ob)->ob_type == &PySet_Type || \\
232 (ob)->ob_type == &PyFrozenSet_Type)
234 #define PySet_New(iterable) \\
235 PyObject_CallFunctionObjArgs((PyObject *)&PySet_Type, (iterable), NULL)
237 #define Pyx_PyFrozenSet_New(iterable) \\
238 PyObject_CallFunctionObjArgs((PyObject *)&PyFrozenSet_Type, (iterable), NULL)
240 #define PySet_Size(anyset) \\
241 PyObject_Size((anyset))
243 #define PySet_Contains(anyset, key) \\
244 PySequence_Contains((anyset), (key))
246 #define PySet_Pop(set) \\
247 PyObject_CallMethod(set, (char *)"pop", NULL)
249 static INLINE int PySet_Clear(PyObject *set) {
250 PyObject *ret = PyObject_CallMethod(set, (char *)"clear", NULL);
251 if (!ret) return -1;
252 Py_DECREF(ret); return 0;
253 }
255 static INLINE int PySet_Discard(PyObject *set, PyObject *key) {
256 PyObject *ret = PyObject_CallMethod(set, (char *)"discard", (char *)"O", key);
257 if (!ret) return -1;
258 Py_DECREF(ret); return 0;
259 }
261 static INLINE int PySet_Add(PyObject *set, PyObject *key) {
262 PyObject *ret = PyObject_CallMethod(set, (char *)"add", (char *)"O", key);
263 if (!ret) return -1;
264 Py_DECREF(ret); return 0;
265 }
267 #endif /* PyAnySet_CheckExact (<= Py2.4) */
269 #if PY_VERSION_HEX < 0x02040000
270 #ifndef Py_SETOBJECT_H
271 #define Py_SETOBJECT_H
273 static PyTypeObject *__Pyx_PySet_Type = NULL;
274 static PyTypeObject *__Pyx_PyFrozenSet_Type = NULL;
276 #define PySet_Type (*__Pyx_PySet_Type)
277 #define PyFrozenSet_Type (*__Pyx_PyFrozenSet_Type)
279 #define PyAnySet_Check(ob) \\
280 (PyAnySet_CheckExact(ob) || \\
281 PyType_IsSubtype((ob)->ob_type, &PySet_Type) || \\
282 PyType_IsSubtype((ob)->ob_type, &PyFrozenSet_Type))
284 #define PyFrozenSet_CheckExact(ob) ((ob)->ob_type == &PyFrozenSet_Type)
286 static int __Pyx_Py23SetsImport(void) {
287 PyObject *sets=0, *Set=0, *ImmutableSet=0;
289 sets = PyImport_ImportModule((char *)"sets");
290 if (!sets) goto bad;
291 Set = PyObject_GetAttrString(sets, (char *)"Set");
292 if (!Set) goto bad;
293 ImmutableSet = PyObject_GetAttrString(sets, (char *)"ImmutableSet");
294 if (!ImmutableSet) goto bad;
295 Py_DECREF(sets);
297 __Pyx_PySet_Type = (PyTypeObject*) Set;
298 __Pyx_PyFrozenSet_Type = (PyTypeObject*) ImmutableSet;
300 return 0;
302 bad:
303 Py_XDECREF(sets);
304 Py_XDECREF(Set);
305 Py_XDECREF(ImmutableSet);
306 return -1;
307 }
309 #else
310 static int __Pyx_Py23SetsImport(void) { return 0; }
311 #endif /* !Py_SETOBJECT_H */
312 #endif /* < Py2.4 */
313 #endif /* < Py2.5 */
314 """,
315 init = put_py23_set_init_utility_code,
316 cleanup = """
317 #if PY_VERSION_HEX < 0x02040000
318 Py_XDECREF(__Pyx_PySet_Type); __Pyx_PySet_Type = NULL;
319 Py_XDECREF(__Pyx_PyFrozenSet_Type); __Pyx_PyFrozenSet_Type = NULL;
320 #endif /* < Py2.4 */
321 """)
323 builtin_utility_code = {
324 'exec' : pyexec_utility_code,
325 'getattr3' : getattr3_utility_code,
326 'intern' : intern_utility_code,
327 'set' : py23_set_utility_code,
328 'frozenset' : py23_set_utility_code,
329 }
331 builtin_scope = BuiltinScope()
333 def declare_builtin_func(name, args, ret, cname, py_equiv = "*"):
334 sig = Signature(args, ret)
335 type = sig.function_type()
336 utility = builtin_utility_code.get(name)
337 builtin_scope.declare_builtin_cfunction(name, type, cname, py_equiv, utility)
339 def init_builtin_funcs():
340 for desc in builtin_function_table:
341 declare_builtin_func(*desc)
343 builtin_types = {}
345 def init_builtin_types():
346 global builtin_types
347 for name, cname, funcs in builtin_types_table:
348 utility = builtin_utility_code.get(name)
349 the_type = builtin_scope.declare_builtin_type(name, cname, utility)
350 builtin_types[name] = the_type
351 for name, args, ret, cname in funcs:
352 sig = Signature(args, ret)
353 the_type.scope.declare_cfunction(name, sig.function_type(), None, cname)
355 def init_builtin_structs():
356 for name, cname, attribute_types in builtin_structs_table:
357 scope = StructOrUnionScope(name)
358 for attribute_name, attribute_type in attribute_types:
359 scope.declare_var(
360 attribute_name, attribute_type, None, attribute_name)
361 builtin_scope.declare_struct_or_union(
362 name, "struct", scope, 1, None, cname = cname)
364 def init_builtins():
365 init_builtin_funcs()
366 init_builtin_types()
367 init_builtin_structs()
368 global list_type, tuple_type, dict_type, set_type, unicode_type, type_type
369 type_type = builtin_scope.lookup('type').type
370 list_type = builtin_scope.lookup('list').type
371 tuple_type = builtin_scope.lookup('tuple').type
372 dict_type = builtin_scope.lookup('dict').type
373 set_type = builtin_scope.lookup('set').type
374 unicode_type = builtin_scope.lookup('unicode').type
376 init_builtins()
