Cython 3.3.0a1__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 (335) 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 +1276 -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 +84 -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 +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 +997 -0
  24. Cython/Compiler/CmdLine.py +263 -0
  25. Cython/Compiler/Code.cp315-win_amd64.pyd +0 -0
  26. Cython/Compiler/Code.pxd +152 -0
  27. Cython/Compiler/Code.py +3907 -0
  28. Cython/Compiler/CodeGeneration.py +33 -0
  29. Cython/Compiler/CythonScope.py +194 -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 +15983 -0
  34. Cython/Compiler/FlowControl.cp315-win_amd64.pyd +0 -0
  35. Cython/Compiler/FlowControl.pxd +99 -0
  36. Cython/Compiler/FlowControl.py +1571 -0
  37. Cython/Compiler/FusedNode.cp315-win_amd64.pyd +0 -0
  38. Cython/Compiler/FusedNode.py +976 -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 +2197 -0
  46. Cython/Compiler/MemoryView.py +930 -0
  47. Cython/Compiler/ModuleNode.py +4517 -0
  48. Cython/Compiler/Naming.py +367 -0
  49. Cython/Compiler/Nodes.py +10941 -0
  50. Cython/Compiler/Optimize.py +5455 -0
  51. Cython/Compiler/Options.py +838 -0
  52. Cython/Compiler/ParseTreeTransforms.pxd +79 -0
  53. Cython/Compiler/ParseTreeTransforms.py +4744 -0
  54. Cython/Compiler/Parsing.cp315-win_amd64.pyd +0 -0
  55. Cython/Compiler/Parsing.pxd +9 -0
  56. Cython/Compiler/Parsing.py +4792 -0
  57. Cython/Compiler/Pipeline.py +439 -0
  58. Cython/Compiler/PyrexTypes.py +6111 -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.py +297 -0
  64. Cython/Compiler/Symtab.py +3092 -0
  65. Cython/Compiler/Tests/TestBuffer.py +105 -0
  66. Cython/Compiler/Tests/TestBuiltin.py +117 -0
  67. Cython/Compiler/Tests/TestCmdLine.py +587 -0
  68. Cython/Compiler/Tests/TestCode.py +145 -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 +132 -0
  74. Cython/Compiler/Tests/TestSignatureMatching.py +73 -0
  75. Cython/Compiler/Tests/TestStringEncoding.py +20 -0
  76. Cython/Compiler/Tests/TestTreeFragment.py +63 -0
  77. Cython/Compiler/Tests/TestTreePath.py +103 -0
  78. Cython/Compiler/Tests/TestTypes.py +118 -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 +611 -0
  86. Cython/Compiler/TypeSlots.py +1329 -0
  87. Cython/Compiler/UFuncs.py +311 -0
  88. Cython/Compiler/UtilNodes.py +413 -0
  89. Cython/Compiler/UtilityCode.py +348 -0
  90. Cython/Compiler/Version.py +8 -0
  91. Cython/Compiler/Visitor.cp315-win_amd64.pyd +0 -0
  92. Cython/Compiler/Visitor.pxd +53 -0
  93. Cython/Compiler/Visitor.py +864 -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 +280 -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 +580 -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 +152 -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 +268 -0
  127. Cython/Includes/cpython/exc.pxd +263 -0
  128. Cython/Includes/cpython/fileobject.pxd +57 -0
  129. Cython/Includes/cpython/float.pxd +56 -0
  130. Cython/Includes/cpython/frozendict.pxd +37 -0
  131. Cython/Includes/cpython/function.pxd +65 -0
  132. Cython/Includes/cpython/genobject.pxd +25 -0
  133. Cython/Includes/cpython/getargs.pxd +12 -0
  134. Cython/Includes/cpython/instance.pxd +25 -0
  135. Cython/Includes/cpython/iterator.pxd +36 -0
  136. Cython/Includes/cpython/iterobject.pxd +24 -0
  137. Cython/Includes/cpython/list.pxd +144 -0
  138. Cython/Includes/cpython/long.pxd +180 -0
  139. Cython/Includes/cpython/longintrepr.pxd +14 -0
  140. Cython/Includes/cpython/mapping.pxd +63 -0
  141. Cython/Includes/cpython/marshal.pxd +66 -0
  142. Cython/Includes/cpython/mem.pxd +120 -0
  143. Cython/Includes/cpython/memoryview.pxd +50 -0
  144. Cython/Includes/cpython/method.pxd +49 -0
  145. Cython/Includes/cpython/module.pxd +208 -0
  146. Cython/Includes/cpython/number.pxd +258 -0
  147. Cython/Includes/cpython/object.pxd +430 -0
  148. Cython/Includes/cpython/pycapsule.pxd +143 -0
  149. Cython/Includes/cpython/pylifecycle.pxd +68 -0
  150. Cython/Includes/cpython/pyport.pxd +8 -0
  151. Cython/Includes/cpython/pystate.pxd +95 -0
  152. Cython/Includes/cpython/pythread.pxd +53 -0
  153. Cython/Includes/cpython/ref.pxd +141 -0
  154. Cython/Includes/cpython/sentinel.pxd +17 -0
  155. Cython/Includes/cpython/sequence.pxd +134 -0
  156. Cython/Includes/cpython/set.pxd +119 -0
  157. Cython/Includes/cpython/slice.pxd +70 -0
  158. Cython/Includes/cpython/time.pxd +129 -0
  159. Cython/Includes/cpython/tuple.pxd +72 -0
  160. Cython/Includes/cpython/type.pxd +146 -0
  161. Cython/Includes/cpython/unicode.pxd +639 -0
  162. Cython/Includes/cpython/version.pxd +32 -0
  163. Cython/Includes/cpython/weakref.pxd +78 -0
  164. Cython/Includes/libc/__init__.pxd +1 -0
  165. Cython/Includes/libc/complex.pxd +35 -0
  166. Cython/Includes/libc/errno.pxd +127 -0
  167. Cython/Includes/libc/float.pxd +43 -0
  168. Cython/Includes/libc/limits.pxd +28 -0
  169. Cython/Includes/libc/locale.pxd +46 -0
  170. Cython/Includes/libc/math.pxd +209 -0
  171. Cython/Includes/libc/setjmp.pxd +10 -0
  172. Cython/Includes/libc/signal.pxd +64 -0
  173. Cython/Includes/libc/stddef.pxd +9 -0
  174. Cython/Includes/libc/stdint.pxd +105 -0
  175. Cython/Includes/libc/stdio.pxd +80 -0
  176. Cython/Includes/libc/stdlib.pxd +72 -0
  177. Cython/Includes/libc/string.pxd +50 -0
  178. Cython/Includes/libc/threads.pxd +234 -0
  179. Cython/Includes/libc/time.pxd +52 -0
  180. Cython/Includes/libcpp/__init__.pxd +4 -0
  181. Cython/Includes/libcpp/algorithm.pxd +320 -0
  182. Cython/Includes/libcpp/any.pxd +16 -0
  183. Cython/Includes/libcpp/atomic.pxd +59 -0
  184. Cython/Includes/libcpp/barrier.pxd +22 -0
  185. Cython/Includes/libcpp/bit.pxd +29 -0
  186. Cython/Includes/libcpp/cast.pxd +12 -0
  187. Cython/Includes/libcpp/cmath.pxd +518 -0
  188. Cython/Includes/libcpp/complex.pxd +106 -0
  189. Cython/Includes/libcpp/condition_variable.pxd +322 -0
  190. Cython/Includes/libcpp/deque.pxd +165 -0
  191. Cython/Includes/libcpp/exception.pxd +216 -0
  192. Cython/Includes/libcpp/execution.pxd +15 -0
  193. Cython/Includes/libcpp/forward_list.pxd +63 -0
  194. Cython/Includes/libcpp/functional.pxd +26 -0
  195. Cython/Includes/libcpp/future.pxd +103 -0
  196. Cython/Includes/libcpp/iterator.pxd +34 -0
  197. Cython/Includes/libcpp/latch.pxd +17 -0
  198. Cython/Includes/libcpp/limits.pxd +61 -0
  199. Cython/Includes/libcpp/list.pxd +117 -0
  200. Cython/Includes/libcpp/map.pxd +252 -0
  201. Cython/Includes/libcpp/memory.pxd +115 -0
  202. Cython/Includes/libcpp/mutex.pxd +387 -0
  203. Cython/Includes/libcpp/numbers.pxd +15 -0
  204. Cython/Includes/libcpp/numeric.pxd +131 -0
  205. Cython/Includes/libcpp/optional.pxd +34 -0
  206. Cython/Includes/libcpp/pair.pxd +1 -0
  207. Cython/Includes/libcpp/queue.pxd +25 -0
  208. Cython/Includes/libcpp/random.pxd +166 -0
  209. Cython/Includes/libcpp/semaphore.pxd +43 -0
  210. Cython/Includes/libcpp/set.pxd +228 -0
  211. Cython/Includes/libcpp/shared_mutex.pxd +96 -0
  212. Cython/Includes/libcpp/span.pxd +87 -0
  213. Cython/Includes/libcpp/stack.pxd +11 -0
  214. Cython/Includes/libcpp/stop_token.pxd +117 -0
  215. Cython/Includes/libcpp/string.pxd +355 -0
  216. Cython/Includes/libcpp/string_view.pxd +183 -0
  217. Cython/Includes/libcpp/typeindex.pxd +15 -0
  218. Cython/Includes/libcpp/typeinfo.pxd +10 -0
  219. Cython/Includes/libcpp/unordered_map.pxd +193 -0
  220. Cython/Includes/libcpp/unordered_set.pxd +152 -0
  221. Cython/Includes/libcpp/utility.pxd +30 -0
  222. Cython/Includes/libcpp/vector.pxd +186 -0
  223. Cython/Includes/numpy/math.pxd +150 -0
  224. Cython/Includes/openmp.pxd +50 -0
  225. Cython/Includes/posix/__init__.pxd +1 -0
  226. Cython/Includes/posix/dlfcn.pxd +14 -0
  227. Cython/Includes/posix/fcntl.pxd +86 -0
  228. Cython/Includes/posix/ioctl.pxd +4 -0
  229. Cython/Includes/posix/mman.pxd +101 -0
  230. Cython/Includes/posix/resource.pxd +57 -0
  231. Cython/Includes/posix/select.pxd +21 -0
  232. Cython/Includes/posix/signal.pxd +73 -0
  233. Cython/Includes/posix/stat.pxd +98 -0
  234. Cython/Includes/posix/stdio.pxd +37 -0
  235. Cython/Includes/posix/stdlib.pxd +29 -0
  236. Cython/Includes/posix/strings.pxd +9 -0
  237. Cython/Includes/posix/time.pxd +71 -0
  238. Cython/Includes/posix/types.pxd +30 -0
  239. Cython/Includes/posix/uio.pxd +26 -0
  240. Cython/Includes/posix/unistd.pxd +271 -0
  241. Cython/Includes/posix/wait.pxd +38 -0
  242. Cython/LZSS.py +170 -0
  243. Cython/Plex/Actions.cp315-win_amd64.pyd +0 -0
  244. Cython/Plex/Actions.pxd +24 -0
  245. Cython/Plex/Actions.py +119 -0
  246. Cython/Plex/DFA.cp315-win_amd64.pyd +0 -0
  247. Cython/Plex/DFA.pxd +14 -0
  248. Cython/Plex/DFA.py +164 -0
  249. Cython/Plex/Errors.py +48 -0
  250. Cython/Plex/Lexicons.py +178 -0
  251. Cython/Plex/Machines.cp315-win_amd64.pyd +0 -0
  252. Cython/Plex/Machines.pxd +36 -0
  253. Cython/Plex/Machines.py +238 -0
  254. Cython/Plex/Regexps.py +535 -0
  255. Cython/Plex/Scanners.cp315-win_amd64.pyd +0 -0
  256. Cython/Plex/Scanners.pxd +45 -0
  257. Cython/Plex/Scanners.py +328 -0
  258. Cython/Plex/Transitions.cp315-win_amd64.pyd +0 -0
  259. Cython/Plex/Transitions.pxd +14 -0
  260. Cython/Plex/Transitions.py +239 -0
  261. Cython/Plex/__init__.py +34 -0
  262. Cython/Runtime/__init__.py +1 -0
  263. Cython/Runtime/refnanny.cp315-win_amd64.pyd +0 -0
  264. Cython/Runtime/refnanny.pyx +237 -0
  265. Cython/Shadow.py +1167 -0
  266. Cython/StringIOTree.cp315-win_amd64.pyd +0 -0
  267. Cython/StringIOTree.py +169 -0
  268. Cython/Tempita/__init__.py +4 -0
  269. Cython/Tempita/_looper.py +154 -0
  270. Cython/Tempita/_tempita.cp315-win_amd64.pyd +0 -0
  271. Cython/Tempita/_tempita.py +1087 -0
  272. Cython/TestUtils.py +464 -0
  273. Cython/Tests/TestCodeWriter.py +128 -0
  274. Cython/Tests/TestCythonUtils.py +202 -0
  275. Cython/Tests/TestJediTyper.py +223 -0
  276. Cython/Tests/TestShadow.py +110 -0
  277. Cython/Tests/TestStringIOTree.py +68 -0
  278. Cython/Tests/TestTestUtils.py +89 -0
  279. Cython/Tests/__init__.py +1 -0
  280. Cython/Tests/xmlrunner.py +390 -0
  281. Cython/Utility/AsyncGen.c +1073 -0
  282. Cython/Utility/Buffer.c +866 -0
  283. Cython/Utility/BufferFormatFromTypeInfo.pxd +2 -0
  284. Cython/Utility/Builtins.c +933 -0
  285. Cython/Utility/CConvert.pyx +149 -0
  286. Cython/Utility/CMath.c +104 -0
  287. Cython/Utility/CommonStructures.c +244 -0
  288. Cython/Utility/Complex.c +378 -0
  289. Cython/Utility/Coroutine.c +2337 -0
  290. Cython/Utility/CpdefEnums.pyx +107 -0
  291. Cython/Utility/CppConvert.pyx +282 -0
  292. Cython/Utility/CppSupport.cpp +151 -0
  293. Cython/Utility/CythonFunction.c +2072 -0
  294. Cython/Utility/Dataclasses.c +101 -0
  295. Cython/Utility/Embed.c +129 -0
  296. Cython/Utility/Exceptions.c +1038 -0
  297. Cython/Utility/ExtensionTypes.c +1158 -0
  298. Cython/Utility/FunctionArguments.c +1045 -0
  299. Cython/Utility/FusedFunction.pyx +44 -0
  300. Cython/Utility/ImportExport.c +930 -0
  301. Cython/Utility/MatchCase.c +979 -0
  302. Cython/Utility/MatchCase_Cy.pyx +12 -0
  303. Cython/Utility/MemoryView.pxd +108 -0
  304. Cython/Utility/MemoryView.pyx +1499 -0
  305. Cython/Utility/MemoryView_C.c +1056 -0
  306. Cython/Utility/ModuleSetupCode.c +3352 -0
  307. Cython/Utility/NumpyImportArray.c +46 -0
  308. Cython/Utility/ObjectHandling.c +3372 -0
  309. Cython/Utility/Optimize.c +2563 -0
  310. Cython/Utility/Overflow.c +378 -0
  311. Cython/Utility/Profile.c +736 -0
  312. Cython/Utility/StringTools.c +1414 -0
  313. Cython/Utility/Synchronization.c +438 -0
  314. Cython/Utility/TString.c +369 -0
  315. Cython/Utility/TestCyUtilityLoader.pyx +8 -0
  316. Cython/Utility/TestCythonScope.pyx +75 -0
  317. Cython/Utility/TestUtilityLoader.c +12 -0
  318. Cython/Utility/TypeConversion.c +1556 -0
  319. Cython/Utility/UFuncs.pyx +50 -0
  320. Cython/Utility/UFuncs_C.c +89 -0
  321. Cython/Utility/__init__.py +28 -0
  322. Cython/Utility/arrayarray.h +172 -0
  323. Cython/Utils.cp315-win_amd64.pyd +0 -0
  324. Cython/Utils.py +677 -0
  325. Cython/__init__.py +12 -0
  326. Cython/py.typed +0 -0
  327. cython-3.3.0a1.dist-info/METADATA +394 -0
  328. cython-3.3.0a1.dist-info/RECORD +335 -0
  329. cython-3.3.0a1.dist-info/WHEEL +5 -0
  330. cython-3.3.0a1.dist-info/entry_points.txt +4 -0
  331. cython-3.3.0a1.dist-info/top_level.txt +3 -0
  332. cython.py +29 -0
  333. pyximport/__init__.py +4 -0
  334. pyximport/pyxbuild.py +160 -0
  335. pyximport/pyximport.py +482 -0
@@ -0,0 +1,559 @@
1
+ """
2
+ =====================
3
+ Cython related magics
4
+ =====================
5
+
6
+ Magic command interface for interactive work with Cython
7
+
8
+ .. note::
9
+
10
+ The ``Cython`` package needs to be installed separately. It
11
+ can be obtained using ``easy_install`` or ``pip``.
12
+
13
+ Usage
14
+ =====
15
+
16
+ To enable the magics below, execute ``%load_ext cython``.
17
+
18
+ ``%%cython``
19
+
20
+ {CYTHON_DOC}
21
+
22
+ ``%%cython_inline``
23
+
24
+ {CYTHON_INLINE_DOC}
25
+
26
+ ``%%cython_pyximport``
27
+
28
+ {CYTHON_PYXIMPORT_DOC}
29
+
30
+ Author:
31
+ * Brian Granger
32
+
33
+ Code moved from IPython and adapted by:
34
+ * Martín Gaitán
35
+
36
+ Parts of this code were taken from Cython.inline.
37
+ """
38
+ #-----------------------------------------------------------------------------
39
+ # Copyright (C) 2010-2011, IPython Development Team.
40
+ #
41
+ # Distributed under the terms of the Modified BSD License.
42
+ #
43
+ # The full license is in the file ipython-COPYING.rst, distributed with this software.
44
+ #-----------------------------------------------------------------------------
45
+
46
+
47
+ import os
48
+ import re
49
+ import sys
50
+ import time
51
+ import copy
52
+ import distutils.log
53
+ import textwrap
54
+
55
+ IO_ENCODING = sys.getfilesystemencoding()
56
+
57
+ import hashlib
58
+ from distutils.core import Distribution, Extension
59
+ from distutils.command.build_ext import build_ext
60
+
61
+ from IPython.core import display
62
+ from IPython.core import magic_arguments
63
+ from IPython.core.magic import Magics, magics_class, cell_magic
64
+ try:
65
+ from IPython.paths import get_ipython_cache_dir
66
+ except ImportError:
67
+ # older IPython version
68
+ from IPython.utils.path import get_ipython_cache_dir
69
+ from IPython.utils.text import dedent
70
+
71
+ from ..Shadow import __version__ as cython_version
72
+ from ..Compiler.Errors import CompileError
73
+ from .Inline import cython_inline, load_dynamic
74
+ from .Dependencies import cythonize
75
+ from ..Utils import captured_fd, print_captured
76
+
77
+
78
+ PGO_CONFIG = {
79
+ 'gcc': {
80
+ 'gen': ['-fprofile-generate', '-fprofile-dir={TEMPDIR}'],
81
+ 'use': ['-fprofile-use', '-fprofile-correction', '-fprofile-dir={TEMPDIR}'],
82
+ },
83
+ # blind copy from 'configure' script in CPython 3.7
84
+ 'icc': {
85
+ 'gen': ['-prof-gen'],
86
+ 'use': ['-prof-use'],
87
+ }
88
+ }
89
+ PGO_CONFIG['mingw32'] = PGO_CONFIG['gcc']
90
+
91
+
92
+ @magics_class
93
+ class CythonMagics(Magics):
94
+
95
+ def __init__(self, shell):
96
+ super().__init__(shell)
97
+ self._reloads = {}
98
+ self._code_cache = {}
99
+ self._pyximport_installed = False
100
+
101
+ def _import_all(self, module):
102
+ mdict = module.__dict__
103
+ if '__all__' in mdict:
104
+ keys = mdict['__all__']
105
+ else:
106
+ keys = [k for k in mdict if not k.startswith('_')]
107
+
108
+ for k in keys:
109
+ try:
110
+ self.shell.push({k: mdict[k]})
111
+ except KeyError:
112
+ msg = "'module' object has no attribute '%s'" % k
113
+ raise AttributeError(msg)
114
+
115
+ @cell_magic
116
+ def cython_inline(self, line, cell):
117
+ """Compile and run a Cython code cell using Cython.inline.
118
+
119
+ This magic simply passes the body of the cell to Cython.inline
120
+ and returns the result. If the variables `a` and `b` are defined
121
+ in the user's namespace, here is a simple example that returns
122
+ their sum::
123
+
124
+ %%cython_inline
125
+ return a+b
126
+
127
+ For most purposes, we recommend the usage of the `%%cython` magic.
128
+ """
129
+ locs = self.shell.user_global_ns
130
+ globs = self.shell.user_ns
131
+ return cython_inline(cell, locals=locs, globals=globs)
132
+
133
+ @cell_magic
134
+ def cython_pyximport(self, line, cell):
135
+ """Compile and import a Cython code cell using pyximport.
136
+
137
+ The contents of the cell are written to a `.pyx` file in the current
138
+ working directory, which is then imported using `pyximport`. This
139
+ magic requires a module name to be passed::
140
+
141
+ %%cython_pyximport modulename
142
+ def f(x):
143
+ return 2.0*x
144
+
145
+ The compiled module is then imported and all of its symbols are
146
+ injected into the user's namespace. For most purposes, we recommend
147
+ the usage of the `%%cython` magic.
148
+ """
149
+ module_name = line.strip()
150
+ if not module_name:
151
+ raise ValueError('module name must be given')
152
+ fname = module_name + '.pyx'
153
+ with open(fname, 'w', encoding='utf-8') as f:
154
+ f.write(cell)
155
+ if 'pyximport' not in sys.modules or not self._pyximport_installed:
156
+ import pyximport
157
+ pyximport.install()
158
+ self._pyximport_installed = True
159
+ if module_name in self._reloads:
160
+ module = self._reloads[module_name]
161
+ # Note: reloading extension modules is not actually supported
162
+ # (requires PEP-489 reinitialisation support).
163
+ # Don't know why this should ever have worked as it reads here.
164
+ # All we really need to do is to update the globals below.
165
+ #reload(module)
166
+ else:
167
+ __import__(module_name)
168
+ module = sys.modules[module_name]
169
+ self._reloads[module_name] = module
170
+ self._import_all(module)
171
+
172
+ @magic_arguments.magic_arguments()
173
+ @magic_arguments.argument(
174
+ '-a', '--annotate', action='store_const', const='default', dest='annotate',
175
+ help="Produce a colorized HTML version of the source."
176
+ )
177
+ @magic_arguments.argument(
178
+ '--annotate-fullc', action='store_const', const='fullc', dest='annotate',
179
+ help="Produce a colorized HTML version of the source "
180
+ "which includes entire generated C/C++-code."
181
+ )
182
+ @magic_arguments.argument(
183
+ '-+', '--cplus', action='store_true', default=False,
184
+ help="Output a C++ rather than C file."
185
+ )
186
+ @magic_arguments.argument(
187
+ '-3', dest='language_level', action='store_const', const=3, default=None,
188
+ help="Select Python 3 syntax."
189
+ )
190
+ @magic_arguments.argument(
191
+ '-2', dest='language_level', action='store_const', const=2, default=None,
192
+ help="Select Python 2 syntax."
193
+ )
194
+ @magic_arguments.argument(
195
+ '-f', '--force', action='store_true', default=False,
196
+ help="Force the compilation of a new module, even if the source has been "
197
+ "previously compiled."
198
+ )
199
+ @magic_arguments.argument(
200
+ '-c', '--compile-args', action='append', default=[],
201
+ help="Extra flags to pass to compiler via the `extra_compile_args` "
202
+ "Extension flag (can be specified multiple times)."
203
+ )
204
+ @magic_arguments.argument(
205
+ '--link-args', action='append', default=[],
206
+ help="Extra flags to pass to linker via the `extra_link_args` "
207
+ "Extension flag (can be specified multiple times)."
208
+ )
209
+ @magic_arguments.argument(
210
+ '-l', '--lib', action='append', default=[],
211
+ help="Add a library to link the extension against (can be specified "
212
+ "multiple times)."
213
+ )
214
+ @magic_arguments.argument(
215
+ '-n', '--name',
216
+ help="Specify a name for the Cython module."
217
+ )
218
+ @magic_arguments.argument(
219
+ '-L', dest='library_dirs', metavar='dir', action='append', default=[],
220
+ help="Add a path to the list of library directories (can be specified "
221
+ "multiple times)."
222
+ )
223
+ @magic_arguments.argument(
224
+ '-I', '--include', action='append', default=[],
225
+ help="Add a path to the list of include directories (can be specified "
226
+ "multiple times)."
227
+ )
228
+ @magic_arguments.argument(
229
+ '-S', '--src', action='append', default=[],
230
+ help="Add a path to the list of src files (can be specified "
231
+ "multiple times)."
232
+ )
233
+ @magic_arguments.argument(
234
+ '--pgo', dest='pgo', action='store_true', default=False,
235
+ help=("Enable profile guided optimisation in the C compiler. "
236
+ "Compiles the cell twice and executes it in between to generate a runtime profile.")
237
+ )
238
+ @magic_arguments.argument(
239
+ '--verbose', dest='quiet', action='store_false', default=True,
240
+ help=("Print debug information like generated .c/.cpp file location "
241
+ "and exact gcc/g++ command invoked.")
242
+ )
243
+ @cell_magic
244
+ def cython(self, line, cell):
245
+ """Compile and import everything from a Cython code cell.
246
+
247
+ The contents of the cell are written to a `.pyx` file in the
248
+ directory returned by `get_ipython_cache_dir()/cython` using a filename
249
+ with the hash of the code. This file is then cythonized and compiled.
250
+ The resulting module is imported and all of its symbols are injected
251
+ into the user's namespace. The usage is similar to that of
252
+ `%%cython_pyximport` but you don't have to pass a module name::
253
+
254
+ %%cython
255
+ def f(x):
256
+ return 2.0*x
257
+
258
+ To compile OpenMP codes, pass the required `--compile-args`
259
+ and `--link-args`. For example with gcc::
260
+
261
+ %%cython --compile-args=-fopenmp --link-args=-fopenmp
262
+ ...
263
+
264
+ To enable profile guided optimisation, pass the ``--pgo`` option.
265
+ Note that the cell itself needs to take care of establishing a suitable
266
+ profile when executed. This can be done by implementing the functions to
267
+ optimise, and then calling them directly in the same cell on some realistic
268
+ training data like this::
269
+
270
+ %%cython --pgo
271
+ def critical_function(data):
272
+ for item in data:
273
+ ...
274
+
275
+ # execute function several times to build profile
276
+ from somewhere import some_typical_data
277
+ for _ in range(100):
278
+ critical_function(some_typical_data)
279
+
280
+ In Python 3.5 and later, you can distinguish between the profile and
281
+ non-profile runs as follows::
282
+
283
+ if "_pgo_" in __name__:
284
+ ... # execute critical code here
285
+ """
286
+ args = magic_arguments.parse_argstring(self.cython, line)
287
+ code = cell if cell.endswith('\n') else cell + '\n'
288
+ lib_dir = os.path.join(get_ipython_cache_dir(), 'cython')
289
+ key = (code, line, sys.version_info, sys.executable, cython_version)
290
+
291
+ if not os.path.exists(lib_dir):
292
+ os.makedirs(lib_dir)
293
+
294
+ if args.pgo:
295
+ key += ('pgo',)
296
+ if args.force:
297
+ # Force a new module name by adding the current time to the
298
+ # key which is hashed to determine the module name.
299
+ key += (time.time(),)
300
+
301
+ if args.name:
302
+ module_name = str(args.name) # no-op in Py3
303
+ else:
304
+ module_name = "_cython_magic_" + hashlib.sha256(str(key).encode('utf-8')).hexdigest()
305
+ html_file = os.path.join(lib_dir, module_name + '.html')
306
+ module_path = os.path.join(lib_dir, module_name + self.so_ext)
307
+
308
+ have_module = os.path.isfile(module_path)
309
+ need_cythonize = args.pgo or not have_module
310
+
311
+ if args.annotate:
312
+ if not os.path.isfile(html_file):
313
+ need_cythonize = True
314
+
315
+ extension = None
316
+ if need_cythonize:
317
+ extensions = self._cythonize(module_name, code, lib_dir, args, quiet=args.quiet)
318
+ if extensions is None:
319
+ # Compilation failed and printed error message
320
+ return None
321
+ assert len(extensions) == 1
322
+ extension = extensions[0]
323
+ self._code_cache[key] = module_name
324
+
325
+ if args.pgo:
326
+ self._profile_pgo_wrapper(extension, lib_dir)
327
+
328
+ def print_compiler_output(stdout, stderr, where):
329
+ # On windows, errors are printed to stdout, we redirect both to sys.stderr.
330
+ print_captured(stdout, where, "Content of stdout:\n")
331
+ print_captured(stderr, where, "Content of stderr:\n")
332
+
333
+ get_stderr = get_stdout = None
334
+ try:
335
+ with captured_fd(1) as get_stdout:
336
+ with captured_fd(2) as get_stderr:
337
+ self._build_extension(
338
+ extension, lib_dir, pgo_step_name='use' if args.pgo else None, quiet=args.quiet)
339
+ except (distutils.errors.CompileError, distutils.errors.LinkError):
340
+ # Build failed, print error message from compiler/linker
341
+ print_compiler_output(get_stdout(), get_stderr(), sys.stderr)
342
+ return None
343
+
344
+ # Build seems ok, but we might still want to show any warnings that occurred
345
+ print_compiler_output(get_stdout(), get_stderr(), sys.stdout)
346
+
347
+ module = load_dynamic(module_name, module_path)
348
+ self._import_all(module)
349
+
350
+ if args.annotate:
351
+ try:
352
+ with open(html_file, encoding='utf-8') as f:
353
+ annotated_html = f.read()
354
+ except OSError as e:
355
+ # File could not be opened. Most likely the user has a version
356
+ # of Cython before 0.15.1 (when `cythonize` learned the
357
+ # `force` keyword argument) and has already compiled this
358
+ # exact source without annotation.
359
+ print('Cython completed successfully but the annotated '
360
+ 'source could not be read.', file=sys.stderr)
361
+ print(e, file=sys.stderr)
362
+ else:
363
+ return display.HTML(self.clean_annotated_html(annotated_html))
364
+
365
+ def _profile_pgo_wrapper(self, extension, lib_dir):
366
+ """
367
+ Generate a .c file for a separate extension module that calls the
368
+ module init function of the original module. This makes sure that the
369
+ PGO profiler sees the correct .o file of the final module, but it still
370
+ allows us to import the module under a different name for profiling,
371
+ before recompiling it into the PGO optimised module. Overwriting and
372
+ reimporting the same shared library is not portable.
373
+ """
374
+ extension = copy.copy(extension) # shallow copy, do not modify sources in place!
375
+ module_name = extension.name
376
+ pgo_module_name = '_pgo_' + module_name
377
+ pgo_wrapper_c_file = os.path.join(lib_dir, pgo_module_name + '.c')
378
+ with open(pgo_wrapper_c_file, 'w', encoding='utf-8') as f:
379
+ f.write(textwrap.dedent("""
380
+ #include "Python.h"
381
+ extern PyMODINIT_FUNC PyInit_%(module_name)s(void);
382
+ PyMODINIT_FUNC PyInit_%(pgo_module_name)s(void); /*proto*/
383
+ PyMODINIT_FUNC PyInit_%(pgo_module_name)s(void) {
384
+ return PyInit_%(module_name)s();
385
+ }
386
+ """ % {'module_name': module_name, 'pgo_module_name': pgo_module_name}))
387
+
388
+ extension.sources = extension.sources + [pgo_wrapper_c_file] # do not modify in place!
389
+ extension.name = pgo_module_name
390
+
391
+ self._build_extension(extension, lib_dir, pgo_step_name='gen')
392
+
393
+ # import and execute module code to generate profile
394
+ so_module_path = os.path.join(lib_dir, pgo_module_name + self.so_ext)
395
+ load_dynamic(pgo_module_name, so_module_path)
396
+
397
+ def _cythonize(self, module_name, code, lib_dir, args, quiet=True):
398
+ pyx_file = os.path.join(lib_dir, module_name + '.pyx')
399
+
400
+ c_include_dirs = args.include
401
+ c_src_files = list(map(str, args.src))
402
+ if 'numpy' in code:
403
+ import numpy
404
+ c_include_dirs.append(numpy.get_include())
405
+ with open(pyx_file, 'w', encoding='utf-8') as f:
406
+ f.write(code)
407
+ extension = Extension(
408
+ name=module_name,
409
+ sources=[pyx_file] + c_src_files,
410
+ include_dirs=c_include_dirs,
411
+ library_dirs=args.library_dirs,
412
+ extra_compile_args=args.compile_args,
413
+ extra_link_args=args.link_args,
414
+ libraries=args.lib,
415
+ language='c++' if args.cplus else 'c',
416
+ )
417
+ try:
418
+ opts = dict(
419
+ quiet=quiet,
420
+ annotate=args.annotate,
421
+ force=True,
422
+ language_level=min(3, sys.version_info[0]),
423
+ )
424
+ if args.language_level is not None:
425
+ assert args.language_level in (2, 3)
426
+ opts['language_level'] = args.language_level
427
+ return cythonize([extension], **opts)
428
+ except CompileError:
429
+ return None
430
+
431
+ def _build_extension(self, extension, lib_dir, temp_dir=None, pgo_step_name=None, quiet=True):
432
+ build_extension = self._get_build_extension(
433
+ extension, lib_dir=lib_dir, temp_dir=temp_dir, pgo_step_name=pgo_step_name)
434
+ old_threshold = None
435
+ try:
436
+ if not quiet:
437
+ old_threshold = distutils.log.set_threshold(distutils.log.DEBUG)
438
+ build_extension.run()
439
+ finally:
440
+ if not quiet and old_threshold is not None:
441
+ distutils.log.set_threshold(old_threshold)
442
+
443
+ def _add_pgo_flags(self, build_extension, step_name, temp_dir):
444
+ compiler_type = build_extension.compiler.compiler_type
445
+ if compiler_type == 'unix':
446
+ compiler_cmd = build_extension.compiler.compiler_so
447
+ # TODO: we could try to call "[cmd] --version" for better insights
448
+ if not compiler_cmd:
449
+ pass
450
+ elif 'clang' in compiler_cmd or 'clang' in compiler_cmd[0]:
451
+ compiler_type = 'clang'
452
+ elif 'icc' in compiler_cmd or 'icc' in compiler_cmd[0]:
453
+ compiler_type = 'icc'
454
+ elif 'gcc' in compiler_cmd or 'gcc' in compiler_cmd[0]:
455
+ compiler_type = 'gcc'
456
+ elif 'g++' in compiler_cmd or 'g++' in compiler_cmd[0]:
457
+ compiler_type = 'gcc'
458
+ config = PGO_CONFIG.get(compiler_type)
459
+ orig_flags = []
460
+ if config and step_name in config:
461
+ flags = [f.format(TEMPDIR=temp_dir) for f in config[step_name]]
462
+ for extension in build_extension.extensions:
463
+ orig_flags.append((extension.extra_compile_args, extension.extra_link_args))
464
+ extension.extra_compile_args = extension.extra_compile_args + flags
465
+ extension.extra_link_args = extension.extra_link_args + flags
466
+ else:
467
+ print("No PGO %s configuration known for C compiler type '%s'" % (step_name, compiler_type),
468
+ file=sys.stderr)
469
+ return orig_flags
470
+
471
+ @property
472
+ def so_ext(self):
473
+ """The extension suffix for compiled modules."""
474
+ try:
475
+ return self._so_ext
476
+ except AttributeError:
477
+ self._so_ext = self._get_build_extension().get_ext_filename('')
478
+ return self._so_ext
479
+
480
+ def _clear_distutils_mkpath_cache(self):
481
+ """clear distutils mkpath cache
482
+
483
+ prevents distutils from skipping re-creation of dirs that have been removed
484
+ """
485
+ try:
486
+ from distutils.dir_util import _path_created
487
+ except ImportError:
488
+ pass
489
+ else:
490
+ _path_created.clear()
491
+
492
+ def _get_build_extension(self, extension=None, lib_dir=None, temp_dir=None,
493
+ pgo_step_name=None, _build_ext=build_ext):
494
+ self._clear_distutils_mkpath_cache()
495
+ dist = Distribution()
496
+ config_files = dist.find_config_files()
497
+ try:
498
+ config_files.remove('setup.cfg')
499
+ except ValueError:
500
+ pass
501
+ dist.parse_config_files(config_files)
502
+
503
+ if not temp_dir:
504
+ temp_dir = lib_dir
505
+ add_pgo_flags = self._add_pgo_flags
506
+
507
+ if pgo_step_name:
508
+ base_build_ext = _build_ext
509
+ class _build_ext(_build_ext):
510
+ def build_extensions(self):
511
+ add_pgo_flags(self, pgo_step_name, temp_dir)
512
+ base_build_ext.build_extensions(self)
513
+
514
+ build_extension = _build_ext(dist)
515
+ build_extension.finalize_options()
516
+ if temp_dir:
517
+ build_extension.build_temp = temp_dir
518
+ if lib_dir:
519
+ build_extension.build_lib = lib_dir
520
+ if extension is not None:
521
+ build_extension.extensions = [extension]
522
+ return build_extension
523
+
524
+ @staticmethod
525
+ def clean_annotated_html(html, include_style=True):
526
+ """Clean up the annotated HTML source.
527
+
528
+ Strips the link to the generated C or C++ file, which we do not
529
+ present to the user.
530
+
531
+ Returns an HTML snippet (no <html>, <head>, or <body>),
532
+ containing only the style tag(s) and _contents_ of the body,
533
+ appropriate for embedding multiple times in cell output.
534
+ """
535
+ # extract CSS and body, rather than full HTML document
536
+ chunks = []
537
+ if include_style:
538
+ styles = re.findall("<style.*</style>", html, re.MULTILINE | re.DOTALL)
539
+ chunks.extend(styles)
540
+ # extract body
541
+ body = re.search(
542
+ r"<body[^>]*>(.+)</body>", html, re.MULTILINE | re.DOTALL
543
+ ).group(1)
544
+
545
+ # exclude link to generated file
546
+ r = re.compile('<p>Raw output: <a href="(.*)">(.*)</a>')
547
+ for line in body.splitlines():
548
+ if not r.match(line):
549
+ chunks.append(line)
550
+ return "\n".join(chunks)
551
+
552
+ __doc__ = __doc__.format(
553
+ # rST doesn't see the -+ flag as part of an option list, so we
554
+ # hide it from the module-level docstring.
555
+ CYTHON_DOC=dedent(CythonMagics.cython.__doc__
556
+ .replace('-+, --cplus', '--cplus ')),
557
+ CYTHON_INLINE_DOC=dedent(CythonMagics.cython_inline.__doc__),
558
+ CYTHON_PYXIMPORT_DOC=dedent(CythonMagics.cython_pyximport.__doc__),
559
+ )
@@ -0,0 +1,84 @@
1
+ import os
2
+
3
+ from Cython.Compiler import (
4
+ MemoryView, Code, Options, Pipeline, Errors, Main, Symtab
5
+ )
6
+ from Cython.Compiler.StringEncoding import EncodedString
7
+ from Cython.Compiler.Scanning import SharedUtilitySourceDescriptor
8
+
9
+
10
+ def create_shared_library_pipeline(context, scope, options, result):
11
+
12
+ parse = Pipeline.parse_stage_factory(context)
13
+
14
+ def generate_tree_factory(context):
15
+ def generate_tree(compsrc):
16
+ tree = parse(compsrc)
17
+
18
+ tree.scope.use_utility_code(
19
+ MemoryView.get_view_utility_code(options.shared_utility_qualified_name))
20
+
21
+ tree.scope.use_utility_code(MemoryView._get_memviewslice_declare_code())
22
+ tree.scope.use_utility_code(MemoryView._get_typeinfo_to_format_code())
23
+ context.include_directories.append(Code.get_utility_dir())
24
+ return tree
25
+
26
+ return generate_tree
27
+
28
+ def generate_c_utilities(module_node):
29
+ UtilityCode = Code.UtilityCode
30
+ match_special = UtilityCode.get_special_comment_matcher('/')
31
+ for c_utility_file in os.listdir(Code.get_utility_dir()):
32
+ if not c_utility_file.endswith('.c'):
33
+ continue
34
+ for line in Code.read_utilities_hook(c_utility_file):
35
+ if not ((m := match_special(line)) and (name := m.group('name'))):
36
+ continue
37
+ if not (section_title := UtilityCode.match_section_title(name)):
38
+ continue
39
+ name, section_type = section_title.groups()
40
+ if section_type == 'export':
41
+ module_node.scope.use_utility_code(UtilityCode.load_cached(name, c_utility_file))
42
+ return module_node
43
+
44
+ orig_cimport_from_pyx = Options.cimport_from_pyx
45
+
46
+ def set_cimport_from_pyx(cimport_from_pyx):
47
+ def inner(node):
48
+ Options.cimport_from_pyx = cimport_from_pyx
49
+ return node
50
+ return inner
51
+
52
+ return [
53
+ # "cimport_from_pyx=True" to force generating __Pyx_ExportFunction
54
+ set_cimport_from_pyx(True),
55
+ generate_tree_factory(context),
56
+ *Pipeline.create_pipeline(context, 'pyx', exclude_classes=()),
57
+ generate_c_utilities,
58
+ Pipeline.inject_pxd_code_stage_factory(context),
59
+ Pipeline.inject_utility_code_stage_factory(context, internalise_c_class_entries=False),
60
+ Pipeline.inject_utility_pxd_code_stage_factory(context),
61
+ Pipeline.abort_on_errors,
62
+ Pipeline.generate_pyx_code_stage_factory(options, result),
63
+ set_cimport_from_pyx(orig_cimport_from_pyx),
64
+ ]
65
+
66
+
67
+ def generate_shared_module(options):
68
+ Errors.reset()
69
+
70
+ dest_c_file = options.shared_c_file_path
71
+ pyx_file = os.path.splitext(dest_c_file)[0] + '.pyx'
72
+ module_name = os.path.splitext(os.path.basename(dest_c_file))[0]
73
+
74
+ context = Main.Context.from_options(options)
75
+ scope = Symtab.ModuleScope('MemoryView', parent_module = None, context = context, is_package=False)
76
+
77
+ source_desc = SharedUtilitySourceDescriptor(pyx_file)
78
+ comp_src = Main.CompilationSource(source_desc, EncodedString(module_name), os.getcwd())
79
+ result = Main.create_default_resultobj(comp_src, options)
80
+
81
+ pipeline = create_shared_library_pipeline(context, scope, options, result)
82
+ err, enddata = Pipeline.run_pipeline(pipeline, comp_src)
83
+
84
+ return err, enddata