Cython has moved to github.
cython-devel
view Cython/Compiler/Builtin.py @ 1169:30b7a6c2c7c1
isinstance uses PyObject_TypeCheck when second argument is a type
| author | Robert Bradshaw <robertwb@math.washington.edu> |
|---|---|
| date | Mon Sep 22 23:45:08 2008 -0700 (3 years ago) |
| parents | 5e482c0ce677 |
| children | 3152381ccf89 |
line source
1 #
2 # Pyrex - Builtin Definitions
3 #
5 from Symtab import BuiltinScope, StructOrUnionScope
6 from TypeSlots import Signature
7 import PyrexTypes
9 builtin_function_table = [
10 # name, args, return, C API func, py equiv = "*"
11 ('abs', "O", "O", "PyNumber_Absolute"),
12 #('chr', "", "", ""),
13 #('cmp', "", "", "", ""), # int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
14 #('compile', "", "", ""), # PyObject* Py_CompileString( char *str, char *filename, int start)
15 ('delattr', "OO", "r", "PyObject_DelAttr"),
16 ('dir', "O", "O", "PyObject_Dir"),
17 ('divmod', "OO", "O", "PyNumber_Divmod"),
18 #('eval', "", "", ""),
19 #('execfile', "", "", ""),
20 #('filter', "", "", ""),
21 ('getattr', "OO", "O", "PyObject_GetAttr"),
22 ('getattr3', "OOO", "O", "__Pyx_GetAttr3", "getattr"),
23 ('hasattr', "OO", "b", "PyObject_HasAttr"),
24 ('hash', "O", "l", "PyObject_Hash"),
25 #('hex', "", "", ""),
26 #('id', "", "", ""),
27 #('input', "", "", ""),
28 ('intern', "s", "O", "__Pyx_InternFromString"),
29 ('isinstance', "OO", "b", "PyObject_IsInstance"),
30 ('issubclass', "OO", "b", "PyObject_IsSubclass"),
31 ('iter', "O", "O", "PyObject_GetIter"),
32 ('len', "O", "Z", "PyObject_Length"),
33 #('map', "", "", ""),
34 #('max', "", "", ""),
35 #('min', "", "", ""),
36 #('oct', "", "", ""),
37 # Not worth doing open, when second argument would become mandatory
38 #('open', "ss", "O", "PyFile_FromString"),
39 #('ord', "", "", ""),
40 ('pow', "OOO", "O", "PyNumber_Power"),
41 #('range', "", "", ""),
42 #('raw_input', "", "", ""),
43 #('reduce', "", "", ""),
44 ('reload', "O", "O", "PyImport_ReloadModule"),
45 ('repr', "O", "O", "PyObject_Repr"),
46 #('round', "", "", ""),
47 ('setattr', "OOO", "r", "PyObject_SetAttr"),
48 #('sum', "", "", ""),
49 #('unichr', "", "", ""),
50 #('unicode', "", "", ""),
51 #('vars', "", "", ""),
52 #('zip', "", "", ""),
53 # Can't do these easily until we have builtin type entries.
54 #('typecheck', "OO", "i", "PyObject_TypeCheck", False),
55 #('issubtype', "OO", "i", "PyType_IsSubtype", False),
57 # Put in namespace append optimization.
58 ('__Pyx_PyObject_Append', "OO", "O", "__Pyx_PyObject_Append"),
59 ]
61 # Builtin types
62 # bool
63 # buffer
64 # classmethod
65 # dict
66 # enumerate
67 # file
68 # float
69 # int
70 # list
71 # long
72 # object
73 # property
74 # slice
75 # staticmethod
76 # super
77 # str
78 # tuple
79 # type
80 # xrange
82 builtin_types_table = [
84 ("type", "PyType_Type", []),
85 # ("str", "PyBytes_Type", []),
86 ("unicode", "PyUnicode_Type", []),
87 ("file", "PyFile_Type", []),
88 # ("slice", "PySlice_Type", []),
89 # ("set", "PySet_Type", []),
90 ("frozenset", "PyFrozenSet_Type", []),
92 ("tuple", "PyTuple_Type", []),
94 ("list", "PyList_Type", [("append", "OO", "i", "PyList_Append"),
95 ("insert", "OiO", "i", "PyList_Insert"),
96 ("sort", "O", "i", "PyList_Sort"),
97 ("reverse","O", "i", "PyList_Reverse")]),
99 ("dict", "PyDict_Type", [("items", "O", "O", "PyDict_Items"),
100 ("keys", "O", "O", "PyDict_Keys"),
101 ("values","O", "O", "PyDict_Values")]),
102 ]
104 builtin_structs_table = [
105 ('Py_buffer', 'Py_buffer',
106 [("buf", PyrexTypes.c_void_ptr_type),
107 ("obj", PyrexTypes.py_object_type),
108 ("len", PyrexTypes.c_py_ssize_t_type),
109 ("itemsize", PyrexTypes.c_py_ssize_t_type),
110 ("readonly", PyrexTypes.c_bint_type),
111 ("ndim", PyrexTypes.c_int_type),
112 ("format", PyrexTypes.c_char_ptr_type),
113 ("shape", PyrexTypes.c_py_ssize_t_ptr_type),
114 ("strides", PyrexTypes.c_py_ssize_t_ptr_type),
115 ("suboffsets", PyrexTypes.c_py_ssize_t_ptr_type),
116 ("internal", PyrexTypes.c_void_ptr_type),
117 ])
118 ]
120 getattr3_utility_code = ["""
121 static PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
122 ""","""
123 static PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
124 PyObject *r = PyObject_GetAttr(o, n);
125 if (!r) {
126 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
127 goto bad;
128 PyErr_Clear();
129 r = d;
130 Py_INCREF(d);
131 }
132 return r;
133 bad:
134 return 0;
135 }
136 """]
138 intern_utility_code = ["""
139 #if PY_MAJOR_VERSION >= 3
140 # define __Pyx_InternFromString(s) PyUnicode_InternFromString(s)
141 #else
142 # define __Pyx_InternFromString(s) PyString_InternFromString(s)
143 #endif
144 ""","""
145 """]
147 builtin_utility_code = {
148 'getattr3': getattr3_utility_code,
149 'intern' : intern_utility_code,
150 }
152 builtin_scope = BuiltinScope()
154 def declare_builtin_func(name, args, ret, cname, py_equiv = "*"):
155 sig = Signature(args, ret)
156 type = sig.function_type()
157 utility = builtin_utility_code.get(name)
158 builtin_scope.declare_builtin_cfunction(name, type, cname, py_equiv, utility)
160 def init_builtin_funcs():
161 for desc in builtin_function_table:
162 declare_builtin_func(*desc)
164 def init_builtin_types():
165 for name, cname, funcs in builtin_types_table:
166 the_type = builtin_scope.declare_builtin_type(name, cname)
167 for name, args, ret, cname in funcs:
168 sig = Signature(args, ret)
169 the_type.scope.declare_cfunction(name, sig.function_type(), None, cname)
171 def init_builtin_structs():
172 for name, cname, attribute_types in builtin_structs_table:
173 scope = StructOrUnionScope(name)
174 for attribute_name, attribute_type in attribute_types:
175 scope.declare_var(
176 attribute_name, attribute_type, None, attribute_name)
177 builtin_scope.declare_struct_or_union(
178 name, "struct", scope, 1, None, cname = cname)
180 def init_builtins():
181 init_builtin_funcs()
182 init_builtin_types()
183 init_builtin_structs()
184 global list_type, tuple_type, dict_type, unicode_type, type_type
185 type_type = builtin_scope.lookup('type').type
186 list_type = builtin_scope.lookup('list').type
187 tuple_type = builtin_scope.lookup('tuple').type
188 dict_type = builtin_scope.lookup('dict').type
189 unicode_type = builtin_scope.lookup('unicode').type
191 init_builtins()
