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', "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"),
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", []),
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
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),
148 getattr3_utility_code = UtilityCode(
150 static PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
153 static PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
154 PyObject *r = PyObject_GetAttr(o, n);
156 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
168 pyexec_utility_code = UtilityCode(
170 #if PY_VERSION_HEX < 0x02040000
178 static PyObject* __Pyx_PyRun(PyObject*, PyObject*, PyObject*);
181 static PyObject* __Pyx_PyRun(PyObject* o, PyObject* globals, PyObject* locals) {
186 if (!globals || globals == Py_None) {
187 globals = PyModule_GetDict(%s);""" % Naming.module_cname + """
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);
195 if (!locals || locals == Py_None) {
200 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
201 PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
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");
210 result = PyEval_EvalCode((PyCodeObject *)o, globals, locals);
214 if (PyUnicode_Check(o)) {
215 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
216 s = PyUnicode_AsUTF8String(o);
219 #if PY_MAJOR_VERSION >= 3
220 } else if (!PyBytes_Check(o)) {
222 } else if (!PyString_Check(o)) {
224 PyErr_SetString(PyExc_TypeError,
225 "exec: arg 1 must be string, bytes or code object");
228 #if PY_MAJOR_VERSION >= 3
229 code = PyBytes_AS_STRING(o);
231 code = PyString_AS_STRING(o);
233 if (PyEval_MergeCompilerFlags(&cf)) {
234 result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
236 result = PyRun_String(code, Py_file_input, globals, locals);
248 intern_utility_code = UtilityCode(
250 static PyObject* __Pyx_Intern(PyObject* s); /* proto */
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);
259 #if PY_MAJOR_VERSION >= 3
260 PyUnicode_InternInPlace(&s);
262 PyString_InternInPlace(&s);
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))
273 py23_set_utility_code = UtilityCode(
275 #if PY_VERSION_HEX < 0x02050000
276 #ifndef PyAnySet_CheckExact
278 #define PyAnySet_CheckExact(ob) \\
279 ((ob)->ob_type == &PySet_Type || \\
280 (ob)->ob_type == &PyFrozenSet_Type)
282 #define PySet_New(iterable) \\
283 PyObject_CallFunctionObjArgs((PyObject *)&PySet_Type, (iterable), NULL)
285 #define Pyx_PyFrozenSet_New(iterable) \\
286 PyObject_CallFunctionObjArgs((PyObject *)&PyFrozenSet_Type, (iterable), NULL)
288 #define PySet_Size(anyset) \\
289 PyObject_Size((anyset))
291 #define PySet_Contains(anyset, key) \\
292 PySequence_Contains((anyset), (key))
294 #define PySet_Pop(set) \\
295 PyObject_CallMethod(set, (char *)"pop", NULL)
297 static CYTHON_INLINE int PySet_Clear(PyObject *set) {
298 PyObject *ret = PyObject_CallMethod(set, (char *)"clear", NULL);
300 Py_DECREF(ret); return 0;
303 static CYTHON_INLINE int PySet_Discard(PyObject *set, PyObject *key) {
304 PyObject *ret = PyObject_CallMethod(set, (char *)"discard", (char *)"O", key);
306 Py_DECREF(ret); return 0;
309 static CYTHON_INLINE int PySet_Add(PyObject *set, PyObject *key) {
310 PyObject *ret = PyObject_CallMethod(set, (char *)"add", (char *)"O", key);
312 Py_DECREF(ret); return 0;
315 #endif /* PyAnySet_CheckExact (<= Py2.4) */
317 #if PY_VERSION_HEX < 0x02040000
318 #ifndef Py_SETOBJECT_H
319 #define Py_SETOBJECT_H
321 static PyTypeObject *__Pyx_PySet_Type = NULL;
322 static PyTypeObject *__Pyx_PyFrozenSet_Type = NULL;
324 #define PySet_Type (*__Pyx_PySet_Type)
325 #define PyFrozenSet_Type (*__Pyx_PyFrozenSet_Type)
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))
332 #define PyFrozenSet_CheckExact(ob) ((ob)->ob_type == &PyFrozenSet_Type)
334 static int __Pyx_Py23SetsImport(void) {
335 PyObject *sets=0, *Set=0, *ImmutableSet=0;
337 sets = PyImport_ImportModule((char *)"sets");
339 Set = PyObject_GetAttrString(sets, (char *)"Set");
341 ImmutableSet = PyObject_GetAttrString(sets, (char *)"ImmutableSet");
342 if (!ImmutableSet) goto bad;
345 __Pyx_PySet_Type = (PyTypeObject*) Set;
346 __Pyx_PyFrozenSet_Type = (PyTypeObject*) ImmutableSet;
353 Py_XDECREF(ImmutableSet);
358 static int __Pyx_Py23SetsImport(void) { return 0; }
359 #endif /* !Py_SETOBJECT_H */
363 init = put_py23_set_init_utility_code,
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;
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,
379 builtin_scope = BuiltinScope()
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)
387 def init_builtin_funcs():
388 for desc in builtin_function_table:
389 declare_builtin_func(*desc)
393 def init_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)
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)
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