Cython has moved to github.
cython-devel
view Cython/Compiler/Builtin.py @ 2552:e2532920c40c
fix intern() optimisation
| author | Stefan Behnel <scoder@users.berlios.de> |
|---|---|
| date | Thu Oct 22 11:24:48 2009 +0200 (2 years ago) |
| parents | 015626586350 |
| children | 953338c74a1c |
line source
1 #
2 # Pyrex - Builtin Definitions
3 #
5 from Symtab import BuiltinScope, StructOrUnionScope
6 from Code 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"), # 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"),
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),
62 # Put in namespace append optimization.
63 ('__Pyx_PyObject_Append', "OO", "O", "__Pyx_PyObject_Append"),
64 ]
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
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", []),
121 ]
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 ]
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 """)
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;
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 }
190 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
191 PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
192 }
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 }
231 return result;
232 bad:
233 Py_XDECREF(s);
234 return 0;
235 }
236 """)
238 intern_utility_code = UtilityCode(
239 proto = """
240 static PyObject* __Pyx_Intern(PyObject* s); /* proto */
241 """,
242 impl = '''
243 static PyObject* __Pyx_Intern(PyObject* s) {
244 if (!(likely(PyString_CheckExact(s)))) {
245 PyErr_Format(PyExc_TypeError, "Expected str, got %s", Py_TYPE(s)->tp_name);
246 return 0;
247 }
248 Py_INCREF(s);
249 #if PY_MAJOR_VERSION >= 3
250 PyUnicode_InternInPlace(&s);
251 #else
252 PyString_InternInPlace(&s);
253 #endif
254 return s;
255 }
256 ''')
258 def put_py23_set_init_utility_code(code, pos):
259 code.putln("#if PY_VERSION_HEX < 0x02040000")
260 code.putln(code.error_goto_if_neg("__Pyx_Py23SetsImport()", pos))
261 code.putln("#endif")
263 py23_set_utility_code = UtilityCode(
264 proto = """
265 #if PY_VERSION_HEX < 0x02050000
266 #ifndef PyAnySet_CheckExact
268 #define PyAnySet_CheckExact(ob) \\
269 ((ob)->ob_type == &PySet_Type || \\
270 (ob)->ob_type == &PyFrozenSet_Type)
272 #define PySet_New(iterable) \\
273 PyObject_CallFunctionObjArgs((PyObject *)&PySet_Type, (iterable), NULL)
275 #define Pyx_PyFrozenSet_New(iterable) \\
276 PyObject_CallFunctionObjArgs((PyObject *)&PyFrozenSet_Type, (iterable), NULL)
278 #define PySet_Size(anyset) \\
279 PyObject_Size((anyset))
281 #define PySet_Contains(anyset, key) \\
282 PySequence_Contains((anyset), (key))
284 #define PySet_Pop(set) \\
285 PyObject_CallMethod(set, (char *)"pop", NULL)
287 static INLINE int PySet_Clear(PyObject *set) {
288 PyObject *ret = PyObject_CallMethod(set, (char *)"clear", NULL);
289 if (!ret) return -1;
290 Py_DECREF(ret); return 0;
291 }
293 static INLINE int PySet_Discard(PyObject *set, PyObject *key) {
294 PyObject *ret = PyObject_CallMethod(set, (char *)"discard", (char *)"O", key);
295 if (!ret) return -1;
296 Py_DECREF(ret); return 0;
297 }
299 static INLINE int PySet_Add(PyObject *set, PyObject *key) {
300 PyObject *ret = PyObject_CallMethod(set, (char *)"add", (char *)"O", key);
301 if (!ret) return -1;
302 Py_DECREF(ret); return 0;
303 }
305 #endif /* PyAnySet_CheckExact (<= Py2.4) */
307 #if PY_VERSION_HEX < 0x02040000
308 #ifndef Py_SETOBJECT_H
309 #define Py_SETOBJECT_H
311 static PyTypeObject *__Pyx_PySet_Type = NULL;
312 static PyTypeObject *__Pyx_PyFrozenSet_Type = NULL;
314 #define PySet_Type (*__Pyx_PySet_Type)
315 #define PyFrozenSet_Type (*__Pyx_PyFrozenSet_Type)
317 #define PyAnySet_Check(ob) \\
318 (PyAnySet_CheckExact(ob) || \\
319 PyType_IsSubtype((ob)->ob_type, &PySet_Type) || \\
320 PyType_IsSubtype((ob)->ob_type, &PyFrozenSet_Type))
322 #define PyFrozenSet_CheckExact(ob) ((ob)->ob_type == &PyFrozenSet_Type)
324 static int __Pyx_Py23SetsImport(void) {
325 PyObject *sets=0, *Set=0, *ImmutableSet=0;
327 sets = PyImport_ImportModule((char *)"sets");
328 if (!sets) goto bad;
329 Set = PyObject_GetAttrString(sets, (char *)"Set");
330 if (!Set) goto bad;
331 ImmutableSet = PyObject_GetAttrString(sets, (char *)"ImmutableSet");
332 if (!ImmutableSet) goto bad;
333 Py_DECREF(sets);
335 __Pyx_PySet_Type = (PyTypeObject*) Set;
336 __Pyx_PyFrozenSet_Type = (PyTypeObject*) ImmutableSet;
338 return 0;
340 bad:
341 Py_XDECREF(sets);
342 Py_XDECREF(Set);
343 Py_XDECREF(ImmutableSet);
344 return -1;
345 }
347 #else
348 static int __Pyx_Py23SetsImport(void) { return 0; }
349 #endif /* !Py_SETOBJECT_H */
350 #endif /* < Py2.4 */
351 #endif /* < Py2.5 */
352 """,
353 init = put_py23_set_init_utility_code,
354 cleanup = """
355 #if PY_VERSION_HEX < 0x02040000
356 Py_XDECREF(__Pyx_PySet_Type); __Pyx_PySet_Type = NULL;
357 Py_XDECREF(__Pyx_PyFrozenSet_Type); __Pyx_PyFrozenSet_Type = NULL;
358 #endif /* < Py2.4 */
359 """)
361 builtin_utility_code = {
362 'exec' : pyexec_utility_code,
363 'getattr3' : getattr3_utility_code,
364 'intern' : intern_utility_code,
365 'set' : py23_set_utility_code,
366 'frozenset' : py23_set_utility_code,
367 }
369 builtin_scope = BuiltinScope()
371 def declare_builtin_func(name, args, ret, cname, py_equiv = "*"):
372 sig = Signature(args, ret)
373 type = sig.function_type()
374 utility = builtin_utility_code.get(name)
375 builtin_scope.declare_builtin_cfunction(name, type, cname, py_equiv, utility)
377 def init_builtin_funcs():
378 for desc in builtin_function_table:
379 declare_builtin_func(*desc)
381 builtin_types = {}
383 def init_builtin_types():
384 global builtin_types
385 for name, cname, funcs in builtin_types_table:
386 utility = builtin_utility_code.get(name)
387 the_type = builtin_scope.declare_builtin_type(name, cname, utility)
388 builtin_types[name] = the_type
389 for name, args, ret, cname in funcs:
390 sig = Signature(args, ret)
391 the_type.scope.declare_cfunction(name, sig.function_type(), None, cname)
393 def init_builtin_structs():
394 for name, cname, attribute_types in builtin_structs_table:
395 scope = StructOrUnionScope(name)
396 for attribute_name, attribute_type in attribute_types:
397 scope.declare_var(
398 attribute_name, attribute_type, None, attribute_name)
399 builtin_scope.declare_struct_or_union(
400 name, "struct", scope, 1, None, cname = cname)
402 def init_builtins():
403 init_builtin_funcs()
404 init_builtin_types()
405 init_builtin_structs()
406 global list_type, tuple_type, dict_type, set_type, type_type
407 global bytes_type, str_type, unicode_type
408 type_type = builtin_scope.lookup('type').type
409 list_type = builtin_scope.lookup('list').type
410 tuple_type = builtin_scope.lookup('tuple').type
411 dict_type = builtin_scope.lookup('dict').type
412 set_type = builtin_scope.lookup('set').type
413 bytes_type = builtin_scope.lookup('bytes').type
414 str_type = builtin_scope.lookup('str').type
415 unicode_type = builtin_scope.lookup('unicode').type
417 init_builtins()
