2 # Pyrex - Builtin Definitions
5 from Symtab import BuiltinScope, StructOrUnionScope
6 from Code import UtilityCode
7 from TypeSlots import Signature
11 builtin_function_table = [
12 # name, args, return, C API func, py equiv = "*"
13 ('abs', "O", "O", "PyNumber_Absolute"),
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"),
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"),
41 # Not worth doing open, when second argument would become mandatory
42 #('open', "ss", "O", "PyFile_FromString"),
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"),
53 ('type', "O", "O", "PyObject_Type"),
54 #('unichr', "", "", ""),
55 #('unicode', "", "", ""),
56 #('vars', "", "", ""),
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),
62 # Put in namespace append optimization.
63 ('__Pyx_PyObject_Append', "OO", "O", "__Pyx_PyObject_Append"),
87 builtin_types_table = [
89 ("type", "PyType_Type", []),
91 ("bool", "PyBool_Type", []),
92 ("int", "PyInt_Type", []),
93 ("long", "PyLong_Type", []),
94 ("float", "PyFloat_Type", []),
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", []),
100 ("bytes", "PyBytes_Type", []),
101 ("str", "PyString_Type", []),
102 ("unicode", "PyUnicode_Type", []),
104 ("tuple", "PyTuple_Type", []),
106 ("list", "PyList_Type", [("insert", "OZO", "i", "PyList_Insert")]),
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")]),
113 ("slice", "PySlice_Type", []),
114 ("file", "PyFile_Type", []),
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", []),
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),
141 getattr3_utility_code = UtilityCode(
143 static PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
146 static PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
147 PyObject *r = PyObject_GetAttr(o, n);
149 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
161 pyexec_utility_code = UtilityCode(
163 #if PY_VERSION_HEX < 0x02040000
168 static PyObject* __Pyx_PyRun(PyObject*, PyObject*, PyObject*);
171 static PyObject* __Pyx_PyRun(PyObject* o, PyObject* globals, PyObject* locals) {
176 if (!globals || globals == Py_None) {
177 globals = PyModule_GetDict(%s);""" % Naming.module_cname + """
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);
185 if (!locals || locals == Py_None) {
190 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
191 PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
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");
200 result = PyEval_EvalCode((PyCodeObject *)o, globals, locals);
204 if (PyUnicode_Check(o)) {
205 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
206 s = PyUnicode_AsUTF8String(o);
209 #if PY_MAJOR_VERSION >= 3
210 } else if (!PyBytes_Check(o)) {
212 } else if (!PyString_Check(o)) {
214 PyErr_SetString(PyExc_TypeError,
215 "exec: arg 1 must be string, bytes or code object");
218 #if PY_MAJOR_VERSION >= 3
219 code = PyBytes_AS_STRING(o);
221 code = PyString_AS_STRING(o);
223 if (PyEval_MergeCompilerFlags(&cf)) {
224 result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
226 result = PyRun_String(code, Py_file_input, globals, locals);
238 intern_utility_code = UtilityCode(
240 #if PY_MAJOR_VERSION >= 3
241 # define __Pyx_InternFromString(s) PyUnicode_InternFromString(s)
243 # define __Pyx_InternFromString(s) PyString_InternFromString(s)
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))
252 py23_set_utility_code = UtilityCode(
254 #if PY_VERSION_HEX < 0x02050000
255 #ifndef PyAnySet_CheckExact
257 #define PyAnySet_CheckExact(ob) \\
258 ((ob)->ob_type == &PySet_Type || \\
259 (ob)->ob_type == &PyFrozenSet_Type)
261 #define PySet_New(iterable) \\
262 PyObject_CallFunctionObjArgs((PyObject *)&PySet_Type, (iterable), NULL)
264 #define Pyx_PyFrozenSet_New(iterable) \\
265 PyObject_CallFunctionObjArgs((PyObject *)&PyFrozenSet_Type, (iterable), NULL)
267 #define PySet_Size(anyset) \\
268 PyObject_Size((anyset))
270 #define PySet_Contains(anyset, key) \\
271 PySequence_Contains((anyset), (key))
273 #define PySet_Pop(set) \\
274 PyObject_CallMethod(set, (char *)"pop", NULL)
276 static INLINE int PySet_Clear(PyObject *set) {
277 PyObject *ret = PyObject_CallMethod(set, (char *)"clear", NULL);
279 Py_DECREF(ret); return 0;
282 static INLINE int PySet_Discard(PyObject *set, PyObject *key) {
283 PyObject *ret = PyObject_CallMethod(set, (char *)"discard", (char *)"O", key);
285 Py_DECREF(ret); return 0;
288 static INLINE int PySet_Add(PyObject *set, PyObject *key) {
289 PyObject *ret = PyObject_CallMethod(set, (char *)"add", (char *)"O", key);
291 Py_DECREF(ret); return 0;
294 #endif /* PyAnySet_CheckExact (<= Py2.4) */
296 #if PY_VERSION_HEX < 0x02040000
297 #ifndef Py_SETOBJECT_H
298 #define Py_SETOBJECT_H
300 static PyTypeObject *__Pyx_PySet_Type = NULL;
301 static PyTypeObject *__Pyx_PyFrozenSet_Type = NULL;
303 #define PySet_Type (*__Pyx_PySet_Type)
304 #define PyFrozenSet_Type (*__Pyx_PyFrozenSet_Type)
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))
311 #define PyFrozenSet_CheckExact(ob) ((ob)->ob_type == &PyFrozenSet_Type)
313 static int __Pyx_Py23SetsImport(void) {
314 PyObject *sets=0, *Set=0, *ImmutableSet=0;
316 sets = PyImport_ImportModule((char *)"sets");
318 Set = PyObject_GetAttrString(sets, (char *)"Set");
320 ImmutableSet = PyObject_GetAttrString(sets, (char *)"ImmutableSet");
321 if (!ImmutableSet) goto bad;
324 __Pyx_PySet_Type = (PyTypeObject*) Set;
325 __Pyx_PyFrozenSet_Type = (PyTypeObject*) ImmutableSet;
332 Py_XDECREF(ImmutableSet);
337 static int __Pyx_Py23SetsImport(void) { return 0; }
338 #endif /* !Py_SETOBJECT_H */
342 init = put_py23_set_init_utility_code,
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;
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,
358 builtin_scope = BuiltinScope()
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)
366 def init_builtin_funcs():
367 for desc in builtin_function_table:
368 declare_builtin_func(*desc)
372 def init_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)
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:
387 attribute_name, attribute_type, None, attribute_name)
388 builtin_scope.declare_struct_or_union(
389 name, "struct", scope, 1, None, cname = cname)
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