Fix Py_ssize_t/Py_ssize_t* issues in buffer API signatures by renaming
[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',     "O",    "O",     "__Pyx_Intern"),
32     ('isinstance', "OO",   "b",     "PyObject_IsInstance"),
33     ('issubclass', "OO",   "b",     "PyObject_IsSubclass"),
34     #('iter',       "O",    "O",     "PyObject_GetIter"),   # optimised later on
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 types_that_construct_their_instance = (
124     # some builtin types do not always return an instance of
125     # themselves - these do:
126     'type', 'bool', 'long', 'float', 'bytes', 'unicode', 'tuple', 'list',
127     'dict', 'file', 'set', 'frozenset'
128     # 'str',             # only in Py3.x
129     )
130
131         
132 builtin_structs_table = [
133     ('Py_buffer', 'Py_buffer',
134      [("buf",        PyrexTypes.c_void_ptr_type),
135       ("obj",        PyrexTypes.py_object_type),
136       ("len",        PyrexTypes.c_py_ssize_t_type),
137       ("itemsize",   PyrexTypes.c_py_ssize_t_type),
138       ("readonly",   PyrexTypes.c_bint_type),
139       ("ndim",       PyrexTypes.c_int_type),
140       ("format",     PyrexTypes.c_char_ptr_type),
141       ("shape",      PyrexTypes.c_py_ssize_t_ptr_type),
142       ("strides",    PyrexTypes.c_py_ssize_t_ptr_type),
143       ("suboffsets", PyrexTypes.c_py_ssize_t_ptr_type),
144       ("internal",   PyrexTypes.c_void_ptr_type),
145       ])
146 ]
147
148 getattr3_utility_code = UtilityCode(
149 proto = """
150 static PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
151 """,
152 impl = """
153 static PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
154     PyObject *r = PyObject_GetAttr(o, n);
155     if (!r) {
156         if (!PyErr_ExceptionMatches(PyExc_AttributeError))
157             goto bad;
158         PyErr_Clear();
159         r = d;
160         Py_INCREF(d);
161     }
162     return r;
163 bad:
164     return 0;
165 }
166 """)
167
168 pyexec_utility_code = UtilityCode(
169 proto = """
170 #if PY_VERSION_HEX < 0x02040000
171 #ifndef Py_COMPILE_H
172 #include "compile.h"
173 #endif
174 #ifndef Py_EVAL_H
175 #include "eval.h"
176 #endif
177 #endif
178 static PyObject* __Pyx_PyRun(PyObject*, PyObject*, PyObject*);
179 """,
180 impl = """
181 static PyObject* __Pyx_PyRun(PyObject* o, PyObject* globals, PyObject* locals) {
182     PyObject* result;
183     PyObject* s = 0;
184     char *code = 0;
185
186     if (!globals || globals == Py_None) {
187         globals = PyModule_GetDict(%s);""" % Naming.module_cname + """
188         if (!globals)
189             goto bad;
190     } else if (!PyDict_Check(globals)) {
191         PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s",
192                      globals->ob_type->tp_name);
193         goto bad;
194     }
195     if (!locals || locals == Py_None) {
196         locals = globals;
197     }
198
199
200     if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
201         PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
202     }
203
204     if (PyCode_Check(o)) {
205         if (PyCode_GetNumFree((PyCodeObject *)o) > 0) {
206             PyErr_SetString(PyExc_TypeError,
207                 "code object passed to exec() may not contain free variables");
208             goto bad;
209         }
210         result = PyEval_EvalCode((PyCodeObject *)o, globals, locals);
211     } else {
212         PyCompilerFlags cf;
213         cf.cf_flags = 0;
214         if (PyUnicode_Check(o)) {
215             cf.cf_flags = PyCF_SOURCE_IS_UTF8;
216             s = PyUnicode_AsUTF8String(o);
217             if (!s) goto bad;
218             o = s;
219         #if PY_MAJOR_VERSION >= 3
220         } else if (!PyBytes_Check(o)) {
221         #else
222         } else if (!PyString_Check(o)) {
223         #endif
224             PyErr_SetString(PyExc_TypeError,
225                 "exec: arg 1 must be string, bytes or code object");
226             goto bad;
227         }
228         #if PY_MAJOR_VERSION >= 3
229         code = PyBytes_AS_STRING(o);
230         #else
231         code = PyString_AS_STRING(o);
232         #endif
233         if (PyEval_MergeCompilerFlags(&cf)) {
234             result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
235         } else {
236             result = PyRun_String(code, Py_file_input, globals, locals);
237         }
238         Py_XDECREF(s);
239     }
240
241     return result;
242 bad:
243     Py_XDECREF(s);
244     return 0;
245 }
246 """)
247
248 intern_utility_code = UtilityCode(
249 proto = """
250 static PyObject* __Pyx_Intern(PyObject* s); /* proto */
251 """,
252 impl = '''
253 static PyObject* __Pyx_Intern(PyObject* s) {
254     if (!(likely(PyString_CheckExact(s)))) {
255         PyErr_Format(PyExc_TypeError, "Expected str, got %s", Py_TYPE(s)->tp_name);
256         return 0;
257     }
258     Py_INCREF(s);
259     #if PY_MAJOR_VERSION >= 3
260     PyUnicode_InternInPlace(&s);
261     #else
262     PyString_InternInPlace(&s);
263     #endif
264     return s;
265 }
266 ''')
267
268 def put_py23_set_init_utility_code(code, pos):
269     code.putln("#if PY_VERSION_HEX < 0x02040000")
270     code.putln(code.error_goto_if_neg("__Pyx_Py23SetsImport()", pos))
271     code.putln("#endif")
272
273 py23_set_utility_code = UtilityCode(
274 proto = """
275 #if PY_VERSION_HEX < 0x02050000
276 #ifndef PyAnySet_CheckExact
277
278 #define PyAnySet_CheckExact(ob) \\
279     ((ob)->ob_type == &PySet_Type || \\
280      (ob)->ob_type == &PyFrozenSet_Type)
281
282 #define PySet_New(iterable) \\
283     PyObject_CallFunctionObjArgs((PyObject *)&PySet_Type, (iterable), NULL)
284
285 #define Pyx_PyFrozenSet_New(iterable) \\
286     PyObject_CallFunctionObjArgs((PyObject *)&PyFrozenSet_Type, (iterable), NULL)
287
288 #define PySet_Size(anyset) \\
289     PyObject_Size((anyset))
290
291 #define PySet_Contains(anyset, key) \\
292     PySequence_Contains((anyset), (key))
293
294 #define PySet_Pop(set) \\
295     PyObject_CallMethod(set, (char *)"pop", NULL)
296
297 static CYTHON_INLINE int PySet_Clear(PyObject *set) {
298     PyObject *ret = PyObject_CallMethod(set, (char *)"clear", NULL);
299     if (!ret) return -1;
300     Py_DECREF(ret); return 0;
301 }
302
303 static CYTHON_INLINE int PySet_Discard(PyObject *set, PyObject *key) {
304     PyObject *ret = PyObject_CallMethod(set, (char *)"discard", (char *)"O", key);
305     if (!ret) return -1;
306     Py_DECREF(ret); return 0;
307 }
308
309 static CYTHON_INLINE int PySet_Add(PyObject *set, PyObject *key) {
310     PyObject *ret = PyObject_CallMethod(set, (char *)"add", (char *)"O", key);
311     if (!ret) return -1;
312     Py_DECREF(ret); return 0;
313 }
314
315 #endif /* PyAnySet_CheckExact (<= Py2.4) */
316
317 #if PY_VERSION_HEX < 0x02040000
318 #ifndef Py_SETOBJECT_H
319 #define Py_SETOBJECT_H
320
321 static PyTypeObject *__Pyx_PySet_Type = NULL;
322 static PyTypeObject *__Pyx_PyFrozenSet_Type = NULL;
323
324 #define PySet_Type (*__Pyx_PySet_Type)
325 #define PyFrozenSet_Type (*__Pyx_PyFrozenSet_Type)
326
327 #define PyAnySet_Check(ob) \\
328     (PyAnySet_CheckExact(ob) || \\
329      PyType_IsSubtype((ob)->ob_type, &PySet_Type) || \\
330      PyType_IsSubtype((ob)->ob_type, &PyFrozenSet_Type))
331
332 #define PyFrozenSet_CheckExact(ob) ((ob)->ob_type == &PyFrozenSet_Type)
333
334 static int __Pyx_Py23SetsImport(void) {
335     PyObject *sets=0, *Set=0, *ImmutableSet=0;
336
337     sets = PyImport_ImportModule((char *)"sets");
338     if (!sets) goto bad;
339     Set = PyObject_GetAttrString(sets, (char *)"Set");
340     if (!Set) goto bad;
341     ImmutableSet = PyObject_GetAttrString(sets, (char *)"ImmutableSet");
342     if (!ImmutableSet) goto bad;
343     Py_DECREF(sets);
344
345     __Pyx_PySet_Type       = (PyTypeObject*) Set;
346     __Pyx_PyFrozenSet_Type = (PyTypeObject*) ImmutableSet;
347
348     return 0;
349
350  bad:
351     Py_XDECREF(sets);
352     Py_XDECREF(Set);
353     Py_XDECREF(ImmutableSet);
354     return -1;
355 }
356
357 #else
358 static int __Pyx_Py23SetsImport(void) { return 0; }
359 #endif /* !Py_SETOBJECT_H */
360 #endif /* < Py2.4  */
361 #endif /* < Py2.5  */
362 """,
363 init = put_py23_set_init_utility_code,
364 cleanup = """
365 #if PY_VERSION_HEX < 0x02040000
366 Py_XDECREF(__Pyx_PySet_Type); __Pyx_PySet_Type = NULL;
367 Py_XDECREF(__Pyx_PyFrozenSet_Type); __Pyx_PyFrozenSet_Type = NULL;
368 #endif /* < Py2.4  */
369 """)
370
371 builtin_utility_code = {
372     'exec'      : pyexec_utility_code,
373     'getattr3'  : getattr3_utility_code,
374     'intern'    : intern_utility_code,
375     'set'       : py23_set_utility_code,
376     'frozenset' : py23_set_utility_code,
377 }
378
379 builtin_scope = BuiltinScope()
380
381 def declare_builtin_func(name, args, ret, cname, py_equiv = "*"):
382     sig = Signature(args, ret)
383     type = sig.function_type()
384     utility = builtin_utility_code.get(name)
385     builtin_scope.declare_builtin_cfunction(name, type, cname, py_equiv, utility)
386
387 def init_builtin_funcs():
388     for desc in builtin_function_table:
389         declare_builtin_func(*desc)
390
391 builtin_types = {}
392
393 def init_builtin_types():
394     global builtin_types
395     for name, cname, funcs in builtin_types_table:
396         utility = builtin_utility_code.get(name)
397         the_type = builtin_scope.declare_builtin_type(name, cname, utility)
398         builtin_types[name] = the_type
399         for name, args, ret, cname in funcs:
400             sig = Signature(args, ret)
401             the_type.scope.declare_cfunction(name, sig.function_type(), None, cname)
402
403 def init_builtin_structs():
404     for name, cname, attribute_types in builtin_structs_table:
405         scope = StructOrUnionScope(name)
406         for attribute_name, attribute_type in attribute_types:
407             scope.declare_var(attribute_name, attribute_type, None,
408                               attribute_name, allow_pyobject=True)
409         builtin_scope.declare_struct_or_union(
410             name, "struct", scope, 1, None, cname = cname)
411
412 def init_builtins():
413     init_builtin_funcs()
414     init_builtin_types()
415     init_builtin_structs()
416     global list_type, tuple_type, dict_type, set_type, frozenset_type
417     global bytes_type, str_type, unicode_type
418     global float_type, bool_type, type_type
419     type_type  = builtin_scope.lookup('type').type
420     list_type  = builtin_scope.lookup('list').type
421     tuple_type = builtin_scope.lookup('tuple').type
422     dict_type  = builtin_scope.lookup('dict').type
423     set_type   = builtin_scope.lookup('set').type
424     frozenset_type = builtin_scope.lookup('frozenset').type
425     bytes_type = builtin_scope.lookup('bytes').type
426     str_type   = builtin_scope.lookup('str').type
427     unicode_type = builtin_scope.lookup('unicode').type
428     float_type = builtin_scope.lookup('float').type
429     bool_type  = builtin_scope.lookup('bool').type
430
431 init_builtins()