1 # cython.* namespace for pure mode.
5 def empty_decorator(x):
10 def locals(**arg_types):
11 return empty_decorator
13 def inline(f, *args, **kwds):
14 if isinstance(f, basestring):
15 from Cython.Build.Inline import cython_inline
16 return cython_inline(f, *args, **kwds)
18 assert len(args) == len(kwds) == 0
22 from Cython.Build.Inline import RuntimeCompiledFunction
23 return RuntimeCompiledFunction(f)
39 # Emulated language constructs
42 if hasattr(type, '__call__'):
54 return pointer(type(arg))([arg])
56 def declare(type=None, value=None, **kwds):
57 if type is not None and hasattr(type, '__call__'):
66 """Support for 'with nogil' statement
70 def __exit__(self, exc_class, exc, tb):
71 return exc_class is None
78 class CythonType(object):
80 def _pointer(self, n=1):
85 def __getitem__(self, ix):
86 return array(self, ix)
89 class PointerType(CythonType):
91 def __init__(self, value=None):
92 if isinstance(value, ArrayType):
93 self._items = [cast(self._basetype, a) for a in value._items]
94 elif isinstance(value, list):
95 self._items = [cast(self._basetype, a) for a in value]
101 def __getitem__(self, ix):
103 raise IndexError("negative indexing not allowed in C")
104 return self._items[ix]
106 def __setitem__(self, ix, value):
108 raise IndexError("negative indexing not allowed in C")
109 self._items[ix] = cast(self._basetype, value)
111 class ArrayType(PointerType):
114 self._items = [None] * self._n
117 class StructType(CythonType):
119 def __init__(self, **data):
120 for key, value in data.iteritems():
121 setattr(self, key, value)
123 def __setattr__(self, key, value):
124 if key in self._members:
125 self.__dict__[key] = cast(self._members[key], value)
127 raise AttributeError("Struct has no member '%s'" % key)
130 class UnionType(CythonType):
132 def __init__(self, **data):
134 raise AttributeError("Union can only store one field at a time.")
135 for key, value in data.iteritems():
136 setattr(self, key, value)
138 def __setattr__(self, key, value):
139 if key in '__dict__':
140 CythonType.__setattr__(self, key, value)
141 elif key in self._members:
142 self.__dict__ = {key: cast(self._members[key], value)}
144 raise AttributeError("Union has no member '%s'" % key)
146 def pointer(basetype):
147 class PointerInstance(PointerType):
149 return PointerInstance
151 def array(basetype, n):
152 class ArrayInstance(ArrayType):
157 def struct(**members):
158 class StructInstance(StructType):
161 setattr(StructInstance, key, None)
162 return StructInstance
164 def union(**members):
165 class UnionInstance(UnionType):
168 setattr(UnionInstance, key, None)
171 class typedef(CythonType):
173 def __init__(self, type):
174 self._basetype = type
176 def __call__(self, value=None):
177 if value is not None:
178 value = cast(self._basetype, value)
186 except NameError: # Py3
196 from sets import Set as set, ImmutableSet as frozenset
200 int_types = ['char', 'short', 'Py_UNICODE', 'int', 'long', 'longlong', 'Py_ssize_t', 'size_t']
201 float_types = ['longdouble', 'double', 'float']
202 complex_types = ['longdoublecomplex', 'doublecomplex', 'floatcomplex', 'complex']
203 other_types = ['bint', 'void']
207 for name in int_types:
208 gs[name] = typedef(py_int)
209 if name != 'Py_UNICODE' and not name.endswith('size_t'):
210 gs['u'+name] = typedef(py_int)
211 gs['s'+name] = typedef(py_int)
213 for name in float_types:
214 gs[name] = typedef(py_float)
216 for name in complex_types:
217 gs[name] = typedef(py_complex)
222 for t in int_types + float_types + complex_types + other_types:
223 for i in range(1, 4):
224 gs["%s_%s" % ('p'*i, t)] = globals()[t]._pointer(i)