Cython 3.3.0__cp315-cp315-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (339) hide show
  1. Cython/Build/BuildExecutable.py +156 -0
  2. Cython/Build/Cache.py +199 -0
  3. Cython/Build/Cythonize.py +349 -0
  4. Cython/Build/Dependencies.py +1281 -0
  5. Cython/Build/Distutils.py +1 -0
  6. Cython/Build/Inline.py +467 -0
  7. Cython/Build/IpythonMagic.py +559 -0
  8. Cython/Build/SharedModule.py +165 -0
  9. Cython/Build/Tests/TestCyCache.py +195 -0
  10. Cython/Build/Tests/TestCythonizeArgsParser.py +480 -0
  11. Cython/Build/Tests/TestDependencies.py +133 -0
  12. Cython/Build/Tests/TestInline.py +177 -0
  13. Cython/Build/Tests/TestIpythonMagic.py +303 -0
  14. Cython/Build/Tests/TestRecythonize.py +212 -0
  15. Cython/Build/Tests/TestStripLiterals.py +155 -0
  16. Cython/Build/Tests/__init__.py +1 -0
  17. Cython/Build/__init__.py +11 -0
  18. Cython/CodeWriter.py +825 -0
  19. Cython/Compiler/AnalysedTreeTransforms.py +97 -0
  20. Cython/Compiler/Annotate.py +328 -0
  21. Cython/Compiler/AutoDocTransforms.py +320 -0
  22. Cython/Compiler/Buffer.py +679 -0
  23. Cython/Compiler/Builtin.py +1102 -0
  24. Cython/Compiler/CmdLine.py +373 -0
  25. Cython/Compiler/Code.cp315-win_amd64.pyd +0 -0
  26. Cython/Compiler/Code.pxd +154 -0
  27. Cython/Compiler/Code.py +3760 -0
  28. Cython/Compiler/CodeGeneration.py +33 -0
  29. Cython/Compiler/CythonScope.py +208 -0
  30. Cython/Compiler/Dataclass.py +890 -0
  31. Cython/Compiler/DebugFlags.py +24 -0
  32. Cython/Compiler/Errors.py +310 -0
  33. Cython/Compiler/ExprNodes.py +16273 -0
  34. Cython/Compiler/FlowControl.cp315-win_amd64.pyd +0 -0
  35. Cython/Compiler/FlowControl.pxd +112 -0
  36. Cython/Compiler/FlowControl.py +1573 -0
  37. Cython/Compiler/FusedNode.cp315-win_amd64.pyd +0 -0
  38. Cython/Compiler/FusedNode.py +978 -0
  39. Cython/Compiler/Future.py +16 -0
  40. Cython/Compiler/Interpreter.py +57 -0
  41. Cython/Compiler/Lexicon.py +422 -0
  42. Cython/Compiler/LineTable.cp315-win_amd64.pyd +0 -0
  43. Cython/Compiler/LineTable.py +114 -0
  44. Cython/Compiler/Main.py +856 -0
  45. Cython/Compiler/MatchCaseNodes.py +2200 -0
  46. Cython/Compiler/MemoryView.py +930 -0
  47. Cython/Compiler/ModuleNode.py +4548 -0
  48. Cython/Compiler/Naming.py +370 -0
  49. Cython/Compiler/Nodes.py +11304 -0
  50. Cython/Compiler/Optimize.py +5564 -0
  51. Cython/Compiler/Options.py +840 -0
  52. Cython/Compiler/ParseTreeTransforms.pxd +80 -0
  53. Cython/Compiler/ParseTreeTransforms.py +4808 -0
  54. Cython/Compiler/Parsing.cp315-win_amd64.pyd +0 -0
  55. Cython/Compiler/Parsing.pxd +9 -0
  56. Cython/Compiler/Parsing.py +4809 -0
  57. Cython/Compiler/Pipeline.py +439 -0
  58. Cython/Compiler/PyrexTypes.py +6588 -0
  59. Cython/Compiler/Pythran.py +232 -0
  60. Cython/Compiler/Scanning.cp315-win_amd64.pyd +0 -0
  61. Cython/Compiler/Scanning.pxd +70 -0
  62. Cython/Compiler/Scanning.py +720 -0
  63. Cython/Compiler/StringEncoding.cp315-win_amd64.pyd +0 -0
  64. Cython/Compiler/StringEncoding.py +354 -0
  65. Cython/Compiler/Symtab.py +3121 -0
  66. Cython/Compiler/Tests/TestBuffer.py +105 -0
  67. Cython/Compiler/Tests/TestBuiltin.py +196 -0
  68. Cython/Compiler/Tests/TestCmdLine.py +652 -0
  69. Cython/Compiler/Tests/TestCode.py +145 -0
  70. Cython/Compiler/Tests/TestFlowControl.py +65 -0
  71. Cython/Compiler/Tests/TestGrammar.py +202 -0
  72. Cython/Compiler/Tests/TestMemView.py +71 -0
  73. Cython/Compiler/Tests/TestParseTreeTransforms.py +285 -0
  74. Cython/Compiler/Tests/TestScanning.py +132 -0
  75. Cython/Compiler/Tests/TestSignatureMatching.py +73 -0
  76. Cython/Compiler/Tests/TestStringEncoding.py +20 -0
  77. Cython/Compiler/Tests/TestTreeFragment.py +63 -0
  78. Cython/Compiler/Tests/TestTreePath.py +103 -0
  79. Cython/Compiler/Tests/TestTypes.py +119 -0
  80. Cython/Compiler/Tests/TestUtilityLoad.py +112 -0
  81. Cython/Compiler/Tests/TestVisitor.py +119 -0
  82. Cython/Compiler/Tests/Utils.py +36 -0
  83. Cython/Compiler/Tests/__init__.py +1 -0
  84. Cython/Compiler/TreeFragment.py +279 -0
  85. Cython/Compiler/TreePath.py +303 -0
  86. Cython/Compiler/TypeInference.py +611 -0
  87. Cython/Compiler/TypeSlots.py +1329 -0
  88. Cython/Compiler/UFuncs.py +317 -0
  89. Cython/Compiler/UtilNodes.py +389 -0
  90. Cython/Compiler/UtilityCode.py +354 -0
  91. Cython/Compiler/Version.py +8 -0
  92. Cython/Compiler/Visitor.cp315-win_amd64.pyd +0 -0
  93. Cython/Compiler/Visitor.pxd +52 -0
  94. Cython/Compiler/Visitor.py +914 -0
  95. Cython/Compiler/__init__.py +1 -0
  96. Cython/Coverage.py +448 -0
  97. Cython/Debugger/Cygdb.py +214 -0
  98. Cython/Debugger/DebugWriter.py +82 -0
  99. Cython/Debugger/Tests/TestLibCython.py +280 -0
  100. Cython/Debugger/Tests/__init__.py +1 -0
  101. Cython/Debugger/Tests/cfuncs.c +8 -0
  102. Cython/Debugger/Tests/codefile +49 -0
  103. Cython/Debugger/Tests/test_libcython_in_gdb.py +580 -0
  104. Cython/Debugger/Tests/test_libpython_in_gdb.py +90 -0
  105. Cython/Debugger/__init__.py +1 -0
  106. Cython/Debugger/libcython.py +1548 -0
  107. Cython/Debugger/libpython.py +2821 -0
  108. Cython/Debugging.py +20 -0
  109. Cython/Distutils/__init__.py +2 -0
  110. Cython/Distutils/build_ext.py +143 -0
  111. Cython/Distutils/extension.py +96 -0
  112. Cython/Distutils/old_build_ext.py +351 -0
  113. Cython/Includes/cpython/__init__.pxd +173 -0
  114. Cython/Includes/cpython/array.pxd +152 -0
  115. Cython/Includes/cpython/bool.pxd +37 -0
  116. Cython/Includes/cpython/buffer.pxd +112 -0
  117. Cython/Includes/cpython/bytearray.pxd +33 -0
  118. Cython/Includes/cpython/bytes.pxd +200 -0
  119. Cython/Includes/cpython/cellobject.pxd +35 -0
  120. Cython/Includes/cpython/ceval.pxd +8 -0
  121. Cython/Includes/cpython/codecs.pxd +121 -0
  122. Cython/Includes/cpython/complex.pxd +60 -0
  123. Cython/Includes/cpython/contextvars.pxd +145 -0
  124. Cython/Includes/cpython/conversion.pxd +36 -0
  125. Cython/Includes/cpython/datetime.pxd +395 -0
  126. Cython/Includes/cpython/descr.pxd +26 -0
  127. Cython/Includes/cpython/dict.pxd +268 -0
  128. Cython/Includes/cpython/exc.pxd +263 -0
  129. Cython/Includes/cpython/fileobject.pxd +57 -0
  130. Cython/Includes/cpython/float.pxd +56 -0
  131. Cython/Includes/cpython/frozendict.pxd +37 -0
  132. Cython/Includes/cpython/function.pxd +65 -0
  133. Cython/Includes/cpython/genobject.pxd +25 -0
  134. Cython/Includes/cpython/getargs.pxd +12 -0
  135. Cython/Includes/cpython/instance.pxd +25 -0
  136. Cython/Includes/cpython/iterator.pxd +36 -0
  137. Cython/Includes/cpython/iterobject.pxd +24 -0
  138. Cython/Includes/cpython/list.pxd +144 -0
  139. Cython/Includes/cpython/long.pxd +180 -0
  140. Cython/Includes/cpython/longintrepr.pxd +14 -0
  141. Cython/Includes/cpython/mapping.pxd +63 -0
  142. Cython/Includes/cpython/marshal.pxd +66 -0
  143. Cython/Includes/cpython/mem.pxd +120 -0
  144. Cython/Includes/cpython/memoryview.pxd +50 -0
  145. Cython/Includes/cpython/method.pxd +49 -0
  146. Cython/Includes/cpython/module.pxd +208 -0
  147. Cython/Includes/cpython/number.pxd +258 -0
  148. Cython/Includes/cpython/object.pxd +430 -0
  149. Cython/Includes/cpython/pycapsule.pxd +143 -0
  150. Cython/Includes/cpython/pylifecycle.pxd +68 -0
  151. Cython/Includes/cpython/pyport.pxd +8 -0
  152. Cython/Includes/cpython/pystate.pxd +95 -0
  153. Cython/Includes/cpython/pythread.pxd +53 -0
  154. Cython/Includes/cpython/ref.pxd +141 -0
  155. Cython/Includes/cpython/sentinel.pxd +17 -0
  156. Cython/Includes/cpython/sequence.pxd +134 -0
  157. Cython/Includes/cpython/set.pxd +119 -0
  158. Cython/Includes/cpython/slice.pxd +70 -0
  159. Cython/Includes/cpython/time.pxd +129 -0
  160. Cython/Includes/cpython/tuple.pxd +72 -0
  161. Cython/Includes/cpython/type.pxd +146 -0
  162. Cython/Includes/cpython/unicode.pxd +639 -0
  163. Cython/Includes/cpython/version.pxd +32 -0
  164. Cython/Includes/cpython/weakref.pxd +78 -0
  165. Cython/Includes/libc/__init__.pxd +1 -0
  166. Cython/Includes/libc/complex.pxd +35 -0
  167. Cython/Includes/libc/errno.pxd +127 -0
  168. Cython/Includes/libc/float.pxd +43 -0
  169. Cython/Includes/libc/limits.pxd +28 -0
  170. Cython/Includes/libc/locale.pxd +46 -0
  171. Cython/Includes/libc/math.pxd +209 -0
  172. Cython/Includes/libc/setjmp.pxd +10 -0
  173. Cython/Includes/libc/signal.pxd +64 -0
  174. Cython/Includes/libc/stddef.pxd +9 -0
  175. Cython/Includes/libc/stdint.pxd +105 -0
  176. Cython/Includes/libc/stdio.pxd +80 -0
  177. Cython/Includes/libc/stdlib.pxd +72 -0
  178. Cython/Includes/libc/string.pxd +50 -0
  179. Cython/Includes/libc/threads.pxd +234 -0
  180. Cython/Includes/libc/time.pxd +52 -0
  181. Cython/Includes/libcpp/__init__.pxd +4 -0
  182. Cython/Includes/libcpp/algorithm.pxd +320 -0
  183. Cython/Includes/libcpp/any.pxd +16 -0
  184. Cython/Includes/libcpp/atomic.pxd +59 -0
  185. Cython/Includes/libcpp/barrier.pxd +22 -0
  186. Cython/Includes/libcpp/bit.pxd +29 -0
  187. Cython/Includes/libcpp/cast.pxd +12 -0
  188. Cython/Includes/libcpp/cmath.pxd +518 -0
  189. Cython/Includes/libcpp/complex.pxd +106 -0
  190. Cython/Includes/libcpp/condition_variable.pxd +322 -0
  191. Cython/Includes/libcpp/deque.pxd +165 -0
  192. Cython/Includes/libcpp/exception.pxd +216 -0
  193. Cython/Includes/libcpp/execution.pxd +15 -0
  194. Cython/Includes/libcpp/forward_list.pxd +63 -0
  195. Cython/Includes/libcpp/functional.pxd +26 -0
  196. Cython/Includes/libcpp/future.pxd +103 -0
  197. Cython/Includes/libcpp/iterator.pxd +34 -0
  198. Cython/Includes/libcpp/latch.pxd +17 -0
  199. Cython/Includes/libcpp/limits.pxd +61 -0
  200. Cython/Includes/libcpp/list.pxd +117 -0
  201. Cython/Includes/libcpp/map.pxd +252 -0
  202. Cython/Includes/libcpp/memory.pxd +115 -0
  203. Cython/Includes/libcpp/mutex.pxd +387 -0
  204. Cython/Includes/libcpp/numbers.pxd +15 -0
  205. Cython/Includes/libcpp/numeric.pxd +131 -0
  206. Cython/Includes/libcpp/optional.pxd +34 -0
  207. Cython/Includes/libcpp/pair.pxd +1 -0
  208. Cython/Includes/libcpp/queue.pxd +25 -0
  209. Cython/Includes/libcpp/random.pxd +166 -0
  210. Cython/Includes/libcpp/semaphore.pxd +43 -0
  211. Cython/Includes/libcpp/set.pxd +228 -0
  212. Cython/Includes/libcpp/shared_mutex.pxd +96 -0
  213. Cython/Includes/libcpp/span.pxd +87 -0
  214. Cython/Includes/libcpp/stack.pxd +11 -0
  215. Cython/Includes/libcpp/stop_token.pxd +117 -0
  216. Cython/Includes/libcpp/string.pxd +355 -0
  217. Cython/Includes/libcpp/string_view.pxd +183 -0
  218. Cython/Includes/libcpp/typeindex.pxd +15 -0
  219. Cython/Includes/libcpp/typeinfo.pxd +10 -0
  220. Cython/Includes/libcpp/unordered_map.pxd +193 -0
  221. Cython/Includes/libcpp/unordered_set.pxd +152 -0
  222. Cython/Includes/libcpp/utility.pxd +30 -0
  223. Cython/Includes/libcpp/vector.pxd +186 -0
  224. Cython/Includes/numpy/math.pxd +150 -0
  225. Cython/Includes/openmp.pxd +50 -0
  226. Cython/Includes/posix/__init__.pxd +1 -0
  227. Cython/Includes/posix/dlfcn.pxd +14 -0
  228. Cython/Includes/posix/fcntl.pxd +86 -0
  229. Cython/Includes/posix/ioctl.pxd +4 -0
  230. Cython/Includes/posix/mman.pxd +101 -0
  231. Cython/Includes/posix/resource.pxd +57 -0
  232. Cython/Includes/posix/select.pxd +21 -0
  233. Cython/Includes/posix/signal.pxd +73 -0
  234. Cython/Includes/posix/stat.pxd +98 -0
  235. Cython/Includes/posix/stdio.pxd +37 -0
  236. Cython/Includes/posix/stdlib.pxd +29 -0
  237. Cython/Includes/posix/strings.pxd +9 -0
  238. Cython/Includes/posix/time.pxd +71 -0
  239. Cython/Includes/posix/types.pxd +30 -0
  240. Cython/Includes/posix/uio.pxd +26 -0
  241. Cython/Includes/posix/unistd.pxd +271 -0
  242. Cython/Includes/posix/wait.pxd +38 -0
  243. Cython/LZSS.cp315-win_amd64.pyd +0 -0
  244. Cython/LZSS.py +184 -0
  245. Cython/Plex/Actions.cp315-win_amd64.pyd +0 -0
  246. Cython/Plex/Actions.pxd +24 -0
  247. Cython/Plex/Actions.py +119 -0
  248. Cython/Plex/DFA.cp315-win_amd64.pyd +0 -0
  249. Cython/Plex/DFA.pxd +14 -0
  250. Cython/Plex/DFA.py +164 -0
  251. Cython/Plex/Errors.py +48 -0
  252. Cython/Plex/Lexicons.py +178 -0
  253. Cython/Plex/Machines.cp315-win_amd64.pyd +0 -0
  254. Cython/Plex/Machines.pxd +36 -0
  255. Cython/Plex/Machines.py +238 -0
  256. Cython/Plex/Regexps.py +535 -0
  257. Cython/Plex/Scanners.cp315-win_amd64.pyd +0 -0
  258. Cython/Plex/Scanners.pxd +45 -0
  259. Cython/Plex/Scanners.py +328 -0
  260. Cython/Plex/Transitions.cp315-win_amd64.pyd +0 -0
  261. Cython/Plex/Transitions.pxd +14 -0
  262. Cython/Plex/Transitions.py +239 -0
  263. Cython/Plex/__init__.py +34 -0
  264. Cython/Runtime/__init__.py +1 -0
  265. Cython/Runtime/refnanny.cp315-win_amd64.pyd +0 -0
  266. Cython/Runtime/refnanny.pyx +237 -0
  267. Cython/Shadow.py +1174 -0
  268. Cython/StringIOTree.cp315-win_amd64.pyd +0 -0
  269. Cython/StringIOTree.py +169 -0
  270. Cython/Tempita/__init__.py +4 -0
  271. Cython/Tempita/_looper.py +154 -0
  272. Cython/Tempita/_tempita.cp315-win_amd64.pyd +0 -0
  273. Cython/Tempita/_tempita.py +1087 -0
  274. Cython/TestUtils.py +472 -0
  275. Cython/Tests/TestCodeWriter.py +128 -0
  276. Cython/Tests/TestCythonUtils.py +202 -0
  277. Cython/Tests/TestJediTyper.py +223 -0
  278. Cython/Tests/TestShadow.py +125 -0
  279. Cython/Tests/TestStringIOTree.py +68 -0
  280. Cython/Tests/TestTestUtils.py +89 -0
  281. Cython/Tests/__init__.py +1 -0
  282. Cython/Tests/xmlrunner.py +390 -0
  283. Cython/Utility/AsyncGen.c +1152 -0
  284. Cython/Utility/Buffer.c +866 -0
  285. Cython/Utility/BufferFormatFromTypeInfo.pxd +2 -0
  286. Cython/Utility/Builtins.c +1068 -0
  287. Cython/Utility/CConvert.pyx +153 -0
  288. Cython/Utility/CMath.c +104 -0
  289. Cython/Utility/CommonStructures.c +244 -0
  290. Cython/Utility/Complex.c +378 -0
  291. Cython/Utility/Coroutine.c +2344 -0
  292. Cython/Utility/CpdefEnums.pyx +119 -0
  293. Cython/Utility/CppConvert.pyx +282 -0
  294. Cython/Utility/CppSupport.cpp +151 -0
  295. Cython/Utility/CythonFunction.c +2185 -0
  296. Cython/Utility/Dataclasses.c +101 -0
  297. Cython/Utility/Embed.c +129 -0
  298. Cython/Utility/Exceptions.c +1331 -0
  299. Cython/Utility/Exceptions_Cy.pyx +109 -0
  300. Cython/Utility/ExtensionTypes.c +1199 -0
  301. Cython/Utility/FunctionArguments.c +1052 -0
  302. Cython/Utility/FusedFunction.pyx +44 -0
  303. Cython/Utility/ImportExport.c +972 -0
  304. Cython/Utility/MatchCase.c +981 -0
  305. Cython/Utility/MatchCase_Cy.pyx +12 -0
  306. Cython/Utility/MemoryView.pxd +108 -0
  307. Cython/Utility/MemoryView.pyx +1499 -0
  308. Cython/Utility/MemoryView_C.c +1056 -0
  309. Cython/Utility/ModuleSetupCode.c +3319 -0
  310. Cython/Utility/NumpyImportArray.c +46 -0
  311. Cython/Utility/ObjectHandling.c +3404 -0
  312. Cython/Utility/Optimize.c +2564 -0
  313. Cython/Utility/Overflow.c +378 -0
  314. Cython/Utility/Profile.c +736 -0
  315. Cython/Utility/StringTools.c +1534 -0
  316. Cython/Utility/Synchronization.c +438 -0
  317. Cython/Utility/TString.c +369 -0
  318. Cython/Utility/TestCyUtilityLoader.pyx +8 -0
  319. Cython/Utility/TestCythonScope.pyx +75 -0
  320. Cython/Utility/TestUtilityLoader.c +12 -0
  321. Cython/Utility/TypeConversion.c +1588 -0
  322. Cython/Utility/UFuncs.pyx +50 -0
  323. Cython/Utility/UFuncs_C.c +89 -0
  324. Cython/Utility/__init__.py +28 -0
  325. Cython/Utility/arrayarray.h +172 -0
  326. Cython/Utils.cp315-win_amd64.pyd +0 -0
  327. Cython/Utils.py +680 -0
  328. Cython/__init__.py +12 -0
  329. Cython/_shared.cp315-win_amd64.pyd +0 -0
  330. Cython/py.typed +0 -0
  331. cython-3.3.0.dist-info/METADATA +555 -0
  332. cython-3.3.0.dist-info/RECORD +339 -0
  333. cython-3.3.0.dist-info/WHEEL +5 -0
  334. cython-3.3.0.dist-info/entry_points.txt +4 -0
  335. cython-3.3.0.dist-info/top_level.txt +3 -0
  336. cython.py +29 -0
  337. pyximport/__init__.py +4 -0
  338. pyximport/pyxbuild.py +160 -0
  339. pyximport/pyximport.py +482 -0
@@ -0,0 +1,1499 @@
1
+ #################### View.MemoryView ####################
2
+
3
+ # cython: language_level=3
4
+ # cython: binding=False
5
+
6
+ # This utility provides cython.array and cython.view.memoryview
7
+
8
+ from __future__ import absolute_import
9
+
10
+ cimport cython
11
+
12
+ # from cpython cimport ...
13
+ cdef extern from "Python.h":
14
+ ctypedef struct PyObject
15
+ int PyIndex_Check(object)
16
+ PyObject *PyExc_IndexError
17
+ PyObject *PyExc_ValueError
18
+ PyObject *PyExc_TypeError
19
+
20
+ cdef extern from "pythread.h":
21
+ ctypedef void *PyThread_type_lock
22
+
23
+ PyThread_type_lock PyThread_allocate_lock()
24
+ void PyThread_free_lock(PyThread_type_lock)
25
+
26
+ cdef extern from "<string.h>":
27
+ void *memset(void *b, int c, size_t len)
28
+
29
+ cdef extern from *:
30
+ bint __PYX_CYTHON_ATOMICS_ENABLED()
31
+ bint __PYX_GET_CYTHON_COMPILING_IN_CPYTHON_FREETHREADING()
32
+ int PyObject_GetBuffer(object, Py_buffer *, int) except -1
33
+ void PyBuffer_Release(Py_buffer *)
34
+
35
+ ctypedef struct PyObject
36
+ void Py_INCREF(PyObject *)
37
+ void Py_DECREF(PyObject *)
38
+
39
+ void* PyMem_Malloc(size_t n)
40
+ void PyMem_Free(void *p)
41
+ void* PyObject_Malloc(size_t n)
42
+ void PyObject_Free(void *p)
43
+
44
+ PyObject* PyErr_SetString(PyObject* exc_type, const char* message) # No 'except NULL', callers must propagate.
45
+ PyObject* PyErr_Format(PyObject* exc_type, const char* message, ...) # No 'except NULL', callers must propagate.
46
+
47
+ ctypedef size_t uintptr_t "__pyx_uintptr_t"
48
+
49
+ cdef struct __pyx_memoryview "__pyx_memoryview_obj":
50
+ Py_buffer view
51
+ PyObject *obj
52
+ const __Pyx_TypeInfo *typeinfo
53
+
54
+ ctypedef struct {{memviewslice_name}}:
55
+ __pyx_memoryview *memview
56
+ char *data
57
+ Py_ssize_t shape[{{max_dims}}]
58
+ Py_ssize_t strides[{{max_dims}}]
59
+ Py_ssize_t suboffsets[{{max_dims}}]
60
+
61
+ void __PYX_INC_MEMVIEW({{memviewslice_name}} *memslice, int have_gil)
62
+ void __PYX_XCLEAR_MEMVIEW({{memviewslice_name}} *memslice, int have_gil)
63
+
64
+ ctypedef struct __pyx_buffer "Py_buffer":
65
+ PyObject *obj
66
+
67
+ PyObject *Py_None
68
+
69
+ cdef enum:
70
+ PyBUF_C_CONTIGUOUS,
71
+ PyBUF_F_CONTIGUOUS,
72
+ PyBUF_ANY_CONTIGUOUS
73
+ PyBUF_FORMAT
74
+ PyBUF_WRITABLE
75
+ PyBUF_STRIDES
76
+ PyBUF_INDIRECT
77
+ PyBUF_ND
78
+ PyBUF_RECORDS
79
+ PyBUF_RECORDS_RO
80
+
81
+ ctypedef struct __Pyx_TypeInfo:
82
+ pass
83
+
84
+ cdef extern from *:
85
+ """
86
+ typedef int (*__pyx_memoryview_to_dtype_func_type)(char*, PyObject*);
87
+ """
88
+ ctypedef int __pyx_atomic_int_type
89
+ {{memviewslice_name}} slice_copy_contig "__pyx_memoryview_copy_new_contig"(
90
+ {{memviewslice_name}} *from_mvs,
91
+ const char *mode, int ndim,
92
+ Py_ssize_t sizeof_dtype, int contig_flag,
93
+ bint dtype_is_object) except * nogil
94
+ bint slice_is_contig "__pyx_memviewslice_is_contig" (
95
+ {{memviewslice_name}} mvs, char order, int ndim) nogil
96
+ bint slices_overlap "__pyx_slices_overlap" ({{memviewslice_name}} *slice1,
97
+ {{memviewslice_name}} *slice2,
98
+ int ndim, size_t itemsize) nogil
99
+
100
+ ctypedef int (*to_dtype_func_type "__pyx_memoryview_to_dtype_func_type")(char *, object) except 0
101
+
102
+ int slice_memviewslice "__pyx_memoryview_slice_memviewslice" (
103
+ {{memviewslice_name}} *dst,
104
+ Py_ssize_t shape, Py_ssize_t stride, Py_ssize_t suboffset,
105
+ int dim, int new_ndim, int *suboffset_dim,
106
+ Py_ssize_t start, Py_ssize_t stop, Py_ssize_t step,
107
+ int have_start, int have_stop, int have_step,
108
+ bint is_slice) except -1 nogil
109
+
110
+
111
+ cdef extern from "<stdlib.h>":
112
+ void *malloc(size_t) nogil
113
+ void free(void *) nogil
114
+ void *memcpy(void *dest, void *src, size_t n) nogil
115
+
116
+ # the sequence abstract base class
117
+ cdef object __pyx_collections_abc_Sequence "__pyx_collections_abc_Sequence"
118
+ try:
119
+ __pyx_collections_abc_Sequence = __import__("collections.abc").abc.Sequence
120
+ except:
121
+ # it isn't a big problem if this fails
122
+ __pyx_collections_abc_Sequence = None
123
+
124
+ #
125
+ ### cython.array class
126
+ #
127
+
128
+ @cython.collection_type("sequence")
129
+ @cname("__pyx_array")
130
+ cdef class array:
131
+
132
+ cdef:
133
+ char *data
134
+ Py_ssize_t len
135
+ char *format
136
+ int ndim
137
+ Py_ssize_t *_shape
138
+ Py_ssize_t *_strides
139
+ Py_ssize_t itemsize
140
+ unicode mode # FIXME: this should have been a simple 'char'
141
+ bytes _format
142
+ void (*callback_free_data)(void *data) noexcept
143
+ # cdef object _memview
144
+ cdef bint free_data
145
+ cdef bint dtype_is_object
146
+
147
+ def __cinit__(array self, tuple shape, Py_ssize_t itemsize, format not None,
148
+ mode="c", bint allocate_buffer=True):
149
+
150
+ cdef int idx
151
+ cdef Py_ssize_t dim
152
+
153
+ self.ndim = <int> len(shape)
154
+ self.itemsize = itemsize
155
+
156
+ if cython.unlikely(not self.ndim):
157
+ _err_ValueError("Empty shape tuple for cython.array")
158
+
159
+ if cython.unlikely(itemsize <= 0):
160
+ _err_ValueError("itemsize <= 0 for cython.array")
161
+
162
+ if not isinstance(format, bytes):
163
+ format = format.encode('ASCII')
164
+ self._format = format # keep a reference to the byte string
165
+ self.format = self._format
166
+
167
+ # use single malloc() for both shape and strides
168
+ self._shape = <Py_ssize_t *> PyObject_Malloc(sizeof(Py_ssize_t) * 2 * <size_t> self.ndim)
169
+ self._strides = self._shape + self.ndim
170
+
171
+ if not self._shape:
172
+ raise MemoryError, "unable to allocate shape and strides."
173
+
174
+ # cdef Py_ssize_t dim, stride
175
+ for idx, dim in enumerate(shape):
176
+ if dim <= 0:
177
+ raise ValueError, f"Invalid shape in axis {idx}: {dim}."
178
+ self._shape[idx] = dim
179
+
180
+ cdef char order
181
+ if mode == 'c':
182
+ order = b'C'
183
+ self.mode = u'c'
184
+ elif mode == 'fortran':
185
+ order = b'F'
186
+ self.mode = u'fortran'
187
+ else:
188
+ raise ValueError, f"Invalid mode, expected 'c' or 'fortran', got {mode}"
189
+
190
+ self.len = fill_contig_strides_array(self._shape, self._strides, itemsize, self.ndim, order)
191
+
192
+ self.free_data = allocate_buffer
193
+ self.dtype_is_object = format == b'O'
194
+
195
+ if allocate_buffer:
196
+ _allocate_buffer(self)
197
+
198
+ @cname('getbuffer')
199
+ def __getbuffer__(self, Py_buffer *info, int flags):
200
+ cdef int bufmode = -1
201
+ if flags & (PyBUF_C_CONTIGUOUS | PyBUF_F_CONTIGUOUS | PyBUF_ANY_CONTIGUOUS):
202
+ if self.mode == u"c":
203
+ bufmode = PyBUF_C_CONTIGUOUS | PyBUF_ANY_CONTIGUOUS
204
+ elif self.mode == u"fortran":
205
+ bufmode = PyBUF_F_CONTIGUOUS | PyBUF_ANY_CONTIGUOUS
206
+ if cython.unlikely(not (flags & bufmode)):
207
+ _err_ValueError("Can only create a buffer that is contiguous in memory.")
208
+ info.buf = self.data
209
+ info.len = self.len
210
+
211
+ if flags & PyBUF_STRIDES:
212
+ info.ndim = self.ndim
213
+ info.shape = self._shape
214
+ info.strides = self._strides
215
+ else:
216
+ info.ndim = 1
217
+ info.shape = &self.len if flags & PyBUF_ND else NULL
218
+ info.strides = NULL
219
+
220
+ info.suboffsets = NULL
221
+ info.itemsize = self.itemsize
222
+ info.readonly = 0
223
+ info.format = self.format if flags & PyBUF_FORMAT else NULL
224
+ info.obj = self
225
+
226
+ def __dealloc__(array self):
227
+ if self.callback_free_data != NULL:
228
+ self.callback_free_data(self.data)
229
+ elif self.free_data and self.data is not NULL:
230
+ if self.dtype_is_object:
231
+ refcount_objects_in_slice(self.data, self._shape, self._strides, self.ndim, inc=False)
232
+ free(self.data)
233
+ PyObject_Free(self._shape)
234
+
235
+ @property
236
+ def memview(self):
237
+ return array.get_memview(self)
238
+
239
+ # treat as cython_final
240
+ @cname('get_memview')
241
+ cdef get_memview(self):
242
+ flags = PyBUF_ANY_CONTIGUOUS|PyBUF_FORMAT|PyBUF_WRITABLE
243
+ return memoryview(self, flags, self.dtype_is_object)
244
+
245
+ def __len__(self):
246
+ return self._shape[0]
247
+
248
+ def __getattr__(self, attr):
249
+ return getattr(self.memview, attr)
250
+
251
+ def __getitem__(self, item):
252
+ return self.memview[item]
253
+
254
+ def __setitem__(self, item, value):
255
+ self.memview[item] = value
256
+
257
+ # Sequence methods
258
+ try:
259
+ count = __pyx_collections_abc_Sequence.count
260
+ index = __pyx_collections_abc_Sequence.index
261
+ except:
262
+ pass
263
+
264
+ @cname("__pyx_array_allocate_buffer")
265
+ cdef int _allocate_buffer(array self) except -1:
266
+ # use malloc() for backwards compatibility
267
+ # in case external code wants to change the data pointer
268
+ cdef Py_ssize_t i
269
+ cdef PyObject **p
270
+
271
+ self.free_data = True
272
+ self.data = <char *>malloc(<size_t> self.len)
273
+ if not self.data:
274
+ raise MemoryError, "unable to allocate array data."
275
+
276
+ if self.dtype_is_object:
277
+ p = <PyObject **> self.data
278
+ for i in range(self.len // self.itemsize):
279
+ p[i] = Py_None
280
+ Py_INCREF(Py_None)
281
+ return 0
282
+
283
+
284
+ @cname("__pyx_array_new")
285
+ cdef array array_cwrapper(tuple shape, Py_ssize_t itemsize, char *format, const char *c_mode, char *buf):
286
+ cdef array result
287
+ cdef str mode = "fortran" if c_mode[0] == b'f' else "c" # this often comes from a constant C string.
288
+
289
+ if buf is NULL:
290
+ result = array.__new__(array, shape, itemsize, format, mode)
291
+ else:
292
+ result = array.__new__(array, shape, itemsize, format, mode, allocate_buffer=False)
293
+ result.data = buf
294
+
295
+ return result
296
+
297
+
298
+ #
299
+ ### Memoryview constants and cython.view.memoryview class
300
+ #
301
+
302
+ # Disable generic_contiguous, as it makes trouble verifying contiguity:
303
+ # - 'contiguous' or '::1' means the dimension is contiguous with dtype
304
+ # - 'indirect_contiguous' means a contiguous list of pointers
305
+ # - dtype contiguous must be contiguous in the first or last dimension
306
+ # from the start, or from the dimension following the last indirect dimension
307
+ #
308
+ # e.g.
309
+ # int[::indirect_contiguous, ::contiguous, :]
310
+ #
311
+ # is valid (list of pointers to 2d fortran-contiguous array), but
312
+ #
313
+ # int[::generic_contiguous, ::contiguous, :]
314
+ #
315
+ # would mean you'd have assert dimension 0 to be indirect (and pointer contiguous) at runtime.
316
+ # So it doesn't bring any performance benefit, and it's only confusing.
317
+
318
+ @cname('__pyx_MemviewEnum')
319
+ cdef class Enum(object):
320
+ cdef object name
321
+ def __init__(self, name):
322
+ self.name = name
323
+ def __repr__(self):
324
+ return self.name
325
+
326
+ cdef generic = Enum("<strided and direct or indirect>")
327
+ cdef strided = Enum("<strided and direct>") # default
328
+ cdef indirect = Enum("<strided and indirect>")
329
+ # Disable generic_contiguous, as it is a troublemaker
330
+ #cdef generic_contiguous = Enum("<contiguous and direct or indirect>")
331
+ cdef contiguous = Enum("<contiguous and direct>")
332
+ cdef indirect_contiguous = Enum("<contiguous and indirect>")
333
+
334
+ # 'follow' is implied when the first or last axis is ::1
335
+
336
+
337
+ # pre-allocate thread locks for reuse
338
+ ## note that this could be implemented in a more beautiful way in "normal" Cython,
339
+ ## but this code gets merged into the user module and not everything works there.
340
+ cdef int __pyx_memoryview_thread_locks_used = 0
341
+ cdef PyThread_type_lock[{{THREAD_LOCKS_PREALLOCATED}}] __pyx_memoryview_thread_locks = [
342
+ {{for _ in range(THREAD_LOCKS_PREALLOCATED)}}
343
+ PyThread_allocate_lock(),
344
+ {{endfor}}
345
+ ]
346
+
347
+
348
+ @cname('__pyx_memoryview')
349
+ cdef class memoryview:
350
+
351
+ cdef object obj
352
+ cdef object _size
353
+ # This comes before acquisition_count so can't be removed without breaking ABI compatibility
354
+ cdef void* _unused
355
+ cdef PyThread_type_lock lock
356
+ cdef __pyx_atomic_int_type acquisition_count
357
+ cdef Py_buffer view
358
+ cdef int flags
359
+ cdef bint dtype_is_object
360
+ cdef const __Pyx_TypeInfo *typeinfo
361
+
362
+ def __cinit__(memoryview self, object obj, int flags, bint dtype_is_object=False):
363
+ self.obj = obj
364
+ self.flags = flags
365
+ if type(self) is memoryview or obj is not None:
366
+ PyObject_GetBuffer(obj, &self.view, flags)
367
+ if <PyObject *> self.view.obj == NULL:
368
+ (<__pyx_buffer *> &self.view).obj = Py_None
369
+ Py_INCREF(Py_None)
370
+
371
+ if not __PYX_CYTHON_ATOMICS_ENABLED():
372
+ global __pyx_memoryview_thread_locks_used
373
+ if (__pyx_memoryview_thread_locks_used < {{THREAD_LOCKS_PREALLOCATED}} and
374
+ # preallocated locks cannot be made thread-safe in freethreading
375
+ not __PYX_GET_CYTHON_COMPILING_IN_CPYTHON_FREETHREADING()):
376
+ self.lock = __pyx_memoryview_thread_locks[__pyx_memoryview_thread_locks_used]
377
+ __pyx_memoryview_thread_locks_used += 1
378
+ if self.lock is NULL:
379
+ self.lock = PyThread_allocate_lock()
380
+ if self.lock is NULL:
381
+ raise MemoryError
382
+
383
+ if flags & PyBUF_FORMAT:
384
+ self.dtype_is_object = (self.view.format[0] == b'O' and self.view.format[1] == b'\0')
385
+ else:
386
+ self.dtype_is_object = dtype_is_object
387
+
388
+ with cython.cdivision(True):
389
+ assert <uintptr_t>(&self.acquisition_count) % sizeof(__pyx_atomic_int_type) == 0
390
+ self.typeinfo = NULL
391
+
392
+ def __dealloc__(memoryview self):
393
+ if self.obj is not None:
394
+ PyBuffer_Release(&self.view)
395
+ elif (<__pyx_buffer *> &self.view).obj == Py_None:
396
+ # Undo the incref in __cinit__() above.
397
+ (<__pyx_buffer *> &self.view).obj = NULL
398
+ Py_DECREF(Py_None)
399
+
400
+ cdef int i
401
+ global __pyx_memoryview_thread_locks_used
402
+ if self.lock != NULL:
403
+ for i in range(0 if __PYX_GET_CYTHON_COMPILING_IN_CPYTHON_FREETHREADING() else __pyx_memoryview_thread_locks_used):
404
+ if __pyx_memoryview_thread_locks[i] is self.lock:
405
+ __pyx_memoryview_thread_locks_used -= 1
406
+ if i != __pyx_memoryview_thread_locks_used:
407
+ __pyx_memoryview_thread_locks[i], __pyx_memoryview_thread_locks[__pyx_memoryview_thread_locks_used] = (
408
+ __pyx_memoryview_thread_locks[__pyx_memoryview_thread_locks_used], __pyx_memoryview_thread_locks[i])
409
+ break
410
+ else:
411
+ PyThread_free_lock(self.lock)
412
+
413
+ # treat as cython.final
414
+ cdef char *get_item_pointer(memoryview self, index: tuple) except NULL:
415
+ cdef Py_ssize_t dim
416
+ cdef char *itemp = <char *> self.view.buf
417
+
418
+ for dim, idx in enumerate(index):
419
+ itemp = pybuffer_index(&self.view, itemp, idx, dim)
420
+
421
+ return itemp
422
+
423
+ #@cname('__pyx_memoryview_getitem')
424
+ def __getitem__(memoryview self, object index):
425
+ cdef char *buffer
426
+ if self.view.ndim == 1 and isinstance(index, int):
427
+ buffer = <char *> self.view.buf
428
+ return self.convert_item_to_object(
429
+ pybuffer_index(&self.view, buffer, index, 0))
430
+
431
+ if index is Ellipsis:
432
+ return self
433
+
434
+ have_slices, indices = _unellipsify(index, self.view.ndim)
435
+
436
+ cdef char *itemp
437
+ if have_slices:
438
+ return memview_slice(self, indices)
439
+ else:
440
+ itemp = memoryview.get_item_pointer(self, <tuple> indices)
441
+ return self.convert_item_to_object(itemp)
442
+
443
+ def __setitem__(memoryview self, object index, object value):
444
+ if self.view.readonly:
445
+ raise TypeError, "Cannot assign to read-only memoryview"
446
+
447
+ if self.view.ndim == 1 and isinstance(index, int):
448
+ memoryview.setitem_indexed1(self, index, value)
449
+ return
450
+
451
+ have_slices, indices = _unellipsify(index, self.view.ndim)
452
+
453
+ if have_slices:
454
+ obj = memoryview.is_slice(self, value)
455
+ target_slice = memview_slice(self, indices)
456
+ if obj is not None:
457
+ memoryview.setitem_slice_assignment(self, target_slice, obj)
458
+ else:
459
+ memoryview.setitem_slice_assign_scalar(self, target_slice, value)
460
+ else:
461
+ memoryview.setitem_indexed(self, <tuple> indices, value)
462
+
463
+ # treat as cython.final
464
+ cdef is_slice(self, obj):
465
+ if not isinstance(obj, memoryview):
466
+ try:
467
+ obj = memoryview(obj, self.flags & ~PyBUF_WRITABLE | PyBUF_ANY_CONTIGUOUS,
468
+ self.dtype_is_object)
469
+ except TypeError:
470
+ return None
471
+
472
+ return obj
473
+
474
+ # treat as cython.final
475
+ cdef setitem_slice_assignment(self, dst, src):
476
+ cdef {{memviewslice_name}} dst_slice
477
+ cdef {{memviewslice_name}} src_slice
478
+ cdef {{memviewslice_name}} msrc = get_slice_from_memview(src, &src_slice)[0]
479
+ cdef {{memviewslice_name}} mdst = get_slice_from_memview(dst, &dst_slice)[0]
480
+
481
+ memoryview_copy_contents(msrc, mdst, src.ndim, dst.ndim, self.dtype_is_object)
482
+
483
+ # treat as cython.final
484
+ cdef setitem_slice_assign_scalar(self, memoryview dst, value):
485
+ cdef int array[128]
486
+ cdef void *tmp = NULL
487
+ cdef void *item
488
+
489
+ cdef {{memviewslice_name}} *dst_slice
490
+ cdef {{memviewslice_name}} tmp_slice
491
+ dst_slice = get_slice_from_memview(dst, &tmp_slice)
492
+
493
+ if <size_t>self.view.itemsize > sizeof(array):
494
+ tmp = PyMem_Malloc(<size_t> self.view.itemsize)
495
+ if tmp == NULL:
496
+ raise MemoryError
497
+ item = tmp
498
+ else:
499
+ item = <void *> array
500
+
501
+ try:
502
+ if self.dtype_is_object:
503
+ (<PyObject **> item)[0] = <PyObject *> value
504
+ else:
505
+ self.assign_item_from_object(<char *> item, value)
506
+
507
+ # It would be easy to support indirect dimensions, but it's easier
508
+ # to disallow :)
509
+ if self.view.suboffsets != NULL:
510
+ assert_direct_dimensions(self.view.suboffsets, self.view.ndim)
511
+ slice_assign_scalar(dst_slice, dst.view.ndim, <size_t> self.view.itemsize,
512
+ item, self.dtype_is_object)
513
+ finally:
514
+ PyMem_Free(tmp)
515
+
516
+ # treat as cython.final
517
+ cdef setitem_indexed(self, indices: tuple, value):
518
+ cdef char *itemp = memoryview.get_item_pointer(self, indices)
519
+ self.assign_item_from_object(itemp, value)
520
+
521
+ # treat as cython.final
522
+ cdef setitem_indexed1(self, index, value):
523
+ cdef char *buffer = <char *> self.view.buf
524
+ cdef char *itemp = pybuffer_index(&self.view, buffer, index, 0)
525
+ self.assign_item_from_object(itemp, value)
526
+
527
+ cdef convert_item_to_object(self, char *itemp):
528
+ """Only used if instantiated manually by the user, or if Cython doesn't
529
+ know how to convert the type"""
530
+ import struct
531
+ cdef bytes bytesitem
532
+ # Do a manual and complete check here instead of this easy hack
533
+ bytesitem = itemp[:self.view.itemsize]
534
+ try:
535
+ result = struct.unpack(self.view.format, bytesitem)
536
+ except struct.error:
537
+ _err_ValueError("Unable to convert item to object")
538
+ else:
539
+ if len(self.view.format) == 1:
540
+ return result[0]
541
+ return result
542
+
543
+ cdef assign_item_from_object(self, char *itemp, object value):
544
+ """Only used if instantiated manually by the user, or if Cython doesn't
545
+ know how to convert the type"""
546
+ import struct
547
+ cdef char c
548
+ cdef bytes bytesvalue
549
+ cdef Py_ssize_t i
550
+
551
+ if isinstance(value, tuple):
552
+ bytesvalue = struct.pack(self.view.format, *value)
553
+ else:
554
+ bytesvalue = struct.pack(self.view.format, value)
555
+
556
+ for i, c in enumerate(bytesvalue):
557
+ itemp[i] = c
558
+
559
+ @cname('getbuffer')
560
+ def __getbuffer__(self, Py_buffer *info, int flags):
561
+ if cython.unlikely(flags & PyBUF_WRITABLE and self.view.readonly):
562
+ _err_ValueError("Cannot create writable memory view from read-only memoryview")
563
+
564
+ if flags & PyBUF_ND:
565
+ info.shape = self.view.shape
566
+ else:
567
+ info.shape = NULL
568
+
569
+ if flags & PyBUF_STRIDES:
570
+ info.strides = self.view.strides
571
+ else:
572
+ info.strides = NULL
573
+
574
+ if flags & PyBUF_INDIRECT:
575
+ info.suboffsets = self.view.suboffsets
576
+ else:
577
+ info.suboffsets = NULL
578
+
579
+ if flags & PyBUF_FORMAT:
580
+ info.format = self.view.format
581
+ else:
582
+ info.format = NULL
583
+
584
+ info.buf = self.view.buf
585
+ info.ndim = self.view.ndim
586
+ info.itemsize = self.view.itemsize
587
+ info.len = self.view.len
588
+ info.readonly = self.view.readonly
589
+ info.obj = self
590
+
591
+ # Some properties that have the same semantics as in NumPy
592
+ @property
593
+ def T(self):
594
+ cdef _memoryviewslice result = memoryview_copy(self)
595
+ transpose_memslice(&result.from_slice)
596
+ return result
597
+
598
+ @property
599
+ def base(self):
600
+ return self._get_base()
601
+
602
+ cdef _get_base(self):
603
+ return self.obj
604
+
605
+ @property
606
+ def shape(self):
607
+ return tuple([length for length in self.view.shape[:self.view.ndim]])
608
+
609
+ @property
610
+ def strides(self):
611
+ if cython.unlikely(self.view.strides == NULL):
612
+ # Note: we always ask for strides, so if this is not set it's a bug
613
+ _err_ValueError("Buffer view does not expose strides")
614
+
615
+ return tuple([stride for stride in self.view.strides[:self.view.ndim]])
616
+
617
+ @property
618
+ def suboffsets(self):
619
+ if self.view.suboffsets == NULL:
620
+ return (-1,) * self.view.ndim
621
+
622
+ return tuple([suboffset for suboffset in self.view.suboffsets[:self.view.ndim]])
623
+
624
+ @property
625
+ def ndim(self):
626
+ return self.view.ndim
627
+
628
+ @property
629
+ def itemsize(self):
630
+ return self.view.itemsize
631
+
632
+ @property
633
+ def nbytes(self):
634
+ return self.size * self.view.itemsize
635
+
636
+ @property
637
+ def size(self):
638
+ cdef size_t result
639
+ if self._size is None:
640
+ result = 1
641
+
642
+ for length in self.view.shape[:self.view.ndim]:
643
+ result *= <size_t> length
644
+
645
+ self._size = result
646
+
647
+ return self._size
648
+
649
+ def __len__(self):
650
+ if self.view.ndim >= 1:
651
+ return self.view.shape[0]
652
+
653
+ return 0
654
+
655
+ def __repr__(self):
656
+ return "<MemoryView of %r at 0x%x>" % (self.base.__class__.__name__,
657
+ id(self))
658
+
659
+ def __str__(self):
660
+ return "<MemoryView of %r object>" % (self.base.__class__.__name__,)
661
+
662
+ # Support the same attributes as memoryview slices
663
+ def is_c_contig(self):
664
+ cdef {{memviewslice_name}} *mslice
665
+ cdef {{memviewslice_name}} tmp
666
+ mslice = get_slice_from_memview(self, &tmp)
667
+ return slice_is_contig(mslice[0], 'C', self.view.ndim)
668
+
669
+ def is_f_contig(self):
670
+ cdef {{memviewslice_name}} *mslice
671
+ cdef {{memviewslice_name}} tmp
672
+ mslice = get_slice_from_memview(self, &tmp)
673
+ return slice_is_contig(mslice[0], 'F', self.view.ndim)
674
+
675
+ def copy(self):
676
+ cdef {{memviewslice_name}} mslice
677
+ cdef int flags = self.flags & ~PyBUF_F_CONTIGUOUS
678
+
679
+ slice_copy(self, &mslice)
680
+ mslice = slice_copy_contig(&mslice, "c", self.view.ndim,
681
+ self.view.itemsize,
682
+ flags|PyBUF_C_CONTIGUOUS,
683
+ self.dtype_is_object)
684
+
685
+ return memoryview_copy_from_slice(self, &mslice)
686
+
687
+ def copy_fortran(self):
688
+ cdef {{memviewslice_name}} src, dst
689
+ cdef int flags = self.flags & ~PyBUF_C_CONTIGUOUS
690
+
691
+ slice_copy(self, &src)
692
+ dst = slice_copy_contig(&src, "fortran", self.view.ndim,
693
+ self.view.itemsize,
694
+ flags|PyBUF_F_CONTIGUOUS,
695
+ self.dtype_is_object)
696
+
697
+ return memoryview_copy_from_slice(self, &dst)
698
+
699
+
700
+ @cname('__pyx_memoryview_new')
701
+ cdef memoryview_cwrapper(object o, int flags, bint dtype_is_object, const __Pyx_TypeInfo *typeinfo):
702
+ cdef memoryview result = memoryview(o, flags, dtype_is_object)
703
+ result.typeinfo = typeinfo
704
+ return result
705
+
706
+ @cname('__pyx_memoryview_check')
707
+ cdef inline bint memoryview_check(object o) noexcept:
708
+ return isinstance(o, memoryview)
709
+
710
+
711
+ @cname('__pyx_memoryview_err_invalid_index')
712
+ cdef int _err_invalid_index(item) except -1:
713
+ type_name = str(type(item))
714
+ PyErr_Format(PyExc_TypeError, "Cannot index with type '%.200U'", <PyObject*> type_name)
715
+ return -1
716
+
717
+
718
+ cdef tuple[bint, tuple] _unellipsify_index_tuple(index_tuple: tuple, int ndim):
719
+ """
720
+ Replace all ellipses with full slices and fill incomplete indices with full slices.
721
+ """
722
+ cdef Py_ssize_t first_ellipsis_index = -1
723
+ cdef Py_ssize_t idx, ellipsis_end, indices_from_ellipsis
724
+
725
+ have_slices = False
726
+
727
+ idx = 0
728
+ for item in index_tuple:
729
+ if item is Ellipsis:
730
+ have_slices = True
731
+ if first_ellipsis_index == -1:
732
+ first_ellipsis_index = idx
733
+ elif isinstance(item, slice):
734
+ have_slices = True
735
+ elif cython.unlikely(not PyIndex_Check(item)):
736
+ _err_invalid_index(item)
737
+ idx += 1
738
+
739
+ if first_ellipsis_index >= 0:
740
+ result = [slice(None)] * ndim
741
+
742
+ for idx in range(first_ellipsis_index):
743
+ result[idx] = index_tuple[idx]
744
+
745
+ indices_from_ellipsis = len(index_tuple) - first_ellipsis_index
746
+ ellipsis_end = ndim - indices_from_ellipsis
747
+
748
+ for idx in range(1, indices_from_ellipsis):
749
+ item = index_tuple[first_ellipsis_index + idx]
750
+ if item is not Ellipsis:
751
+ result[ellipsis_end + idx] = item
752
+
753
+ index_tuple = tuple(result)
754
+
755
+ elif ndim > idx:
756
+ # Fill missing indices at the end with full slice.
757
+ have_slices = True
758
+ index_tuple += (slice(None),) * (ndim - idx)
759
+
760
+ return have_slices, index_tuple
761
+
762
+
763
+ cdef tuple[bint, tuple] _unellipsify(object index, int ndim):
764
+ """
765
+ Replace all ellipses with full slices and fill incomplete indices with full slices.
766
+ """
767
+ result: tuple
768
+ have_slices = False
769
+
770
+ if index is Ellipsis:
771
+ have_slices = True
772
+ result = (slice(None),) * ndim
773
+
774
+ elif isinstance(index, tuple):
775
+ return _unellipsify_index_tuple(<tuple> index, ndim)
776
+
777
+ else:
778
+ if isinstance(index, slice):
779
+ have_slices = True
780
+ elif cython.unlikely(not PyIndex_Check(index)):
781
+ _err_invalid_index(index)
782
+
783
+ # 1-2 dim are so common that they merit a special case.
784
+ if ndim == 1:
785
+ result = (index,)
786
+ elif ndim == 2:
787
+ have_slices = True
788
+ result = (index, slice(None))
789
+ else:
790
+ have_slices = True
791
+ result = (index,) + (slice(None),) * (ndim - 1)
792
+
793
+ return have_slices, result
794
+
795
+
796
+ cdef int assert_direct_dimensions(Py_ssize_t *suboffsets, int ndim) except -1:
797
+ for suboffset in suboffsets[:ndim]:
798
+ if cython.unlikely(suboffset >= 0):
799
+ _err_ValueError("Indirect dimensions not supported")
800
+ return 0 # return type just used as an error flag
801
+
802
+ #
803
+ ### Slicing a memoryview
804
+ #
805
+
806
+ @cname('__pyx_memview_slice')
807
+ cdef memoryview memview_slice(memoryview memview, tuple indices):
808
+ cdef int new_ndim = 0, suboffset_dim = -1, dim
809
+ cdef bint negative_step
810
+ cdef {{memviewslice_name}} src, dst
811
+ cdef {{memviewslice_name}} *p_src
812
+
813
+ # dst is copied by value in memoryview_fromslice -- initialize it
814
+ # src is never copied
815
+ memset(&dst, 0, sizeof(dst))
816
+
817
+ cdef _memoryviewslice memviewsliceobj
818
+
819
+ assert memview.view.ndim > 0
820
+
821
+ if isinstance(memview, _memoryviewslice):
822
+ memviewsliceobj = memview
823
+ p_src = &memviewsliceobj.from_slice
824
+ else:
825
+ slice_copy(memview, &src)
826
+ p_src = &src
827
+
828
+ # Note: don't use variable src at this point
829
+ # SubNote: we should be able to declare variables in blocks...
830
+
831
+ # memoryview_fromslice() will inc our dst slice
832
+ dst.memview = p_src.memview
833
+ dst.data = p_src.data
834
+
835
+ # Put everything in temps to avoid this bloody warning:
836
+ # "Argument evaluation order in C function call is undefined and
837
+ # may not be as expected"
838
+ cdef {{memviewslice_name}} *p_dst = &dst
839
+ cdef int *p_suboffset_dim = &suboffset_dim
840
+ cdef Py_ssize_t start, stop, step, cindex
841
+ cdef bint have_start, have_stop, have_step
842
+
843
+ for dim, index in enumerate(indices):
844
+ if PyIndex_Check(index):
845
+ cindex = index
846
+ slice_memviewslice(
847
+ p_dst, p_src.shape[dim], p_src.strides[dim], p_src.suboffsets[dim],
848
+ dim, new_ndim, p_suboffset_dim,
849
+ cindex, 0, 0, # start, stop, step
850
+ 0, 0, 0, # have_{start,stop,step}
851
+ False)
852
+ elif index is None:
853
+ p_dst.shape[new_ndim] = 1
854
+ p_dst.strides[new_ndim] = 0
855
+ p_dst.suboffsets[new_ndim] = -1
856
+ new_ndim += 1
857
+ else:
858
+ start = index.start or 0
859
+ stop = index.stop or 0
860
+ step = index.step or 0
861
+
862
+ have_start = index.start is not None
863
+ have_stop = index.stop is not None
864
+ have_step = index.step is not None
865
+
866
+ slice_memviewslice(
867
+ p_dst, p_src.shape[dim], p_src.strides[dim], p_src.suboffsets[dim],
868
+ dim, new_ndim, p_suboffset_dim,
869
+ start, stop, step,
870
+ have_start, have_stop, have_step,
871
+ True)
872
+ new_ndim += 1
873
+
874
+ if isinstance(memview, _memoryviewslice):
875
+ return memoryview_fromslice(dst, new_ndim,
876
+ memviewsliceobj.to_object_func,
877
+ memviewsliceobj.to_dtype_func,
878
+ memview.dtype_is_object)
879
+ else:
880
+ return memoryview_fromslice(dst, new_ndim, NULL, NULL,
881
+ memview.dtype_is_object)
882
+
883
+
884
+ #
885
+ ### Index a memoryview
886
+ #
887
+ @cname('__pyx_pybuffer_index')
888
+ cdef char *pybuffer_index(Py_buffer *view, char *bufp, Py_ssize_t index,
889
+ Py_ssize_t dim) except NULL:
890
+ cdef Py_ssize_t shape, stride, suboffset = -1
891
+ cdef Py_ssize_t itemsize = view.itemsize
892
+ cdef char *resultp
893
+
894
+ if view.ndim == 0:
895
+ shape = view.len // itemsize
896
+ stride = itemsize
897
+ else:
898
+ shape = view.shape[dim]
899
+ stride = view.strides[dim]
900
+ if view.suboffsets != NULL:
901
+ suboffset = view.suboffsets[dim]
902
+
903
+ if index < 0:
904
+ index += view.shape[dim]
905
+ if cython.unlikely(index < 0):
906
+ _err_IndexError("Out of bounds on buffer access (axis %zd)", dim)
907
+
908
+ if cython.unlikely(index >= shape):
909
+ _err_IndexError("Out of bounds on buffer access (axis %zd)", dim)
910
+
911
+ resultp = bufp + index * stride
912
+ if suboffset >= 0:
913
+ resultp = (<char **> resultp)[0] + suboffset
914
+
915
+ return resultp
916
+
917
+ #
918
+ ### Transposing a memoryviewslice
919
+ #
920
+ @cname('__pyx_memslice_transpose')
921
+ cdef int transpose_memslice({{memviewslice_name}} *memslice) except -1 nogil:
922
+ cdef int ndim = memslice.memview.view.ndim
923
+
924
+ cdef Py_ssize_t *shape = memslice.shape
925
+ cdef Py_ssize_t *strides = memslice.strides
926
+
927
+ # reverse strides and shape
928
+ cdef int i, j
929
+ for i in range(ndim // 2):
930
+ j = ndim - 1 - i
931
+ strides[i], strides[j] = strides[j], strides[i]
932
+ shape[i], shape[j] = shape[j], shape[i]
933
+
934
+ if cython.unlikely(memslice.suboffsets[i] >= 0 or memslice.suboffsets[j] >= 0):
935
+ _err(PyExc_ValueError, "Cannot transpose memoryview with indirect dimensions")
936
+
937
+ return 0
938
+
939
+ #
940
+ ### Creating new memoryview objects from slices and memoryviews
941
+ #
942
+ @cython.collection_type("sequence")
943
+ @cname('__pyx_memoryviewslice')
944
+ cdef class _memoryviewslice(memoryview):
945
+ "Internal class for passing memoryview slices to Python"
946
+
947
+ # We need this to keep our shape/strides/suboffset pointers valid
948
+ cdef {{memviewslice_name}} from_slice
949
+ # We need this only to print it's class' name
950
+ cdef object from_object
951
+
952
+ cdef object (*to_object_func)(char *)
953
+ cdef to_dtype_func_type to_dtype_func
954
+
955
+ def __dealloc__(self):
956
+ __PYX_XCLEAR_MEMVIEW(&self.from_slice, 1)
957
+
958
+ cdef convert_item_to_object(self, char *itemp):
959
+ if self.to_object_func != NULL:
960
+ return self.to_object_func(itemp)
961
+ else:
962
+ return memoryview.convert_item_to_object(self, itemp)
963
+
964
+ cdef assign_item_from_object(self, char *itemp, object value):
965
+ if self.to_dtype_func != NULL:
966
+ self.to_dtype_func(itemp, value)
967
+ else:
968
+ memoryview.assign_item_from_object(self, itemp, value)
969
+
970
+ cdef _get_base(self):
971
+ return self.from_object
972
+
973
+ # Sequence methods
974
+ try:
975
+ count = __pyx_collections_abc_Sequence.count
976
+ index = __pyx_collections_abc_Sequence.index
977
+ except:
978
+ pass
979
+
980
+ try:
981
+ if __pyx_collections_abc_Sequence:
982
+ # The main value of registering _memoryviewslice as a
983
+ # Sequence is that it can be used in structural pattern
984
+ # matching in Python 3.10+
985
+ __pyx_collections_abc_Sequence.register(_memoryviewslice)
986
+ __pyx_collections_abc_Sequence.register(array)
987
+ except:
988
+ pass # ignore failure, it's a minor issue
989
+
990
+ @cname('__pyx_memoryview_fromslice')
991
+ cdef memoryview_fromslice({{memviewslice_name}} memviewslice,
992
+ int ndim,
993
+ object (*to_object_func)(char *),
994
+ to_dtype_func_type to_dtype_func,
995
+ bint dtype_is_object):
996
+
997
+ cdef _memoryviewslice result
998
+
999
+ if <PyObject *> memviewslice.memview == Py_None:
1000
+ return None
1001
+
1002
+ # assert 0 < ndim <= memviewslice.memview.view.ndim, (
1003
+ # ndim, memviewslice.memview.view.ndim)
1004
+
1005
+ result = _memoryviewslice.__new__(_memoryviewslice, None, 0, dtype_is_object)
1006
+
1007
+ result.from_slice = memviewslice
1008
+ __PYX_INC_MEMVIEW(&memviewslice, 1)
1009
+
1010
+ result.from_object = (<memoryview> memviewslice.memview)._get_base()
1011
+ result.typeinfo = memviewslice.memview.typeinfo
1012
+
1013
+ result.view = memviewslice.memview.view
1014
+ result.view.buf = <void *> memviewslice.data
1015
+ result.view.ndim = ndim
1016
+ (<__pyx_buffer *> &result.view).obj = Py_None
1017
+ Py_INCREF(Py_None)
1018
+
1019
+ if (<memoryview>memviewslice.memview).flags & PyBUF_WRITABLE:
1020
+ result.flags = PyBUF_RECORDS
1021
+ else:
1022
+ result.flags = PyBUF_RECORDS_RO
1023
+
1024
+ result.view.shape = <Py_ssize_t *> result.from_slice.shape
1025
+ result.view.strides = <Py_ssize_t *> result.from_slice.strides
1026
+
1027
+ # only set suboffsets if actually used, otherwise set to NULL to improve compatibility
1028
+ result.view.suboffsets = NULL
1029
+ for suboffset in result.from_slice.suboffsets[:ndim]:
1030
+ if suboffset >= 0:
1031
+ result.view.suboffsets = <Py_ssize_t *> result.from_slice.suboffsets
1032
+ break
1033
+
1034
+ cdef Py_ssize_t length
1035
+ result.view.len = result.view.itemsize
1036
+ for length in result.view.shape[:ndim]:
1037
+ result.view.len *= length
1038
+
1039
+ result.to_object_func = to_object_func
1040
+ result.to_dtype_func = to_dtype_func
1041
+
1042
+ return result
1043
+
1044
+ @cname('__pyx_memoryview_get_slice_from_memoryview')
1045
+ cdef {{memviewslice_name}} *get_slice_from_memview(memoryview memview,
1046
+ {{memviewslice_name}} *mslice) except NULL:
1047
+ cdef _memoryviewslice obj
1048
+ if isinstance(memview, _memoryviewslice):
1049
+ obj = memview
1050
+ return &obj.from_slice
1051
+ else:
1052
+ slice_copy(memview, mslice)
1053
+ return mslice
1054
+
1055
+ @cname('__pyx_memoryview_slice_copy')
1056
+ cdef void slice_copy(memoryview memview, {{memviewslice_name}} *dst) noexcept:
1057
+ cdef int dim
1058
+ cdef (Py_ssize_t*) shape, strides, suboffsets
1059
+
1060
+ shape = memview.view.shape
1061
+ strides = memview.view.strides
1062
+ suboffsets = memview.view.suboffsets
1063
+
1064
+ dst.memview = <__pyx_memoryview *> memview
1065
+ dst.data = <char *> memview.view.buf
1066
+
1067
+ for dim in range(memview.view.ndim):
1068
+ dst.shape[dim] = shape[dim]
1069
+ dst.strides[dim] = strides[dim]
1070
+ dst.suboffsets[dim] = suboffsets[dim] if suboffsets else -1
1071
+
1072
+ @cname('__pyx_memoryview_copy_object')
1073
+ cdef memoryview_copy(memoryview memview):
1074
+ "Create a new memoryview object"
1075
+ cdef {{memviewslice_name}} memviewslice
1076
+ slice_copy(memview, &memviewslice)
1077
+ return memoryview_copy_from_slice(memview, &memviewslice)
1078
+
1079
+ @cname('__pyx_memoryview_copy_object_from_slice')
1080
+ cdef memoryview_copy_from_slice(memoryview memview, {{memviewslice_name}} *memviewslice):
1081
+ """
1082
+ Create a new memoryview object from a given memoryview object and slice.
1083
+ """
1084
+ cdef object (*to_object_func)(char *)
1085
+ cdef to_dtype_func_type to_dtype_func
1086
+
1087
+ if isinstance(memview, _memoryviewslice):
1088
+ to_object_func = (<_memoryviewslice> memview).to_object_func
1089
+ to_dtype_func = (<_memoryviewslice> memview).to_dtype_func
1090
+ else:
1091
+ to_object_func = NULL
1092
+ to_dtype_func = NULL
1093
+
1094
+ return memoryview_fromslice(memviewslice[0], memview.view.ndim,
1095
+ to_object_func, to_dtype_func,
1096
+ memview.dtype_is_object)
1097
+
1098
+
1099
+ #
1100
+ ### Copy the contents of a memoryview slices
1101
+ #
1102
+ cdef Py_ssize_t abs_py_ssize_t(Py_ssize_t arg) noexcept nogil:
1103
+ return -arg if arg < 0 else arg
1104
+
1105
+ @cname('__pyx_get_best_slice_order')
1106
+ cdef char get_best_order({{memviewslice_name}} *mslice, int ndim) noexcept nogil:
1107
+ """
1108
+ Figure out the best memory access order for a given slice.
1109
+ """
1110
+ cdef int i
1111
+ cdef Py_ssize_t c_stride = 0
1112
+ cdef Py_ssize_t f_stride = 0
1113
+
1114
+ for i in range(ndim - 1, -1, -1):
1115
+ if mslice.shape[i] > 1:
1116
+ c_stride = mslice.strides[i]
1117
+ break
1118
+
1119
+ for i in range(ndim):
1120
+ if mslice.shape[i] > 1:
1121
+ f_stride = mslice.strides[i]
1122
+ break
1123
+
1124
+ if abs_py_ssize_t(c_stride) <= abs_py_ssize_t(f_stride):
1125
+ return 'C'
1126
+ else:
1127
+ return 'F'
1128
+
1129
+ @cython.cdivision(True)
1130
+ cdef void _copy_strided_to_strided(char *src_data, Py_ssize_t *src_strides,
1131
+ char *dst_data, Py_ssize_t *dst_strides,
1132
+ Py_ssize_t *src_shape, Py_ssize_t *dst_shape,
1133
+ int ndim, size_t itemsize) noexcept nogil:
1134
+ # Note: src_extent is 1 if we're broadcasting
1135
+ # dst_extent always >= src_extent as we don't do reductions
1136
+ cdef Py_ssize_t i
1137
+ cdef Py_ssize_t src_extent = src_shape[0]
1138
+ cdef Py_ssize_t dst_extent = dst_shape[0]
1139
+ cdef Py_ssize_t src_stride = src_strides[0]
1140
+ cdef Py_ssize_t dst_stride = dst_strides[0]
1141
+
1142
+ if ndim == 1:
1143
+ if (src_stride > 0 and dst_stride > 0 and
1144
+ <size_t> src_stride == itemsize == <size_t> dst_stride):
1145
+ memcpy(dst_data, src_data, itemsize * <size_t> dst_extent)
1146
+ else:
1147
+ for i in range(dst_extent):
1148
+ memcpy(dst_data, src_data, itemsize)
1149
+ src_data += src_stride
1150
+ dst_data += dst_stride
1151
+ else:
1152
+ for i in range(dst_extent):
1153
+ _copy_strided_to_strided(src_data, src_strides + 1,
1154
+ dst_data, dst_strides + 1,
1155
+ src_shape + 1, dst_shape + 1,
1156
+ ndim - 1, itemsize)
1157
+ src_data += src_stride
1158
+ dst_data += dst_stride
1159
+
1160
+ cdef void copy_strided_to_strided({{memviewslice_name}} *src,
1161
+ {{memviewslice_name}} *dst,
1162
+ int ndim, size_t itemsize) noexcept nogil:
1163
+ _copy_strided_to_strided(src.data, src.strides, dst.data, dst.strides,
1164
+ src.shape, dst.shape, ndim, itemsize)
1165
+
1166
+ @cname('__pyx_memoryview_slice_get_size')
1167
+ cdef size_t slice_get_size({{memviewslice_name}} *src, int ndim) noexcept nogil:
1168
+ "Return the size of the memory occupied by the slice in number of bytes"
1169
+ cdef Py_ssize_t shape
1170
+ cdef size_t size = <size_t> src.memview.view.itemsize
1171
+
1172
+ for shape in src.shape[:ndim]:
1173
+ size *= <size_t> shape
1174
+
1175
+ return size
1176
+
1177
+ @cname('__pyx_fill_contig_strides_array')
1178
+ cdef Py_ssize_t fill_contig_strides_array(
1179
+ Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t stride,
1180
+ int ndim, char order) noexcept nogil:
1181
+ """
1182
+ Fill the strides array for a slice with C or F contiguous strides.
1183
+ This is like PyBuffer_FillContiguousStrides, but compatible with py < 2.6
1184
+ """
1185
+ cdef int idx
1186
+
1187
+ if order == 'F':
1188
+ for idx in range(ndim):
1189
+ strides[idx] = stride
1190
+ stride *= shape[idx]
1191
+ else:
1192
+ for idx in range(ndim - 1, -1, -1):
1193
+ strides[idx] = stride
1194
+ stride *= shape[idx]
1195
+
1196
+ return stride
1197
+
1198
+ @cname('__pyx_memoryview_copy_data_to_temp')
1199
+ cdef void *copy_data_to_temp({{memviewslice_name}} *src,
1200
+ {{memviewslice_name}} *tmpslice,
1201
+ char order,
1202
+ int ndim) except NULL nogil:
1203
+ """
1204
+ Copy a direct slice to temporary contiguous memory. The caller should free
1205
+ the result when done.
1206
+ """
1207
+ cdef int i
1208
+ cdef void *result
1209
+
1210
+ cdef size_t itemsize = <size_t> src.memview.view.itemsize
1211
+ cdef size_t size = slice_get_size(src, ndim)
1212
+
1213
+ result = malloc(size)
1214
+ if cython.unlikely(not result):
1215
+ _err_no_memory()
1216
+
1217
+ # tmpslice[0] = src
1218
+ tmpslice.data = <char *> result
1219
+ tmpslice.memview = src.memview
1220
+ for i in range(ndim):
1221
+ tmpslice.shape[i] = src.shape[i]
1222
+ tmpslice.suboffsets[i] = -1
1223
+
1224
+ fill_contig_strides_array(&tmpslice.shape[0], &tmpslice.strides[0], <Py_ssize_t> itemsize, ndim, order)
1225
+
1226
+ # We need to broadcast strides again
1227
+ for i in range(ndim):
1228
+ if tmpslice.shape[i] == 1:
1229
+ tmpslice.strides[i] = 0
1230
+
1231
+ if slice_is_contig(src[0], order, ndim):
1232
+ memcpy(result, src.data, size)
1233
+ else:
1234
+ copy_strided_to_strided(src, tmpslice, ndim, itemsize)
1235
+
1236
+ return result
1237
+
1238
+
1239
+ # Use 'with gil' functions and avoid 'with gil' blocks, as the code within the blocks
1240
+ # has temporaries that need the GIL to clean up
1241
+ @cname('__pyx_memoryview_err_extents')
1242
+ cdef int _err_extents(int i, Py_ssize_t extent1,
1243
+ Py_ssize_t extent2) except -1 with gil:
1244
+ PyErr_Format(
1245
+ PyExc_ValueError,
1246
+ "got differing extents in dimension %d (got %zd and %zd)",
1247
+ i, extent1, extent2)
1248
+ return -1
1249
+
1250
+ @cname('__pyx_memoryview_err_dim')
1251
+ cdef int _err_dim(PyObject *error, const char* msg, int dim) except -1 with gil:
1252
+ PyErr_Format(error, msg, dim)
1253
+ return -1
1254
+
1255
+ @cname('__pyx_memoryview_err')
1256
+ cdef int _err(PyObject *error, const char* msg) except -1 with gil:
1257
+ PyErr_SetString(error, msg)
1258
+ return -1
1259
+
1260
+ @cname('__pyx_memoryview_err_no_memory')
1261
+ cdef int _err_no_memory() except -1 with gil:
1262
+ raise MemoryError
1263
+
1264
+ # Simple exception raising functions.
1265
+
1266
+ @cname('__pyx_memoryview_err_ValueError')
1267
+ cdef int _err_ValueError(const char* msg) except -1:
1268
+ PyErr_SetString(PyExc_ValueError, msg)
1269
+ return -1
1270
+
1271
+ @cname('__pyx_memoryview_err_IndexError')
1272
+ cdef int _err_IndexError(const char* msg, Py_ssize_t index) except -1:
1273
+ PyErr_Format(PyExc_IndexError, msg, index)
1274
+ return -1
1275
+
1276
+
1277
+ @cname('__pyx_memoryview_copy_contents')
1278
+ cdef int memoryview_copy_contents({{memviewslice_name}} src,
1279
+ {{memviewslice_name}} dst,
1280
+ int src_ndim, int dst_ndim,
1281
+ bint dtype_is_object) except -1 nogil:
1282
+ """
1283
+ Copy memory from slice src to slice dst.
1284
+ Check for overlapping memory and verify the shapes.
1285
+ """
1286
+ cdef void *tmpdata = NULL
1287
+ cdef size_t itemsize = <size_t> src.memview.view.itemsize
1288
+ cdef int i
1289
+ cdef char order = get_best_order(&src, src_ndim)
1290
+ cdef bint broadcasting = False
1291
+ cdef bint direct_copy = False
1292
+ cdef {{memviewslice_name}} tmp
1293
+
1294
+ if src_ndim < dst_ndim:
1295
+ broadcast_leading(&src, src_ndim, dst_ndim)
1296
+ elif dst_ndim < src_ndim:
1297
+ broadcast_leading(&dst, dst_ndim, src_ndim)
1298
+
1299
+ cdef int ndim = max(src_ndim, dst_ndim)
1300
+
1301
+ for i in range(ndim):
1302
+ if src.shape[i] != dst.shape[i]:
1303
+ if cython.likely(src.shape[i] == 1):
1304
+ broadcasting = True
1305
+ src.strides[i] = 0
1306
+ else:
1307
+ _err_extents(i, dst.shape[i], src.shape[i])
1308
+
1309
+ if cython.unlikely(src.suboffsets[i] >= 0):
1310
+ _err_dim(PyExc_ValueError, "Dimension %d is not direct", i)
1311
+
1312
+ if slices_overlap(&src, &dst, ndim, itemsize):
1313
+ # slices overlap, copy to temp, copy temp to dst
1314
+ if not slice_is_contig(src, order, ndim):
1315
+ order = get_best_order(&dst, ndim)
1316
+
1317
+ tmpdata = copy_data_to_temp(&src, &tmp, order, ndim)
1318
+ src = tmp
1319
+
1320
+ if not broadcasting:
1321
+ # See if both slices have equal contiguity, in that case perform a
1322
+ # direct copy. This only works when we are not broadcasting.
1323
+ if slice_is_contig(src, 'C', ndim):
1324
+ direct_copy = slice_is_contig(dst, 'C', ndim)
1325
+ elif slice_is_contig(src, 'F', ndim):
1326
+ direct_copy = slice_is_contig(dst, 'F', ndim)
1327
+
1328
+ if direct_copy:
1329
+ # Contiguous slices with same order
1330
+ refcount_copying(&dst, dtype_is_object, ndim, inc=False)
1331
+ memcpy(dst.data, src.data, slice_get_size(&src, ndim))
1332
+ refcount_copying(&dst, dtype_is_object, ndim, inc=True)
1333
+ free(tmpdata)
1334
+ return 0
1335
+
1336
+ if order == 'F' == get_best_order(&dst, ndim):
1337
+ # see if both slices have Fortran order, transpose them to match our
1338
+ # C-style indexing order
1339
+ transpose_memslice(&src)
1340
+ transpose_memslice(&dst)
1341
+
1342
+ refcount_copying(&dst, dtype_is_object, ndim, inc=False)
1343
+ copy_strided_to_strided(&src, &dst, ndim, itemsize)
1344
+ refcount_copying(&dst, dtype_is_object, ndim, inc=True)
1345
+
1346
+ free(tmpdata)
1347
+ return 0
1348
+
1349
+ @cname('__pyx_memoryview_broadcast_leading')
1350
+ cdef void broadcast_leading({{memviewslice_name}} *mslice,
1351
+ int ndim,
1352
+ int ndim_other) noexcept nogil:
1353
+ cdef int i
1354
+ cdef int offset = ndim_other - ndim
1355
+
1356
+ for i in range(ndim - 1, -1, -1):
1357
+ mslice.shape[i + offset] = mslice.shape[i]
1358
+ mslice.strides[i + offset] = mslice.strides[i]
1359
+ mslice.suboffsets[i + offset] = mslice.suboffsets[i]
1360
+
1361
+ for i in range(offset):
1362
+ mslice.shape[i] = 1
1363
+ mslice.strides[i] = mslice.strides[0]
1364
+ mslice.suboffsets[i] = -1
1365
+
1366
+ #
1367
+ ### Take care of refcounting the objects in slices. Do this separately from any copying,
1368
+ ### to minimize acquiring the GIL
1369
+ #
1370
+
1371
+ @cname('__pyx_memoryview_refcount_copying')
1372
+ cdef void refcount_copying({{memviewslice_name}} *dst, bint dtype_is_object, int ndim, bint inc) noexcept nogil:
1373
+ # incref or decref the objects in the destination slice if the dtype is object
1374
+ if dtype_is_object:
1375
+ refcount_objects_in_slice_with_gil(dst.data, dst.shape, dst.strides, ndim, inc)
1376
+
1377
+ @cname('__pyx_memoryview_refcount_objects_in_slice_with_gil')
1378
+ cdef void refcount_objects_in_slice_with_gil(char *data, Py_ssize_t *shape,
1379
+ Py_ssize_t *strides, int ndim,
1380
+ bint inc) noexcept with gil:
1381
+ refcount_objects_in_slice(data, shape, strides, ndim, inc)
1382
+
1383
+ @cname('__pyx_memoryview_refcount_objects_in_slice')
1384
+ cdef void refcount_objects_in_slice(char *data, Py_ssize_t *shape,
1385
+ Py_ssize_t *strides, int ndim, bint inc) noexcept:
1386
+ cdef Py_ssize_t i
1387
+ cdef Py_ssize_t stride = strides[0]
1388
+
1389
+ for i in range(shape[0]):
1390
+ if ndim == 1:
1391
+ if inc:
1392
+ Py_INCREF((<PyObject **> data)[0])
1393
+ else:
1394
+ Py_DECREF((<PyObject **> data)[0])
1395
+ else:
1396
+ refcount_objects_in_slice(data, shape + 1, strides + 1, ndim - 1, inc)
1397
+
1398
+ data += stride
1399
+
1400
+ #
1401
+ ### Scalar to slice assignment
1402
+ #
1403
+ @cname('__pyx_memoryview_slice_assign_scalar')
1404
+ cdef void slice_assign_scalar({{memviewslice_name}} *dst, int ndim,
1405
+ size_t itemsize, void *item,
1406
+ bint dtype_is_object) noexcept nogil:
1407
+ refcount_copying(dst, dtype_is_object, ndim, inc=False)
1408
+ _slice_assign_scalar(dst.data, dst.shape, dst.strides, ndim, itemsize, item)
1409
+ refcount_copying(dst, dtype_is_object, ndim, inc=True)
1410
+
1411
+
1412
+ @cname('__pyx_memoryview__slice_assign_scalar')
1413
+ cdef void _slice_assign_scalar(char *data, Py_ssize_t *shape,
1414
+ Py_ssize_t *strides, int ndim,
1415
+ size_t itemsize, void *item) noexcept nogil:
1416
+ cdef Py_ssize_t i
1417
+ cdef Py_ssize_t stride = strides[0]
1418
+ cdef Py_ssize_t extent = shape[0]
1419
+
1420
+ if ndim == 1:
1421
+ for i in range(extent):
1422
+ memcpy(data, item, itemsize)
1423
+ data += stride
1424
+ else:
1425
+ for i in range(extent):
1426
+ _slice_assign_scalar(data, shape + 1, strides + 1, ndim - 1, itemsize, item)
1427
+ data += stride
1428
+
1429
+
1430
+ ############### BufferFormatFromTypeInfo ###############
1431
+ cdef extern from *:
1432
+ ctypedef struct __Pyx_StructField
1433
+
1434
+ cdef enum:
1435
+ __PYX_BUF_FLAGS_PACKED_STRUCT
1436
+ __PYX_BUF_FLAGS_INTEGER_COMPLEX
1437
+
1438
+ ctypedef struct __Pyx_TypeInfo:
1439
+ char* name
1440
+ const __Pyx_StructField* fields
1441
+ size_t size
1442
+ size_t arraysize[8]
1443
+ int ndim
1444
+ char typegroup
1445
+ char is_unsigned
1446
+ int flags
1447
+
1448
+ ctypedef struct __Pyx_StructField:
1449
+ const __Pyx_TypeInfo* type
1450
+ char* name
1451
+ size_t offset
1452
+
1453
+ ctypedef struct __Pyx_BufFmt_StackElem:
1454
+ const __Pyx_StructField* field
1455
+ size_t parent_offset
1456
+
1457
+ #ctypedef struct __Pyx_BufFmt_Context:
1458
+ # __Pyx_StructField root
1459
+ __Pyx_BufFmt_StackElem* head
1460
+
1461
+ struct __pyx_typeinfo_string:
1462
+ char string[3]
1463
+
1464
+ __pyx_typeinfo_string __Pyx_TypeInfoToFormat(const __Pyx_TypeInfo *)
1465
+
1466
+
1467
+ @cname('__pyx_format_from_typeinfo')
1468
+ cdef bytes format_from_typeinfo(const __Pyx_TypeInfo *type):
1469
+ cdef const __Pyx_StructField *field
1470
+ cdef __pyx_typeinfo_string fmt
1471
+ cdef bytes part, result
1472
+ cdef Py_ssize_t i
1473
+
1474
+ if type.typegroup == 'S':
1475
+ assert type.fields != NULL
1476
+ assert type.fields.type != NULL
1477
+
1478
+ if type.flags & __PYX_BUF_FLAGS_PACKED_STRUCT:
1479
+ alignment = b'^'
1480
+ else:
1481
+ alignment = b''
1482
+
1483
+ parts = [b"T{"]
1484
+ field = type.fields
1485
+
1486
+ while field.type:
1487
+ part = format_from_typeinfo(field.type)
1488
+ parts.append(part + b':' + field.name + b':')
1489
+ field += 1
1490
+
1491
+ result = alignment.join(parts) + b'}'
1492
+ else:
1493
+ fmt = __Pyx_TypeInfoToFormat(type)
1494
+ result = fmt.string
1495
+ if type.arraysize[0]:
1496
+ extents = [f"{type.arraysize[i]}" for i in range(type.ndim)]
1497
+ result = f"({u','.join(extents)})".encode('ascii') + result
1498
+
1499
+ return result