-devel mergeback
[cython.git] / Cython / Compiler / Builtin.py
1 #
2 #   Pyrex - Builtin Definitions
3 #
4
5 from Symtab import BuiltinScope, StructOrUnionScope
6 from Code import UtilityCode
7 from TypeSlots import Signature
8 import PyrexTypes
9 import Naming
10
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"),   # optimised later on
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     ('type',       "O",    "O",     "PyObject_Type"),
54     #('unichr',    "",     "",      ""),
55     #('unicode',   "",     "",      ""),
56     #('vars',      "",     "",      ""),
57     #('zip',       "",     "",      ""),
58     #  Can't do these easily until we have builtin type entries.
59     #('typecheck',  "OO",   "i",     "PyObject_TypeCheck", False),
60     #('issubtype',  "OO",   "i",     "PyType_IsSubtype",   False),
61
62     # Put in namespace append optimization.
63     ('__Pyx_PyObject_Append', "OO",  "O",     "__Pyx_PyObject_Append"),
64 ]
65
66 # Builtin types
67 #  bool
68 #  buffer
69 #  classmethod
70 #  dict
71 #  enumerate
72 #  file
73 #  float
74 #  int
75 #  list
76 #  long
77 #  object
78 #  property
79 #  slice
80 #  staticmethod
81 #  super
82 #  str
83 #  tuple
84 #  type
85 #  xrange
86
87 builtin_types_table = [
88
89     ("type",    "PyType_Type",     []),
90
91     ("bool",    "PyBool_Type",     []),
92     ("int",     "PyInt_Type",      []),
93     ("long",    "PyLong_Type",     []),
94     ("float",   "PyFloat_Type",    []),
95     
96 # Until we have a way to access attributes of a type, 
97 # we don't want to make this one builtin.    
98 #    ("complex", "PyComplex_Type",  []),
99
100     ("bytes",   "PyBytes_Type",    []),
101     ("str",     "PyString_Type",   []),
102     ("unicode", "PyUnicode_Type",  []),
103
104     ("tuple",   "PyTuple_Type",    []),
105
106     ("list",    "PyList_Type",     [("insert", "OZO",  "i", "PyList_Insert")]),
107
108     ("dict",    "PyDict_Type",     [("items", "O",   "O", "PyDict_Items"),
109                                     ("keys",  "O",   "O", "PyDict_Keys"),
110                                     ("values","O",   "O", "PyDict_Values"),
111                                     ("copy",  "O",   "O", "PyDict_Copy")]),
112
113     ("slice",   "PySlice_Type",    []),
114     ("file",    "PyFile_Type",     []),
115
116     ("set",       "PySet_Type",    [("clear",   "O",  "i", "PySet_Clear"), 
117                                     ("discard", "OO", "i", "PySet_Discard"),
118                                     ("add",     "OO", "i", "PySet_Add"),
119                                     ("pop",     "O",  "O", "PySet_Pop")]),
120     ("frozenset", "PyFrozenSet_Type", []),
121 ]
122
123
124         
125 builtin_structs_table = [
126     ('Py_buffer', 'Py_buffer',
127      [("buf",        PyrexTypes.c_void_ptr_type),
128       ("obj",        PyrexTypes.py_object_type),
129       ("len",        PyrexTypes.c_py_ssize_t_type),
130       ("itemsize",   PyrexTypes.c_py_ssize_t_type),
131       ("readonly",   PyrexTypes.c_bint_type),
132       ("ndim",       PyrexTypes.c_int_type),
133       ("format",     PyrexTypes.c_char_ptr_type),
134       ("shape",      PyrexTypes.c_py_ssize_t_ptr_type),
135       ("strides",    PyrexTypes.c_py_ssize_t_ptr_type),
136       ("suboffsets", PyrexTypes.c_py_ssize_t_ptr_type),
137       ("internal",   PyrexTypes.c_void_ptr_type),
138       ])
139 ]
140
141 getattr3_utility_code = UtilityCode(
142 proto = """
143 static PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
144 """,
145 impl = """
146 static PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
147     PyObject *r = PyObject_GetAttr(o, n);
148     if (!r) {
149         if (!PyErr_ExceptionMatches(PyExc_AttributeError))
150             goto bad;
151         PyErr_Clear();
152         r = d;
153         Py_INCREF(d);
154     }
155     return r;
156 bad:
157     return 0;
158 }
159 """)
160
161 pyexec_utility_code = UtilityCode(
162 proto = """
163 #if PY_VERSION_HEX < 0x02040000
164 #ifndef Py_EVAL_H
165 #include "eval.h"
166 #endif
167 #endif
168 static PyObject* __Pyx_PyRun(PyObject*, PyObject*, PyObject*);
169 """,
170 impl = """
171 static PyObject* __Pyx_PyRun(PyObject* o, PyObject* globals, PyObject* locals) {
172     PyObject* result;
173     PyObject* s = 0;
174     char *code = 0;
175
176     if (!globals || globals == Py_None) {
177         globals = PyModule_GetDict(%s);""" % Naming.module_cname + """
178         if (!globals)
179             goto bad;
180     } else if (!PyDict_Check(globals)) {
181         PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s",
182                      globals->ob_type->tp_name);
183         goto bad;
184     }
185     if (!locals || locals == Py_None) {
186         locals = globals;
187     }
188
189
190     if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
191         PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
192     }
193
194     if (PyCode_Check(o)) {
195         if (PyCode_GetNumFree((PyCodeObject *)o) > 0) {
196             PyErr_SetString(PyExc_TypeError,
197                 "code object passed to exec() may not contain free variables");
198             goto bad;
199         }
200         result = PyEval_EvalCode((PyCodeObject *)o, globals, locals);
201     } else {
202         PyCompilerFlags cf;
203         cf.cf_flags = 0;
204         if (PyUnicode_Check(o)) {
205             cf.cf_flags = PyCF_SOURCE_IS_UTF8;
206             s = PyUnicode_AsUTF8String(o);
207             if (!s) goto bad;
208             o = s;
209         #if PY_MAJOR_VERSION >= 3
210         } else if (!PyBytes_Check(o)) {
211         #else
212         } else if (!PyString_Check(o)) {
213         #endif
214             PyErr_SetString(PyExc_TypeError,
215                 "exec: arg 1 must be string, bytes or code object");
216             goto bad;
217         }
218         #if PY_MAJOR_VERSION >= 3
219         code = PyBytes_AS_STRING(o);
220         #else
221         code = PyString_AS_STRING(o);
222         #endif
223         if (PyEval_MergeCompilerFlags(&cf)) {
224             result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
225         } else {
226             result = PyRun_String(code, Py_file_input, globals, locals);
227         }
228         Py_XDECREF(s);
229     }
230
231     return result;
232 bad:
233     Py_XDECREF(s);
234     return 0;
235 }
236 """)
237
238 intern_utility_code = UtilityCode(
239 proto = """
240 #if PY_MAJOR_VERSION >= 3
241 #  define __Pyx_InternFromString(s) PyUnicode_InternFromString(s)
242 #else
243 #  define __Pyx_InternFromString(s) PyString_InternFromString(s)
244 #endif
245 """)
246
247 def put_py23_set_init_utility_code(code, pos):
248     code.putln("#if PY_VERSION_HEX < 0x02040000")
249     code.putln(code.error_goto_if_neg("__Pyx_Py23SetsImport()", pos))
250     code.putln("#endif")
251
252 py23_set_utility_code = UtilityCode(
253 proto = """
254 #if PY_VERSION_HEX < 0x02050000
255 #ifndef PyAnySet_CheckExact
256
257 #define PyAnySet_CheckExact(ob) \\
258     ((ob)->ob_type == &PySet_Type || \\
259      (ob)->ob_type == &PyFrozenSet_Type)
260
261 #define PySet_New(iterable) \\
262     PyObject_CallFunctionObjArgs((PyObject *)&PySet_Type, (iterable), NULL)
263
264 #define Pyx_PyFrozenSet_New(iterable) \\
265     PyObject_CallFunctionObjArgs((PyObject *)&PyFrozenSet_Type, (iterable), NULL)
266
267 #define PySet_Size(anyset) \\
268     PyObject_Size((anyset))
269
270 #define PySet_Contains(anyset, key) \\
271     PySequence_Contains((anyset), (key))
272
273 #define PySet_Pop(set) \\
274     PyObject_CallMethod(set, (char *)"pop", NULL)
275
276 static INLINE int PySet_Clear(PyObject *set) {
277     PyObject *ret = PyObject_CallMethod(set, (char *)"clear", NULL);
278     if (!ret) return -1;
279     Py_DECREF(ret); return 0;
280 }
281
282 static INLINE int PySet_Discard(PyObject *set, PyObject *key) {
283     PyObject *ret = PyObject_CallMethod(set, (char *)"discard", (char *)"O", key);
284     if (!ret) return -1;
285     Py_DECREF(ret); return 0;
286 }
287
288 static INLINE int PySet_Add(PyObject *set, PyObject *key) {
289     PyObject *ret = PyObject_CallMethod(set, (char *)"add", (char *)"O", key);
290     if (!ret) return -1;
291     Py_DECREF(ret); return 0;
292 }
293
294 #endif /* PyAnySet_CheckExact (<= Py2.4) */
295
296 #if PY_VERSION_HEX < 0x02040000
297 #ifndef Py_SETOBJECT_H
298 #define Py_SETOBJECT_H
299
300 static PyTypeObject *__Pyx_PySet_Type = NULL;
301 static PyTypeObject *__Pyx_PyFrozenSet_Type = NULL;
302
303 #define PySet_Type (*__Pyx_PySet_Type)
304 #define PyFrozenSet_Type (*__Pyx_PyFrozenSet_Type)
305
306 #define PyAnySet_Check(ob) \\
307     (PyAnySet_CheckExact(ob) || \\
308      PyType_IsSubtype((ob)->ob_type, &PySet_Type) || \\
309      PyType_IsSubtype((ob)->ob_type, &PyFrozenSet_Type))
310
311 #define PyFrozenSet_CheckExact(ob) ((ob)->ob_type == &PyFrozenSet_Type)
312
313 static int __Pyx_Py23SetsImport(void) {
314     PyObject *sets=0, *Set=0, *ImmutableSet=0;
315
316     sets = PyImport_ImportModule((char *)"sets");
317     if (!sets) goto bad;
318     Set = PyObject_GetAttrString(sets, (char *)"Set");
319     if (!Set) goto bad;
320     ImmutableSet = PyObject_GetAttrString(sets, (char *)"ImmutableSet");
321     if (!ImmutableSet) goto bad;
322     Py_DECREF(sets);
323
324     __Pyx_PySet_Type       = (PyTypeObject*) Set;
325     __Pyx_PyFrozenSet_Type = (PyTypeObject*) ImmutableSet;
326
327     return 0;
328
329  bad:
330     Py_XDECREF(sets);
331     Py_XDECREF(Set);
332     Py_XDECREF(ImmutableSet);
333     return -1;
334 }
335
336 #else
337 static int __Pyx_Py23SetsImport(void) { return 0; }
338 #endif /* !Py_SETOBJECT_H */
339 #endif /* < Py2.4  */
340 #endif /* < Py2.5  */
341 """,
342 init = put_py23_set_init_utility_code,
343 cleanup = """
344 #if PY_VERSION_HEX < 0x02040000
345 Py_XDECREF(__Pyx_PySet_Type); __Pyx_PySet_Type = NULL;
346 Py_XDECREF(__Pyx_PyFrozenSet_Type); __Pyx_PyFrozenSet_Type = NULL;
347 #endif /* < Py2.4  */
348 """)
349
350 builtin_utility_code = {
351     'exec'      : pyexec_utility_code,
352     'getattr3'  : getattr3_utility_code,
353     'intern'    : intern_utility_code,
354     'set'       : py23_set_utility_code,
355     'frozenset' : py23_set_utility_code,
356 }
357
358 builtin_scope = BuiltinScope()
359
360 def declare_builtin_func(name, args, ret, cname, py_equiv = "*"):
361     sig = Signature(args, ret)
362     type = sig.function_type()
363     utility = builtin_utility_code.get(name)
364     builtin_scope.declare_builtin_cfunction(name, type, cname, py_equiv, utility)
365
366 def init_builtin_funcs():
367     for desc in builtin_function_table:
368         declare_builtin_func(*desc)
369
370 builtin_types = {}
371
372 def init_builtin_types():
373     global builtin_types
374     for name, cname, funcs in builtin_types_table:
375         utility = builtin_utility_code.get(name)
376         the_type = builtin_scope.declare_builtin_type(name, cname, utility)
377         builtin_types[name] = the_type
378         for name, args, ret, cname in funcs:
379             sig = Signature(args, ret)
380             the_type.scope.declare_cfunction(name, sig.function_type(), None, cname)
381
382 def init_builtin_structs():
383     for name, cname, attribute_types in builtin_structs_table:
384         scope = StructOrUnionScope(name)
385         for attribute_name, attribute_type in attribute_types:
386             scope.declare_var(
387                 attribute_name, attribute_type, None, attribute_name)
388         builtin_scope.declare_struct_or_union(
389             name, "struct", scope, 1, None, cname = cname)
390
391 def init_builtins():
392     init_builtin_funcs()
393     init_builtin_types()
394     init_builtin_structs()
395     global list_type, tuple_type, dict_type, set_type, bytes_type, unicode_type, type_type
396     type_type  = builtin_scope.lookup('type').type
397     list_type  = builtin_scope.lookup('list').type
398     tuple_type = builtin_scope.lookup('tuple').type
399     dict_type  = builtin_scope.lookup('dict').type
400     set_type   = builtin_scope.lookup('set').type
401     bytes_type = builtin_scope.lookup('bytes').type
402     unicode_type = builtin_scope.lookup('unicode').type
403
404 init_builtins()