Cython 3.2.0__cp39-abi3-win32.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 (333) hide show
  1. Cython/Build/BuildExecutable.py +169 -0
  2. Cython/Build/Cache.py +199 -0
  3. Cython/Build/Cythonize.py +350 -0
  4. Cython/Build/Dependencies.py +1314 -0
  5. Cython/Build/Distutils.py +1 -0
  6. Cython/Build/Inline.py +463 -0
  7. Cython/Build/IpythonMagic.py +560 -0
  8. Cython/Build/SharedModule.py +94 -0
  9. Cython/Build/Tests/TestCyCache.py +194 -0
  10. Cython/Build/Tests/TestCythonizeArgsParser.py +481 -0
  11. Cython/Build/Tests/TestDependencies.py +133 -0
  12. Cython/Build/Tests/TestInline.py +177 -0
  13. Cython/Build/Tests/TestIpythonMagic.py +287 -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 +815 -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 +680 -0
  23. Cython/Compiler/Builtin.py +984 -0
  24. Cython/Compiler/CmdLine.py +263 -0
  25. Cython/Compiler/Code.pxd +149 -0
  26. Cython/Compiler/Code.py +3746 -0
  27. Cython/Compiler/Code.pyd +0 -0
  28. Cython/Compiler/CodeGeneration.py +33 -0
  29. Cython/Compiler/CythonScope.py +191 -0
  30. Cython/Compiler/Dataclass.py +864 -0
  31. Cython/Compiler/DebugFlags.py +24 -0
  32. Cython/Compiler/Errors.py +297 -0
  33. Cython/Compiler/ExprNodes.py +15562 -0
  34. Cython/Compiler/FlowControl.pxd +97 -0
  35. Cython/Compiler/FlowControl.py +1451 -0
  36. Cython/Compiler/FlowControl.pyd +0 -0
  37. Cython/Compiler/FusedNode.py +971 -0
  38. Cython/Compiler/FusedNode.pyd +0 -0
  39. Cython/Compiler/Future.py +16 -0
  40. Cython/Compiler/Interpreter.py +57 -0
  41. Cython/Compiler/Lexicon.py +421 -0
  42. Cython/Compiler/LineTable.py +114 -0
  43. Cython/Compiler/LineTable.pyd +0 -0
  44. Cython/Compiler/Main.py +857 -0
  45. Cython/Compiler/MatchCaseNodes.py +259 -0
  46. Cython/Compiler/MemoryView.py +905 -0
  47. Cython/Compiler/ModuleNode.py +4235 -0
  48. Cython/Compiler/Naming.py +363 -0
  49. Cython/Compiler/Nodes.py +10831 -0
  50. Cython/Compiler/Optimize.py +5288 -0
  51. Cython/Compiler/Options.py +843 -0
  52. Cython/Compiler/ParseTreeTransforms.pxd +78 -0
  53. Cython/Compiler/ParseTreeTransforms.py +4638 -0
  54. Cython/Compiler/Parsing.pxd +9 -0
  55. Cython/Compiler/Parsing.py +4775 -0
  56. Cython/Compiler/Parsing.pyd +0 -0
  57. Cython/Compiler/Pipeline.py +439 -0
  58. Cython/Compiler/PyrexTypes.py +5870 -0
  59. Cython/Compiler/Pythran.py +232 -0
  60. Cython/Compiler/Scanning.pxd +48 -0
  61. Cython/Compiler/Scanning.py +701 -0
  62. Cython/Compiler/Scanning.pyd +0 -0
  63. Cython/Compiler/StringEncoding.py +298 -0
  64. Cython/Compiler/Symtab.py +3073 -0
  65. Cython/Compiler/Tests/TestBuffer.py +105 -0
  66. Cython/Compiler/Tests/TestBuiltin.py +72 -0
  67. Cython/Compiler/Tests/TestCmdLine.py +586 -0
  68. Cython/Compiler/Tests/TestCode.py +144 -0
  69. Cython/Compiler/Tests/TestFlowControl.py +65 -0
  70. Cython/Compiler/Tests/TestGrammar.py +202 -0
  71. Cython/Compiler/Tests/TestMemView.py +71 -0
  72. Cython/Compiler/Tests/TestParseTreeTransforms.py +285 -0
  73. Cython/Compiler/Tests/TestScanning.py +134 -0
  74. Cython/Compiler/Tests/TestSignatureMatching.py +73 -0
  75. Cython/Compiler/Tests/TestStringEncoding.py +21 -0
  76. Cython/Compiler/Tests/TestTreeFragment.py +63 -0
  77. Cython/Compiler/Tests/TestTreePath.py +103 -0
  78. Cython/Compiler/Tests/TestTypes.py +75 -0
  79. Cython/Compiler/Tests/TestUtilityLoad.py +112 -0
  80. Cython/Compiler/Tests/TestVisitor.py +61 -0
  81. Cython/Compiler/Tests/Utils.py +36 -0
  82. Cython/Compiler/Tests/__init__.py +1 -0
  83. Cython/Compiler/TreeFragment.py +278 -0
  84. Cython/Compiler/TreePath.py +303 -0
  85. Cython/Compiler/TypeInference.py +591 -0
  86. Cython/Compiler/TypeSlots.py +1174 -0
  87. Cython/Compiler/UFuncs.py +311 -0
  88. Cython/Compiler/UtilNodes.py +389 -0
  89. Cython/Compiler/UtilityCode.py +344 -0
  90. Cython/Compiler/Version.py +8 -0
  91. Cython/Compiler/Visitor.pxd +53 -0
  92. Cython/Compiler/Visitor.py +861 -0
  93. Cython/Compiler/Visitor.pyd +0 -0
  94. Cython/Compiler/__init__.py +1 -0
  95. Cython/Coverage.py +448 -0
  96. Cython/Debugger/Cygdb.py +177 -0
  97. Cython/Debugger/DebugWriter.py +82 -0
  98. Cython/Debugger/Tests/TestLibCython.py +275 -0
  99. Cython/Debugger/Tests/__init__.py +1 -0
  100. Cython/Debugger/Tests/cfuncs.c +8 -0
  101. Cython/Debugger/Tests/codefile +49 -0
  102. Cython/Debugger/Tests/test_libcython_in_gdb.py +578 -0
  103. Cython/Debugger/Tests/test_libpython_in_gdb.py +90 -0
  104. Cython/Debugger/__init__.py +1 -0
  105. Cython/Debugger/libcython.py +1548 -0
  106. Cython/Debugger/libpython.py +2821 -0
  107. Cython/Debugging.py +20 -0
  108. Cython/Distutils/__init__.py +2 -0
  109. Cython/Distutils/build_ext.py +139 -0
  110. Cython/Distutils/extension.py +96 -0
  111. Cython/Distutils/old_build_ext.py +351 -0
  112. Cython/Includes/cpython/__init__.pxd +173 -0
  113. Cython/Includes/cpython/array.pxd +178 -0
  114. Cython/Includes/cpython/bool.pxd +37 -0
  115. Cython/Includes/cpython/buffer.pxd +112 -0
  116. Cython/Includes/cpython/bytearray.pxd +33 -0
  117. Cython/Includes/cpython/bytes.pxd +200 -0
  118. Cython/Includes/cpython/cellobject.pxd +35 -0
  119. Cython/Includes/cpython/ceval.pxd +8 -0
  120. Cython/Includes/cpython/codecs.pxd +121 -0
  121. Cython/Includes/cpython/complex.pxd +60 -0
  122. Cython/Includes/cpython/contextvars.pxd +145 -0
  123. Cython/Includes/cpython/conversion.pxd +36 -0
  124. Cython/Includes/cpython/datetime.pxd +395 -0
  125. Cython/Includes/cpython/descr.pxd +26 -0
  126. Cython/Includes/cpython/dict.pxd +187 -0
  127. Cython/Includes/cpython/exc.pxd +263 -0
  128. Cython/Includes/cpython/fileobject.pxd +57 -0
  129. Cython/Includes/cpython/float.pxd +47 -0
  130. Cython/Includes/cpython/function.pxd +65 -0
  131. Cython/Includes/cpython/genobject.pxd +25 -0
  132. Cython/Includes/cpython/getargs.pxd +12 -0
  133. Cython/Includes/cpython/instance.pxd +25 -0
  134. Cython/Includes/cpython/iterator.pxd +36 -0
  135. Cython/Includes/cpython/iterobject.pxd +24 -0
  136. Cython/Includes/cpython/list.pxd +92 -0
  137. Cython/Includes/cpython/long.pxd +149 -0
  138. Cython/Includes/cpython/longintrepr.pxd +14 -0
  139. Cython/Includes/cpython/mapping.pxd +63 -0
  140. Cython/Includes/cpython/marshal.pxd +66 -0
  141. Cython/Includes/cpython/mem.pxd +120 -0
  142. Cython/Includes/cpython/memoryview.pxd +50 -0
  143. Cython/Includes/cpython/method.pxd +49 -0
  144. Cython/Includes/cpython/module.pxd +208 -0
  145. Cython/Includes/cpython/number.pxd +258 -0
  146. Cython/Includes/cpython/object.pxd +433 -0
  147. Cython/Includes/cpython/pycapsule.pxd +143 -0
  148. Cython/Includes/cpython/pylifecycle.pxd +68 -0
  149. Cython/Includes/cpython/pyport.pxd +8 -0
  150. Cython/Includes/cpython/pystate.pxd +95 -0
  151. Cython/Includes/cpython/pythread.pxd +53 -0
  152. Cython/Includes/cpython/ref.pxd +141 -0
  153. Cython/Includes/cpython/sequence.pxd +134 -0
  154. Cython/Includes/cpython/set.pxd +119 -0
  155. Cython/Includes/cpython/slice.pxd +70 -0
  156. Cython/Includes/cpython/time.pxd +129 -0
  157. Cython/Includes/cpython/tuple.pxd +72 -0
  158. Cython/Includes/cpython/type.pxd +53 -0
  159. Cython/Includes/cpython/unicode.pxd +639 -0
  160. Cython/Includes/cpython/version.pxd +32 -0
  161. Cython/Includes/cpython/weakref.pxd +78 -0
  162. Cython/Includes/libc/__init__.pxd +1 -0
  163. Cython/Includes/libc/complex.pxd +35 -0
  164. Cython/Includes/libc/errno.pxd +127 -0
  165. Cython/Includes/libc/float.pxd +43 -0
  166. Cython/Includes/libc/limits.pxd +28 -0
  167. Cython/Includes/libc/locale.pxd +46 -0
  168. Cython/Includes/libc/math.pxd +209 -0
  169. Cython/Includes/libc/setjmp.pxd +10 -0
  170. Cython/Includes/libc/signal.pxd +64 -0
  171. Cython/Includes/libc/stddef.pxd +9 -0
  172. Cython/Includes/libc/stdint.pxd +105 -0
  173. Cython/Includes/libc/stdio.pxd +80 -0
  174. Cython/Includes/libc/stdlib.pxd +72 -0
  175. Cython/Includes/libc/string.pxd +50 -0
  176. Cython/Includes/libc/threads.pxd +234 -0
  177. Cython/Includes/libc/time.pxd +52 -0
  178. Cython/Includes/libcpp/__init__.pxd +4 -0
  179. Cython/Includes/libcpp/algorithm.pxd +320 -0
  180. Cython/Includes/libcpp/any.pxd +16 -0
  181. Cython/Includes/libcpp/atomic.pxd +59 -0
  182. Cython/Includes/libcpp/barrier.pxd +22 -0
  183. Cython/Includes/libcpp/bit.pxd +29 -0
  184. Cython/Includes/libcpp/cast.pxd +12 -0
  185. Cython/Includes/libcpp/cmath.pxd +518 -0
  186. Cython/Includes/libcpp/complex.pxd +106 -0
  187. Cython/Includes/libcpp/condition_variable.pxd +322 -0
  188. Cython/Includes/libcpp/deque.pxd +165 -0
  189. Cython/Includes/libcpp/exception.pxd +86 -0
  190. Cython/Includes/libcpp/execution.pxd +15 -0
  191. Cython/Includes/libcpp/forward_list.pxd +63 -0
  192. Cython/Includes/libcpp/functional.pxd +26 -0
  193. Cython/Includes/libcpp/future.pxd +103 -0
  194. Cython/Includes/libcpp/iterator.pxd +34 -0
  195. Cython/Includes/libcpp/latch.pxd +17 -0
  196. Cython/Includes/libcpp/limits.pxd +61 -0
  197. Cython/Includes/libcpp/list.pxd +117 -0
  198. Cython/Includes/libcpp/map.pxd +252 -0
  199. Cython/Includes/libcpp/memory.pxd +115 -0
  200. Cython/Includes/libcpp/mutex.pxd +387 -0
  201. Cython/Includes/libcpp/numbers.pxd +15 -0
  202. Cython/Includes/libcpp/numeric.pxd +131 -0
  203. Cython/Includes/libcpp/optional.pxd +34 -0
  204. Cython/Includes/libcpp/pair.pxd +1 -0
  205. Cython/Includes/libcpp/queue.pxd +25 -0
  206. Cython/Includes/libcpp/random.pxd +166 -0
  207. Cython/Includes/libcpp/semaphore.pxd +43 -0
  208. Cython/Includes/libcpp/set.pxd +228 -0
  209. Cython/Includes/libcpp/shared_mutex.pxd +96 -0
  210. Cython/Includes/libcpp/span.pxd +87 -0
  211. Cython/Includes/libcpp/stack.pxd +11 -0
  212. Cython/Includes/libcpp/stop_token.pxd +117 -0
  213. Cython/Includes/libcpp/string.pxd +355 -0
  214. Cython/Includes/libcpp/string_view.pxd +183 -0
  215. Cython/Includes/libcpp/typeindex.pxd +15 -0
  216. Cython/Includes/libcpp/typeinfo.pxd +10 -0
  217. Cython/Includes/libcpp/unordered_map.pxd +193 -0
  218. Cython/Includes/libcpp/unordered_set.pxd +152 -0
  219. Cython/Includes/libcpp/utility.pxd +30 -0
  220. Cython/Includes/libcpp/vector.pxd +186 -0
  221. Cython/Includes/numpy/math.pxd +150 -0
  222. Cython/Includes/openmp.pxd +50 -0
  223. Cython/Includes/posix/__init__.pxd +1 -0
  224. Cython/Includes/posix/dlfcn.pxd +14 -0
  225. Cython/Includes/posix/fcntl.pxd +86 -0
  226. Cython/Includes/posix/ioctl.pxd +4 -0
  227. Cython/Includes/posix/mman.pxd +101 -0
  228. Cython/Includes/posix/resource.pxd +57 -0
  229. Cython/Includes/posix/select.pxd +21 -0
  230. Cython/Includes/posix/signal.pxd +73 -0
  231. Cython/Includes/posix/stat.pxd +98 -0
  232. Cython/Includes/posix/stdio.pxd +37 -0
  233. Cython/Includes/posix/stdlib.pxd +29 -0
  234. Cython/Includes/posix/strings.pxd +9 -0
  235. Cython/Includes/posix/time.pxd +71 -0
  236. Cython/Includes/posix/types.pxd +30 -0
  237. Cython/Includes/posix/uio.pxd +26 -0
  238. Cython/Includes/posix/unistd.pxd +271 -0
  239. Cython/Includes/posix/wait.pxd +38 -0
  240. Cython/Plex/Actions.pxd +24 -0
  241. Cython/Plex/Actions.py +119 -0
  242. Cython/Plex/Actions.pyd +0 -0
  243. Cython/Plex/DFA.pxd +14 -0
  244. Cython/Plex/DFA.py +164 -0
  245. Cython/Plex/DFA.pyd +0 -0
  246. Cython/Plex/Errors.py +48 -0
  247. Cython/Plex/Lexicons.py +178 -0
  248. Cython/Plex/Machines.pxd +36 -0
  249. Cython/Plex/Machines.py +238 -0
  250. Cython/Plex/Machines.pyd +0 -0
  251. Cython/Plex/Regexps.py +535 -0
  252. Cython/Plex/Scanners.pxd +45 -0
  253. Cython/Plex/Scanners.py +328 -0
  254. Cython/Plex/Scanners.pyd +0 -0
  255. Cython/Plex/Transitions.pxd +14 -0
  256. Cython/Plex/Transitions.py +239 -0
  257. Cython/Plex/Transitions.pyd +0 -0
  258. Cython/Plex/__init__.py +34 -0
  259. Cython/Runtime/__init__.py +1 -0
  260. Cython/Runtime/refnanny.pyd +0 -0
  261. Cython/Runtime/refnanny.pyx +237 -0
  262. Cython/Shadow.py +690 -0
  263. Cython/Shadow.pyi +521 -0
  264. Cython/StringIOTree.py +170 -0
  265. Cython/StringIOTree.pyd +0 -0
  266. Cython/Tempita/__init__.py +4 -0
  267. Cython/Tempita/_looper.py +154 -0
  268. Cython/Tempita/_tempita.py +1091 -0
  269. Cython/Tempita/_tempita.pyd +0 -0
  270. Cython/TestUtils.py +422 -0
  271. Cython/Tests/TestCodeWriter.py +128 -0
  272. Cython/Tests/TestCythonUtils.py +202 -0
  273. Cython/Tests/TestJediTyper.py +223 -0
  274. Cython/Tests/TestShadow.py +114 -0
  275. Cython/Tests/TestStringIOTree.py +67 -0
  276. Cython/Tests/TestTestUtils.py +90 -0
  277. Cython/Tests/__init__.py +1 -0
  278. Cython/Tests/xmlrunner.py +390 -0
  279. Cython/Utility/AsyncGen.c +1031 -0
  280. Cython/Utility/Buffer.c +865 -0
  281. Cython/Utility/BufferFormatFromTypeInfo.pxd +2 -0
  282. Cython/Utility/Builtins.c +810 -0
  283. Cython/Utility/CConvert.pyx +134 -0
  284. Cython/Utility/CMath.c +104 -0
  285. Cython/Utility/CommonStructures.c +226 -0
  286. Cython/Utility/Complex.c +378 -0
  287. Cython/Utility/Coroutine.c +2300 -0
  288. Cython/Utility/CpdefEnums.pyx +103 -0
  289. Cython/Utility/CppConvert.pyx +282 -0
  290. Cython/Utility/CppSupport.cpp +151 -0
  291. Cython/Utility/CythonFunction.c +1832 -0
  292. Cython/Utility/Dataclasses.c +101 -0
  293. Cython/Utility/Embed.c +121 -0
  294. Cython/Utility/Exceptions.c +1016 -0
  295. Cython/Utility/ExtensionTypes.c +996 -0
  296. Cython/Utility/FunctionArguments.c +1043 -0
  297. Cython/Utility/FusedFunction.pyx +44 -0
  298. Cython/Utility/ImportExport.c +907 -0
  299. Cython/Utility/MemoryView.pxd +188 -0
  300. Cython/Utility/MemoryView.pyx +1482 -0
  301. Cython/Utility/MemoryView_C.c +927 -0
  302. Cython/Utility/ModuleSetupCode.c +3203 -0
  303. Cython/Utility/NumpyImportArray.c +46 -0
  304. Cython/Utility/ObjectHandling.c +3273 -0
  305. Cython/Utility/Optimize.c +1603 -0
  306. Cython/Utility/Overflow.c +384 -0
  307. Cython/Utility/Printing.c +86 -0
  308. Cython/Utility/Profile.c +732 -0
  309. Cython/Utility/StringTools.c +1379 -0
  310. Cython/Utility/Synchronization.c +399 -0
  311. Cython/Utility/TString.c +356 -0
  312. Cython/Utility/TestCyUtilityLoader.pyx +8 -0
  313. Cython/Utility/TestCythonScope.pyx +75 -0
  314. Cython/Utility/TestUtilityLoader.c +12 -0
  315. Cython/Utility/TypeConversion.c +1385 -0
  316. Cython/Utility/UFuncs.pyx +50 -0
  317. Cython/Utility/UFuncs_C.c +89 -0
  318. Cython/Utility/__init__.py +28 -0
  319. Cython/Utility/arrayarray.h +167 -0
  320. Cython/Utils.py +687 -0
  321. Cython/Utils.pyd +0 -0
  322. Cython/__init__.py +10 -0
  323. Cython/__init__.pyi +7 -0
  324. Cython/py.typed +0 -0
  325. cython-3.2.0.dist-info/METADATA +85 -0
  326. cython-3.2.0.dist-info/RECORD +333 -0
  327. cython-3.2.0.dist-info/WHEEL +5 -0
  328. cython-3.2.0.dist-info/entry_points.txt +4 -0
  329. cython-3.2.0.dist-info/top_level.txt +3 -0
  330. cython.py +29 -0
  331. pyximport/__init__.py +4 -0
  332. pyximport/pyxbuild.py +160 -0
  333. pyximport/pyximport.py +482 -0
@@ -0,0 +1,971 @@
1
+ import copy
2
+ import hashlib
3
+
4
+ from . import (ExprNodes, PyrexTypes,
5
+ ParseTreeTransforms, StringEncoding, Errors,
6
+ Naming)
7
+ from .ExprNodes import CloneNode, CodeObjectNode, ProxyNode, TupleNode
8
+ from .Nodes import FuncDefNode, StatListNode, DefNode
9
+ from ..Utils import OrderedSet
10
+ from .Errors import error, CannotSpecialize
11
+
12
+
13
+ class FusedCFuncDefNode(StatListNode):
14
+ """
15
+ This node replaces a function with fused arguments. It deep-copies the
16
+ function for every permutation of fused types, and allocates a new local
17
+ scope for it. It keeps track of the original function in self.node, and
18
+ the entry of the original function in the symbol table is given the
19
+ 'fused_cfunction' attribute which points back to us.
20
+ Then when a function lookup occurs (to e.g. call it), the call can be
21
+ dispatched to the right function.
22
+
23
+ node FuncDefNode the original function
24
+ nodes [FuncDefNode] list of copies of node with different specific types
25
+ py_func DefNode the fused python function subscriptable from
26
+ Python space
27
+ __signatures__ A DictNode mapping signature specialization strings
28
+ to PyCFunction nodes
29
+ resulting_fused_function PyCFunction for the fused DefNode that delegates
30
+ to specializations
31
+ fused_func_assignment Assignment of the fused function to the function name
32
+ defaults_tuple TupleNode of defaults (letting PyCFunctionNode build
33
+ defaults would result in many different tuples)
34
+ specialized_pycfuncs List of synthesized pycfunction nodes for the
35
+ specializations
36
+
37
+ fused_compound_types All fused (compound) types (e.g. floating[:])
38
+ """
39
+
40
+ __signatures__ = None
41
+ resulting_fused_function = None
42
+ fused_func_assignment = None
43
+ py_func = None
44
+ defaults_tuple = None
45
+ decorators = None
46
+
47
+ child_attrs = StatListNode.child_attrs + [
48
+ '__signatures__', 'resulting_fused_function', 'fused_func_assignment']
49
+
50
+ def __init__(self, node, env):
51
+ super().__init__(node.pos)
52
+
53
+ self.nodes = []
54
+ self.node = node
55
+
56
+ is_def = isinstance(self.node, DefNode)
57
+ if is_def:
58
+ # self.node.decorators = []
59
+ self.copy_def(env)
60
+ else:
61
+ self.copy_cdef(env)
62
+
63
+ # Perform some sanity checks. If anything fails, it's a bug
64
+ for n in self.nodes:
65
+ assert not n.entry.type.is_fused
66
+ assert not n.local_scope.return_type.is_fused
67
+ if node.return_type.is_fused:
68
+ assert not n.return_type.is_fused
69
+
70
+ if not is_def and n.cfunc_declarator.optional_arg_count:
71
+ assert n.type.op_arg_struct
72
+
73
+ node.entry.fused_cfunction = self
74
+ # Copy the nodes as AnalyseDeclarationsTransform will prepend
75
+ # self.py_func to self.stats, as we only want specialized
76
+ # CFuncDefNodes in self.nodes
77
+ self.stats = self.nodes[:]
78
+
79
+ def copy_def(self, env):
80
+ """
81
+ Create a copy of the original def or lambda function for specialized
82
+ versions.
83
+ """
84
+ fused_compound_types = PyrexTypes.unique(
85
+ [arg.type for arg in self.node.args if arg.type.is_fused])
86
+ fused_types = self._get_fused_base_types(fused_compound_types)
87
+ permutations = PyrexTypes.get_all_specialized_permutations(fused_types)
88
+
89
+ self.fused_compound_types = fused_compound_types
90
+
91
+ if self.node.entry in env.pyfunc_entries:
92
+ env.pyfunc_entries.remove(self.node.entry)
93
+
94
+ for cname, fused_to_specific in permutations:
95
+ copied_node = copy.deepcopy(self.node)
96
+ # keep signature object identity for special casing in DefNode.analyse_declarations()
97
+ copied_node.entry.signature = self.node.entry.signature
98
+
99
+ self._specialize_function_args(copied_node.args, fused_to_specific)
100
+ copied_node.return_type = self.node.return_type.specialize(
101
+ fused_to_specific)
102
+ copied_node.code_object = CodeObjectNode(copied_node)
103
+ copied_node.analyse_declarations(env)
104
+ # copied_node.is_staticmethod = self.node.is_staticmethod
105
+ # copied_node.is_classmethod = self.node.is_classmethod
106
+ self.create_new_local_scope(copied_node, env, fused_to_specific)
107
+ self.specialize_copied_def(copied_node, cname, self.node.entry,
108
+ fused_to_specific, fused_compound_types)
109
+
110
+ PyrexTypes.specialize_entry(copied_node.entry, cname)
111
+ copied_node.entry.used = True
112
+ env.entries[copied_node.entry.name] = copied_node.entry
113
+
114
+ specialised_type_names = [
115
+ sarg.type.declaration_code('', for_display=True)
116
+ for (farg, sarg) in zip(self.node.args, copied_node.args)
117
+ if farg.type.is_fused
118
+ ]
119
+ copied_node.name = StringEncoding.EncodedString(f"{copied_node.name}[{','.join(specialised_type_names)}]")
120
+
121
+ if not self.replace_fused_typechecks(copied_node):
122
+ break
123
+
124
+ self.orig_py_func = self.node
125
+ self.py_func = self.make_fused_cpdef(self.node, env, is_def=True)
126
+
127
+ def copy_cdef(self, env):
128
+ """
129
+ Create a copy of the original c(p)def function for all specialized
130
+ versions.
131
+ """
132
+ permutations = self.node.type.get_all_specialized_permutations()
133
+ # print 'Node %s has %d specializations:' % (self.node.entry.name,
134
+ # len(permutations))
135
+ # import pprint; pprint.pprint([d for cname, d in permutations])
136
+
137
+ # Prevent copying of the python function
138
+ self.orig_py_func = orig_py_func = self.node.py_func
139
+ self.node.py_func = None
140
+ if orig_py_func:
141
+ env.pyfunc_entries.remove(orig_py_func.entry)
142
+
143
+ fused_types = self.node.type.get_fused_types()
144
+ self.fused_compound_types = fused_types
145
+
146
+ new_cfunc_entries = []
147
+ for cname, fused_to_specific in permutations:
148
+ copied_node = copy.deepcopy(self.node)
149
+
150
+ # Make the types in our CFuncType specific.
151
+ try:
152
+ type = copied_node.type.specialize(fused_to_specific)
153
+ except CannotSpecialize:
154
+ # unlike for the argument types, specializing the return type can fail
155
+ error(copied_node.pos, "Return type is a fused type that cannot "
156
+ "be determined from the function arguments")
157
+ self.py_func = None # this is just to let the compiler exit gracefully
158
+ return
159
+ entry = copied_node.entry
160
+ type.specialize_entry(entry, cname)
161
+
162
+ # Reuse existing Entries (e.g. from .pxd files).
163
+ for orig_entry in env.cfunc_entries:
164
+ if entry.cname == orig_entry.cname and type.same_as_resolved_type(orig_entry.type):
165
+ copied_node.entry = orig_entry
166
+ if not copied_node.entry.func_cname:
167
+ copied_node.entry.func_cname = entry.func_cname
168
+ entry = orig_entry
169
+ type = orig_entry.type
170
+ break
171
+ else:
172
+ new_cfunc_entries.append(entry)
173
+
174
+ copied_node.type = type
175
+ entry.type, type.entry = type, entry
176
+
177
+ entry.used = (entry.used or
178
+ self.node.entry.defined_in_pxd or
179
+ env.is_c_class_scope or
180
+ entry.is_cmethod)
181
+
182
+ if self.node.cfunc_declarator.optional_arg_count:
183
+ self.node.cfunc_declarator.declare_optional_arg_struct(
184
+ type, env, fused_cname=cname)
185
+
186
+ copied_node.return_type = type.return_type
187
+ self.create_new_local_scope(copied_node, env, fused_to_specific)
188
+
189
+ # Make the argument types in the CFuncDeclarator specific
190
+ self._specialize_function_args(copied_node.cfunc_declarator.args,
191
+ fused_to_specific)
192
+
193
+ # If a cpdef, declare all specialized cpdefs (this
194
+ # also calls analyse_declarations)
195
+ copied_node.declare_cpdef_wrapper(env)
196
+ if copied_node.py_func:
197
+ env.pyfunc_entries.remove(copied_node.py_func.entry)
198
+
199
+ self.specialize_copied_def(
200
+ copied_node.py_func, cname, self.node.entry.as_variable,
201
+ fused_to_specific, fused_types)
202
+
203
+ if not self.replace_fused_typechecks(copied_node):
204
+ break
205
+
206
+ # replace old entry with new entries
207
+ if self.node.entry in env.cfunc_entries:
208
+ cindex = env.cfunc_entries.index(self.node.entry)
209
+ env.cfunc_entries[cindex:cindex+1] = new_cfunc_entries
210
+ else:
211
+ env.cfunc_entries.extend(new_cfunc_entries)
212
+
213
+ if orig_py_func:
214
+ self.py_func = self.make_fused_cpdef(orig_py_func, env,
215
+ is_def=False)
216
+ else:
217
+ self.py_func = orig_py_func
218
+
219
+ def _get_fused_base_types(self, fused_compound_types):
220
+ """
221
+ Get a list of unique basic fused types, from a list of
222
+ (possibly) compound fused types.
223
+ """
224
+ base_types = []
225
+ seen = set()
226
+ for fused_type in fused_compound_types:
227
+ fused_type.get_fused_types(result=base_types, seen=seen)
228
+ return base_types
229
+
230
+ def _specialize_function_args(self, args, fused_to_specific):
231
+ for arg in args:
232
+ if arg.type.is_fused:
233
+ arg.type = arg.type.specialize(fused_to_specific)
234
+ if arg.type.is_memoryviewslice:
235
+ arg.type.validate_memslice_dtype(arg.pos)
236
+ if arg.annotation:
237
+ # TODO might be nice if annotations were specialized instead?
238
+ # (Or might be hard to do reliably)
239
+ arg.annotation.untyped = True
240
+
241
+ def create_new_local_scope(self, node, env, f2s):
242
+ """
243
+ Create a new local scope for the copied node and append it to
244
+ self.nodes. A new local scope is needed because the arguments with the
245
+ fused types are already in the local scope, and we need the specialized
246
+ entries created after analyse_declarations on each specialized version
247
+ of the (CFunc)DefNode.
248
+ f2s is a dict mapping each fused type to its specialized version
249
+ """
250
+ node.create_local_scope(env)
251
+ node.local_scope.fused_to_specific = f2s
252
+
253
+ # This is copied from the original function, set it to false to
254
+ # stop recursion
255
+ node.has_fused_arguments = False
256
+ self.nodes.append(node)
257
+
258
+ def specialize_copied_def(self, node, cname, py_entry, f2s, fused_compound_types):
259
+ """Specialize the copy of a DefNode given the copied node,
260
+ the specialization cname and the original DefNode entry"""
261
+ fused_types = self._get_fused_base_types(fused_compound_types)
262
+ type_strings = [
263
+ PyrexTypes.specialization_signature_string(fused_type, f2s)
264
+ for fused_type in fused_types
265
+ ]
266
+
267
+ node.specialized_signature_string = '|'.join(type_strings)
268
+
269
+ node.entry.pymethdef_cname = PyrexTypes.get_fused_cname(
270
+ cname, node.entry.pymethdef_cname)
271
+ node.entry.doc = py_entry.doc
272
+ node.entry.doc_cname = py_entry.doc_cname
273
+
274
+ def replace_fused_typechecks(self, copied_node):
275
+ """
276
+ Branch-prune fused type checks like
277
+
278
+ if fused_t is int:
279
+ ...
280
+
281
+ Returns whether an error was issued and whether we should stop in
282
+ in order to prevent a flood of errors.
283
+ """
284
+ num_errors = Errors.get_errors_count()
285
+ transform = ParseTreeTransforms.ReplaceFusedTypeChecks(
286
+ copied_node.local_scope)
287
+ transform(copied_node)
288
+
289
+ if Errors.get_errors_count() > num_errors:
290
+ return False
291
+
292
+ return True
293
+
294
+ def _fused_instance_checks(self, normal_types, pyx_code, env):
295
+ """
296
+ Generate Cython code for instance checks, matching an object to
297
+ specialized types.
298
+ """
299
+ for specialized_type in normal_types:
300
+ # all_numeric = all_numeric and specialized_type.is_numeric
301
+ py_type_name = specialized_type.py_type_name()
302
+ pyx_code.put_chunk(
303
+ f"""
304
+ if isinstance(arg, {py_type_name}):
305
+ return '{specialized_type.specialization_string}'
306
+ """
307
+ )
308
+
309
+ def _dtype_name(self, dtype):
310
+ name = str(dtype).replace('_', '__').replace(' ', '_')
311
+ if dtype.is_typedef:
312
+ name = Naming.fused_dtype_prefix + name
313
+ return name
314
+
315
+ def _dtype_type(self, dtype):
316
+ if dtype.is_typedef:
317
+ return self._dtype_name(dtype)
318
+ return str(dtype)
319
+
320
+ def _sizeof_dtype(self, dtype):
321
+ if dtype.is_pyobject:
322
+ return 'sizeof(void *)'
323
+ else:
324
+ return f"sizeof({self._dtype_type(dtype)})"
325
+
326
+ def _buffer_check_numpy_dtype_setup_cases(self, pyx_code):
327
+ "Setup some common cases to match dtypes against specializations"
328
+ with pyx_code.indenter("if kind in u'iu':"):
329
+ pyx_code.putln("pass")
330
+ pyx_code.named_insertion_point("dtype_int")
331
+
332
+ with pyx_code.indenter("elif kind == u'f':"):
333
+ pyx_code.putln("pass")
334
+ pyx_code.named_insertion_point("dtype_float")
335
+
336
+ with pyx_code.indenter("elif kind == u'c':"):
337
+ pyx_code.putln("pass")
338
+ pyx_code.named_insertion_point("dtype_complex")
339
+
340
+ def _buffer_check_numpy_dtype(self, pyx_code, specialized_buffer_types, pythran_types):
341
+ """
342
+ Match a numpy dtype object to the individual specializations.
343
+ """
344
+ self._buffer_check_numpy_dtype_setup_cases(pyx_code)
345
+
346
+ for specialized_type in pythran_types+specialized_buffer_types:
347
+ final_type = specialized_type
348
+ if specialized_type.is_pythran_expr:
349
+ specialized_type = specialized_type.org_buffer
350
+ dtype = specialized_type.dtype
351
+
352
+ itemsize_match = self._sizeof_dtype(dtype) + " == itemsize"
353
+ signed_match = f" and not ({self._dtype_name(dtype)}_is_signed ^ dtype_signed)"
354
+
355
+ dtypes = [
356
+ (dtype.is_int, pyx_code['dtype_int']),
357
+ (dtype.is_float, pyx_code['dtype_float']),
358
+ (dtype.is_complex, pyx_code['dtype_complex'])
359
+ ]
360
+
361
+ for dtype_category, codewriter in dtypes:
362
+ if not dtype_category:
363
+ continue
364
+
365
+ cond = f'{itemsize_match} and (<Py_ssize_t>arg.ndim) == {specialized_type.ndim}'
366
+ if dtype.is_int:
367
+ cond += signed_match
368
+ if final_type.is_pythran_expr:
369
+ cond += ' and arg_is_pythran_compatible'
370
+
371
+ with codewriter.indenter(f"if {cond}:"):
372
+ #codewriter.putln("print 'buffer match found based on numpy dtype'")
373
+ codewriter.putln(f"return '{final_type.specialization_string}'")
374
+
375
+ def _buffer_parse_format_string_check(self, pyx_code, decl_code, specialized_type, env):
376
+ """
377
+ For each specialized type, try to coerce the object to a memoryview
378
+ slice of that type. This means obtaining a buffer and parsing the
379
+ format string.
380
+ TODO: separate buffer acquisition from format parsing
381
+ """
382
+ dtype = specialized_type.dtype
383
+ if specialized_type.is_buffer:
384
+ axes = [('direct', 'strided')] * specialized_type.ndim
385
+ else:
386
+ axes = specialized_type.axes
387
+
388
+ memslice_type = PyrexTypes.MemoryViewSliceType(dtype, axes)
389
+ memslice_type.create_from_py_utility_code(env)
390
+ coerce_from_py_func = memslice_type.from_py_function
391
+
392
+ decl_code.putln(
393
+ f"{Naming.memviewslice_cname} {coerce_from_py_func}(object, int)")
394
+
395
+ match = specialized_type.specialization_string
396
+ sizeof_dtype = self._sizeof_dtype(dtype)
397
+ ndim_dtype = specialized_type.ndim
398
+
399
+ # Use the memoryview object to check itemsize and ndim.
400
+ # In principle it could check more, but these are the easiest to do quickly.
401
+ pyx_code.put_chunk(
402
+ f"""
403
+ # try {dtype}
404
+ if (((itemsize == -1 and arg_as_memoryview.itemsize == {sizeof_dtype})
405
+ or itemsize == {sizeof_dtype})
406
+ and arg_as_memoryview.ndim == {ndim_dtype}):
407
+ memslice = {coerce_from_py_func}(arg_as_memoryview, 0)
408
+ if memslice.memview:
409
+ __PYX_XCLEAR_MEMVIEW(&memslice, 1)
410
+ # print 'found a match for the buffer through format parsing'
411
+ return '{match}'
412
+ else:
413
+ __pyx_PyErr_Clear()
414
+ """
415
+ )
416
+
417
+ def _buffer_checks(self, buffer_types, pythran_types, pyx_code, decl_code, accept_none, env):
418
+ """
419
+ Generate Cython code to match objects to buffer specializations.
420
+ First try to get a numpy dtype object and match it against the individual
421
+ specializations. If that fails, try naively to coerce the object
422
+ to each specialization, which obtains the buffer each time and tries
423
+ to match the format string.
424
+ """
425
+ # The first thing to find a match in this loop breaks out of the loop
426
+ pyx_code.put_chunk(
427
+ """
428
+ """ + ("arg_is_pythran_compatible = False" if pythran_types else "") + """
429
+ if ndarray is not None:
430
+ if isinstance(arg, ndarray):
431
+ dtype = arg.dtype
432
+ """ + ("arg_is_pythran_compatible = True" if pythran_types else "") + """
433
+ elif __pyx_memoryview_check(arg):
434
+ arg_base = arg.base
435
+ if isinstance(arg_base, ndarray):
436
+ dtype = arg_base.dtype
437
+ else:
438
+ dtype = None
439
+ else:
440
+ dtype = None
441
+
442
+ itemsize = -1
443
+ if dtype is not None:
444
+ itemsize = dtype.itemsize
445
+ kind = ord(dtype.kind)
446
+ dtype_signed = kind == u'i'
447
+ """)
448
+ pyx_code.indent(2)
449
+ if pythran_types:
450
+ pyx_code.put_chunk(
451
+ """
452
+ # Pythran only supports the endianness of the current compiler
453
+ byteorder = dtype.byteorder
454
+ if byteorder == "<" and not __Pyx_Is_Little_Endian():
455
+ arg_is_pythran_compatible = False
456
+ elif byteorder == ">" and __Pyx_Is_Little_Endian():
457
+ arg_is_pythran_compatible = False
458
+ if arg_is_pythran_compatible:
459
+ cur_stride = itemsize
460
+ shape = arg.shape
461
+ strides = arg.strides
462
+ for i in range(arg.ndim-1, -1, -1):
463
+ if (<Py_ssize_t>strides[i]) != cur_stride:
464
+ arg_is_pythran_compatible = False
465
+ break
466
+ cur_stride *= <Py_ssize_t> shape[i]
467
+ else:
468
+ arg_is_pythran_compatible = not (arg.flags.f_contiguous and (<Py_ssize_t>arg.ndim) > 1)
469
+ """)
470
+ self._buffer_check_numpy_dtype(pyx_code, buffer_types, pythran_types)
471
+ pyx_code.dedent(2)
472
+
473
+ if accept_none:
474
+ # If None is acceptable, then Cython <3.0 matched None with the
475
+ # first type. This behaviour isn't ideal, but keep it for backwards
476
+ # compatibility. Better behaviour would be to see if subsequent
477
+ # arguments give a stronger match.
478
+ pyx_code.put_chunk(
479
+ f"""
480
+ if arg is None:
481
+ return '{buffer_types[0].specialization_string}'
482
+ """
483
+ )
484
+
485
+ # creating a Cython memoryview from a Python memoryview avoids the
486
+ # need to get the buffer multiple times, and we can
487
+ # also use it to check itemsizes etc
488
+ pyx_code.put_chunk(
489
+ """
490
+ try:
491
+ arg_as_memoryview = memoryview(arg)
492
+ except (ValueError, TypeError):
493
+ pass
494
+ """)
495
+ with pyx_code.indenter("else:"):
496
+ for specialized_type in buffer_types:
497
+ self._buffer_parse_format_string_check(
498
+ pyx_code, decl_code, specialized_type, env)
499
+
500
+ def _buffer_declarations(self, pyx_code, decl_code, all_buffer_types, pythran_types):
501
+ """
502
+ If we have any buffer specializations, write out some variable and type declarations.
503
+ """
504
+ decl_code.put_chunk(
505
+ f"""
506
+ ctypedef struct {Naming.memviewslice_cname}:
507
+ void *memview
508
+
509
+ void __PYX_XCLEAR_MEMVIEW({Naming.memviewslice_cname} *, int have_gil)
510
+ bint __pyx_memoryview_check(object)
511
+ """)
512
+
513
+ pyx_code['local_variable_declarations'].put_chunk(
514
+ f"""
515
+ cdef {Naming.memviewslice_cname} memslice
516
+ cdef Py_ssize_t itemsize
517
+ cdef bint dtype_signed
518
+ cdef Py_UCS4 kind
519
+
520
+ itemsize = -1
521
+ """)
522
+
523
+ if pythran_types:
524
+ pyx_code['local_variable_declarations'].put_chunk("""
525
+ cdef bint arg_is_pythran_compatible
526
+ cdef Py_ssize_t cur_stride
527
+ cdef Py_ssize_t i
528
+ """)
529
+
530
+ pyx_code['local_variable_declarations'].put_chunk(
531
+ """
532
+ cdef memoryview arg_as_memoryview
533
+ """
534
+ )
535
+
536
+ seen_typedefs = set()
537
+ seen_int_dtypes = set()
538
+ seen_structs = set()
539
+ for buffer_type in all_buffer_types:
540
+ dtype = buffer_type.dtype
541
+ dtype_name = self._dtype_name(dtype)
542
+ if dtype.is_struct_or_union:
543
+ if dtype_name not in seen_structs:
544
+ seen_structs.add(dtype_name)
545
+ decl_code.putln(
546
+ f'ctypedef {dtype.kind} {dtype_name} "{dtype.empty_declaration_code()}": pass')
547
+
548
+ elif dtype.is_typedef:
549
+ if dtype_name not in seen_typedefs:
550
+ seen_typedefs.add(dtype_name)
551
+ decl_code.putln(
552
+ f'ctypedef {dtype.resolve()} {dtype_name} "{dtype.empty_declaration_code()}"')
553
+
554
+ # 'is_signed' is also needed for typedefs.
555
+ if dtype.is_int:
556
+ if str(dtype) not in seen_int_dtypes:
557
+ seen_int_dtypes.add(str(dtype))
558
+ dtype_type = self._dtype_type(dtype)
559
+ pyx_code['local_variable_declarations'].put_chunk(
560
+ f"""
561
+ cdef bint {dtype_name}_is_signed
562
+ {dtype_name}_is_signed = not (<{dtype_type}> -1 > 0)
563
+ """)
564
+
565
+ def _split_fused_types(self, arg):
566
+ """
567
+ Specialize fused types and split into normal types and buffer types.
568
+ """
569
+ specialized_types = PyrexTypes.get_specialized_types(arg.type)
570
+
571
+ # Prefer long over int, etc by sorting (see type classes in PyrexTypes.py)
572
+ specialized_types.sort()
573
+
574
+ seen_py_type_names = set()
575
+ normal_types, buffer_types, pythran_types = [], [], []
576
+ has_object_fallback = False
577
+ for specialized_type in specialized_types:
578
+ py_type_name = specialized_type.py_type_name()
579
+ if py_type_name:
580
+ if py_type_name in seen_py_type_names:
581
+ continue
582
+ seen_py_type_names.add(py_type_name)
583
+ if py_type_name == 'object':
584
+ has_object_fallback = True
585
+ else:
586
+ normal_types.append(specialized_type)
587
+ elif specialized_type.is_pythran_expr:
588
+ pythran_types.append(specialized_type)
589
+ elif specialized_type.is_buffer or specialized_type.is_memoryviewslice:
590
+ buffer_types.append(specialized_type)
591
+
592
+ return normal_types, buffer_types, pythran_types, has_object_fallback
593
+
594
+ def _unpack_argument(self, pyx_code, arg, arg_tuple_idx, min_positional_args, default_idx):
595
+ pyx_code.put_chunk(
596
+ f"""
597
+ # PROCESSING ARGUMENT {arg_tuple_idx}
598
+ if {arg_tuple_idx} < len(<tuple>args):
599
+ arg = (<tuple>args)[{arg_tuple_idx}]
600
+ elif kwargs is not None and '{arg.name}' in <dict>kwargs:
601
+ arg = (<dict>kwargs)['{arg.name}']
602
+ else:
603
+ """
604
+ )
605
+ pyx_code.indent()
606
+ if arg.default:
607
+ pyx_code.putln(
608
+ f"arg = (<tuple>defaults)[{default_idx}]")
609
+ elif arg_tuple_idx < min_positional_args:
610
+ pyx_code.putln(
611
+ 'raise TypeError("Expected at least %d argument%s, got %d" % ('
612
+ f'''{min_positional_args}, {'"s"' if min_positional_args != 1 else '""'}, len(<tuple>args)))'''
613
+ )
614
+ else:
615
+ pyx_code.putln(f"""raise TypeError("Missing keyword-only argument: '%s'" % "{arg.name}")""")
616
+ pyx_code.dedent()
617
+
618
+ def make_fused_cpdef(self, orig_py_func, env, is_def):
619
+ """
620
+ This creates the function that is indexable from Python and does
621
+ runtime dispatch based on the argument types. The function gets the
622
+ arg tuple and kwargs dict (or None) and the defaults tuple
623
+ as arguments from the Binding Fused Function's tp_call.
624
+ """
625
+ from . import TreeFragment, Code, UtilityCode
626
+
627
+ min_positional_args = (
628
+ self.node.num_required_args - self.node.num_required_kw_args
629
+ if is_def else
630
+ sum(1 for arg in self.node.args if arg.default is None)
631
+ )
632
+
633
+ pyx_code = Code.PyxCodeWriter()
634
+ decl_code = Code.PyxCodeWriter()
635
+ type_mapper = Code.PyxCodeWriter()
636
+ decl_code.put_chunk(
637
+ """
638
+ cdef extern from *:
639
+ type __Pyx_ImportNumPyArrayTypeIfAvailable()
640
+
641
+ # from FusedFunction utility code
642
+ object __pyx_ff_match_signatures_single(dict signatures, dest_type)
643
+ object __pyx_ff_match_signatures(dict signatures, tuple dest_sig, dict sigindex)
644
+ """)
645
+ decl_code.indent()
646
+
647
+ pyx_code.put_chunk(
648
+ """
649
+ def __pyx_fused_cpdef(signatures, args, kwargs, defaults, _fused_sigindex={}):
650
+ # FIXME: use a typed signature - currently fails badly because
651
+ # default arguments inherit the types we specify here!
652
+
653
+ if kwargs is not None and not kwargs:
654
+ kwargs = None
655
+
656
+ # instance check body
657
+ """)
658
+
659
+ pyx_code.indent() # indent following code to function body
660
+ pyx_code.named_insertion_point("imports")
661
+
662
+ fused_index = 0
663
+ default_idx = 0
664
+ all_buffer_types = OrderedSet()
665
+ seen_fused_types = set()
666
+ for i, arg in enumerate(self.node.args):
667
+ if arg.type.is_fused:
668
+ arg_fused_types = arg.type.get_fused_types()
669
+ if len(arg_fused_types) > 1:
670
+ raise NotImplementedError("Determination of more than one fused base "
671
+ "type per argument is not implemented.")
672
+ fused_type = arg_fused_types[0]
673
+
674
+ if arg.type.is_fused and fused_type not in seen_fused_types:
675
+ seen_fused_types.add(fused_type)
676
+
677
+ normal_types, buffer_types, pythran_types, has_object_fallback = self._split_fused_types(arg)
678
+ self._unpack_argument(pyx_code, arg, i, min_positional_args, default_idx)
679
+
680
+ mapper_arg_types = ['object', 'type']
681
+ mapper_arg_names = ['arg']
682
+ if buffer_types or pythran_types:
683
+ mapper_arg_names.append('ndarray')
684
+
685
+ mapper_sig = ', '.join(f"{atype} {aname}" for atype, aname in zip(mapper_arg_types, mapper_arg_names))
686
+ mapper_args = ', '.join(mapper_arg_names)
687
+
688
+ mapper_decl_code = type_mapper.insertion_point()
689
+ mapper_decl_code.put_chunk(
690
+ """
691
+ cdef extern from *:
692
+ void __pyx_PyErr_Clear "PyErr_Clear" ()
693
+ int __Pyx_Is_Little_Endian()
694
+ """
695
+ )
696
+ mapper_decl_code.indent()
697
+
698
+ type_mapper.putln('')
699
+ type_mapper.putln("@TYPE_MAPPER_CNAME_PLACEHOLDER")
700
+ with type_mapper.indenter(f"cdef str map_fused_type({mapper_sig}):"):
701
+
702
+ type_mapper.named_insertion_point("local_variable_declarations")
703
+
704
+ if normal_types:
705
+ self._fused_instance_checks(normal_types, type_mapper, env)
706
+
707
+ if buffer_types or pythran_types:
708
+ mapper_buffer_types = OrderedSet()
709
+ mapper_buffer_types.update(buffer_types)
710
+ mapper_buffer_types.update(ty.org_buffer for ty in pythran_types)
711
+
712
+ self._buffer_declarations(type_mapper, mapper_decl_code, mapper_buffer_types, pythran_types)
713
+
714
+ self._buffer_checks(
715
+ buffer_types, pythran_types, type_mapper, mapper_decl_code,
716
+ arg.accept_none, env)
717
+
718
+ type_mapper.putln("return 'object'" if has_object_fallback else "return None")
719
+
720
+ type_mapper_impl = type_mapper.getvalue()
721
+ type_mapper.reset()
722
+
723
+ # Generate a unique name for the mapper function based on type declarations and implementation.
724
+ impl_hash = hashlib.sha256(type_mapper_impl.encode('utf-8')).hexdigest()
725
+ type_mapper_cname = (
726
+ f"__pyx_ff_map_fused_{impl_hash[:6]}"
727
+ f"_{len(mapper_arg_names)}_{len(fused_type.types)}"
728
+ f"_{PyrexTypes.type_list_identifier(fused_type.types)}"
729
+ )
730
+ type_mapper_impl = type_mapper_impl.replace(
731
+ "\n@TYPE_MAPPER_CNAME_PLACEHOLDER\n", f"\n@cname('{type_mapper_cname}')\n")
732
+ # print(''.join(f"{i:3d} {line}" for i, line in enumerate(type_mapper_impl.splitlines(keepends=True))))
733
+
734
+ env.use_utility_code(
735
+ UtilityCode.CythonUtilityCode(type_mapper_impl, name=type_mapper_cname))
736
+
737
+ decl_code.putln(f"str {type_mapper_cname}({mapper_sig})")
738
+ pyx_code.putln(f"dest_sig{fused_index} = {type_mapper_cname}({mapper_args})")
739
+
740
+ fused_index += 1
741
+ all_buffer_types.update(buffer_types)
742
+ all_buffer_types.update(ty.org_buffer for ty in pythran_types)
743
+
744
+ if arg.default:
745
+ default_idx += 1
746
+
747
+ if all_buffer_types:
748
+ env.use_utility_code(
749
+ Code.UtilityCode.load_cached("IsLittleEndian", "ModuleSetupCode.c"))
750
+ env.use_utility_code(
751
+ Code.UtilityCode.load_cached("Import", "ImportExport.c"))
752
+ env.use_utility_code(
753
+ Code.UtilityCode.load_cached("ImportNumPyArray", "ImportExport.c"))
754
+
755
+ pyx_code['imports'].put_chunk(
756
+ """
757
+ cdef type ndarray
758
+ ndarray = __Pyx_ImportNumPyArrayTypeIfAvailable()
759
+ """)
760
+
761
+ if len(seen_fused_types) == 1:
762
+ # Fast and common case: a single fused type across all arguments.
763
+ env.use_utility_code(
764
+ UtilityCode.CythonUtilityCode.load("match_signatures_single", "FusedFunction.pyx"))
765
+ pyx_code.put_chunk(
766
+ """
767
+ return __pyx_ff_match_signatures_single(<dict> signatures, dest_sig0)
768
+ """
769
+ )
770
+ else:
771
+ env.use_utility_code(
772
+ UtilityCode.CythonUtilityCode.load("match_signatures", "FusedFunction.pyx"))
773
+ dest_sig_tuple = ', '.join(f'dest_sig{i}' for i in range(len(seen_fused_types)))
774
+ pyx_code.put_chunk(
775
+ f"""
776
+ return __pyx_ff_match_signatures(<dict> signatures, ({dest_sig_tuple}), <dict> _fused_sigindex)
777
+ """
778
+ )
779
+
780
+ fragment_code = pyx_code.getvalue()
781
+ # print(decl_code.getvalue())
782
+ # print(fragment_code)
783
+ # print(''.join(f"{i:3d} {line}" for i, line in enumerate(fragment_code.splitlines(keepends=True))))
784
+ from .Optimize import ConstantFolding
785
+ fragment = TreeFragment.TreeFragment(
786
+ fragment_code, level='module', pipeline=[ConstantFolding()])
787
+ ast = TreeFragment.SetPosTransform(self.node.pos)(fragment.root)
788
+ UtilityCode.declare_declarations_in_scope(
789
+ decl_code.getvalue(), env.global_scope())
790
+ ast.scope = env
791
+ # FIXME: for static methods of cdef classes, we build the wrong signature here: first arg becomes 'self'
792
+ ast.analyse_declarations(env)
793
+ py_func = ast.stats[-1] # the DefNode
794
+ self.fragment_scope = ast.scope
795
+
796
+ if isinstance(self.node, DefNode):
797
+ py_func.specialized_cpdefs = self.nodes[:]
798
+ else:
799
+ py_func.specialized_cpdefs = [n.py_func for n in self.nodes]
800
+
801
+ return py_func
802
+
803
+ def update_fused_defnode_entry(self, env):
804
+ copy_attributes = (
805
+ 'name', 'pos', 'cname', 'func_cname', 'pyfunc_cname',
806
+ 'pymethdef_cname', 'doc', 'doc_cname', 'is_member',
807
+ 'scope'
808
+ )
809
+
810
+ entry = self.py_func.entry
811
+
812
+ for attr in copy_attributes:
813
+ setattr(entry, attr,
814
+ getattr(self.orig_py_func.entry, attr))
815
+
816
+ self.py_func.name = self.orig_py_func.name
817
+ self.py_func.doc = self.orig_py_func.doc
818
+
819
+ env.entries.pop('__pyx_fused_cpdef', None)
820
+ if isinstance(self.node, DefNode):
821
+ env.entries[entry.name] = entry
822
+ else:
823
+ env.entries[entry.name].as_variable = entry
824
+
825
+ env.pyfunc_entries.append(entry)
826
+
827
+ self.py_func.entry.fused_cfunction = self
828
+ def_nodes = []
829
+ for node in self.nodes:
830
+ if isinstance(self.node, DefNode):
831
+ def_nodes.append(node)
832
+ node.fused_py_func = self.py_func
833
+ else:
834
+ def_nodes.append(node.py_func)
835
+ node.py_func.fused_py_func = self.py_func
836
+ node.entry.as_variable = entry
837
+
838
+ self.synthesize_defnodes(def_nodes)
839
+
840
+ def analyse_expressions(self, env):
841
+ """
842
+ Analyse the expressions. Take care to only evaluate default arguments
843
+ once and clone the result for all specializations
844
+ """
845
+ for fused_compound_type in self.fused_compound_types:
846
+ for fused_type in fused_compound_type.get_fused_types():
847
+ for specialization_type in fused_type.types:
848
+ if specialization_type.is_complex:
849
+ specialization_type.create_declaration_utility_code(env)
850
+
851
+ if self.py_func:
852
+ self.__signatures__ = self.__signatures__.analyse_expressions(env)
853
+ self.py_func = self.py_func.analyse_expressions(env)
854
+ self.resulting_fused_function = self.resulting_fused_function.analyse_expressions(env)
855
+ self.fused_func_assignment = self.fused_func_assignment.analyse_expressions(env)
856
+
857
+ self.defaults = defaults = []
858
+
859
+ for arg in self.node.args:
860
+ if arg.default:
861
+ arg.default = arg.default.analyse_expressions(env)
862
+ if arg.default.is_literal:
863
+ defaults.append(copy.copy(arg.default))
864
+ else:
865
+ # coerce the argument to temp since CloneNode really requires a temp
866
+ defaults.append(ProxyNode(arg.default.coerce_to_temp(env)))
867
+ else:
868
+ defaults.append(None)
869
+
870
+ for i, stat in enumerate(self.stats):
871
+ stat = self.stats[i] = stat.analyse_expressions(env)
872
+ if isinstance(stat, FuncDefNode) and stat is not self.py_func:
873
+ # the dispatcher specifically doesn't want its defaults overriding
874
+ for arg, default in zip(stat.args, defaults):
875
+ if default is not None:
876
+ if default.is_literal:
877
+ arg.default = default.coerce_to(arg.type, env)
878
+ else:
879
+ arg.default = CloneNode(default).analyse_expressions(env).coerce_to(arg.type, env)
880
+
881
+ if self.py_func:
882
+ args = [CloneNode(default) for default in defaults if default]
883
+ self.defaults_tuple = TupleNode(self.pos, args=args)
884
+ self.defaults_tuple = self.defaults_tuple.analyse_types(env, skip_children=True).coerce_to_pyobject(env)
885
+ self.defaults_tuple = ProxyNode(self.defaults_tuple)
886
+
887
+ fused_func = self.resulting_fused_function.arg
888
+ fused_func.defaults_tuple = CloneNode(self.defaults_tuple)
889
+
890
+ for i, pycfunc in enumerate(self.specialized_pycfuncs):
891
+ pycfunc = self.specialized_pycfuncs[i] = pycfunc.analyse_types(env)
892
+ pycfunc.defaults_tuple = CloneNode(self.defaults_tuple)
893
+ return self
894
+
895
+ def synthesize_defnodes(self, nodes):
896
+ """
897
+ Create the __signatures__ dict of PyCFunctionNode specializations.
898
+ """
899
+ # For the moment, fused functions do not support METH_FASTCALL
900
+ for node in nodes:
901
+ node.entry.signature.use_fastcall = False
902
+
903
+ signatures = [StringEncoding.EncodedString(node.specialized_signature_string)
904
+ for node in nodes]
905
+ keys = [ExprNodes.UnicodeNode(node.pos, value=sig)
906
+ for node, sig in zip(nodes, signatures)]
907
+ values = [ExprNodes.PyCFunctionNode.from_defnode(node, binding=True)
908
+ for node in nodes]
909
+
910
+ self.__signatures__ = ExprNodes.DictNode.from_pairs(self.pos, zip(keys, values))
911
+
912
+ self.specialized_pycfuncs = values
913
+ for pycfuncnode in values:
914
+ pycfuncnode.is_specialization = True
915
+ # use code object from first defnode to get as close to a correct signature as possible
916
+ self.py_func.code_object = CodeObjectNode(nodes[0])
917
+
918
+ def generate_function_definitions(self, env, code):
919
+ if self.py_func:
920
+ self.py_func.pymethdef_required = True
921
+ self.fused_func_assignment.generate_function_definitions(env, code)
922
+
923
+ from . import Options
924
+ for stat in self.stats:
925
+ if isinstance(stat, FuncDefNode) and (
926
+ stat.entry.used or
927
+ (Options.cimport_from_pyx and not stat.entry.visibility == 'extern')):
928
+ code.mark_pos(stat.pos)
929
+ stat.generate_function_definitions(env, code)
930
+
931
+ def generate_execution_code(self, code):
932
+ # Note: all def function specialization are wrapped in PyCFunction
933
+ # nodes in the self.__signatures__ dictnode.
934
+ for default in self.defaults:
935
+ if default is not None:
936
+ default.generate_evaluation_code(code)
937
+
938
+ if self.py_func:
939
+ self.defaults_tuple.generate_evaluation_code(code)
940
+
941
+ super().generate_execution_code(code)
942
+
943
+ if self.__signatures__:
944
+ signatures = self.__signatures__
945
+ signatures.generate_evaluation_code(code)
946
+ fused_func = self.resulting_fused_function
947
+ fused_func.generate_evaluation_code(code)
948
+
949
+ code.putln(
950
+ f"((__pyx_FusedFunctionObject *) {fused_func.result()})->__signatures__ = {signatures.result()};")
951
+
952
+ signatures.generate_giveref(code)
953
+ signatures.generate_post_assignment_code(code)
954
+ signatures.free_temps(code)
955
+
956
+ self.fused_func_assignment.generate_execution_code(code)
957
+
958
+ # Dispose of results
959
+ fused_func.generate_disposal_code(code)
960
+ fused_func.free_temps(code)
961
+ self.defaults_tuple.generate_disposal_code(code)
962
+ self.defaults_tuple.free_temps(code)
963
+
964
+ for default in self.defaults:
965
+ if default is not None:
966
+ default.generate_disposal_code(code)
967
+ default.free_temps(code)
968
+
969
+ def annotate(self, code):
970
+ for stat in self.stats:
971
+ stat.annotate(code)