Shadow.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. # cython.* namespace for pure mode.
  2. from __future__ import absolute_import
  3. __version__ = "0.29.37"
  4. try:
  5. from __builtin__ import basestring
  6. except ImportError:
  7. basestring = str
  8. # BEGIN shameless copy from Cython/minivect/minitypes.py
  9. class _ArrayType(object):
  10. is_array = True
  11. subtypes = ['dtype']
  12. def __init__(self, dtype, ndim, is_c_contig=False, is_f_contig=False,
  13. inner_contig=False, broadcasting=None):
  14. self.dtype = dtype
  15. self.ndim = ndim
  16. self.is_c_contig = is_c_contig
  17. self.is_f_contig = is_f_contig
  18. self.inner_contig = inner_contig or is_c_contig or is_f_contig
  19. self.broadcasting = broadcasting
  20. def __repr__(self):
  21. axes = [":"] * self.ndim
  22. if self.is_c_contig:
  23. axes[-1] = "::1"
  24. elif self.is_f_contig:
  25. axes[0] = "::1"
  26. return "%s[%s]" % (self.dtype, ", ".join(axes))
  27. def index_type(base_type, item):
  28. """
  29. Support array type creation by slicing, e.g. double[:, :] specifies
  30. a 2D strided array of doubles. The syntax is the same as for
  31. Cython memoryviews.
  32. """
  33. class InvalidTypeSpecification(Exception):
  34. pass
  35. def verify_slice(s):
  36. if s.start or s.stop or s.step not in (None, 1):
  37. raise InvalidTypeSpecification(
  38. "Only a step of 1 may be provided to indicate C or "
  39. "Fortran contiguity")
  40. if isinstance(item, tuple):
  41. step_idx = None
  42. for idx, s in enumerate(item):
  43. verify_slice(s)
  44. if s.step and (step_idx or idx not in (0, len(item) - 1)):
  45. raise InvalidTypeSpecification(
  46. "Step may only be provided once, and only in the "
  47. "first or last dimension.")
  48. if s.step == 1:
  49. step_idx = idx
  50. return _ArrayType(base_type, len(item),
  51. is_c_contig=step_idx == len(item) - 1,
  52. is_f_contig=step_idx == 0)
  53. elif isinstance(item, slice):
  54. verify_slice(item)
  55. return _ArrayType(base_type, 1, is_c_contig=bool(item.step))
  56. else:
  57. # int[8] etc.
  58. assert int(item) == item # array size must be a plain integer
  59. array(base_type, item)
  60. # END shameless copy
  61. compiled = False
  62. _Unspecified = object()
  63. # Function decorators
  64. def _empty_decorator(x):
  65. return x
  66. def locals(**arg_types):
  67. return _empty_decorator
  68. def test_assert_path_exists(*paths):
  69. return _empty_decorator
  70. def test_fail_if_path_exists(*paths):
  71. return _empty_decorator
  72. class _EmptyDecoratorAndManager(object):
  73. def __call__(self, x):
  74. return x
  75. def __enter__(self):
  76. pass
  77. def __exit__(self, exc_type, exc_value, traceback):
  78. pass
  79. class _Optimization(object):
  80. pass
  81. cclass = ccall = cfunc = _EmptyDecoratorAndManager()
  82. returns = wraparound = boundscheck = initializedcheck = nonecheck = \
  83. embedsignature = cdivision = cdivision_warnings = \
  84. always_allows_keywords = profile = linetrace = infer_types = \
  85. unraisable_tracebacks = freelist = \
  86. lambda _: _EmptyDecoratorAndManager()
  87. exceptval = lambda _=None, check=True: _EmptyDecoratorAndManager()
  88. overflowcheck = lambda _: _EmptyDecoratorAndManager()
  89. optimization = _Optimization()
  90. overflowcheck.fold = optimization.use_switch = \
  91. optimization.unpack_method_calls = lambda arg: _EmptyDecoratorAndManager()
  92. final = internal = type_version_tag = no_gc_clear = no_gc = _empty_decorator
  93. binding = lambda _: _empty_decorator
  94. _cython_inline = None
  95. def inline(f, *args, **kwds):
  96. if isinstance(f, basestring):
  97. global _cython_inline
  98. if _cython_inline is None:
  99. from Cython.Build.Inline import cython_inline as _cython_inline
  100. return _cython_inline(f, *args, **kwds)
  101. else:
  102. assert len(args) == len(kwds) == 0
  103. return f
  104. def compile(f):
  105. from Cython.Build.Inline import RuntimeCompiledFunction
  106. return RuntimeCompiledFunction(f)
  107. # Special functions
  108. def cdiv(a, b):
  109. q = a / b
  110. if q < 0:
  111. q += 1
  112. return q
  113. def cmod(a, b):
  114. r = a % b
  115. if (a*b) < 0:
  116. r -= b
  117. return r
  118. # Emulated language constructs
  119. def cast(type, *args, **kwargs):
  120. kwargs.pop('typecheck', None)
  121. assert not kwargs
  122. if hasattr(type, '__call__'):
  123. return type(*args)
  124. else:
  125. return args[0]
  126. def sizeof(arg):
  127. return 1
  128. def typeof(arg):
  129. return arg.__class__.__name__
  130. # return type(arg)
  131. def address(arg):
  132. return pointer(type(arg))([arg])
  133. def declare(type=None, value=_Unspecified, **kwds):
  134. if type not in (None, object) and hasattr(type, '__call__'):
  135. if value is not _Unspecified:
  136. return type(value)
  137. else:
  138. return type()
  139. else:
  140. return value
  141. class _nogil(object):
  142. """Support for 'with nogil' statement and @nogil decorator.
  143. """
  144. def __call__(self, x):
  145. if callable(x):
  146. # Used as function decorator => return the function unchanged.
  147. return x
  148. # Used as conditional context manager or to create an "@nogil(True/False)" decorator => keep going.
  149. return self
  150. def __enter__(self):
  151. pass
  152. def __exit__(self, exc_class, exc, tb):
  153. return exc_class is None
  154. nogil = _nogil()
  155. gil = _nogil()
  156. del _nogil
  157. # Emulated types
  158. class CythonMetaType(type):
  159. def __getitem__(type, ix):
  160. return array(type, ix)
  161. CythonTypeObject = CythonMetaType('CythonTypeObject', (object,), {})
  162. class CythonType(CythonTypeObject):
  163. def _pointer(self, n=1):
  164. for i in range(n):
  165. self = pointer(self)
  166. return self
  167. class PointerType(CythonType):
  168. def __init__(self, value=None):
  169. if isinstance(value, (ArrayType, PointerType)):
  170. self._items = [cast(self._basetype, a) for a in value._items]
  171. elif isinstance(value, list):
  172. self._items = [cast(self._basetype, a) for a in value]
  173. elif value is None or value == 0:
  174. self._items = []
  175. else:
  176. raise ValueError
  177. def __getitem__(self, ix):
  178. if ix < 0:
  179. raise IndexError("negative indexing not allowed in C")
  180. return self._items[ix]
  181. def __setitem__(self, ix, value):
  182. if ix < 0:
  183. raise IndexError("negative indexing not allowed in C")
  184. self._items[ix] = cast(self._basetype, value)
  185. def __eq__(self, value):
  186. if value is None and not self._items:
  187. return True
  188. elif type(self) != type(value):
  189. return False
  190. else:
  191. return not self._items and not value._items
  192. def __repr__(self):
  193. return "%s *" % (self._basetype,)
  194. class ArrayType(PointerType):
  195. def __init__(self):
  196. self._items = [None] * self._n
  197. class StructType(CythonType):
  198. def __init__(self, cast_from=_Unspecified, **data):
  199. if cast_from is not _Unspecified:
  200. # do cast
  201. if len(data) > 0:
  202. raise ValueError('Cannot accept keyword arguments when casting.')
  203. if type(cast_from) is not type(self):
  204. raise ValueError('Cannot cast from %s'%cast_from)
  205. for key, value in cast_from.__dict__.items():
  206. setattr(self, key, value)
  207. else:
  208. for key, value in data.items():
  209. setattr(self, key, value)
  210. def __setattr__(self, key, value):
  211. if key in self._members:
  212. self.__dict__[key] = cast(self._members[key], value)
  213. else:
  214. raise AttributeError("Struct has no member '%s'" % key)
  215. class UnionType(CythonType):
  216. def __init__(self, cast_from=_Unspecified, **data):
  217. if cast_from is not _Unspecified:
  218. # do type cast
  219. if len(data) > 0:
  220. raise ValueError('Cannot accept keyword arguments when casting.')
  221. if isinstance(cast_from, dict):
  222. datadict = cast_from
  223. elif type(cast_from) is type(self):
  224. datadict = cast_from.__dict__
  225. else:
  226. raise ValueError('Cannot cast from %s'%cast_from)
  227. else:
  228. datadict = data
  229. if len(datadict) > 1:
  230. raise AttributeError("Union can only store one field at a time.")
  231. for key, value in datadict.items():
  232. setattr(self, key, value)
  233. def __setattr__(self, key, value):
  234. if key == '__dict__':
  235. CythonType.__setattr__(self, key, value)
  236. elif key in self._members:
  237. self.__dict__ = {key: cast(self._members[key], value)}
  238. else:
  239. raise AttributeError("Union has no member '%s'" % key)
  240. def pointer(basetype):
  241. class PointerInstance(PointerType):
  242. _basetype = basetype
  243. return PointerInstance
  244. def array(basetype, n):
  245. class ArrayInstance(ArrayType):
  246. _basetype = basetype
  247. _n = n
  248. return ArrayInstance
  249. def struct(**members):
  250. class StructInstance(StructType):
  251. _members = members
  252. for key in members:
  253. setattr(StructInstance, key, None)
  254. return StructInstance
  255. def union(**members):
  256. class UnionInstance(UnionType):
  257. _members = members
  258. for key in members:
  259. setattr(UnionInstance, key, None)
  260. return UnionInstance
  261. class typedef(CythonType):
  262. def __init__(self, type, name=None):
  263. self._basetype = type
  264. self.name = name
  265. def __call__(self, *arg):
  266. value = cast(self._basetype, *arg)
  267. return value
  268. def __repr__(self):
  269. return self.name or str(self._basetype)
  270. __getitem__ = index_type
  271. class _FusedType(CythonType):
  272. __getitem__ = index_type
  273. def fused_type(*args):
  274. if not args:
  275. raise TypeError("Expected at least one type as argument")
  276. # Find the numeric type with biggest rank if all types are numeric
  277. rank = -1
  278. for type in args:
  279. if type not in (py_int, py_long, py_float, py_complex):
  280. break
  281. if type_ordering.index(type) > rank:
  282. result_type = type
  283. else:
  284. return result_type
  285. # Not a simple numeric type, return a fused type instance. The result
  286. # isn't really meant to be used, as we can't keep track of the context in
  287. # pure-mode. Casting won't do anything in this case.
  288. return _FusedType()
  289. def _specialized_from_args(signatures, args, kwargs):
  290. "Perhaps this should be implemented in a TreeFragment in Cython code"
  291. raise Exception("yet to be implemented")
  292. py_int = typedef(int, "int")
  293. try:
  294. py_long = typedef(long, "long")
  295. except NameError: # Py3
  296. py_long = typedef(int, "long")
  297. py_float = typedef(float, "float")
  298. py_complex = typedef(complex, "double complex")
  299. # Predefined types
  300. int_types = ['char', 'short', 'Py_UNICODE', 'int', 'Py_UCS4', 'long', 'longlong', 'Py_ssize_t', 'size_t']
  301. float_types = ['longdouble', 'double', 'float']
  302. complex_types = ['longdoublecomplex', 'doublecomplex', 'floatcomplex', 'complex']
  303. other_types = ['bint', 'void', 'Py_tss_t']
  304. to_repr = {
  305. 'longlong': 'long long',
  306. 'longdouble': 'long double',
  307. 'longdoublecomplex': 'long double complex',
  308. 'doublecomplex': 'double complex',
  309. 'floatcomplex': 'float complex',
  310. }.get
  311. gs = globals()
  312. # note: cannot simply name the unicode type here as 2to3 gets in the way and replaces it by str
  313. try:
  314. import __builtin__ as builtins
  315. except ImportError: # Py3
  316. import builtins
  317. gs['unicode'] = typedef(getattr(builtins, 'unicode', str), 'unicode')
  318. del builtins
  319. for name in int_types:
  320. reprname = to_repr(name, name)
  321. gs[name] = typedef(py_int, reprname)
  322. if name not in ('Py_UNICODE', 'Py_UCS4') and not name.endswith('size_t'):
  323. gs['u'+name] = typedef(py_int, "unsigned " + reprname)
  324. gs['s'+name] = typedef(py_int, "signed " + reprname)
  325. for name in float_types:
  326. gs[name] = typedef(py_float, to_repr(name, name))
  327. for name in complex_types:
  328. gs[name] = typedef(py_complex, to_repr(name, name))
  329. bint = typedef(bool, "bint")
  330. void = typedef(None, "void")
  331. Py_tss_t = typedef(None, "Py_tss_t")
  332. for t in int_types + float_types + complex_types + other_types:
  333. for i in range(1, 4):
  334. gs["%s_%s" % ('p'*i, t)] = gs[t]._pointer(i)
  335. NULL = gs['p_void'](0)
  336. # looks like 'gs' has some users out there by now...
  337. #del gs
  338. integral = floating = numeric = _FusedType()
  339. type_ordering = [py_int, py_long, py_float, py_complex]
  340. class CythonDotParallel(object):
  341. """
  342. The cython.parallel module.
  343. """
  344. __all__ = ['parallel', 'prange', 'threadid']
  345. def parallel(self, num_threads=None):
  346. return nogil
  347. def prange(self, start=0, stop=None, step=1, nogil=False, schedule=None, chunksize=None, num_threads=None):
  348. if stop is None:
  349. stop = start
  350. start = 0
  351. return range(start, stop, step)
  352. def threadid(self):
  353. return 0
  354. # def threadsavailable(self):
  355. # return 1
  356. import sys
  357. sys.modules['cython.parallel'] = CythonDotParallel()
  358. del sys