1 # cython.* namespace for pure mode.
5 _Unspecified = object()
9 def _empty_decorator(x):
12 def locals(**arg_types):
13 return _empty_decorator
15 def test_assert_path_exists(path):
16 return _empty_decorator
18 def test_fail_if_path_exists(path):
19 return _empty_decorator
21 class _EmptyDecoratorAndManager(object):
22 def __call__(self, x):
26 def __exit__(self, exc_type, exc_value, traceback):
29 def inline(f, *args, **kwds):
30 if isinstance(f, basestring):
31 from Cython.Build.Inline import cython_inline
32 return cython_inline(f, *args, **kwds)
34 assert len(args) == len(kwds) == 0
38 from Cython.Build.Inline import RuntimeCompiledFunction
39 return RuntimeCompiledFunction(f)
55 # Emulated language constructs
57 def cast(type, *args):
58 if hasattr(type, '__call__'):
70 return pointer(type(arg))([arg])
72 def declare(type=None, value=_Unspecified, **kwds):
73 if type is not None and hasattr(type, '__call__'):
74 if value is not _Unspecified:
82 """Support for 'with nogil' statement
86 def __exit__(self, exc_class, exc, tb):
87 return exc_class is None
94 class CythonMetaType(type):
96 def __getitem__(type, ix):
97 return array(type, ix)
99 CythonTypeObject = CythonMetaType('CythonTypeObject', (object,), {})
101 class CythonType(CythonTypeObject):
103 def _pointer(self, n=1):
108 class PointerType(CythonType):
110 def __init__(self, value=None):
111 if isinstance(value, (ArrayType, PointerType)):
112 self._items = [cast(self._basetype, a) for a in value._items]
113 elif isinstance(value, list):
114 self._items = [cast(self._basetype, a) for a in value]
115 elif value is None or value is 0:
120 def __getitem__(self, ix):
122 raise IndexError("negative indexing not allowed in C")
123 return self._items[ix]
125 def __setitem__(self, ix, value):
127 raise IndexError("negative indexing not allowed in C")
128 self._items[ix] = cast(self._basetype, value)
130 def __eq__(self, value):
131 if value is None and not self._items:
133 elif type(self) != type(value):
136 return not self._items and not value._items
138 class ArrayType(PointerType):
141 self._items = [None] * self._n
144 class StructType(CythonType):
146 def __init__(self, cast_from=_Unspecified, **data):
147 if cast_from is not _Unspecified:
150 raise ValueError('Cannot accept keyword arguments when casting.')
151 if type(cast_from) is not type(self):
152 raise ValueError('Cannot cast from %s'%cast_from)
153 for key, value in cast_from.__dict__.items():
154 setattr(self, key, value)
156 for key, value in data.iteritems():
157 setattr(self, key, value)
159 def __setattr__(self, key, value):
160 if key in self._members:
161 self.__dict__[key] = cast(self._members[key], value)
163 raise AttributeError("Struct has no member '%s'" % key)
166 class UnionType(CythonType):
168 def __init__(self, cast_from=_Unspecified, **data):
169 if cast_from is not _Unspecified:
172 raise ValueError('Cannot accept keyword arguments when casting.')
173 if isinstance(cast_from, dict):
175 elif type(cast_from) is type(self):
176 datadict = cast_from.__dict__
178 raise ValueError('Cannot cast from %s'%cast_from)
181 if len(datadict) > 1:
182 raise AttributeError("Union can only store one field at a time.")
183 for key, value in datadict.iteritems():
184 setattr(self, key, value)
186 def __setattr__(self, key, value):
187 if key in '__dict__':
188 CythonType.__setattr__(self, key, value)
189 elif key in self._members:
190 self.__dict__ = {key: cast(self._members[key], value)}
192 raise AttributeError("Union has no member '%s'" % key)
194 def pointer(basetype):
195 class PointerInstance(PointerType):
197 return PointerInstance
199 def array(basetype, n):
200 class ArrayInstance(ArrayType):
205 def struct(**members):
206 class StructInstance(StructType):
209 setattr(StructInstance, key, None)
210 return StructInstance
212 def union(**members):
213 class UnionInstance(UnionType):
216 setattr(UnionInstance, key, None)
219 class typedef(CythonType):
221 def __init__(self, type):
222 self._basetype = type
224 def __call__(self, *arg):
225 value = cast(self._basetype, *arg)
233 except NameError: # Py3
241 from builtins import set, frozenset
245 from __builtin__ import set, frozenset
248 from sets import Set as set, ImmutableSet as frozenset
252 int_types = ['char', 'short', 'Py_UNICODE', 'int', 'long', 'longlong', 'Py_ssize_t', 'size_t']
253 float_types = ['longdouble', 'double', 'float']
254 complex_types = ['longdoublecomplex', 'doublecomplex', 'floatcomplex', 'complex']
255 other_types = ['bint', 'void']
259 for name in int_types:
260 gs[name] = typedef(py_int)
261 if name != 'Py_UNICODE' and not name.endswith('size_t'):
262 gs['u'+name] = typedef(py_int)
263 gs['s'+name] = typedef(py_int)
265 for name in float_types:
266 gs[name] = typedef(py_float)
268 for name in complex_types:
269 gs[name] = typedef(py_complex)
274 for t in int_types + float_types + complex_types + other_types:
275 for i in range(1, 4):
276 gs["%s_%s" % ('p'*i, t)] = globals()[t]._pointer(i)