Cython 3.1.0__py3-none-any.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 (316) hide show
  1. Cython/Build/BuildExecutable.py +169 -0
  2. Cython/Build/Cache.py +199 -0
  3. Cython/Build/Cythonize.py +323 -0
  4. Cython/Build/Dependencies.py +1306 -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 +76 -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 +8 -0
  18. Cython/CodeWriter.py +811 -0
  19. Cython/Compiler/AnalysedTreeTransforms.py +97 -0
  20. Cython/Compiler/Annotate.py +326 -0
  21. Cython/Compiler/AutoDocTransforms.py +320 -0
  22. Cython/Compiler/Buffer.py +680 -0
  23. Cython/Compiler/Builtin.py +934 -0
  24. Cython/Compiler/CmdLine.py +259 -0
  25. Cython/Compiler/Code.pxd +148 -0
  26. Cython/Compiler/Code.py +3375 -0
  27. Cython/Compiler/CodeGeneration.py +33 -0
  28. Cython/Compiler/CythonScope.py +187 -0
  29. Cython/Compiler/Dataclass.py +868 -0
  30. Cython/Compiler/DebugFlags.py +24 -0
  31. Cython/Compiler/Errors.py +295 -0
  32. Cython/Compiler/ExprNodes.py +15267 -0
  33. Cython/Compiler/FlowControl.pxd +97 -0
  34. Cython/Compiler/FlowControl.py +1455 -0
  35. Cython/Compiler/FusedNode.py +1002 -0
  36. Cython/Compiler/Future.py +16 -0
  37. Cython/Compiler/Interpreter.py +57 -0
  38. Cython/Compiler/Lexicon.py +340 -0
  39. Cython/Compiler/LineTable.py +114 -0
  40. Cython/Compiler/Main.py +853 -0
  41. Cython/Compiler/MatchCaseNodes.py +259 -0
  42. Cython/Compiler/MemoryView.py +922 -0
  43. Cython/Compiler/ModuleNode.py +4024 -0
  44. Cython/Compiler/Naming.py +374 -0
  45. Cython/Compiler/Nodes.py +10826 -0
  46. Cython/Compiler/Optimize.py +5256 -0
  47. Cython/Compiler/Options.py +835 -0
  48. Cython/Compiler/ParseTreeTransforms.pxd +77 -0
  49. Cython/Compiler/ParseTreeTransforms.py +4509 -0
  50. Cython/Compiler/Parsing.pxd +9 -0
  51. Cython/Compiler/Parsing.py +4789 -0
  52. Cython/Compiler/Pipeline.py +439 -0
  53. Cython/Compiler/PyrexTypes.py +5762 -0
  54. Cython/Compiler/Pythran.py +232 -0
  55. Cython/Compiler/Scanning.pxd +40 -0
  56. Cython/Compiler/Scanning.py +577 -0
  57. Cython/Compiler/StringEncoding.py +347 -0
  58. Cython/Compiler/Symtab.py +3080 -0
  59. Cython/Compiler/Tests/TestBuffer.py +105 -0
  60. Cython/Compiler/Tests/TestBuiltin.py +72 -0
  61. Cython/Compiler/Tests/TestCmdLine.py +586 -0
  62. Cython/Compiler/Tests/TestCode.py +86 -0
  63. Cython/Compiler/Tests/TestFlowControl.py +65 -0
  64. Cython/Compiler/Tests/TestGrammar.py +202 -0
  65. Cython/Compiler/Tests/TestMemView.py +71 -0
  66. Cython/Compiler/Tests/TestParseTreeTransforms.py +285 -0
  67. Cython/Compiler/Tests/TestScanning.py +134 -0
  68. Cython/Compiler/Tests/TestSignatureMatching.py +73 -0
  69. Cython/Compiler/Tests/TestStringEncoding.py +33 -0
  70. Cython/Compiler/Tests/TestTreeFragment.py +63 -0
  71. Cython/Compiler/Tests/TestTreePath.py +103 -0
  72. Cython/Compiler/Tests/TestTypes.py +75 -0
  73. Cython/Compiler/Tests/TestUtilityLoad.py +112 -0
  74. Cython/Compiler/Tests/TestVisitor.py +61 -0
  75. Cython/Compiler/Tests/Utils.py +36 -0
  76. Cython/Compiler/Tests/__init__.py +1 -0
  77. Cython/Compiler/TreeFragment.py +278 -0
  78. Cython/Compiler/TreePath.py +303 -0
  79. Cython/Compiler/TypeInference.py +584 -0
  80. Cython/Compiler/TypeSlots.py +1181 -0
  81. Cython/Compiler/UFuncs.py +311 -0
  82. Cython/Compiler/UtilNodes.py +389 -0
  83. Cython/Compiler/UtilityCode.py +344 -0
  84. Cython/Compiler/Version.py +8 -0
  85. Cython/Compiler/Visitor.pxd +53 -0
  86. Cython/Compiler/Visitor.py +861 -0
  87. Cython/Compiler/__init__.py +1 -0
  88. Cython/Coverage.py +448 -0
  89. Cython/Debugger/Cygdb.py +175 -0
  90. Cython/Debugger/DebugWriter.py +82 -0
  91. Cython/Debugger/Tests/TestLibCython.py +275 -0
  92. Cython/Debugger/Tests/__init__.py +1 -0
  93. Cython/Debugger/Tests/cfuncs.c +8 -0
  94. Cython/Debugger/Tests/codefile +49 -0
  95. Cython/Debugger/Tests/test_libcython_in_gdb.py +578 -0
  96. Cython/Debugger/Tests/test_libpython_in_gdb.py +90 -0
  97. Cython/Debugger/__init__.py +1 -0
  98. Cython/Debugger/libcython.py +1548 -0
  99. Cython/Debugger/libpython.py +2821 -0
  100. Cython/Debugging.py +20 -0
  101. Cython/Distutils/__init__.py +2 -0
  102. Cython/Distutils/build_ext.py +139 -0
  103. Cython/Distutils/extension.py +96 -0
  104. Cython/Distutils/old_build_ext.py +351 -0
  105. Cython/Includes/cpython/__init__.pxd +173 -0
  106. Cython/Includes/cpython/array.pxd +174 -0
  107. Cython/Includes/cpython/bool.pxd +37 -0
  108. Cython/Includes/cpython/buffer.pxd +112 -0
  109. Cython/Includes/cpython/bytearray.pxd +33 -0
  110. Cython/Includes/cpython/bytes.pxd +200 -0
  111. Cython/Includes/cpython/cellobject.pxd +35 -0
  112. Cython/Includes/cpython/ceval.pxd +8 -0
  113. Cython/Includes/cpython/codecs.pxd +121 -0
  114. Cython/Includes/cpython/complex.pxd +60 -0
  115. Cython/Includes/cpython/contextvars.pxd +145 -0
  116. Cython/Includes/cpython/conversion.pxd +36 -0
  117. Cython/Includes/cpython/datetime.pxd +395 -0
  118. Cython/Includes/cpython/descr.pxd +26 -0
  119. Cython/Includes/cpython/dict.pxd +187 -0
  120. Cython/Includes/cpython/exc.pxd +263 -0
  121. Cython/Includes/cpython/fileobject.pxd +57 -0
  122. Cython/Includes/cpython/float.pxd +47 -0
  123. Cython/Includes/cpython/function.pxd +65 -0
  124. Cython/Includes/cpython/genobject.pxd +25 -0
  125. Cython/Includes/cpython/getargs.pxd +12 -0
  126. Cython/Includes/cpython/instance.pxd +25 -0
  127. Cython/Includes/cpython/iterator.pxd +36 -0
  128. Cython/Includes/cpython/iterobject.pxd +24 -0
  129. Cython/Includes/cpython/list.pxd +92 -0
  130. Cython/Includes/cpython/long.pxd +149 -0
  131. Cython/Includes/cpython/longintrepr.pxd +14 -0
  132. Cython/Includes/cpython/mapping.pxd +63 -0
  133. Cython/Includes/cpython/marshal.pxd +66 -0
  134. Cython/Includes/cpython/mem.pxd +120 -0
  135. Cython/Includes/cpython/memoryview.pxd +50 -0
  136. Cython/Includes/cpython/method.pxd +49 -0
  137. Cython/Includes/cpython/module.pxd +208 -0
  138. Cython/Includes/cpython/number.pxd +258 -0
  139. Cython/Includes/cpython/object.pxd +433 -0
  140. Cython/Includes/cpython/pycapsule.pxd +143 -0
  141. Cython/Includes/cpython/pylifecycle.pxd +68 -0
  142. Cython/Includes/cpython/pyport.pxd +8 -0
  143. Cython/Includes/cpython/pystate.pxd +95 -0
  144. Cython/Includes/cpython/pythread.pxd +53 -0
  145. Cython/Includes/cpython/ref.pxd +67 -0
  146. Cython/Includes/cpython/sequence.pxd +134 -0
  147. Cython/Includes/cpython/set.pxd +119 -0
  148. Cython/Includes/cpython/slice.pxd +70 -0
  149. Cython/Includes/cpython/time.pxd +129 -0
  150. Cython/Includes/cpython/tuple.pxd +72 -0
  151. Cython/Includes/cpython/type.pxd +53 -0
  152. Cython/Includes/cpython/unicode.pxd +639 -0
  153. Cython/Includes/cpython/version.pxd +32 -0
  154. Cython/Includes/cpython/weakref.pxd +78 -0
  155. Cython/Includes/libc/__init__.pxd +1 -0
  156. Cython/Includes/libc/complex.pxd +35 -0
  157. Cython/Includes/libc/errno.pxd +127 -0
  158. Cython/Includes/libc/float.pxd +43 -0
  159. Cython/Includes/libc/limits.pxd +28 -0
  160. Cython/Includes/libc/locale.pxd +46 -0
  161. Cython/Includes/libc/math.pxd +209 -0
  162. Cython/Includes/libc/setjmp.pxd +10 -0
  163. Cython/Includes/libc/signal.pxd +64 -0
  164. Cython/Includes/libc/stddef.pxd +9 -0
  165. Cython/Includes/libc/stdint.pxd +105 -0
  166. Cython/Includes/libc/stdio.pxd +80 -0
  167. Cython/Includes/libc/stdlib.pxd +72 -0
  168. Cython/Includes/libc/string.pxd +50 -0
  169. Cython/Includes/libc/threads.pxd +84 -0
  170. Cython/Includes/libc/time.pxd +51 -0
  171. Cython/Includes/libcpp/__init__.pxd +4 -0
  172. Cython/Includes/libcpp/algorithm.pxd +320 -0
  173. Cython/Includes/libcpp/any.pxd +16 -0
  174. Cython/Includes/libcpp/atomic.pxd +59 -0
  175. Cython/Includes/libcpp/barrier.pxd +22 -0
  176. Cython/Includes/libcpp/bit.pxd +29 -0
  177. Cython/Includes/libcpp/cast.pxd +12 -0
  178. Cython/Includes/libcpp/cmath.pxd +518 -0
  179. Cython/Includes/libcpp/complex.pxd +106 -0
  180. Cython/Includes/libcpp/deque.pxd +165 -0
  181. Cython/Includes/libcpp/exception.pxd +86 -0
  182. Cython/Includes/libcpp/execution.pxd +15 -0
  183. Cython/Includes/libcpp/forward_list.pxd +63 -0
  184. Cython/Includes/libcpp/functional.pxd +26 -0
  185. Cython/Includes/libcpp/future.pxd +103 -0
  186. Cython/Includes/libcpp/iterator.pxd +34 -0
  187. Cython/Includes/libcpp/latch.pxd +17 -0
  188. Cython/Includes/libcpp/limits.pxd +61 -0
  189. Cython/Includes/libcpp/list.pxd +117 -0
  190. Cython/Includes/libcpp/map.pxd +252 -0
  191. Cython/Includes/libcpp/memory.pxd +115 -0
  192. Cython/Includes/libcpp/mutex.pxd +130 -0
  193. Cython/Includes/libcpp/numbers.pxd +15 -0
  194. Cython/Includes/libcpp/numeric.pxd +131 -0
  195. Cython/Includes/libcpp/optional.pxd +34 -0
  196. Cython/Includes/libcpp/pair.pxd +1 -0
  197. Cython/Includes/libcpp/queue.pxd +25 -0
  198. Cython/Includes/libcpp/random.pxd +166 -0
  199. Cython/Includes/libcpp/semaphore.pxd +44 -0
  200. Cython/Includes/libcpp/set.pxd +228 -0
  201. Cython/Includes/libcpp/shared_mutex.pxd +72 -0
  202. Cython/Includes/libcpp/span.pxd +87 -0
  203. Cython/Includes/libcpp/stack.pxd +11 -0
  204. Cython/Includes/libcpp/stop_token.pxd +105 -0
  205. Cython/Includes/libcpp/string.pxd +355 -0
  206. Cython/Includes/libcpp/string_view.pxd +181 -0
  207. Cython/Includes/libcpp/typeindex.pxd +15 -0
  208. Cython/Includes/libcpp/typeinfo.pxd +10 -0
  209. Cython/Includes/libcpp/unordered_map.pxd +193 -0
  210. Cython/Includes/libcpp/unordered_set.pxd +152 -0
  211. Cython/Includes/libcpp/utility.pxd +30 -0
  212. Cython/Includes/libcpp/vector.pxd +186 -0
  213. Cython/Includes/openmp.pxd +50 -0
  214. Cython/Includes/posix/__init__.pxd +1 -0
  215. Cython/Includes/posix/dlfcn.pxd +14 -0
  216. Cython/Includes/posix/fcntl.pxd +86 -0
  217. Cython/Includes/posix/ioctl.pxd +4 -0
  218. Cython/Includes/posix/mman.pxd +101 -0
  219. Cython/Includes/posix/resource.pxd +57 -0
  220. Cython/Includes/posix/select.pxd +21 -0
  221. Cython/Includes/posix/signal.pxd +73 -0
  222. Cython/Includes/posix/stat.pxd +98 -0
  223. Cython/Includes/posix/stdio.pxd +37 -0
  224. Cython/Includes/posix/stdlib.pxd +29 -0
  225. Cython/Includes/posix/strings.pxd +9 -0
  226. Cython/Includes/posix/time.pxd +71 -0
  227. Cython/Includes/posix/types.pxd +30 -0
  228. Cython/Includes/posix/uio.pxd +26 -0
  229. Cython/Includes/posix/unistd.pxd +271 -0
  230. Cython/Includes/posix/wait.pxd +38 -0
  231. Cython/Plex/Actions.pxd +24 -0
  232. Cython/Plex/Actions.py +119 -0
  233. Cython/Plex/DFA.pxd +14 -0
  234. Cython/Plex/DFA.py +164 -0
  235. Cython/Plex/Errors.py +48 -0
  236. Cython/Plex/Lexicons.py +178 -0
  237. Cython/Plex/Machines.pxd +36 -0
  238. Cython/Plex/Machines.py +238 -0
  239. Cython/Plex/Regexps.py +539 -0
  240. Cython/Plex/Scanners.pxd +47 -0
  241. Cython/Plex/Scanners.py +360 -0
  242. Cython/Plex/Transitions.pxd +14 -0
  243. Cython/Plex/Transitions.py +239 -0
  244. Cython/Plex/__init__.py +34 -0
  245. Cython/Runtime/__init__.py +1 -0
  246. Cython/Runtime/refnanny.pyx +237 -0
  247. Cython/Shadow.py +690 -0
  248. Cython/Shadow.pyi +521 -0
  249. Cython/StringIOTree.py +170 -0
  250. Cython/Tempita/__init__.py +4 -0
  251. Cython/Tempita/_looper.py +154 -0
  252. Cython/Tempita/_tempita.py +1091 -0
  253. Cython/TestUtils.py +410 -0
  254. Cython/Tests/TestCodeWriter.py +128 -0
  255. Cython/Tests/TestCythonUtils.py +202 -0
  256. Cython/Tests/TestJediTyper.py +223 -0
  257. Cython/Tests/TestShadow.py +114 -0
  258. Cython/Tests/TestStringIOTree.py +67 -0
  259. Cython/Tests/TestTestUtils.py +90 -0
  260. Cython/Tests/__init__.py +1 -0
  261. Cython/Tests/xmlrunner.py +390 -0
  262. Cython/Utility/AsyncGen.c +1002 -0
  263. Cython/Utility/Buffer.c +875 -0
  264. Cython/Utility/BufferFormatFromTypeInfo.pxd +2 -0
  265. Cython/Utility/Builtins.c +776 -0
  266. Cython/Utility/CConvert.pyx +134 -0
  267. Cython/Utility/CMath.c +104 -0
  268. Cython/Utility/CommonStructures.c +118 -0
  269. Cython/Utility/Complex.c +378 -0
  270. Cython/Utility/Coroutine.c +2206 -0
  271. Cython/Utility/CpdefEnums.pyx +103 -0
  272. Cython/Utility/CppConvert.pyx +279 -0
  273. Cython/Utility/CppSupport.cpp +143 -0
  274. Cython/Utility/CythonFunction.c +1794 -0
  275. Cython/Utility/Dataclasses.c +185 -0
  276. Cython/Utility/Dataclasses.py +112 -0
  277. Cython/Utility/Embed.c +125 -0
  278. Cython/Utility/Exceptions.c +1012 -0
  279. Cython/Utility/ExtensionTypes.c +809 -0
  280. Cython/Utility/FunctionArguments.c +965 -0
  281. Cython/Utility/ImportExport.c +987 -0
  282. Cython/Utility/Lock.c +136 -0
  283. Cython/Utility/MemoryView.pxd +187 -0
  284. Cython/Utility/MemoryView.pyx +1481 -0
  285. Cython/Utility/MemoryView_C.c +1046 -0
  286. Cython/Utility/ModuleSetupCode.c +3059 -0
  287. Cython/Utility/NumpyImportArray.c +46 -0
  288. Cython/Utility/ObjectHandling.c +3342 -0
  289. Cython/Utility/Optimize.c +1589 -0
  290. Cython/Utility/Overflow.c +404 -0
  291. Cython/Utility/Printing.c +86 -0
  292. Cython/Utility/Profile.c +709 -0
  293. Cython/Utility/StringTools.c +1259 -0
  294. Cython/Utility/TestCyUtilityLoader.pyx +8 -0
  295. Cython/Utility/TestCythonScope.pyx +75 -0
  296. Cython/Utility/TestUtilityLoader.c +12 -0
  297. Cython/Utility/TypeConversion.c +1284 -0
  298. Cython/Utility/UFuncs.pyx +50 -0
  299. Cython/Utility/UFuncs_C.c +89 -0
  300. Cython/Utility/__init__.py +28 -0
  301. Cython/Utility/arrayarray.h +148 -0
  302. Cython/Utils.py +687 -0
  303. Cython/__init__.py +10 -0
  304. Cython/__init__.pyi +7 -0
  305. Cython/py.typed +0 -0
  306. cython-3.1.0.dist-info/COPYING.txt +19 -0
  307. cython-3.1.0.dist-info/LICENSE.txt +176 -0
  308. cython-3.1.0.dist-info/METADATA +636 -0
  309. cython-3.1.0.dist-info/RECORD +316 -0
  310. cython-3.1.0.dist-info/WHEEL +5 -0
  311. cython-3.1.0.dist-info/entry_points.txt +4 -0
  312. cython-3.1.0.dist-info/top_level.txt +3 -0
  313. cython.py +29 -0
  314. pyximport/__init__.py +4 -0
  315. pyximport/pyxbuild.py +160 -0
  316. pyximport/pyximport.py +482 -0
@@ -0,0 +1,3375 @@
1
+ #
2
+ # Code output module
3
+ #
4
+
5
+
6
+ import cython
7
+ cython.declare(os=object, re=object, operator=object, textwrap=object,
8
+ Template=object, Naming=object, Options=object, StringEncoding=object,
9
+ Utils=object, SourceDescriptor=object, StringIOTree=object,
10
+ DebugFlags=object, defaultdict=object,
11
+ closing=object, partial=object, wraps=object)
12
+
13
+ import hashlib
14
+ import operator
15
+ import os
16
+ import re
17
+ import shutil
18
+ import textwrap
19
+ from string import Template
20
+ from functools import partial, wraps
21
+ from contextlib import closing, contextmanager
22
+ from collections import defaultdict
23
+
24
+ from . import Naming
25
+ from . import Options
26
+ from . import DebugFlags
27
+ from . import StringEncoding
28
+ from .. import Utils
29
+ from .Scanning import SourceDescriptor
30
+ from ..StringIOTree import StringIOTree
31
+
32
+
33
+ renamed_py2_builtins_map = {
34
+ # builtins that had different names in Py2 code
35
+ 'unicode' : 'str',
36
+ 'basestring' : 'str',
37
+ 'xrange' : 'range',
38
+ 'raw_input' : 'input',
39
+ }
40
+
41
+ ctypedef_builtins_map = {
42
+ # types of builtins in "ctypedef class" statements which we don't
43
+ # import either because the names conflict with C types or because
44
+ # the type simply is not exposed.
45
+ 'py_int' : '&PyLong_Type',
46
+ 'py_long' : '&PyLong_Type',
47
+ 'py_float' : '&PyFloat_Type',
48
+ 'wrapper_descriptor' : '&PyWrapperDescr_Type',
49
+ }
50
+
51
+ basicsize_builtins_map = {
52
+ # builtins whose type has a different tp_basicsize than sizeof(...)
53
+ 'PyTypeObject': 'PyHeapTypeObject',
54
+ }
55
+
56
+ # Builtins as of Python version ...
57
+ KNOWN_PYTHON_BUILTINS_VERSION = (3, 13, 0, 'alpha', 5)
58
+ KNOWN_PYTHON_BUILTINS = frozenset([
59
+ 'ArithmeticError',
60
+ 'AssertionError',
61
+ 'AttributeError',
62
+ 'BaseException',
63
+ 'BaseExceptionGroup',
64
+ 'BlockingIOError',
65
+ 'BrokenPipeError',
66
+ 'BufferError',
67
+ 'BytesWarning',
68
+ 'ChildProcessError',
69
+ 'ConnectionAbortedError',
70
+ 'ConnectionError',
71
+ 'ConnectionRefusedError',
72
+ 'ConnectionResetError',
73
+ 'DeprecationWarning',
74
+ 'EOFError',
75
+ 'Ellipsis',
76
+ 'EncodingWarning',
77
+ 'EnvironmentError',
78
+ 'Exception',
79
+ 'ExceptionGroup',
80
+ 'False',
81
+ 'FileExistsError',
82
+ 'FileNotFoundError',
83
+ 'FloatingPointError',
84
+ 'FutureWarning',
85
+ 'GeneratorExit',
86
+ 'IOError',
87
+ 'ImportError',
88
+ 'ImportWarning',
89
+ '_IncompleteInputError',
90
+ 'IndentationError',
91
+ 'IndexError',
92
+ 'InterruptedError',
93
+ 'IsADirectoryError',
94
+ 'KeyError',
95
+ 'KeyboardInterrupt',
96
+ 'LookupError',
97
+ 'MemoryError',
98
+ 'ModuleNotFoundError',
99
+ 'NameError',
100
+ 'None',
101
+ 'NotADirectoryError',
102
+ 'NotImplemented',
103
+ 'NotImplementedError',
104
+ 'OSError',
105
+ 'OverflowError',
106
+ 'PendingDeprecationWarning',
107
+ 'PermissionError',
108
+ 'ProcessLookupError',
109
+ 'PythonFinalizationError',
110
+ 'RecursionError',
111
+ 'ReferenceError',
112
+ 'ResourceWarning',
113
+ 'RuntimeError',
114
+ 'RuntimeWarning',
115
+ 'StopAsyncIteration',
116
+ 'StopIteration',
117
+ 'SyntaxError',
118
+ 'SyntaxWarning',
119
+ 'SystemError',
120
+ 'SystemExit',
121
+ 'TabError',
122
+ 'TimeoutError',
123
+ 'True',
124
+ 'TypeError',
125
+ 'UnboundLocalError',
126
+ 'UnicodeDecodeError',
127
+ 'UnicodeEncodeError',
128
+ 'UnicodeError',
129
+ 'UnicodeTranslateError',
130
+ 'UnicodeWarning',
131
+ 'UserWarning',
132
+ 'ValueError',
133
+ 'Warning',
134
+ 'WindowsError',
135
+ 'ZeroDivisionError',
136
+ '__build_class__',
137
+ '__debug__',
138
+ '__import__',
139
+ 'abs',
140
+ 'aiter',
141
+ 'all',
142
+ 'anext',
143
+ 'any',
144
+ 'ascii',
145
+ 'bin',
146
+ 'bool',
147
+ 'breakpoint',
148
+ 'bytearray',
149
+ 'bytes',
150
+ 'callable',
151
+ 'chr',
152
+ 'classmethod',
153
+ 'compile',
154
+ 'complex',
155
+ 'copyright',
156
+ 'credits',
157
+ 'delattr',
158
+ 'dict',
159
+ 'dir',
160
+ 'divmod',
161
+ 'enumerate',
162
+ 'eval',
163
+ 'exec',
164
+ 'exit',
165
+ 'filter',
166
+ 'float',
167
+ 'format',
168
+ 'frozenset',
169
+ 'getattr',
170
+ 'globals',
171
+ 'hasattr',
172
+ 'hash',
173
+ 'help',
174
+ 'hex',
175
+ 'id',
176
+ 'input',
177
+ 'int',
178
+ 'isinstance',
179
+ 'issubclass',
180
+ 'iter',
181
+ 'len',
182
+ 'license',
183
+ 'list',
184
+ 'locals',
185
+ 'map',
186
+ 'max',
187
+ 'memoryview',
188
+ 'min',
189
+ 'next',
190
+ 'object',
191
+ 'oct',
192
+ 'open',
193
+ 'ord',
194
+ 'pow',
195
+ 'print',
196
+ 'property',
197
+ 'quit',
198
+ 'range',
199
+ 'repr',
200
+ 'reversed',
201
+ 'round',
202
+ 'set',
203
+ 'setattr',
204
+ 'slice',
205
+ 'sorted',
206
+ 'staticmethod',
207
+ 'str',
208
+ 'sum',
209
+ 'super',
210
+ 'tuple',
211
+ 'type',
212
+ 'vars',
213
+ 'zip',
214
+ ])
215
+
216
+ uncachable_builtins = [
217
+ # Global/builtin names that cannot be cached because they may or may not
218
+ # be available at import time, for various reasons:
219
+ ## Python 3.13+
220
+ '_IncompleteInputError',
221
+ 'PythonFinalizationError',
222
+ ## Python 3.11+
223
+ 'BaseExceptionGroup',
224
+ 'ExceptionGroup',
225
+ ## - Py3.10+
226
+ 'aiter',
227
+ 'anext',
228
+ 'EncodingWarning',
229
+ ## - Py3.7+
230
+ 'breakpoint', # might deserve an implementation in Cython
231
+ ## - platform specific
232
+ 'WindowsError',
233
+ ## - others
234
+ '_', # e.g. used by gettext
235
+ ]
236
+
237
+ special_py_methods = cython.declare(frozenset, frozenset((
238
+ '__cinit__', '__dealloc__', '__richcmp__', '__next__',
239
+ '__await__', '__aiter__', '__anext__',
240
+ '__getbuffer__', '__releasebuffer__',
241
+ )))
242
+
243
+ modifier_output_mapper = {
244
+ 'inline': 'CYTHON_INLINE'
245
+ }.get
246
+
247
+ cleanup_level_for_type_prefix = cython.declare(object, {
248
+ 'ustring': None,
249
+ 'tuple': 2,
250
+ 'slice': 2,
251
+ }.get)
252
+
253
+
254
+ class IncludeCode:
255
+ """
256
+ An include file and/or verbatim C code to be included in the
257
+ generated sources.
258
+ """
259
+ # attributes:
260
+ #
261
+ # pieces {order: unicode}: pieces of C code to be generated.
262
+ # For the included file, the key "order" is zero.
263
+ # For verbatim include code, the "order" is the "order"
264
+ # attribute of the original IncludeCode where this piece
265
+ # of C code was first added. This is needed to prevent
266
+ # duplication if the same include code is found through
267
+ # multiple cimports.
268
+ # location int: where to put this include in the C sources, one
269
+ # of the constants INITIAL, EARLY, LATE
270
+ # order int: sorting order (automatically set by increasing counter)
271
+
272
+ # Constants for location. If the same include occurs with different
273
+ # locations, the earliest one takes precedence.
274
+ INITIAL = 0
275
+ EARLY = 1
276
+ LATE = 2
277
+
278
+ counter = 1 # Counter for "order"
279
+
280
+ def __init__(self, include=None, verbatim=None, late=True, initial=False):
281
+ self.order = self.counter
282
+ type(self).counter += 1
283
+ self.pieces = {}
284
+
285
+ if include:
286
+ if include[0] == '<' and include[-1] == '>':
287
+ self.pieces[0] = '#include {}'.format(include)
288
+ late = False # system include is never late
289
+ else:
290
+ self.pieces[0] = '#include "{}"'.format(include)
291
+
292
+ if verbatim:
293
+ self.pieces[self.order] = verbatim
294
+
295
+ if initial:
296
+ self.location = self.INITIAL
297
+ elif late:
298
+ self.location = self.LATE
299
+ else:
300
+ self.location = self.EARLY
301
+
302
+ def dict_update(self, d, key):
303
+ """
304
+ Insert `self` in dict `d` with key `key`. If that key already
305
+ exists, update the attributes of the existing value with `self`.
306
+ """
307
+ if key in d:
308
+ other = d[key]
309
+ other.location = min(self.location, other.location)
310
+ other.pieces.update(self.pieces)
311
+ else:
312
+ d[key] = self
313
+
314
+ def sortkey(self):
315
+ return self.order
316
+
317
+ def mainpiece(self):
318
+ """
319
+ Return the main piece of C code, corresponding to the include
320
+ file. If there was no include file, return None.
321
+ """
322
+ return self.pieces.get(0)
323
+
324
+ def write(self, code):
325
+ # Write values of self.pieces dict, sorted by the keys
326
+ for k in sorted(self.pieces):
327
+ code.putln(self.pieces[k])
328
+
329
+
330
+ def get_utility_dir():
331
+ # make this a function and not global variables:
332
+ # http://trac.cython.org/cython_trac/ticket/475
333
+ Cython_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
334
+ return os.path.join(Cython_dir, "Utility")
335
+
336
+ read_utilities_hook = None
337
+ """
338
+ Override the hook for reading a utilities file that contains code fragments used
339
+ by the codegen.
340
+
341
+ The hook functions takes the path of the utilities file, and returns a list
342
+ of strings, one per line.
343
+
344
+ The default behavior is to open a file relative to get_utility_dir().
345
+ """
346
+
347
+ def read_utilities_from_utility_dir(path):
348
+ """
349
+ Read all lines of the file at the provided path from a path relative
350
+ to get_utility_dir().
351
+ """
352
+ filename = os.path.join(get_utility_dir(), path)
353
+ with closing(Utils.open_source_file(filename, encoding='UTF-8')) as f:
354
+ return f.readlines()
355
+
356
+ # by default, read utilities from the utility directory.
357
+ read_utilities_hook = read_utilities_from_utility_dir
358
+
359
+
360
+ class AbstractUtilityCode:
361
+
362
+ requires = None
363
+
364
+ def put_code(self, output):
365
+ pass
366
+
367
+ def get_tree(self, **kwargs):
368
+ return None
369
+
370
+ def get_shared_library_scope(self, **kwargs):
371
+ return None
372
+
373
+
374
+ class UtilityCodeBase(AbstractUtilityCode):
375
+ """
376
+ Support for loading utility code from a file.
377
+
378
+ Code sections in the file can be specified as follows:
379
+
380
+ ##### MyUtility.proto #####
381
+
382
+ [proto declarations]
383
+
384
+ ##### MyUtility.init #####
385
+
386
+ [code run at module initialization]
387
+
388
+ ##### MyUtility #####
389
+ #@requires: MyOtherUtility
390
+ #@substitute: naming
391
+
392
+ [definitions]
393
+
394
+ ##### MyUtility #####
395
+ #@substitute: tempita
396
+
397
+ [requires tempita substitution
398
+ - context can't be specified here though so only
399
+ tempita utility that requires no external context
400
+ will benefit from this tag
401
+ - only necessary when @required from non-tempita code]
402
+
403
+ for prototypes and implementation respectively. For non-python or
404
+ -cython files backslashes should be used instead. 5 to 30 comment
405
+ characters may be used on either side.
406
+
407
+ If the @cname decorator is not used and this is a CythonUtilityCode,
408
+ one should pass in the 'name' keyword argument to be used for name
409
+ mangling of such entries.
410
+ """
411
+
412
+ is_cython_utility = False
413
+ _utility_cache = {}
414
+
415
+ @classmethod
416
+ def _add_utility(cls, utility, name, type, lines, begin_lineno, tags=None):
417
+ if utility is None:
418
+ return
419
+
420
+ code = '\n'.join(lines)
421
+ if tags and 'substitute' in tags and 'naming' in tags['substitute']:
422
+ try:
423
+ new_code = Template(code).substitute(vars(Naming))
424
+ except (KeyError, ValueError) as e:
425
+ raise RuntimeError(
426
+ f"Error parsing templated utility code '{name}.{type}' at line {begin_lineno:d}: {e}")
427
+ if new_code == code:
428
+ raise RuntimeError(
429
+ f"Found useless 'substitute: naming' declaration without replacements. ({name}.{type}:{begin_lineno:d})")
430
+ code = new_code
431
+
432
+ # remember correct line numbers at least until after templating
433
+ code = '\n' * begin_lineno + code
434
+
435
+ if type == 'proto':
436
+ utility[0] = code
437
+ elif type == 'impl':
438
+ utility[1] = code
439
+ else:
440
+ all_tags = utility[2]
441
+ all_tags[type] = code
442
+
443
+ if tags:
444
+ all_tags = utility[2]
445
+ for name, values in tags.items():
446
+ all_tags.setdefault(name, set()).update(values)
447
+
448
+ @classmethod
449
+ def load_utilities_from_file(cls, path):
450
+ utilities = cls._utility_cache.get(path)
451
+ if utilities:
452
+ return utilities
453
+
454
+ _, ext = os.path.splitext(path)
455
+ if ext in ('.pyx', '.py', '.pxd', '.pxi'):
456
+ comment = '#'
457
+ strip_comments = partial(re.compile(r'^\s*#(?!\s*cython\s*:).*').sub, '')
458
+ rstrip = str.rstrip
459
+ else:
460
+ comment = '/'
461
+ strip_comments = partial(re.compile(r'^\s*//.*|/\*[^*]*\*/').sub, '')
462
+ rstrip = partial(re.compile(r'\s+(\\?)$').sub, r'\1')
463
+ match_special = re.compile(
464
+ (r'^%(C)s{5,30}\s*(?P<name>(?:\w|\.)+)\s*%(C)s{5,30}|'
465
+ r'^%(C)s+@(?P<tag>\w+)\s*:\s*(?P<value>(?:\w|[.:])+)') %
466
+ {'C': comment}).match
467
+ match_type = re.compile(r'(.+)[.](proto(?:[.]\S+)?|impl|init|cleanup|module_state_decls)$').match
468
+
469
+ all_lines = read_utilities_hook(path)
470
+
471
+ utilities = defaultdict(lambda: [None, None, {}])
472
+ lines = []
473
+ tags = defaultdict(set)
474
+ utility = name = type = None
475
+ begin_lineno = 0
476
+
477
+ for lineno, line in enumerate(all_lines):
478
+ m = match_special(line)
479
+ if m:
480
+ if m.group('name'):
481
+ cls._add_utility(utility, name, type, lines, begin_lineno, tags)
482
+
483
+ begin_lineno = lineno + 1
484
+ del lines[:]
485
+ tags.clear()
486
+
487
+ name = m.group('name')
488
+ mtype = match_type(name)
489
+ if mtype:
490
+ name, type = mtype.groups()
491
+ else:
492
+ type = 'impl'
493
+ utility = utilities[name]
494
+ else:
495
+ tags[m.group('tag')].add(m.group('value'))
496
+ lines.append('') # keep line number correct
497
+ else:
498
+ lines.append(rstrip(strip_comments(line)))
499
+
500
+ if utility is None:
501
+ raise ValueError("Empty utility code file")
502
+
503
+ # Don't forget to add the last utility code
504
+ cls._add_utility(utility, name, type, lines, begin_lineno, tags)
505
+
506
+ utilities = dict(utilities) # un-defaultdict-ify
507
+ cls._utility_cache[path] = utilities
508
+ return utilities
509
+
510
+ @classmethod
511
+ def load(cls, util_code_name, from_file, **kwargs):
512
+ """
513
+ Load utility code from a file specified by from_file (relative to
514
+ Cython/Utility) and name util_code_name.
515
+ """
516
+
517
+ if '::' in util_code_name:
518
+ from_file, util_code_name = util_code_name.rsplit('::', 1)
519
+ assert from_file
520
+ utilities = cls.load_utilities_from_file(from_file)
521
+ proto, impl, tags = utilities[util_code_name]
522
+
523
+ if tags:
524
+ if "substitute" in tags and "tempita" in tags["substitute"]:
525
+ if not issubclass(cls, TempitaUtilityCode):
526
+ return TempitaUtilityCode.load(util_code_name, from_file, **kwargs)
527
+ orig_kwargs = kwargs.copy()
528
+ for name, values in tags.items():
529
+ if name in kwargs:
530
+ continue
531
+ # only pass lists when we have to: most argument expect one value or None
532
+ if name == 'requires':
533
+ if orig_kwargs:
534
+ values = [cls.load(dep, from_file, **orig_kwargs)
535
+ for dep in sorted(values)]
536
+ else:
537
+ # dependencies are rarely unique, so use load_cached() when we can
538
+ values = [cls.load_cached(dep, from_file)
539
+ for dep in sorted(values)]
540
+ elif name == 'substitute':
541
+ # don't want to pass "naming" or "tempita" to the constructor
542
+ # since these will have been handled
543
+ values = values - {'naming', 'tempita'}
544
+ if not values:
545
+ continue
546
+ elif not values:
547
+ values = None
548
+ elif len(values) == 1:
549
+ values = list(values)[0]
550
+ kwargs[name] = values
551
+
552
+ if proto is not None:
553
+ kwargs['proto'] = proto
554
+ if impl is not None:
555
+ kwargs['impl'] = impl
556
+
557
+ if 'name' not in kwargs:
558
+ kwargs['name'] = util_code_name
559
+
560
+ if 'file' not in kwargs and from_file:
561
+ kwargs['file'] = from_file
562
+ return cls(**kwargs)
563
+
564
+ @classmethod
565
+ def load_cached(cls, utility_code_name, from_file, __cache={}):
566
+ """
567
+ Calls .load(), but using a per-type cache based on utility name and file name.
568
+ """
569
+ key = (utility_code_name, from_file, cls)
570
+ try:
571
+ return __cache[key]
572
+ except KeyError:
573
+ pass
574
+ code = __cache[key] = cls.load(utility_code_name, from_file)
575
+ return code
576
+
577
+ @classmethod
578
+ def load_as_string(cls, util_code_name, from_file, include_requires=False, **kwargs):
579
+ """
580
+ Load a utility code as a string. Returns (proto, implementation).
581
+
582
+ If 'include_requires=True', concatenates all requirements before the actually
583
+ requested utility code, separately for proto and impl part.
584
+
585
+ In a lot of cases it may be better to use regular "load" and "CCodeWriter.put_code_here"
586
+ since that is able to apply the code transformations to the code too.
587
+ """
588
+ util = cls.load(util_code_name, from_file, **kwargs)
589
+
590
+ if not include_requires:
591
+ return (util.format_code(util.proto),
592
+ util.format_code(util.impl))
593
+
594
+ protos, impls = [], []
595
+ def prepend(util_code):
596
+ if util_code.requires:
597
+ for dep in util_code.requires:
598
+ prepend(dep)
599
+ if util_code.proto:
600
+ protos.append(util_code.format_code(util_code.proto))
601
+ if util_code.impl:
602
+ impls.append(util_code.format_code(util_code.impl))
603
+
604
+ prepend(util)
605
+ return "".join(protos), "".join(impls)
606
+
607
+ def format_code(self, code_string, replace_empty_lines=re.compile(r'\n\n+').sub):
608
+ """
609
+ Format a code section for output.
610
+ """
611
+ if code_string:
612
+ code_string = replace_empty_lines('\n', code_string.strip()) + '\n\n'
613
+ return code_string
614
+
615
+ def __repr__(self):
616
+ return "<%s(%s)>" % (type(self).__name__, self.name)
617
+
618
+ def get_tree(self, **kwargs):
619
+ return None
620
+
621
+ def get_shared_library_scope(self, **kwargs):
622
+ return None
623
+
624
+ def __deepcopy__(self, memodict=None):
625
+ # No need to deep-copy utility code since it's essentially immutable.
626
+ return self
627
+
628
+
629
+ class UtilityCode(UtilityCodeBase):
630
+ """
631
+ Stores utility code to add during code generation.
632
+
633
+ See GlobalState.put_utility_code.
634
+
635
+ hashes/equals by instance
636
+
637
+ proto C prototypes
638
+ impl implementation code
639
+ init code to call on module initialization
640
+ requires utility code dependencies
641
+ proto_block the place in the resulting file where the prototype should
642
+ end up
643
+ name name of the utility code (or None)
644
+ file filename of the utility code file this utility was loaded
645
+ from (or None)
646
+ """
647
+ code_parts = ["proto", "impl", "init", "cleanup", "module_state_decls"]
648
+
649
+ def __init__(self, proto=None, impl=None, init=None, cleanup=None,
650
+ module_state_decls=None, requires=None,
651
+ proto_block='utility_code_proto', name=None, file=None):
652
+ # proto_block: Which code block to dump prototype in. See GlobalState.
653
+ self.proto = proto
654
+ self.impl = impl
655
+ self.init = init
656
+ self.cleanup = cleanup
657
+ self.module_state_decls = module_state_decls
658
+ self.requires = requires
659
+ self._cache = {}
660
+ self.specialize_list = []
661
+ self.proto_block = proto_block
662
+ self.name = name
663
+ self.file = file
664
+
665
+ # cached for use in hash and eq
666
+ self._parts_tuple = tuple(getattr(self, part, None) for part in self.code_parts)
667
+
668
+ def __hash__(self):
669
+ return hash(self._parts_tuple)
670
+
671
+ def __eq__(self, other):
672
+ if self is other:
673
+ return True
674
+ self_type, other_type = type(self), type(other)
675
+ if self_type is not other_type and not (isinstance(other, self_type) or isinstance(self, other_type)):
676
+ return False
677
+
678
+ return self._parts_tuple == other._parts_tuple
679
+
680
+ def none_or_sub(self, s, context):
681
+ """
682
+ Format a string in this utility code with context. If None, do nothing.
683
+ """
684
+ if s is None:
685
+ return None
686
+ return s % context
687
+
688
+ def specialize(self, pyrex_type=None, **data):
689
+ name = self.name
690
+ if pyrex_type is not None:
691
+ data['type'] = pyrex_type.empty_declaration_code()
692
+ data['type_name'] = pyrex_type.specialization_name()
693
+ name = "%s[%s]" % (name, data['type_name'])
694
+ # Dicts aren't hashable...
695
+ key = tuple(sorted(data.items()))
696
+ try:
697
+ return self._cache[key]
698
+ except KeyError:
699
+ if self.requires is None:
700
+ requires = None
701
+ else:
702
+ requires = [r.specialize(data) for r in self.requires]
703
+
704
+ s = self._cache[key] = UtilityCode(
705
+ self.none_or_sub(self.proto, data),
706
+ self.none_or_sub(self.impl, data),
707
+ self.none_or_sub(self.init, data),
708
+ self.none_or_sub(self.cleanup, data),
709
+ self.none_or_sub(self.module_state_decls, data),
710
+ requires,
711
+ self.proto_block,
712
+ name,
713
+ )
714
+
715
+ self.specialize_list.append(s)
716
+ return s
717
+
718
+ def _put_code_section(self, writer: "CCodeWriter", output: "GlobalState", code_type: str):
719
+ code_string = getattr(self, code_type)
720
+ if not code_string:
721
+ return
722
+
723
+ can_be_reused = code_type in ('proto', 'impl')
724
+
725
+ code_string, result_is_module_specific = process_utility_ccode(self, output, code_string)
726
+
727
+ code_type_name = code_type if code_type != 'impl' else ''
728
+ writer.putln(f"/* {self.name}{'.' if code_type_name else ''}{code_type_name} */")
729
+
730
+ if can_be_reused and not result_is_module_specific:
731
+ # can be reused across modules
732
+ writer.put_or_include(code_string, f'{self.name}_{code_type}')
733
+ else:
734
+ writer.put(code_string)
735
+
736
+ def _put_init_code_section(self, output):
737
+ if not self.init:
738
+ return
739
+ writer = output['init_globals']
740
+ self._put_code_section(writer, output, 'init')
741
+ # 'init' code can end with an 'if' statement for an error condition like:
742
+ # if (check_ok()) ; else
743
+ writer.putln(writer.error_goto_if_PyErr(output.module_pos))
744
+ writer.putln()
745
+
746
+ def put_code(self, output):
747
+ if self.requires:
748
+ for dependency in self.requires:
749
+ output.use_utility_code(dependency)
750
+
751
+ if self.proto:
752
+ self._put_code_section(output[self.proto_block], output, 'proto')
753
+ if self.impl:
754
+ self._put_code_section(output['utility_code_def'], output, 'impl')
755
+ if self.cleanup and Options.generate_cleanup_code:
756
+ self._put_code_section(output['cleanup_globals'], output, 'cleanup')
757
+ if self.module_state_decls:
758
+ self._put_code_section(output['module_state_contents'], output, 'module_state_decls')
759
+
760
+ if self.init:
761
+ self._put_init_code_section(output)
762
+
763
+
764
+ def add_macro_processor(*macro_names, regex=None, is_module_specific=False, _last_macro_processor = [None]):
765
+ """Decorator to chain the code macro processors below.
766
+ """
767
+ last_processor = _last_macro_processor[0]
768
+
769
+ def build_processor(func):
770
+ @wraps(func)
771
+ def process(utility_code: UtilityCode, output, code_string: str):
772
+ # First, call the processing chain in FIFO function definition order.
773
+ result_is_module_specific = False
774
+ if last_processor is not None:
775
+ code_string, result_is_module_specific = last_processor(utility_code, output, code_string)
776
+
777
+ # Detect if we need to do something.
778
+ if macro_names:
779
+ for macro in macro_names:
780
+ if macro in code_string:
781
+ break
782
+ else:
783
+ return code_string, result_is_module_specific
784
+
785
+ # Process the code.
786
+ if regex is None:
787
+ code_string = func(utility_code, output, code_string)
788
+ else:
789
+ code_string = re.sub(regex, partial(func, output), code_string)
790
+
791
+ # Make sure we found and replaced all macro occurrences.
792
+ for macro in macro_names:
793
+ if macro in code_string:
794
+ raise RuntimeError(f"Left-over utility code macro '{macro}()' found in '{utility_code.name}'")
795
+
796
+ result_is_module_specific |= is_module_specific
797
+ return code_string, result_is_module_specific
798
+
799
+ _last_macro_processor[0] = process
800
+ return process
801
+
802
+ return build_processor
803
+
804
+
805
+ @add_macro_processor(
806
+ 'CSTRING',
807
+ regex=r'CSTRING\(\s*"""([^"]*(?:"[^"]+)*)"""\s*\)',
808
+ )
809
+ def _wrap_c_string(_, matchobj):
810
+ """Replace CSTRING('''xyz''') by a C compatible string, taking care of line breaks.
811
+ """
812
+ content = matchobj.group(1).replace('"', r'\042')
813
+ return ''.join(
814
+ f'"{line}\\n"\n' if not line.endswith('\\') or line.endswith('\\\\') else f'"{line[:-1]}"\n'
815
+ for line in content.splitlines())
816
+
817
+
818
+ @add_macro_processor()
819
+ def _format_impl_code(utility_code: UtilityCode, _, impl):
820
+ return utility_code.format_code(impl)
821
+
822
+
823
+ @add_macro_processor(
824
+ 'CALL_UNBOUND_METHOD',
825
+ is_module_specific=True,
826
+ regex=(
827
+ r'CALL_UNBOUND_METHOD\('
828
+ r'([a-zA-Z_]+),\s*' # type cname
829
+ r'"([^"]+)",\s*' # method name
830
+ r'([^),\s]+)' # object cname
831
+ r'((?:,[^),]+)*)' # args*
832
+ r'\)'
833
+ ),
834
+ )
835
+ def _inject_unbound_method(output, matchobj):
836
+ """Replace 'UNBOUND_METHOD(type, "name")' by a constant Python identifier cname.
837
+ """
838
+ type_cname, method_name, obj_cname, args = matchobj.groups()
839
+ type_cname = '&%s' % type_cname
840
+ args = [arg.strip() for arg in args[1:].split(',')] if args else []
841
+ assert len(args) < 3, f"CALL_UNBOUND_METHOD() does not support {len(args):d} call arguments"
842
+ return output.cached_unbound_method_call_code(
843
+ f"{Naming.modulestateglobal_cname}->",
844
+ obj_cname, type_cname, method_name, args)
845
+
846
+
847
+ @add_macro_processor(
848
+ 'PYIDENT', 'PYUNICODE',
849
+ is_module_specific=True,
850
+ regex=r'PY(IDENT|UNICODE)\("([^"]+)"\)',
851
+ )
852
+ def _inject_string_constant(output, matchobj):
853
+ """Replace 'PYIDENT("xyz")' by a constant Python identifier cname.
854
+ """
855
+ str_type, name = matchobj.groups()
856
+ return "%s->%s" % (
857
+ Naming.modulestateglobal_cname,
858
+ output.get_py_string_const(
859
+ StringEncoding.EncodedString(name), identifier=str_type == 'IDENT').cname)
860
+
861
+
862
+ @add_macro_processor(
863
+ 'EMPTY',
864
+ # As long as we use the same C access macros for these names, they are not module specific.
865
+ # is_module_specific=True,
866
+ regex=r'EMPTY\((bytes|unicode|tuple)\)',
867
+ )
868
+ def _inject_empty_collection_constant(output, matchobj):
869
+ """Replace 'EMPTY(bytes|tuple|...)' by a constant Python identifier cname.
870
+ """
871
+ type_name = matchobj.group(1)
872
+ return "%s->%s" % (
873
+ Naming.modulestateglobal_cname,
874
+ getattr(Naming, f'empty_{type_name}'))
875
+
876
+
877
+ @add_macro_processor(
878
+ 'CGLOBAL', # 'NAMED_CGLOBAL', # first is part of second and thus not needed
879
+ is_module_specific=False,
880
+ regex=r'(NAMED_)?CGLOBAL\(([^)]+)\)',
881
+ )
882
+ def _inject_cglobal(output, matchobj):
883
+ is_named, name = matchobj.groups()
884
+ if is_named:
885
+ name = getattr(Naming, name)
886
+ return f"{Naming.modulestateglobal_cname}->{name}"
887
+
888
+
889
+ @add_macro_processor()
890
+ def process_utility_ccode(utility_code, _, code_string):
891
+ """Entry point for code processors, must be defined last.
892
+ """
893
+ return code_string
894
+
895
+
896
+ def sub_tempita(s, context, file=None, name=None, __cache={}):
897
+ "Run tempita on string s with given context."
898
+ if not s:
899
+ return None
900
+
901
+ if file:
902
+ name = f"{file}:{name}"
903
+ if name:
904
+ context['__name'] = name
905
+
906
+ try:
907
+ template = __cache[s]
908
+ except KeyError:
909
+ from ..Tempita import Template
910
+ template = __cache[s] = Template(s, name=name)
911
+
912
+ return template.substitute(context)
913
+
914
+
915
+ class TempitaUtilityCode(UtilityCode):
916
+ def __init__(self, name=None, proto=None, impl=None, init=None, file=None, context=None, **kwargs):
917
+ if context is None:
918
+ context = {}
919
+ else:
920
+ # prevent changes propagating back if context is shared between multiple utility codes.
921
+ context = context.copy()
922
+ proto = sub_tempita(proto, context, file, name)
923
+ impl = sub_tempita(impl, context, file, name)
924
+ init = sub_tempita(init, context, file, name)
925
+ super().__init__(
926
+ proto, impl, init=init, name=name, file=file, **kwargs)
927
+
928
+ @classmethod
929
+ def load_cached(cls, utility_code_name, from_file=None, context=None, __cache={}):
930
+ context_key = tuple(sorted(context.items())) if context else None
931
+ assert hash(context_key) is not None # raise TypeError if not hashable
932
+ key = (cls, from_file, utility_code_name, context_key)
933
+ try:
934
+ return __cache[key]
935
+ except KeyError:
936
+ pass
937
+ code = __cache[key] = cls.load(utility_code_name, from_file, context=context)
938
+ return code
939
+
940
+ def none_or_sub(self, s, context):
941
+ """
942
+ Format a string in this utility code with context. If None, do nothing.
943
+ """
944
+ if s is None:
945
+ return None
946
+ return sub_tempita(s, context, self.file, self.name)
947
+
948
+
949
+ class LazyUtilityCode(UtilityCodeBase):
950
+ """
951
+ Utility code that calls a callback with the root code writer when
952
+ available. Useful when you only have 'env' but not 'code'.
953
+ """
954
+ __name__ = '<lazy>'
955
+ requires = None
956
+
957
+ def __init__(self, callback):
958
+ self.callback = callback
959
+
960
+ def put_code(self, globalstate):
961
+ utility = self.callback(globalstate.rootwriter)
962
+ globalstate.use_utility_code(utility)
963
+
964
+
965
+ class FunctionState:
966
+ # return_label string function return point label
967
+ # error_label string error catch point label
968
+ # error_without_exception boolean Can go to the error label without an exception (e.g. __next__ can return NULL)
969
+ # continue_label string loop continue point label
970
+ # break_label string loop break point label
971
+ # return_from_error_cleanup_label string
972
+ # label_counter integer counter for naming labels
973
+ # exc_vars (string * 3) exception variables for reraise, or None
974
+ # can_trace boolean line tracing is supported in the current context
975
+ # scope Scope the scope object of the current function
976
+
977
+ # Not used for now, perhaps later
978
+ def __init__(self, owner, names_taken=set(), scope=None):
979
+ self.names_taken = names_taken
980
+ self.owner = owner
981
+ self.scope = scope
982
+
983
+ self.error_label = None
984
+ self.label_counter = 0
985
+ self.labels_used = set()
986
+ self.return_label = self.new_label()
987
+ self.new_error_label()
988
+ self.continue_label = None
989
+ self.break_label = None
990
+ self.yield_labels = []
991
+
992
+ self.exc_vars = None
993
+ self.current_except = None
994
+ self.can_trace = False
995
+ self.gil_owned = True
996
+
997
+ self.temps_allocated = [] # of (name, type, manage_ref, static)
998
+ self.temps_free = {} # (type, manage_ref) -> list of free vars with same type/managed status
999
+ self.temps_used_type = {} # name -> (type, manage_ref)
1000
+ self.zombie_temps = set() # temps that must not be reused after release
1001
+ self.temp_counter = 0
1002
+ self.closure_temps = None
1003
+
1004
+ # This is used to collect temporaries, useful to find out which temps
1005
+ # need to be privatized in parallel sections
1006
+ self.collect_temps_stack = []
1007
+
1008
+ # This is used for the error indicator, which needs to be local to the
1009
+ # function. It used to be global, which relies on the GIL being held.
1010
+ # However, exceptions may need to be propagated through 'nogil'
1011
+ # sections, in which case we introduce a race condition.
1012
+ self.should_declare_error_indicator = False
1013
+ self.uses_error_indicator = False
1014
+
1015
+ self.error_without_exception = False
1016
+
1017
+ self.needs_refnanny = False
1018
+
1019
+ # safety checks
1020
+
1021
+ def validate_exit(self):
1022
+ # validate that all allocated temps have been freed
1023
+ if self.temps_allocated:
1024
+ leftovers = self.temps_in_use()
1025
+ if leftovers:
1026
+ msg = "TEMPGUARD: Temps left over at end of '%s': %s" % (self.scope.name, ', '.join([
1027
+ '%s [%s]' % (name, ctype)
1028
+ for name, ctype, is_pytemp in sorted(leftovers)]),
1029
+ )
1030
+ #print(msg)
1031
+ raise RuntimeError(msg)
1032
+
1033
+ # labels
1034
+
1035
+ def new_label(self, name=None):
1036
+ n: cython.size_t = self.label_counter
1037
+ self.label_counter = n + 1
1038
+ label = "%s%d" % (Naming.label_prefix, n)
1039
+ if name is not None:
1040
+ label += '_' + name
1041
+ return label
1042
+
1043
+ def new_yield_label(self, expr_type='yield'):
1044
+ label = self.new_label('resume_from_%s' % expr_type)
1045
+ num_and_label = (len(self.yield_labels) + 1, label)
1046
+ self.yield_labels.append(num_and_label)
1047
+ return num_and_label
1048
+
1049
+ def new_error_label(self, prefix=""):
1050
+ old_err_lbl = self.error_label
1051
+ self.error_label = self.new_label(prefix + 'error')
1052
+ return old_err_lbl
1053
+
1054
+ def get_loop_labels(self):
1055
+ return (
1056
+ self.continue_label,
1057
+ self.break_label)
1058
+
1059
+ def set_loop_labels(self, labels):
1060
+ (self.continue_label,
1061
+ self.break_label) = labels
1062
+
1063
+ def new_loop_labels(self, prefix=""):
1064
+ old_labels = self.get_loop_labels()
1065
+ self.set_loop_labels(
1066
+ (self.new_label(prefix + "continue"),
1067
+ self.new_label(prefix + "break")))
1068
+ return old_labels
1069
+
1070
+ def get_all_labels(self):
1071
+ return (
1072
+ self.continue_label,
1073
+ self.break_label,
1074
+ self.return_label,
1075
+ self.error_label)
1076
+
1077
+ def set_all_labels(self, labels):
1078
+ (self.continue_label,
1079
+ self.break_label,
1080
+ self.return_label,
1081
+ self.error_label) = labels
1082
+
1083
+ def all_new_labels(self):
1084
+ old_labels = self.get_all_labels()
1085
+ new_labels = []
1086
+ for old_label, name in zip(old_labels, ['continue', 'break', 'return', 'error']):
1087
+ if old_label:
1088
+ new_labels.append(self.new_label(name))
1089
+ else:
1090
+ new_labels.append(old_label)
1091
+ self.set_all_labels(new_labels)
1092
+ return old_labels
1093
+
1094
+ def use_label(self, lbl):
1095
+ self.labels_used.add(lbl)
1096
+
1097
+ def label_used(self, lbl):
1098
+ return lbl in self.labels_used
1099
+
1100
+ # temp handling
1101
+
1102
+ def allocate_temp(self, type, manage_ref, static=False, reusable=True):
1103
+ """
1104
+ Allocates a temporary (which may create a new one or get a previously
1105
+ allocated and released one of the same type). Type is simply registered
1106
+ and handed back, but will usually be a PyrexType.
1107
+
1108
+ If type.needs_refcounting, manage_ref comes into play. If manage_ref is set to
1109
+ True, the temp will be decref-ed on return statements and in exception
1110
+ handling clauses. Otherwise the caller has to deal with any reference
1111
+ counting of the variable.
1112
+
1113
+ If not type.needs_refcounting, then manage_ref will be ignored, but it
1114
+ still has to be passed. It is recommended to pass False by convention
1115
+ if it is known that type will never be a reference counted type.
1116
+
1117
+ static=True marks the temporary declaration with "static".
1118
+ This is only used when allocating backing store for a module-level
1119
+ C array literals.
1120
+
1121
+ if reusable=False, the temp will not be reused after release.
1122
+
1123
+ A C string referring to the variable is returned.
1124
+ """
1125
+ if type.is_cv_qualified and not type.is_reference:
1126
+ type = type.cv_base_type
1127
+ elif type.is_reference and not type.is_fake_reference:
1128
+ type = type.ref_base_type
1129
+ elif type.is_cfunction:
1130
+ from . import PyrexTypes
1131
+ type = PyrexTypes.c_ptr_type(type) # A function itself isn't an l-value
1132
+ elif type.is_cpp_class and not type.is_fake_reference and self.scope.directives['cpp_locals']:
1133
+ self.scope.use_utility_code(UtilityCode.load_cached("OptionalLocals", "CppSupport.cpp"))
1134
+ if not type.needs_refcounting:
1135
+ # Make manage_ref canonical, so that manage_ref will always mean
1136
+ # a decref is needed.
1137
+ manage_ref = False
1138
+
1139
+ freelist = self.temps_free.get((type, manage_ref))
1140
+ if reusable and freelist is not None and freelist[0]:
1141
+ result = freelist[0].pop()
1142
+ freelist[1].remove(result)
1143
+ else:
1144
+ while True:
1145
+ self.temp_counter += 1
1146
+ result = "%s%d" % (Naming.codewriter_temp_prefix, self.temp_counter)
1147
+ if result not in self.names_taken: break
1148
+ self.temps_allocated.append((result, type, manage_ref, static))
1149
+ if not reusable:
1150
+ self.zombie_temps.add(result)
1151
+ self.temps_used_type[result] = (type, manage_ref)
1152
+ if DebugFlags.debug_temp_code_comments:
1153
+ self.owner.putln("/* %s allocated (%s)%s */" % (result, type, "" if reusable else " - zombie"))
1154
+
1155
+ if self.collect_temps_stack:
1156
+ self.collect_temps_stack[-1].add((result, type))
1157
+
1158
+ return result
1159
+
1160
+ def release_temp(self, name):
1161
+ """
1162
+ Releases a temporary so that it can be reused by other code needing
1163
+ a temp of the same type.
1164
+ """
1165
+ type, manage_ref = self.temps_used_type[name]
1166
+ freelist = self.temps_free.get((type, manage_ref))
1167
+ if freelist is None:
1168
+ freelist = ([], set()) # keep order in list and make lookups in set fast
1169
+ self.temps_free[(type, manage_ref)] = freelist
1170
+ if name in freelist[1]:
1171
+ raise RuntimeError("Temp %s freed twice!" % name)
1172
+ if name not in self.zombie_temps:
1173
+ freelist[0].append(name)
1174
+ freelist[1].add(name)
1175
+ if DebugFlags.debug_temp_code_comments:
1176
+ self.owner.putln("/* %s released %s*/" % (
1177
+ name, " - zombie" if name in self.zombie_temps else ""))
1178
+
1179
+ def temps_in_use(self):
1180
+ """Return a list of (cname,type,manage_ref) tuples of temp names and their type
1181
+ that are currently in use.
1182
+ """
1183
+ used = []
1184
+ for name, type, manage_ref, static in self.temps_allocated:
1185
+ freelist = self.temps_free.get((type, manage_ref))
1186
+ if freelist is None or name not in freelist[1]:
1187
+ used.append((name, type, manage_ref and type.needs_refcounting))
1188
+ return used
1189
+
1190
+ def temps_holding_reference(self):
1191
+ """Return a list of (cname,type) tuples of temp names and their type
1192
+ that are currently in use. This includes only temps
1193
+ with a reference counted type which owns its reference.
1194
+ """
1195
+ return [(name, type)
1196
+ for name, type, manage_ref in self.temps_in_use()
1197
+ if manage_ref and type.needs_refcounting]
1198
+
1199
+ def all_managed_temps(self):
1200
+ """Return a list of (cname, type) tuples of refcount-managed Python objects.
1201
+ """
1202
+ return [(cname, type)
1203
+ for cname, type, manage_ref, static in self.temps_allocated
1204
+ if manage_ref]
1205
+
1206
+ def all_free_managed_temps(self):
1207
+ """Return a list of (cname, type) tuples of refcount-managed Python
1208
+ objects that are not currently in use. This is used by
1209
+ try-except and try-finally blocks to clean up temps in the
1210
+ error case.
1211
+ """
1212
+ return sorted([ # Enforce deterministic order.
1213
+ (cname, type)
1214
+ for (type, manage_ref), freelist in self.temps_free.items() if manage_ref
1215
+ for cname in freelist[0]
1216
+ ])
1217
+
1218
+ def start_collecting_temps(self):
1219
+ """
1220
+ Useful to find out which temps were used in a code block
1221
+ """
1222
+ self.collect_temps_stack.append(set())
1223
+
1224
+ def stop_collecting_temps(self):
1225
+ return self.collect_temps_stack.pop()
1226
+
1227
+ def init_closure_temps(self, scope):
1228
+ self.closure_temps = ClosureTempAllocator(scope)
1229
+
1230
+
1231
+ class NumConst:
1232
+ """Global info about a Python number constant held by GlobalState.
1233
+
1234
+ cname string
1235
+ value string
1236
+ py_type string int, long, float
1237
+ value_code string evaluation code if different from value
1238
+ """
1239
+
1240
+ def __init__(self, cname, value, py_type, value_code=None):
1241
+ self.cname = cname
1242
+ self.value = value
1243
+ self.py_type = py_type
1244
+ self.value_code = value_code or value
1245
+
1246
+
1247
+ class PyObjectConst:
1248
+ """Global info about a generic constant held by GlobalState.
1249
+ """
1250
+ # cname string
1251
+ # type PyrexType
1252
+
1253
+ def __init__(self, cname, type):
1254
+ self.cname = cname
1255
+ self.type = type
1256
+
1257
+
1258
+ cython.declare(possible_unicode_identifier=object, possible_bytes_identifier=object,
1259
+ replace_identifier=object, find_alphanums=object)
1260
+ possible_unicode_identifier = re.compile(r"(?![0-9])\w+$", re.U).match
1261
+ possible_bytes_identifier = re.compile(br"(?![0-9])\w+$").match
1262
+ replace_identifier = re.compile(r'[^a-zA-Z0-9_]+').sub
1263
+ find_alphanums = re.compile('([a-zA-Z0-9]+)').findall
1264
+
1265
+ class StringConst:
1266
+ """Global info about a C string constant held by GlobalState.
1267
+ """
1268
+ # cname string
1269
+ # text EncodedString or BytesLiteral
1270
+ # py_strings {(identifier, encoding) : PyStringConst}
1271
+
1272
+ def __init__(self, cname, text, byte_string):
1273
+ self.cname = cname
1274
+ self.text = text
1275
+ self.escaped_value = StringEncoding.escape_byte_string(byte_string)
1276
+ self.py_strings = None
1277
+
1278
+ def get_py_string_const(self, encoding, identifier=None):
1279
+ text = self.text
1280
+ intern: cython.bint
1281
+ is_unicode: cython.bint
1282
+
1283
+ if identifier or encoding is None:
1284
+ # unicode string
1285
+ encoding = encoding_key = None
1286
+ is_unicode = True
1287
+ else:
1288
+ # bytes
1289
+ is_unicode = False
1290
+ encoding = encoding.lower()
1291
+ if encoding in ('utf8', 'utf-8', 'ascii', 'usascii', 'us-ascii'):
1292
+ encoding = None
1293
+ encoding_key = None
1294
+ else:
1295
+ encoding_key = ''.join(find_alphanums(encoding))
1296
+
1297
+ if identifier:
1298
+ intern = True
1299
+ elif identifier is None:
1300
+ if isinstance(text, bytes):
1301
+ intern = bool(possible_bytes_identifier(text))
1302
+ else:
1303
+ intern = bool(possible_unicode_identifier(text))
1304
+ else:
1305
+ intern = False
1306
+
1307
+ key = (intern, is_unicode, encoding_key)
1308
+ if self.py_strings is None:
1309
+ self.py_strings = {}
1310
+ else:
1311
+ try:
1312
+ return self.py_strings[key]
1313
+ except KeyError:
1314
+ pass
1315
+
1316
+ pystring_cname = (
1317
+ f"{Naming.interned_prefixes['str'] if intern else Naming.py_const_prefix}"
1318
+ f"{'u' if is_unicode else 'b'}"
1319
+ f"{'_' + encoding_key if encoding_key else ''}"
1320
+ f"_{self.cname[len(Naming.const_prefix):]}"
1321
+ )
1322
+
1323
+ py_string = PyStringConst(pystring_cname, encoding, intern, is_unicode)
1324
+ self.py_strings[key] = py_string
1325
+ return py_string
1326
+
1327
+
1328
+ class PyStringConst:
1329
+ """Global info about a Python string constant held by GlobalState.
1330
+ """
1331
+ # cname string
1332
+ # encoding string
1333
+ # intern boolean
1334
+ # is_unicode boolean
1335
+
1336
+ def __init__(self, cname, encoding, intern=False, is_unicode=False):
1337
+ self.cname = cname
1338
+ self.encoding = encoding
1339
+ self.is_unicode = is_unicode
1340
+ self.intern = intern
1341
+
1342
+ def __lt__(self, other):
1343
+ return self.cname < other.cname
1344
+
1345
+
1346
+ class GlobalState:
1347
+ # filename_table {string : int} for finding filename table indexes
1348
+ # filename_list [string] filenames in filename table order
1349
+ # input_file_contents dict contents (=list of lines) of any file that was used as input
1350
+ # to create this output C code. This is
1351
+ # used to annotate the comments.
1352
+ #
1353
+ # utility_codes set IDs of used utility code (to avoid reinsertion)
1354
+ #
1355
+ # declared_cnames {string:Entry} used in a transition phase to merge pxd-declared
1356
+ # constants etc. into the pyx-declared ones (i.e,
1357
+ # check if constants are already added).
1358
+ # In time, hopefully the literals etc. will be
1359
+ # supplied directly instead.
1360
+ #
1361
+ # const_cnames_used dict global counter for unique constant identifiers
1362
+ #
1363
+
1364
+ # parts {string:CCodeWriter}
1365
+
1366
+
1367
+ # interned_strings
1368
+ # consts
1369
+ # interned_nums
1370
+
1371
+ # directives set Temporary variable used to track
1372
+ # the current set of directives in the code generation
1373
+ # process.
1374
+
1375
+ directives = {}
1376
+
1377
+ code_layout = [
1378
+ 'h_code',
1379
+ 'filename_table',
1380
+ 'utility_code_proto_before_types',
1381
+ 'numeric_typedefs', # Let these detailed individual parts stay!,
1382
+ 'complex_type_declarations', # as the proper solution is to make a full DAG...
1383
+ 'type_declarations', # More coarse-grained blocks would simply hide
1384
+ 'utility_code_proto', # the ugliness, not fix it
1385
+ 'module_declarations',
1386
+ 'typeinfo',
1387
+ 'before_global_var',
1388
+ 'global_var',
1389
+ 'string_decls',
1390
+ 'decls',
1391
+ 'late_includes',
1392
+ 'module_state',
1393
+ 'module_state_contents', # can be used to inject declarations into the modulestate struct
1394
+ 'module_state_end',
1395
+ 'constant_name_defines',
1396
+ 'module_state_clear',
1397
+ 'module_state_traverse',
1398
+ 'module_code', # user code goes here
1399
+ 'module_exttypes',
1400
+ 'initfunc_declarations',
1401
+ 'init_module',
1402
+ 'pystring_table',
1403
+ 'cached_builtins',
1404
+ 'cached_constants',
1405
+ 'init_constants',
1406
+ 'init_codeobjects',
1407
+ 'init_globals', # (utility code called at init-time)
1408
+ 'cleanup_globals',
1409
+ 'cleanup_module',
1410
+ 'main_method',
1411
+ 'utility_code_pragmas', # silence some irrelevant warnings in utility code
1412
+ 'utility_code_def',
1413
+ 'utility_code_pragmas_end', # clean-up the utility_code_pragmas
1414
+ 'end'
1415
+ ]
1416
+
1417
+ # h files can only have a much smaller list of sections
1418
+ h_code_layout = [
1419
+ 'h_code',
1420
+ 'utility_code_proto_before_types',
1421
+ 'type_declarations',
1422
+ 'utility_code_proto',
1423
+ 'end'
1424
+ ]
1425
+
1426
+ def __init__(self, writer, module_node, code_config, common_utility_include_dir=None):
1427
+ self.filename_table = {}
1428
+ self.filename_list = []
1429
+ self.input_file_contents = {}
1430
+ self.utility_codes = set()
1431
+ self.declared_cnames = {}
1432
+ self.in_utility_code_generation = False
1433
+ self.code_config = code_config
1434
+ self.common_utility_include_dir = common_utility_include_dir
1435
+ self.parts = {}
1436
+ self.module_node = module_node # because some utility code generation needs it
1437
+ # (generating backwards-compatible Get/ReleaseBuffer
1438
+
1439
+ self.const_cnames_used = {}
1440
+ self.string_const_index = {}
1441
+ self.dedup_const_index = {}
1442
+ self.pyunicode_ptr_const_index = {}
1443
+ self.codeobject_constants = []
1444
+ self.num_const_index = {}
1445
+ self.arg_default_constants = []
1446
+ self.const_array_counters = {} # counts of differently prefixed arrays of constants
1447
+ self.cached_cmethods = {}
1448
+ self.initialised_constants = set()
1449
+
1450
+ writer.set_global_state(self)
1451
+ self.rootwriter = writer
1452
+
1453
+ def initialize_main_c_code(self):
1454
+ rootwriter = self.rootwriter
1455
+ for i, part in enumerate(self.code_layout):
1456
+ w = self.parts[part] = rootwriter.insertion_point()
1457
+ if i > 0:
1458
+ w.putln("/* #### Code section: %s ### */" % part)
1459
+
1460
+ if not Options.cache_builtins:
1461
+ del self.parts['cached_builtins']
1462
+ else:
1463
+ w = self.parts['cached_builtins']
1464
+ w.start_initcfunc(
1465
+ "int __Pyx_InitCachedBuiltins("
1466
+ f"{Naming.modulestatetype_cname} *{Naming.modulestatevalue_cname})")
1467
+ w.putln(f"CYTHON_UNUSED_VAR({Naming.modulestatevalue_cname});")
1468
+
1469
+ w = self.parts['cached_constants']
1470
+ w.start_initcfunc(
1471
+ "int __Pyx_InitCachedConstants("
1472
+ f"{Naming.modulestatetype_cname} *{Naming.modulestatevalue_cname})",
1473
+ refnanny=True)
1474
+ w.putln(f"CYTHON_UNUSED_VAR({Naming.modulestatevalue_cname});")
1475
+ w.put_setup_refcount_context(StringEncoding.EncodedString("__Pyx_InitCachedConstants"))
1476
+
1477
+ w = self.parts['init_globals']
1478
+ w.start_initcfunc("int __Pyx_InitGlobals(void)")
1479
+
1480
+ w = self.parts['init_constants']
1481
+ w.start_initcfunc(
1482
+ "int __Pyx_InitConstants("
1483
+ f"{Naming.modulestatetype_cname} *{Naming.modulestatevalue_cname})")
1484
+ w.putln(f"CYTHON_UNUSED_VAR({Naming.modulestatevalue_cname});")
1485
+
1486
+ if not Options.generate_cleanup_code:
1487
+ del self.parts['cleanup_globals']
1488
+ else:
1489
+ w = self.parts['cleanup_globals']
1490
+ w.start_initcfunc(
1491
+ "void __Pyx_CleanupGlobals("
1492
+ f"{Naming.modulestatetype_cname} *{Naming.modulestatevalue_cname})")
1493
+ w.putln(f"CYTHON_UNUSED_VAR({Naming.modulestatevalue_cname});")
1494
+
1495
+ code = self.parts['utility_code_proto']
1496
+ code.putln("")
1497
+ code.putln("/* --- Runtime support code (head) --- */")
1498
+
1499
+ code = self.parts['utility_code_def']
1500
+ if self.code_config.emit_linenums:
1501
+ code.write('\n#line 1 "cython_utility"\n')
1502
+ code.putln("")
1503
+ code.putln("/* --- Runtime support code --- */")
1504
+
1505
+ def initialize_main_h_code(self):
1506
+ rootwriter = self.rootwriter
1507
+ for part in self.h_code_layout:
1508
+ self.parts[part] = rootwriter.insertion_point()
1509
+
1510
+ def finalize_main_c_code(self):
1511
+ self.close_global_decls()
1512
+
1513
+ #
1514
+ # utility_code_def
1515
+ #
1516
+ code = self.parts['utility_code_def']
1517
+ util = TempitaUtilityCode.load_cached("TypeConversions", "TypeConversion.c")
1518
+ code.put(util.format_code(util.impl))
1519
+ code.putln("")
1520
+
1521
+ #
1522
+ # utility code pragmas
1523
+ #
1524
+ code = self.parts['utility_code_pragmas']
1525
+ util = UtilityCode.load_cached("UtilityCodePragmas", "ModuleSetupCode.c")
1526
+ code.putln(util.format_code(util.impl))
1527
+ code.putln("")
1528
+ code = self.parts['utility_code_pragmas_end']
1529
+ util = UtilityCode.load_cached("UtilityCodePragmasEnd", "ModuleSetupCode.c")
1530
+ code.putln(util.format_code(util.impl))
1531
+ code.putln("")
1532
+
1533
+ def __getitem__(self, key):
1534
+ return self.parts[key]
1535
+
1536
+ #
1537
+ # Global constants, interned objects, etc.
1538
+ #
1539
+ def close_global_decls(self):
1540
+ # This is called when it is known that no more global declarations will
1541
+ # declared.
1542
+ self.generate_const_declarations()
1543
+ if Options.cache_builtins:
1544
+ w = self.parts['cached_builtins']
1545
+ w.putln("return 0;")
1546
+ if w.label_used(w.error_label):
1547
+ w.put_label(w.error_label)
1548
+ w.putln("return -1;")
1549
+ w.putln("}")
1550
+ w.exit_cfunc_scope()
1551
+
1552
+ w = self.parts['cached_constants']
1553
+ w.put_finish_refcount_context()
1554
+ w.putln("return 0;")
1555
+ if w.label_used(w.error_label):
1556
+ w.put_label(w.error_label)
1557
+ w.put_finish_refcount_context()
1558
+ w.putln("return -1;")
1559
+ w.putln("}")
1560
+ w.exit_cfunc_scope()
1561
+
1562
+ for part in ['init_globals', 'init_constants']:
1563
+ w = self.parts[part]
1564
+ w.putln("return 0;")
1565
+ if w.label_used(w.error_label):
1566
+ w.put_label(w.error_label)
1567
+ w.putln("return -1;")
1568
+ w.putln("}")
1569
+ w.exit_cfunc_scope()
1570
+
1571
+ if Options.generate_cleanup_code:
1572
+ w = self.parts['cleanup_globals']
1573
+ w.putln("}")
1574
+ w.exit_cfunc_scope()
1575
+
1576
+ if Options.generate_cleanup_code:
1577
+ w = self.parts['cleanup_module']
1578
+ w.putln("}")
1579
+ w.exit_cfunc_scope()
1580
+
1581
+ def put_pyobject_decl(self, entry):
1582
+ self['global_var'].putln("static PyObject *%s;" % entry.cname)
1583
+
1584
+ # constant handling at code generation time
1585
+
1586
+ def get_cached_constants_writer(self, target=None):
1587
+ if target is not None:
1588
+ if target in self.initialised_constants:
1589
+ # Return None on second/later calls to prevent duplicate creation code.
1590
+ return None
1591
+ self.initialised_constants.add(target)
1592
+ return self.parts['cached_constants']
1593
+
1594
+ def get_int_const(self, str_value, longness=False):
1595
+ py_type = longness and 'long' or 'int'
1596
+ try:
1597
+ c = self.num_const_index[(str_value, py_type)]
1598
+ except KeyError:
1599
+ c = self.new_num_const(str_value, py_type)
1600
+ return c
1601
+
1602
+ def get_float_const(self, str_value, value_code):
1603
+ try:
1604
+ c = self.num_const_index[(str_value, 'float')]
1605
+ except KeyError:
1606
+ c = self.new_num_const(str_value, 'float', value_code)
1607
+ return c
1608
+
1609
+ def get_py_const(self, prefix, dedup_key=None):
1610
+ if dedup_key is not None:
1611
+ const = self.dedup_const_index.get(dedup_key)
1612
+ if const is not None:
1613
+ return const
1614
+ const = self.new_array_const_cname(prefix)
1615
+ if dedup_key is not None:
1616
+ self.dedup_const_index[dedup_key] = const
1617
+ return const
1618
+
1619
+ def get_argument_default_const(self, type):
1620
+ cname = self.new_const_cname('')
1621
+ c = PyObjectConst(cname, type)
1622
+ self.arg_default_constants.append(c)
1623
+ # Argument default constants aren't currently cleaned up.
1624
+ # If that changes, it needs to account for the fact that they
1625
+ # aren't just Python objects
1626
+ return c
1627
+
1628
+ def get_string_const(self, text):
1629
+ # return a C string constant, creating a new one if necessary
1630
+ if text.is_unicode:
1631
+ byte_string = text.utf8encode()
1632
+ else:
1633
+ byte_string = text.byteencode()
1634
+ try:
1635
+ c = self.string_const_index[byte_string]
1636
+ except KeyError:
1637
+ c = self.new_string_const(text, byte_string)
1638
+ return c
1639
+
1640
+ def get_pyunicode_ptr_const(self, text):
1641
+ # return a Py_UNICODE[] constant, creating a new one if necessary
1642
+ assert text.is_unicode
1643
+ try:
1644
+ c = self.pyunicode_ptr_const_index[text]
1645
+ except KeyError:
1646
+ c = self.pyunicode_ptr_const_index[text] = self.new_const_cname()
1647
+ return c
1648
+
1649
+ def get_py_string_const(self, text, identifier=None):
1650
+ # return a Python string constant, creating a new one if necessary
1651
+ c_string = self.get_string_const(text)
1652
+ py_string = c_string.get_py_string_const(text.encoding, identifier)
1653
+ return py_string
1654
+
1655
+ def get_py_codeobj_const(self, node):
1656
+ idx = len(self.codeobject_constants)
1657
+ name = f"{Naming.codeobjtab_cname}[{idx}]"
1658
+ self.codeobject_constants.append(node)
1659
+ return name
1660
+
1661
+ def get_interned_identifier(self, text):
1662
+ return self.get_py_string_const(text, identifier=True)
1663
+
1664
+ def new_string_const(self, text, byte_string):
1665
+ cname = self.new_string_const_cname(byte_string)
1666
+ c = StringConst(cname, text, byte_string)
1667
+ self.string_const_index[byte_string] = c
1668
+ return c
1669
+
1670
+ def new_num_const(self, value, py_type, value_code=None):
1671
+ cname = self.new_num_const_cname(value, py_type)
1672
+ c = NumConst(cname, value, py_type, value_code)
1673
+ self.num_const_index[(value, py_type)] = c
1674
+ return c
1675
+
1676
+ def new_string_const_cname(self, bytes_value):
1677
+ # Create a new globally-unique nice name for a C string constant.
1678
+ value = bytes_value.decode('ASCII', 'ignore')
1679
+ return self.new_const_cname(value=value)
1680
+
1681
+ def unique_const_cname(self, format_str): # type: (str) -> str
1682
+ used = self.const_cnames_used
1683
+ cname = value = format_str.format(sep='', counter='')
1684
+ while cname in used:
1685
+ counter = used[value] = used[value] + 1
1686
+ cname = format_str.format(sep='_', counter=counter)
1687
+ used[cname] = 1
1688
+ return cname
1689
+
1690
+ def new_num_const_cname(self, value, py_type): # type: (str, str) -> str
1691
+ if py_type == 'long':
1692
+ value += 'L'
1693
+ py_type = 'int'
1694
+ prefix = Naming.interned_prefixes[py_type]
1695
+
1696
+ value = value.replace('.', '_').replace('+', '_').replace('-', 'neg_')
1697
+ if len(value) > 42:
1698
+ # update tests/run/large_integer_T5290.py in case the amount is changed
1699
+ cname = self.unique_const_cname(
1700
+ prefix + "large{counter}_" + value[:18] + "_xxx_" + value[-18:])
1701
+ else:
1702
+ cname = "%s%s" % (prefix, value)
1703
+ return cname
1704
+
1705
+ def new_const_cname(self, prefix='', value=''):
1706
+ value = replace_identifier('_', value)[:32].strip('_')
1707
+ name_suffix = self.unique_const_cname(value + "{sep}{counter}")
1708
+ if prefix:
1709
+ prefix = Naming.interned_prefixes[prefix]
1710
+ else:
1711
+ prefix = Naming.const_prefix
1712
+ return "%s%s" % (prefix, name_suffix)
1713
+
1714
+ def new_array_const_cname(self, prefix: str):
1715
+ count = self.const_array_counters.get(prefix, 0)
1716
+ self.const_array_counters[prefix] = count+1
1717
+ return f"{Naming.pyrex_prefix}{prefix}[{count}]"
1718
+
1719
+ def get_cached_unbound_method(self, type_cname, method_name):
1720
+ key = (type_cname, method_name)
1721
+ try:
1722
+ cname = self.cached_cmethods[key]
1723
+ except KeyError:
1724
+ cname = self.cached_cmethods[key] = self.new_const_cname(
1725
+ 'umethod', '%s_%s' % (type_cname, method_name))
1726
+ return cname
1727
+
1728
+ def cached_unbound_method_call_code(self, modulestate_cname, obj_cname, type_cname, method_name, arg_cnames):
1729
+ # admittedly, not the best place to put this method, but it is reused by UtilityCode and ExprNodes ...
1730
+ utility_code_name = "CallUnboundCMethod%d" % len(arg_cnames)
1731
+ self.use_utility_code(UtilityCode.load_cached(utility_code_name, "ObjectHandling.c"))
1732
+ cache_cname = self.get_cached_unbound_method(type_cname, method_name)
1733
+ args = [obj_cname] + arg_cnames
1734
+ return "__Pyx_%s(&%s%s, %s)" % (
1735
+ utility_code_name,
1736
+ modulestate_cname,
1737
+ cache_cname,
1738
+ ', '.join(args),
1739
+ )
1740
+
1741
+ def add_cached_builtin_decl(self, entry):
1742
+ if entry.is_builtin and entry.is_const:
1743
+ if self.should_declare(entry.cname, entry):
1744
+ self.put_pyobject_decl(entry)
1745
+ name = entry.name
1746
+ if name in renamed_py2_builtins_map:
1747
+ name = renamed_py2_builtins_map[name]
1748
+ self.put_cached_builtin_init(
1749
+ entry.pos, StringEncoding.EncodedString(name),
1750
+ entry.cname)
1751
+
1752
+ def put_cached_builtin_init(self, pos, name, cname):
1753
+ w = self.parts['cached_builtins']
1754
+ cname_in_modulestate = w.name_in_main_c_code_module_state(
1755
+ self.get_interned_identifier(name).cname)
1756
+ self.use_utility_code(
1757
+ UtilityCode.load_cached("GetBuiltinName", "ObjectHandling.c"))
1758
+ w.putln('%s = __Pyx_GetBuiltinName(%s); if (!%s) %s' % (
1759
+ cname,
1760
+ cname_in_modulestate,
1761
+ cname,
1762
+ w.error_goto(pos)))
1763
+
1764
+ def generate_const_declarations(self):
1765
+ self.generate_cached_methods_decls()
1766
+ self.generate_object_constant_decls()
1767
+ self.generate_codeobject_constants()
1768
+ # generate code for string and numeric constants as late as possible
1769
+ # to allow new constants be to created by the earlier stages.
1770
+ # (although the constants themselves are written early)
1771
+ self.generate_string_constants()
1772
+ self.generate_num_constants()
1773
+
1774
+ def _generate_module_array_traverse_and_clear(self, struct_attr_cname, count, may_have_refcycles=True):
1775
+ counter_type = 'int' if count < 2**15 else 'Py_ssize_t'
1776
+ visit_call = "Py_VISIT" if may_have_refcycles else "__Pyx_VISIT_CONST"
1777
+
1778
+ writer = self.parts['module_state_traverse']
1779
+ writer.putln(f"for ({counter_type} i=0; i<{count}; ++i) {{ {visit_call}(traverse_module_state->{struct_attr_cname}[i]); }}")
1780
+
1781
+ writer = self.parts['module_state_clear']
1782
+ writer.putln(f"for ({counter_type} i=0; i<{count}; ++i) {{ Py_CLEAR(clear_module_state->{struct_attr_cname}[i]); }}")
1783
+
1784
+ def generate_object_constant_decls(self):
1785
+ consts = [(len(c.cname), c.cname, c)
1786
+ for c in self.arg_default_constants]
1787
+ consts.sort()
1788
+ for _, cname, c in consts:
1789
+ self.parts['module_state'].putln("%s;" % c.type.declaration_code(cname))
1790
+ if not c.type.needs_refcounting:
1791
+ # Note that py_constants is used for all argument defaults
1792
+ # which aren't necessarily PyObjects, so aren't appropriate
1793
+ # to clear.
1794
+ continue
1795
+
1796
+ self.parts['module_state_clear'].put_xdecref_clear(
1797
+ f"clear_module_state->{cname}",
1798
+ c.type,
1799
+ clear_before_decref=True,
1800
+ nanny=False,
1801
+ )
1802
+
1803
+ if c.type.is_memoryviewslice:
1804
+ # TODO: Implement specific to type like CodeWriter.put_xdecref_clear()
1805
+ cname += "->memview"
1806
+
1807
+ self.parts['module_state_traverse'].putln(
1808
+ f"Py_VISIT(traverse_module_state->{cname});")
1809
+
1810
+ for prefix, count in sorted(self.const_array_counters.items()):
1811
+ struct_attr_cname = f"{Naming.pyrex_prefix}{prefix}"
1812
+ self.parts['module_state'].putln(f"PyObject *{struct_attr_cname}[{count}];")
1813
+
1814
+ # The constant tuples/slices that we create can never participate in reference cycles.
1815
+ self._generate_module_array_traverse_and_clear(struct_attr_cname, count, may_have_refcycles=False)
1816
+
1817
+ cleanup_level = cleanup_level_for_type_prefix(prefix)
1818
+ if cleanup_level is not None and cleanup_level <= Options.generate_cleanup_code:
1819
+ part_writer = self.parts['cleanup_globals']
1820
+ part_writer.put(f"for (size_t i=0; i<{count}; ++i) ")
1821
+ part_writer.putln(
1822
+ "{ Py_CLEAR(%s); }" %
1823
+ part_writer.name_in_main_c_code_module_state(f"{struct_attr_cname}[i]")
1824
+ )
1825
+
1826
+ def generate_cached_methods_decls(self):
1827
+ if not self.cached_cmethods:
1828
+ return
1829
+
1830
+ decl = self.parts['module_state']
1831
+ init = self.parts['init_constants']
1832
+ cnames = []
1833
+ for (type_cname, method_name), cname in sorted(self.cached_cmethods.items()):
1834
+ cnames.append(cname)
1835
+ method_name_cname = self.get_interned_identifier(StringEncoding.EncodedString(method_name)).cname
1836
+ decl.putln('__Pyx_CachedCFunction %s;' % (
1837
+ cname))
1838
+ # split type reference storage as it might not be static
1839
+ init.putln('%s.type = (PyObject*)%s;' % (
1840
+ init.name_in_main_c_code_module_state(cname), type_cname))
1841
+ # method name string isn't static in limited api
1842
+ init.putln(
1843
+ f'{init.name_in_main_c_code_module_state(cname)}.method_name = '
1844
+ f'&{init.name_in_main_c_code_module_state(method_name_cname)};')
1845
+
1846
+ if Options.generate_cleanup_code:
1847
+ cleanup = self.parts['cleanup_globals']
1848
+ for cname in cnames:
1849
+ cleanup.putln(f"Py_CLEAR({init.name_in_main_c_code_module_state(cname)}.method);")
1850
+
1851
+ def generate_string_constants(self):
1852
+ c_consts = [(len(c.cname), c.cname, c) for c in self.string_const_index.values()]
1853
+ c_consts.sort()
1854
+ py_strings = []
1855
+ longest_pystring = 0
1856
+ encodings = set()
1857
+
1858
+ def normalise_encoding_name(py_string):
1859
+ if py_string.encoding and py_string.encoding not in (
1860
+ 'ASCII', 'USASCII', 'US-ASCII', 'UTF8', 'UTF-8'):
1861
+ return f'"{py_string.encoding.lower()}"'
1862
+ else:
1863
+ return '0'
1864
+
1865
+ decls_writer = self.parts['string_decls']
1866
+ for _, cname, c in c_consts:
1867
+ cliteral = StringEncoding.split_string_literal(c.escaped_value)
1868
+ decls_writer.putln(
1869
+ f'static const char {cname}[] = "{cliteral}";',
1870
+ safe=True) # Braces in user strings are not for indentation.
1871
+ if c.py_strings is not None:
1872
+ if len(c.escaped_value) > longest_pystring:
1873
+ # This is not an accurate count since it adds up C escape characters,
1874
+ # but it's probably good enough for an upper bound.
1875
+ longest_pystring = len(c.escaped_value)
1876
+ for py_string in c.py_strings.values():
1877
+ encodings.add(normalise_encoding_name(py_string))
1878
+ py_strings.append((c.cname, len(py_string.cname), py_string))
1879
+
1880
+ for c, cname in sorted(self.pyunicode_ptr_const_index.items()):
1881
+ utf16_array, utf32_array = StringEncoding.encode_pyunicode_string(c)
1882
+ if utf16_array:
1883
+ # Narrow and wide representations differ
1884
+ decls_writer.putln("#ifdef Py_UNICODE_WIDE")
1885
+ decls_writer.putln("static Py_UNICODE %s[] = { %s };" % (cname, utf32_array))
1886
+ if utf16_array:
1887
+ decls_writer.putln("#else")
1888
+ decls_writer.putln("static Py_UNICODE %s[] = { %s };" % (cname, utf16_array))
1889
+ decls_writer.putln("#endif")
1890
+
1891
+ if not py_strings:
1892
+ return
1893
+
1894
+ py_strings.sort()
1895
+
1896
+ w = self.parts['pystring_table']
1897
+ w.putln("")
1898
+
1899
+ # We use only type size macros from "pyport.h" here.
1900
+ w.put(textwrap.dedent("""\
1901
+ typedef struct {
1902
+ const char *s;
1903
+ #if %(max_length)d <= 65535
1904
+ const unsigned short n;
1905
+ #elif %(max_length)d / 2 < INT_MAX
1906
+ const unsigned int n;
1907
+ #elif %(max_length)d / 2 < LONG_MAX
1908
+ const unsigned long n;
1909
+ #else
1910
+ const Py_ssize_t n;
1911
+ #endif
1912
+ #if %(num_encodings)d <= 31
1913
+ const unsigned int encoding : 5;
1914
+ #elif %(num_encodings)d <= 255
1915
+ const unsigned char encoding;
1916
+ #elif %(num_encodings)d <= 65535
1917
+ const unsigned short encoding;
1918
+ #else
1919
+ const Py_ssize_t encoding;
1920
+ #endif
1921
+ const unsigned int is_unicode : 1;
1922
+ const unsigned int intern : 1;
1923
+ } __Pyx_StringTabEntry;
1924
+ """ % dict(
1925
+ max_length=longest_pystring,
1926
+ num_encodings=len(encodings),
1927
+ )))
1928
+
1929
+ py_string_count = len(py_strings)
1930
+ self.parts['module_state'].putln(f"PyObject *{Naming.stringtab_cname}[{py_string_count}];")
1931
+ self._generate_module_array_traverse_and_clear(Naming.stringtab_cname, py_string_count, may_have_refcycles=False)
1932
+
1933
+ encodings = sorted(encodings)
1934
+ encodings.sort(key=len) # stable sort to make sure '0' comes first, index 0
1935
+ assert not encodings or '0' not in encodings or encodings[0] == '0', encodings
1936
+ encodings_map = {encoding: i for i, encoding in enumerate(encodings)}
1937
+ w.putln("static const char * const %s[] = { %s };" % (
1938
+ Naming.stringtab_encodings_cname,
1939
+ ', '.join(encodings),
1940
+ ))
1941
+
1942
+ w.putln("static const __Pyx_StringTabEntry %s[] = {" % Naming.stringtab_cname)
1943
+ for n, (c_cname, _, py_string) in enumerate(py_strings):
1944
+ encodings_index = encodings_map[normalise_encoding_name(py_string)]
1945
+ is_unicode = py_string.is_unicode
1946
+
1947
+ self.parts['constant_name_defines'].putln("#define %s %s[%s]" % (
1948
+ py_string.cname,
1949
+ Naming.stringtab_cname,
1950
+ n))
1951
+
1952
+ w.putln("{%s, sizeof(%s), %d, %d, %d}, /* PyObject cname: %s */" % (
1953
+ c_cname,
1954
+ c_cname,
1955
+ encodings_index,
1956
+ is_unicode,
1957
+ py_string.intern,
1958
+ py_string.cname
1959
+ ))
1960
+ w.putln("{0, 0, 0, 0, 0}")
1961
+ w.putln("};")
1962
+
1963
+ self.use_utility_code(UtilityCode.load_cached("InitStrings", "StringTools.c"))
1964
+
1965
+ init_constants = self.parts['init_constants']
1966
+ init_constants.putln(
1967
+ "if (__Pyx_InitStrings(%s, %s, %s) < 0) %s;" % (
1968
+ Naming.stringtab_cname,
1969
+ init_constants.name_in_main_c_code_module_state(Naming.stringtab_cname),
1970
+ Naming.stringtab_encodings_cname,
1971
+ init_constants.error_goto(self.module_pos)))
1972
+
1973
+ def generate_codeobject_constants(self):
1974
+ w = self.parts['init_codeobjects']
1975
+ init_function = (
1976
+ f"int __Pyx_CreateCodeObjects({Naming.modulestatetype_cname} *{Naming.modulestatevalue_cname})"
1977
+ )
1978
+
1979
+ if not self.codeobject_constants:
1980
+ w.start_initcfunc(init_function)
1981
+ w.putln(f"CYTHON_UNUSED_VAR({Naming.modulestatevalue_cname});")
1982
+ w.putln("return 0;")
1983
+ w.exit_cfunc_scope()
1984
+ w.putln("}")
1985
+ return
1986
+
1987
+ # Create a downsized config struct and build code objects from it.
1988
+ max_flags = 0x3ff # to be adapted when we start using new flags
1989
+ max_func_args = 1
1990
+ max_kwonly_args = 1
1991
+ max_posonly_args = 1
1992
+ max_vars = 1
1993
+ max_line = 1
1994
+ max_positions = 1
1995
+ for node in self.codeobject_constants:
1996
+ def_node = node.def_node
1997
+ if not def_node.is_generator_expression:
1998
+ max_func_args = max(max_func_args, len(def_node.args) - def_node.num_kwonly_args)
1999
+ max_kwonly_args = max(max_kwonly_args, def_node.num_kwonly_args)
2000
+ max_posonly_args = max(max_posonly_args, def_node.num_posonly_args)
2001
+ max_vars = max(max_vars, len(node.varnames))
2002
+ max_line = max(max_line, def_node.pos[1])
2003
+ max_positions = max(max_positions, len(def_node.node_positions))
2004
+
2005
+ # Even for full 64-bit line/column values, one entry in the line table can never be larger than 45 bytes.
2006
+ max_linetable_len = max_positions * 47
2007
+
2008
+ w.put(textwrap.dedent(f"""\
2009
+ typedef struct {{
2010
+ unsigned int argcount : {max_func_args.bit_length()};
2011
+ unsigned int num_posonly_args : {max_posonly_args.bit_length()};
2012
+ unsigned int num_kwonly_args : {max_kwonly_args.bit_length()};
2013
+ unsigned int nlocals : {max_vars.bit_length()};
2014
+ unsigned int flags : {max_flags.bit_length()};
2015
+ unsigned int first_line : {max_line.bit_length()};
2016
+ unsigned int line_table_length : {max_linetable_len.bit_length()};
2017
+ }} __Pyx_PyCode_New_function_description;
2018
+ """))
2019
+
2020
+ self.use_utility_code(UtilityCode.load_cached("NewCodeObj", "ModuleSetupCode.c"))
2021
+
2022
+ w.start_initcfunc(init_function)
2023
+
2024
+ w.putln("PyObject* tuple_dedup_map = PyDict_New();")
2025
+ w.putln("if (unlikely(!tuple_dedup_map)) return -1;")
2026
+
2027
+ for node in self.codeobject_constants:
2028
+ node.generate_codeobj(w, "bad")
2029
+
2030
+ w.putln("Py_DECREF(tuple_dedup_map);")
2031
+ w.putln("return 0;")
2032
+
2033
+ w.putln("bad:")
2034
+ w.putln("Py_DECREF(tuple_dedup_map);")
2035
+ w.putln("return -1;")
2036
+ w.exit_cfunc_scope()
2037
+ w.putln("}")
2038
+
2039
+ code_object_count = len(self.codeobject_constants)
2040
+ self.parts['module_state'].putln(f"PyObject *{Naming.codeobjtab_cname}[{code_object_count}];")
2041
+ # The code objects that we generate only contain plain constants and can never participate in reference cycles.
2042
+ self._generate_module_array_traverse_and_clear(Naming.codeobjtab_cname, code_object_count, may_have_refcycles=False)
2043
+
2044
+ def generate_num_constants(self):
2045
+ consts = [(c.py_type, c.value[0] == '-', len(c.value), c.value, c.value_code, c)
2046
+ for c in self.num_const_index.values()]
2047
+ consts.sort()
2048
+ init_constants = self.parts['init_constants']
2049
+ for py_type, _, _, value, value_code, c in consts:
2050
+ cname = c.cname
2051
+ self.parts['module_state'].putln("PyObject *%s;" % cname)
2052
+ self.parts['module_state_clear'].putln(
2053
+ "Py_CLEAR(clear_module_state->%s);" % cname)
2054
+ self.parts['module_state_traverse'].putln(
2055
+ "__Pyx_VISIT_CONST(traverse_module_state->%s);" % cname)
2056
+ if py_type == 'float':
2057
+ function = 'PyFloat_FromDouble(%s)'
2058
+ elif py_type == 'long':
2059
+ function = 'PyLong_FromString("%s", 0, 0)'
2060
+ elif Utils.long_literal(value):
2061
+ function = 'PyLong_FromString("%s", 0, 0)'
2062
+ elif len(value.lstrip('-')) > 4:
2063
+ function = "PyLong_FromLong(%sL)"
2064
+ else:
2065
+ function = "PyLong_FromLong(%s)"
2066
+ init_cname = init_constants.name_in_main_c_code_module_state(cname)
2067
+ init_constants.putln('%s = %s; %s' % (
2068
+ init_cname, function % value_code,
2069
+ init_constants.error_goto_if_null(init_cname, self.module_pos)))
2070
+
2071
+ # The functions below are there in a transition phase only
2072
+ # and will be deprecated. They are called from Nodes.BlockNode.
2073
+ # The copy&paste duplication is intentional in order to be able
2074
+ # to see quickly how BlockNode worked, until this is replaced.
2075
+
2076
+ def should_declare(self, cname, entry):
2077
+ if cname in self.declared_cnames:
2078
+ other = self.declared_cnames[cname]
2079
+ assert str(entry.type) == str(other.type)
2080
+ assert entry.init == other.init
2081
+ return False
2082
+ else:
2083
+ self.declared_cnames[cname] = entry
2084
+ return True
2085
+
2086
+ #
2087
+ # File name state
2088
+ #
2089
+
2090
+ def lookup_filename(self, source_desc):
2091
+ entry = source_desc.get_filenametable_entry()
2092
+ try:
2093
+ index = self.filename_table[entry]
2094
+ except KeyError:
2095
+ index = len(self.filename_list)
2096
+ self.filename_list.append(source_desc)
2097
+ self.filename_table[entry] = index
2098
+ return index
2099
+
2100
+ def commented_file_contents(self, source_desc):
2101
+ try:
2102
+ return self.input_file_contents[source_desc]
2103
+ except KeyError:
2104
+ pass
2105
+ source_file = source_desc.get_lines(encoding='ASCII',
2106
+ error_handling='ignore')
2107
+ try:
2108
+ F = [' * ' + line.rstrip().replace(
2109
+ '*/', '*[inserted by cython to avoid comment closer]/'
2110
+ ).replace(
2111
+ '/*', '/[inserted by cython to avoid comment start]*'
2112
+ )
2113
+ for line in source_file]
2114
+ finally:
2115
+ if hasattr(source_file, 'close'):
2116
+ source_file.close()
2117
+ if not F: F.append('')
2118
+ self.input_file_contents[source_desc] = F
2119
+ return F
2120
+
2121
+ #
2122
+ # Utility code state
2123
+ #
2124
+
2125
+ def use_utility_code(self, utility_code):
2126
+ """
2127
+ Adds code to the C file. utility_code should
2128
+ a) implement __eq__/__hash__ for the purpose of knowing whether the same
2129
+ code has already been included
2130
+ b) implement put_code, which takes a globalstate instance
2131
+
2132
+ See UtilityCode.
2133
+ """
2134
+ if utility_code and utility_code not in self.utility_codes:
2135
+ self.utility_codes.add(utility_code)
2136
+ utility_code.put_code(self)
2137
+
2138
+ def use_entry_utility_code(self, entry):
2139
+ if entry is None:
2140
+ return
2141
+ if entry.utility_code:
2142
+ self.use_utility_code(entry.utility_code)
2143
+ if entry.utility_code_definition:
2144
+ self.use_utility_code(entry.utility_code_definition)
2145
+
2146
+
2147
+ def funccontext_property(func):
2148
+ name = func.__name__
2149
+ attribute_of = operator.attrgetter(name)
2150
+ def get(self):
2151
+ return attribute_of(self.funcstate)
2152
+ def set(self, value):
2153
+ setattr(self.funcstate, name, value)
2154
+ return property(get, set)
2155
+
2156
+
2157
+ class CCodeConfig:
2158
+ # emit_linenums boolean write #line pragmas?
2159
+ # emit_code_comments boolean copy the original code into C comments?
2160
+ # c_line_in_traceback boolean append the c file and line number to the traceback for exceptions?
2161
+
2162
+ def __init__(self, emit_linenums=True, emit_code_comments=True, c_line_in_traceback=True):
2163
+ self.emit_code_comments = emit_code_comments
2164
+ self.emit_linenums = emit_linenums
2165
+ self.c_line_in_traceback = c_line_in_traceback
2166
+
2167
+
2168
+ class CCodeWriter:
2169
+ """
2170
+ Utility class to output C code.
2171
+
2172
+ When creating an insertion point one must care about the state that is
2173
+ kept:
2174
+ - formatting state (level, bol) is cloned and used in insertion points
2175
+ as well
2176
+ - labels, temps, exc_vars: One must construct a scope in which these can
2177
+ exist by calling enter_cfunc_scope/exit_cfunc_scope (these are for
2178
+ sanity checking and forward compatibility). Created insertion points
2179
+ looses this scope and cannot access it.
2180
+ - marker: Not copied to insertion point
2181
+ - filename_table, filename_list, input_file_contents: All codewriters
2182
+ coming from the same root share the same instances simultaneously.
2183
+ """
2184
+
2185
+ # f file output file
2186
+ # buffer StringIOTree
2187
+
2188
+ # level int indentation level
2189
+ # bol bool beginning of line?
2190
+ # marker string comment to emit before next line
2191
+ # funcstate FunctionState contains state local to a C function used for code
2192
+ # generation (labels and temps state etc.)
2193
+ # globalstate GlobalState contains state global for a C file (input file info,
2194
+ # utility code, declared constants etc.)
2195
+ # pyclass_stack list used during recursive code generation to pass information
2196
+ # about the current class one is in
2197
+ # code_config CCodeConfig configuration options for the C code writer
2198
+
2199
+ @cython.locals(create_from='CCodeWriter')
2200
+ def __init__(self, create_from=None, buffer=None, copy_formatting=False):
2201
+ if buffer is None: buffer = StringIOTree()
2202
+ self.buffer = buffer
2203
+ self.last_pos = None
2204
+ self.last_marked_pos = None
2205
+ self.pyclass_stack = []
2206
+
2207
+ self.funcstate = None
2208
+ self.globalstate = None
2209
+ self.code_config = None
2210
+ self.level = 0
2211
+ self.call_level = 0
2212
+ self.bol = 1
2213
+
2214
+ if create_from is not None:
2215
+ # Use same global state
2216
+ self.set_global_state(create_from.globalstate)
2217
+ self.funcstate = create_from.funcstate
2218
+ # Clone formatting state
2219
+ if copy_formatting:
2220
+ self.level = create_from.level
2221
+ self.bol = create_from.bol
2222
+ self.call_level = create_from.call_level
2223
+ self.last_pos = create_from.last_pos
2224
+ self.last_marked_pos = create_from.last_marked_pos
2225
+
2226
+ def create_new(self, create_from, buffer, copy_formatting):
2227
+ # polymorphic constructor -- very slightly more versatile
2228
+ # than using __class__
2229
+ result = CCodeWriter(create_from, buffer, copy_formatting)
2230
+ return result
2231
+
2232
+ def set_global_state(self, global_state):
2233
+ assert self.globalstate is None # prevent overwriting once it's set
2234
+ self.globalstate = global_state
2235
+ self.code_config = global_state.code_config
2236
+
2237
+ def copyto(self, f):
2238
+ self.buffer.copyto(f)
2239
+
2240
+ def getvalue(self):
2241
+ return self.buffer.getvalue()
2242
+
2243
+ def write(self, s):
2244
+ if '\n' in s:
2245
+ self._write_lines(s)
2246
+ else:
2247
+ self._write_to_buffer(s)
2248
+
2249
+ def _write_lines(self, s):
2250
+ # Cygdb needs to know which Cython source line corresponds to which C line.
2251
+ # Therefore, we write this information into "self.buffer.markers" and then write it from there
2252
+ # into cython_debug/cython_debug_info_* (see ModuleNode._serialize_lineno_map).
2253
+ filename_line = self.last_marked_pos[:2] if self.last_marked_pos else (None, 0)
2254
+ self.buffer.markers.extend([filename_line] * s.count('\n'))
2255
+
2256
+ self._write_to_buffer(s)
2257
+
2258
+ def _write_to_buffer(self, s):
2259
+ self.buffer.write(s)
2260
+
2261
+ def insertion_point(self):
2262
+ other = self.create_new(create_from=self, buffer=self.buffer.insertion_point(), copy_formatting=True)
2263
+ return other
2264
+
2265
+ def new_writer(self):
2266
+ """
2267
+ Creates a new CCodeWriter connected to the same global state, which
2268
+ can later be inserted using insert.
2269
+ """
2270
+ return CCodeWriter(create_from=self)
2271
+
2272
+ def insert(self, writer):
2273
+ """
2274
+ Inserts the contents of another code writer (created with
2275
+ the same global state) in the current location.
2276
+
2277
+ It is ok to write to the inserted writer also after insertion.
2278
+ """
2279
+ assert writer.globalstate is self.globalstate
2280
+ self.buffer.insert(writer.buffer)
2281
+
2282
+ # Properties delegated to function scope
2283
+ @funccontext_property
2284
+ def label_counter(self): pass
2285
+ @funccontext_property
2286
+ def return_label(self): pass
2287
+ @funccontext_property
2288
+ def error_label(self): pass
2289
+ @funccontext_property
2290
+ def labels_used(self): pass
2291
+ @funccontext_property
2292
+ def continue_label(self): pass
2293
+ @funccontext_property
2294
+ def break_label(self): pass
2295
+ @funccontext_property
2296
+ def return_from_error_cleanup_label(self): pass
2297
+ @funccontext_property
2298
+ def yield_labels(self): pass
2299
+
2300
+ def label_interceptor(self, new_labels, orig_labels, skip_to_label=None, pos=None, trace=True):
2301
+ """
2302
+ Helper for generating multiple label interceptor code blocks.
2303
+
2304
+ @param new_labels: the new labels that should be intercepted
2305
+ @param orig_labels: the original labels that we should dispatch to after the interception
2306
+ @param skip_to_label: a label to skip to before starting the code blocks
2307
+ @param pos: the node position to mark for each interceptor block
2308
+ @param trace: add a trace line for the pos marker or not
2309
+ """
2310
+ for label, orig_label in zip(new_labels, orig_labels):
2311
+ if not self.label_used(label):
2312
+ continue
2313
+ if skip_to_label:
2314
+ # jump over the whole interception block
2315
+ self.put_goto(skip_to_label)
2316
+ skip_to_label = None
2317
+
2318
+ if pos is not None:
2319
+ self.mark_pos(pos, trace=trace)
2320
+ self.put_label(label)
2321
+ yield (label, orig_label)
2322
+ self.put_goto(orig_label)
2323
+
2324
+ # Functions delegated to function scope
2325
+ def new_label(self, name=None): return self.funcstate.new_label(name)
2326
+ def new_error_label(self, *args): return self.funcstate.new_error_label(*args)
2327
+ def new_yield_label(self, *args): return self.funcstate.new_yield_label(*args)
2328
+ def get_loop_labels(self): return self.funcstate.get_loop_labels()
2329
+ def set_loop_labels(self, labels): return self.funcstate.set_loop_labels(labels)
2330
+ def new_loop_labels(self, *args): return self.funcstate.new_loop_labels(*args)
2331
+ def get_all_labels(self): return self.funcstate.get_all_labels()
2332
+ def set_all_labels(self, labels): return self.funcstate.set_all_labels(labels)
2333
+ def all_new_labels(self): return self.funcstate.all_new_labels()
2334
+ def use_label(self, lbl): return self.funcstate.use_label(lbl)
2335
+ def label_used(self, lbl): return self.funcstate.label_used(lbl)
2336
+
2337
+
2338
+ def enter_cfunc_scope(self, scope):
2339
+ self.funcstate = FunctionState(self, scope=scope)
2340
+
2341
+ def exit_cfunc_scope(self):
2342
+ if self.funcstate is None:
2343
+ return
2344
+ self.funcstate.validate_exit()
2345
+ self.funcstate = None
2346
+
2347
+ def start_initcfunc(self, signature, scope=None, refnanny=False):
2348
+ """
2349
+ Init code helper function to start a cfunc scope and generate
2350
+ the prototype and function header ("static SIG {") of the function.
2351
+ """
2352
+ proto = self.globalstate.parts['initfunc_declarations']
2353
+ proto.putln(f"static CYTHON_SMALL_CODE {signature}; /*proto*/")
2354
+ self.enter_cfunc_scope(scope)
2355
+ self.putln("")
2356
+ self.putln(f"static {signature} {{")
2357
+ if refnanny:
2358
+ self.put_declare_refcount_context()
2359
+
2360
+ def start_slotfunc(self, class_scope, return_type, c_slot_name, args_signature, needs_funcstate=True, needs_prototype=False):
2361
+ # Slot functions currently live in the class scope as they don't have direct access to the module state.
2362
+ slotfunc_cname = class_scope.mangle_internal(c_slot_name)
2363
+ declaration = f"static {return_type.declaration_code(slotfunc_cname)}({args_signature})"
2364
+
2365
+ if needs_prototype:
2366
+ self.globalstate['decls'].putln(declaration.replace("CYTHON_UNUSED ", "") + "; /*proto*/")
2367
+ if needs_funcstate:
2368
+ self.enter_cfunc_scope(class_scope)
2369
+ self.putln("")
2370
+ self.putln(declaration + " {")
2371
+
2372
+ # constant handling
2373
+
2374
+ def get_py_int(self, str_value, longness):
2375
+ return self.name_in_module_state(
2376
+ self.globalstate.get_int_const(str_value, longness).cname
2377
+ )
2378
+
2379
+ def get_py_float(self, str_value, value_code):
2380
+ return self.name_in_module_state(
2381
+ self.globalstate.get_float_const(str_value, value_code).cname
2382
+ )
2383
+
2384
+ def get_py_const(self, prefix, dedup_key=None):
2385
+ return self.name_in_module_state(
2386
+ self.globalstate.get_py_const(prefix, dedup_key)
2387
+ )
2388
+
2389
+ def get_string_const(self, text):
2390
+ return self.globalstate.get_string_const(text).cname
2391
+
2392
+ def get_pyunicode_ptr_const(self, text):
2393
+ return self.globalstate.get_pyunicode_ptr_const(text)
2394
+
2395
+ def get_py_string_const(self, text, identifier=None):
2396
+ cname = self.globalstate.get_py_string_const(
2397
+ text, identifier).cname
2398
+ return self.name_in_module_state(cname)
2399
+
2400
+ def get_py_codeobj_const(self, node):
2401
+ return self.name_in_module_state(self.globalstate.get_py_codeobj_const(node))
2402
+
2403
+ def get_argument_default_const(self, type):
2404
+ return self.name_in_module_state(self.globalstate.get_argument_default_const(type).cname)
2405
+
2406
+ def intern(self, text):
2407
+ return self.get_py_string_const(text)
2408
+
2409
+ def intern_identifier(self, text):
2410
+ return self.get_py_string_const(text, identifier=True)
2411
+
2412
+ def get_cached_constants_writer(self, target=None):
2413
+ return self.globalstate.get_cached_constants_writer(target)
2414
+
2415
+ def name_in_module_state(self, cname):
2416
+ if self.funcstate.scope is None:
2417
+ # This is a mess. For example, within the codeobj generation
2418
+ # funcstate.scope is None while evaluating the strings, but not while
2419
+ # evaluating the code objects themselves. Right now it doesn't matter
2420
+ # because it all ends up going to the same place, but to actually turn
2421
+ # it into something useful this mess will need to be fixed.
2422
+ return self.name_in_main_c_code_module_state(cname)
2423
+ return self.funcstate.scope.name_in_module_state(cname)
2424
+
2425
+ @staticmethod
2426
+ def name_in_main_c_code_module_state(cname):
2427
+ # The functions where this applies to have the modulestate passed
2428
+ # as an argument to them and so it's better use that argument than
2429
+ # to try to get it from a global variable.
2430
+ return f"{Naming.modulestatevalue_cname}->{cname}"
2431
+
2432
+ @staticmethod
2433
+ def name_in_slot_module_state(cname):
2434
+ # TODO - eventually this will go through PyType_GetModuleByDef
2435
+ # in cases where it's supported.
2436
+ return f"{Naming.modulestateglobal_cname}->{cname}"
2437
+
2438
+ def namespace_cname_in_module_state(self, scope):
2439
+ if scope.is_py_class_scope:
2440
+ return scope.namespace_cname
2441
+ else:
2442
+ return self.name_in_module_state(scope.namespace_cname)
2443
+
2444
+ def typeptr_cname_in_module_state(self, type):
2445
+ if type.is_extension_type:
2446
+ return self.name_in_module_state(type.typeptr_cname)
2447
+ else:
2448
+ return type.typeptr_cname
2449
+
2450
+ # code generation
2451
+
2452
+ def putln(self, code="", safe=False):
2453
+ if self.last_pos and self.bol:
2454
+ self.emit_marker()
2455
+ if self.code_config.emit_linenums and self.last_marked_pos:
2456
+ source_desc, line, _ = self.last_marked_pos
2457
+ self._write_lines(f'\n#line {line} "{source_desc.get_escaped_description()}"\n')
2458
+ if code:
2459
+ if safe:
2460
+ self.put_safe(code)
2461
+ else:
2462
+ self.put(code)
2463
+ self._write_lines("\n")
2464
+ self.bol = 1
2465
+
2466
+ def mark_pos(self, pos, trace=True):
2467
+ if pos is None:
2468
+ return
2469
+ if self.last_marked_pos and self.last_marked_pos[:2] == pos[:2]:
2470
+ return
2471
+ self.last_pos = (pos, trace)
2472
+
2473
+ def emit_marker(self):
2474
+ pos, trace = self.last_pos
2475
+ self.last_marked_pos = pos
2476
+ self.last_pos = None
2477
+ self._write_lines("\n")
2478
+ if self.code_config.emit_code_comments:
2479
+ self.indent()
2480
+ self._write_lines(self._build_marker(pos))
2481
+ if trace:
2482
+ self.write_trace_line(pos)
2483
+
2484
+ def write_trace_line(self, pos):
2485
+ if self.funcstate and self.funcstate.can_trace and self.globalstate.directives['linetrace']:
2486
+ self.indent()
2487
+ self._write_lines(
2488
+ f'__Pyx_TraceLine({pos[1]:d},{self.pos_to_offset(pos):d},{not self.funcstate.gil_owned:d},{self.error_goto(pos)})\n')
2489
+
2490
+ def _build_marker(self, pos):
2491
+ source_desc, line, col = pos
2492
+ assert isinstance(source_desc, SourceDescriptor)
2493
+ contents = self.globalstate.commented_file_contents(source_desc)
2494
+ lines = contents[max(0, line-3):line] # line numbers start at 1
2495
+ lines[-1] += ' # <<<<<<<<<<<<<<'
2496
+ lines += contents[line:line+2]
2497
+ code = "\n".join(lines)
2498
+ return f'/* "{source_desc.get_escaped_description()}":{line:d}\n{code}\n*/\n'
2499
+
2500
+ def put_safe(self, code):
2501
+ # put code, but ignore {}
2502
+ self.write(code)
2503
+ self.bol = 0
2504
+
2505
+ def put_or_include(self, code, name):
2506
+ include_dir = self.globalstate.common_utility_include_dir
2507
+ if include_dir and len(code) > 1024:
2508
+ hash = hashlib.sha256(code.encode('utf8')).hexdigest()
2509
+ include_file = f"{name}_{hash}.h"
2510
+ path = os.path.join(include_dir, include_file)
2511
+ if not os.path.exists(path):
2512
+ tmp_path = f'{path}.tmp{os.getpid()}'
2513
+ done = False
2514
+ try:
2515
+ with Utils.open_new_file(tmp_path) as f:
2516
+ f.write(code)
2517
+ shutil.move(tmp_path, path)
2518
+ done = True
2519
+ except (FileExistsError, PermissionError):
2520
+ # If a different process created the file faster than us,
2521
+ # renaming can fail on Windows. It's ok if the file is there now.
2522
+ if not os.path.exists(path):
2523
+ raise
2524
+ finally:
2525
+ if not done and os.path.exists(tmp_path):
2526
+ os.unlink(tmp_path)
2527
+ # We use forward slashes in the include path to assure identical code generation
2528
+ # under Windows and Posix. C/C++ compilers should still understand it.
2529
+ c_path = path.replace('\\', '/')
2530
+ code = f'#include "{c_path}"\n'
2531
+ self.put(code)
2532
+
2533
+ def put(self, code):
2534
+ fix_indent = False
2535
+ if "{" in code:
2536
+ dl = code.count("{")
2537
+ else:
2538
+ dl = 0
2539
+ if "}" in code:
2540
+ dl -= code.count("}")
2541
+ if dl < 0:
2542
+ self.level += dl
2543
+ elif dl == 0 and code[0] == "}":
2544
+ # special cases like "} else {" need a temporary dedent
2545
+ fix_indent = True
2546
+ self.level -= 1
2547
+ if self.bol:
2548
+ self.indent()
2549
+ self.write(code)
2550
+ self.bol = 0
2551
+ if dl > 0:
2552
+ self.level += dl
2553
+ elif fix_indent:
2554
+ self.level += 1
2555
+
2556
+ def put_code_here(self, utility: UtilityCode):
2557
+ # Puts the impl section of the utility code directly to the current position.
2558
+ # Ensure we don't have a proto section (but do allow init and cleanup sections
2559
+ # because they might be useful in future).
2560
+ assert not utility.proto, utility.name
2561
+ utility._put_code_section(self, self.globalstate, "impl")
2562
+ utility._put_init_code_section(self.globalstate)
2563
+ if utility.cleanup and Options.generate_cleanup_code:
2564
+ utility._put_code_section(
2565
+ self.globalstate['cleanup_globals'], self.globalstate, "cleanup")
2566
+
2567
+ def increase_indent(self):
2568
+ self.level += 1
2569
+
2570
+ def decrease_indent(self):
2571
+ self.level -= 1
2572
+
2573
+ def begin_block(self):
2574
+ self.putln("{")
2575
+ self.increase_indent()
2576
+
2577
+ def end_block(self):
2578
+ self.decrease_indent()
2579
+ self.putln("}")
2580
+
2581
+ def indent(self):
2582
+ self._write_to_buffer(" " * self.level)
2583
+
2584
+ def get_py_version_hex(self, pyversion):
2585
+ return "0x%02X%02X%02X%02X" % (tuple(pyversion) + (0,0,0,0))[:4]
2586
+
2587
+ def put_label(self, lbl):
2588
+ if lbl in self.funcstate.labels_used:
2589
+ self.putln("%s:;" % lbl)
2590
+
2591
+ def put_goto(self, lbl):
2592
+ self.funcstate.use_label(lbl)
2593
+ self.putln("goto %s;" % lbl)
2594
+
2595
+ def put_var_declaration(self, entry, storage_class="",
2596
+ dll_linkage=None, definition=True):
2597
+ #print "Code.put_var_declaration:", entry.name, "definition =", definition ###
2598
+ if entry.visibility == 'private' and not (definition or entry.defined_in_pxd):
2599
+ #print "...private and not definition, skipping", entry.cname ###
2600
+ return
2601
+ if entry.visibility == "private" and not entry.used:
2602
+ #print "...private and not used, skipping", entry.cname ###
2603
+ return
2604
+ if not entry.cf_used:
2605
+ self.put('CYTHON_UNUSED ')
2606
+ if storage_class:
2607
+ self.put("%s " % storage_class)
2608
+ if entry.is_cpp_optional:
2609
+ self.put(entry.type.cpp_optional_declaration_code(
2610
+ entry.cname, dll_linkage=dll_linkage))
2611
+ else:
2612
+ self.put(entry.type.declaration_code(
2613
+ entry.cname, dll_linkage=dll_linkage))
2614
+ if entry.init is not None:
2615
+ self.put_safe(" = %s" % entry.type.literal_code(entry.init))
2616
+ elif entry.type.is_pyobject:
2617
+ self.put(" = NULL")
2618
+ self.putln(";")
2619
+ self.funcstate.scope.use_entry_utility_code(entry)
2620
+
2621
+ def put_temp_declarations(self, func_context):
2622
+ for name, type, manage_ref, static in func_context.temps_allocated:
2623
+ if type.is_cpp_class and not type.is_fake_reference and func_context.scope.directives['cpp_locals']:
2624
+ decl = type.cpp_optional_declaration_code(name)
2625
+ else:
2626
+ decl = type.declaration_code(name)
2627
+ if type.is_pyobject:
2628
+ self.putln("%s = NULL;" % decl)
2629
+ elif type.is_memoryviewslice:
2630
+ self.putln("%s = %s;" % (decl, type.literal_code(type.default_value)))
2631
+ else:
2632
+ self.putln("%s%s;" % (static and "static " or "", decl))
2633
+
2634
+ if func_context.should_declare_error_indicator:
2635
+ if self.funcstate.uses_error_indicator:
2636
+ unused = ''
2637
+ else:
2638
+ unused = 'CYTHON_UNUSED '
2639
+ # Initialize these variables to silence compiler warnings
2640
+ self.putln("%sint %s = 0;" % (unused, Naming.lineno_cname))
2641
+ self.putln("%sconst char *%s = NULL;" % (unused, Naming.filename_cname))
2642
+ self.putln("%sint %s = 0;" % (unused, Naming.clineno_cname))
2643
+
2644
+ def put_generated_by(self):
2645
+ self.putln(Utils.GENERATED_BY_MARKER)
2646
+ self.putln("")
2647
+
2648
+ def put_h_guard(self, guard):
2649
+ self.putln("#ifndef %s" % guard)
2650
+ self.putln("#define %s" % guard)
2651
+
2652
+ def unlikely(self, cond):
2653
+ if Options.gcc_branch_hints:
2654
+ return 'unlikely(%s)' % cond
2655
+ else:
2656
+ return cond
2657
+
2658
+ def build_function_modifiers(self, modifiers, mapper=modifier_output_mapper):
2659
+ if not modifiers:
2660
+ return ''
2661
+ return '%s ' % ' '.join([mapper(m,m) for m in modifiers])
2662
+
2663
+ # Python objects and reference counting
2664
+
2665
+ def entry_as_pyobject(self, entry):
2666
+ type = entry.type
2667
+ if (not entry.is_self_arg and not entry.type.is_complete()
2668
+ or entry.type.is_extension_type):
2669
+ return "(PyObject *)" + entry.cname
2670
+ else:
2671
+ return entry.cname
2672
+
2673
+ def as_pyobject(self, cname, type):
2674
+ from .PyrexTypes import py_object_type, typecast
2675
+ return typecast(py_object_type, type, cname)
2676
+
2677
+ def put_gotref(self, cname, type):
2678
+ type.generate_gotref(self, cname)
2679
+
2680
+ def put_giveref(self, cname, type):
2681
+ type.generate_giveref(self, cname)
2682
+
2683
+ def put_xgiveref(self, cname, type):
2684
+ type.generate_xgiveref(self, cname)
2685
+
2686
+ def put_xgotref(self, cname, type):
2687
+ type.generate_xgotref(self, cname)
2688
+
2689
+ def put_incref(self, cname, type, nanny=True):
2690
+ # Note: original put_Memslice_Incref/Decref also added in some utility code
2691
+ # this is unnecessary since the relevant utility code is loaded anyway if a memoryview is used
2692
+ # and so has been removed. However, it's potentially a feature that might be useful here
2693
+ type.generate_incref(self, cname, nanny=nanny)
2694
+
2695
+ def put_xincref(self, cname, type, nanny=True):
2696
+ type.generate_xincref(self, cname, nanny=nanny)
2697
+
2698
+ def put_decref(self, cname, type, nanny=True, have_gil=True):
2699
+ type.generate_decref(self, cname, nanny=nanny, have_gil=have_gil)
2700
+
2701
+ def put_xdecref(self, cname, type, nanny=True, have_gil=True):
2702
+ type.generate_xdecref(self, cname, nanny=nanny, have_gil=have_gil)
2703
+
2704
+ def put_decref_clear(self, cname, type, clear_before_decref=False, nanny=True, have_gil=True):
2705
+ type.generate_decref_clear(self, cname, clear_before_decref=clear_before_decref,
2706
+ nanny=nanny, have_gil=have_gil)
2707
+
2708
+ def put_xdecref_clear(self, cname, type, clear_before_decref=False, nanny=True, have_gil=True):
2709
+ type.generate_xdecref_clear(self, cname, clear_before_decref=clear_before_decref,
2710
+ nanny=nanny, have_gil=have_gil)
2711
+
2712
+ def put_decref_set(self, cname, type, rhs_cname):
2713
+ type.generate_decref_set(self, cname, rhs_cname)
2714
+
2715
+ def put_xdecref_set(self, cname, type, rhs_cname):
2716
+ type.generate_xdecref_set(self, cname, rhs_cname)
2717
+
2718
+ def put_incref_memoryviewslice(self, slice_cname, type, have_gil):
2719
+ # TODO ideally this would just be merged into "put_incref"
2720
+ type.generate_incref_memoryviewslice(self, slice_cname, have_gil=have_gil)
2721
+
2722
+ def put_var_incref_memoryviewslice(self, entry, have_gil):
2723
+ self.put_incref_memoryviewslice(entry.cname, entry.type, have_gil=have_gil)
2724
+
2725
+ def put_var_gotref(self, entry):
2726
+ self.put_gotref(entry.cname, entry.type)
2727
+
2728
+ def put_var_giveref(self, entry):
2729
+ self.put_giveref(entry.cname, entry.type)
2730
+
2731
+ def put_var_xgotref(self, entry):
2732
+ self.put_xgotref(entry.cname, entry.type)
2733
+
2734
+ def put_var_xgiveref(self, entry):
2735
+ self.put_xgiveref(entry.cname, entry.type)
2736
+
2737
+ def put_var_incref(self, entry, **kwds):
2738
+ self.put_incref(entry.cname, entry.type, **kwds)
2739
+
2740
+ def put_var_xincref(self, entry, **kwds):
2741
+ self.put_xincref(entry.cname, entry.type, **kwds)
2742
+
2743
+ def put_var_decref(self, entry, **kwds):
2744
+ self.put_decref(entry.cname, entry.type, **kwds)
2745
+
2746
+ def put_var_xdecref(self, entry, **kwds):
2747
+ self.put_xdecref(entry.cname, entry.type, **kwds)
2748
+
2749
+ def put_var_decref_clear(self, entry, **kwds):
2750
+ self.put_decref_clear(entry.cname, entry.type, clear_before_decref=entry.in_closure, **kwds)
2751
+
2752
+ def put_var_decref_set(self, entry, rhs_cname, **kwds):
2753
+ self.put_decref_set(entry.cname, entry.type, rhs_cname, **kwds)
2754
+
2755
+ def put_var_xdecref_set(self, entry, rhs_cname, **kwds):
2756
+ self.put_xdecref_set(entry.cname, entry.type, rhs_cname, **kwds)
2757
+
2758
+ def put_var_xdecref_clear(self, entry, **kwds):
2759
+ self.put_xdecref_clear(entry.cname, entry.type, clear_before_decref=entry.in_closure, **kwds)
2760
+
2761
+ def put_var_decrefs(self, entries, used_only = 0):
2762
+ for entry in entries:
2763
+ if not used_only or entry.used:
2764
+ if entry.xdecref_cleanup:
2765
+ self.put_var_xdecref(entry)
2766
+ else:
2767
+ self.put_var_decref(entry)
2768
+
2769
+ def put_var_xdecrefs(self, entries):
2770
+ for entry in entries:
2771
+ self.put_var_xdecref(entry)
2772
+
2773
+ def put_var_xdecrefs_clear(self, entries):
2774
+ for entry in entries:
2775
+ self.put_var_xdecref_clear(entry)
2776
+
2777
+ def put_init_to_py_none(self, cname, type, nanny=True):
2778
+ from .PyrexTypes import py_object_type, typecast
2779
+ py_none = typecast(type, py_object_type, "Py_None")
2780
+ if nanny:
2781
+ self.putln("%s = %s; __Pyx_INCREF(Py_None);" % (cname, py_none))
2782
+ else:
2783
+ self.putln("%s = %s; Py_INCREF(Py_None);" % (cname, py_none))
2784
+
2785
+ def put_init_var_to_py_none(self, entry, template = "%s", nanny=True):
2786
+ code = template % entry.cname
2787
+ #if entry.type.is_extension_type:
2788
+ # code = "((PyObject*)%s)" % code
2789
+ self.put_init_to_py_none(code, entry.type, nanny)
2790
+ if entry.in_closure:
2791
+ self.put_giveref('Py_None')
2792
+
2793
+ def put_pymethoddef(self, entry, term, allow_skip=True, wrapper_code_writer=None):
2794
+ is_number_slot = False
2795
+ if entry.is_special or entry.name == '__getattribute__':
2796
+ from . import TypeSlots
2797
+ if entry.name not in special_py_methods:
2798
+ if TypeSlots.is_binop_number_slot(entry.name):
2799
+ # It's useful if numeric binops are created with meth coexist
2800
+ # so they can be called directly by looking up the name, skipping the
2801
+ # dispatch wrapper that enables the reverse slots. This is most useful
2802
+ # when c_api_binop_methods is False, but there's no reason not to do it
2803
+ # all the time
2804
+ is_number_slot = True
2805
+ elif entry.name == '__getattr__' and not self.globalstate.directives['fast_getattr']:
2806
+ pass
2807
+ # Python's typeobject.c will automatically fill in our slot
2808
+ # in add_operators() (called by PyType_Ready) with a value
2809
+ # that's better than ours.
2810
+ elif allow_skip:
2811
+ return
2812
+
2813
+ method_flags = entry.signature.method_flags()
2814
+ if not method_flags:
2815
+ return
2816
+ if entry.is_special:
2817
+ method_flags += [TypeSlots.method_coexist]
2818
+ func_ptr = wrapper_code_writer.put_pymethoddef_wrapper(entry) if wrapper_code_writer else entry.func_cname
2819
+ # Add required casts, but try not to shadow real warnings.
2820
+ cast = entry.signature.method_function_type()
2821
+ if cast != 'PyCFunction':
2822
+ func_ptr = '(void(*)(void))(%s)%s' % (cast, func_ptr)
2823
+ entry_name = entry.name.as_c_string_literal()
2824
+ if is_number_slot:
2825
+ # Unlike most special functions, binop numeric operator slots are actually generated here
2826
+ # (to ensure that they can be looked up). However, they're sometimes guarded by the preprocessor
2827
+ # so a bit of extra logic is needed
2828
+ slot = TypeSlots.get_slot_table(self.globalstate.directives).get_slot_by_method_name(entry.name)
2829
+ preproc_guard = slot.preprocessor_guard_code()
2830
+ if preproc_guard:
2831
+ self.putln(preproc_guard)
2832
+ self.putln(
2833
+ '{%s, (PyCFunction)%s, %s, %s}%s' % (
2834
+ entry_name,
2835
+ func_ptr,
2836
+ "|".join(method_flags),
2837
+ entry.doc_cname if entry.doc else '0',
2838
+ term))
2839
+ if is_number_slot and preproc_guard:
2840
+ self.putln("#endif")
2841
+
2842
+ def put_pymethoddef_wrapper(self, entry):
2843
+ func_cname = entry.func_cname
2844
+ if entry.is_special:
2845
+ method_flags = entry.signature.method_flags() or []
2846
+ from .TypeSlots import method_noargs
2847
+ if method_noargs in method_flags:
2848
+ # Special NOARGS methods really take no arguments besides 'self', but PyCFunction expects one.
2849
+ func_cname = Naming.method_wrapper_prefix + func_cname
2850
+ self.putln("static PyObject *%s(PyObject *self, CYTHON_UNUSED PyObject *arg) {" % func_cname)
2851
+ func_call = "%s(self)" % entry.func_cname
2852
+ if entry.name == "__next__":
2853
+ self.putln("PyObject *res = %s;" % func_call)
2854
+ # tp_iternext can return NULL without an exception
2855
+ self.putln("if (!res && !PyErr_Occurred()) { PyErr_SetNone(PyExc_StopIteration); }")
2856
+ self.putln("return res;")
2857
+ else:
2858
+ self.putln("return %s;" % func_call)
2859
+ self.putln("}")
2860
+ return func_cname
2861
+
2862
+ # GIL methods
2863
+
2864
+ def use_fast_gil_utility_code(self):
2865
+ if self.globalstate.directives['fast_gil']:
2866
+ self.globalstate.use_utility_code(UtilityCode.load_cached("FastGil", "ModuleSetupCode.c"))
2867
+ else:
2868
+ self.globalstate.use_utility_code(UtilityCode.load_cached("NoFastGil", "ModuleSetupCode.c"))
2869
+
2870
+ def put_ensure_gil(self, declare_gilstate=True, variable=None):
2871
+ """
2872
+ Acquire the GIL. The generated code is safe even when no PyThreadState
2873
+ has been allocated for this thread (for threads not initialized by
2874
+ using the Python API). Additionally, the code generated by this method
2875
+ may be called recursively.
2876
+ """
2877
+ if self.globalstate.directives['subinterpreters_compatible'] != 'no':
2878
+ from .Errors import warning
2879
+ warning(
2880
+ self.last_marked_pos,
2881
+ "Acquiring the GIL is currently very unlikely to work correctly with subinterpreters.",
2882
+ 2
2883
+ )
2884
+ self.globalstate.use_utility_code(
2885
+ UtilityCode.load_cached("ForceInitThreads", "ModuleSetupCode.c"))
2886
+ self.use_fast_gil_utility_code()
2887
+ if not variable:
2888
+ variable = '__pyx_gilstate_save'
2889
+ if declare_gilstate:
2890
+ self.put("PyGILState_STATE ")
2891
+ self.putln("%s = __Pyx_PyGILState_Ensure();" % variable)
2892
+
2893
+ def put_release_ensured_gil(self, variable=None):
2894
+ """
2895
+ Releases the GIL, corresponds to `put_ensure_gil`.
2896
+ """
2897
+ self.use_fast_gil_utility_code()
2898
+ if not variable:
2899
+ variable = '__pyx_gilstate_save'
2900
+ self.putln("__Pyx_PyGILState_Release(%s);" % variable)
2901
+
2902
+ def put_acquire_freethreading_lock(self):
2903
+ self.putln("#if CYTHON_COMPILING_IN_CPYTHON_FREETHREADING")
2904
+ self.putln(f"PyMutex_Lock(&{Naming.parallel_freethreading_mutex});")
2905
+ self.putln("#endif")
2906
+
2907
+ def put_release_freethreading_lock(self):
2908
+ self.putln("#if CYTHON_COMPILING_IN_CPYTHON_FREETHREADING")
2909
+ self.putln(f"PyMutex_Unlock(&{Naming.parallel_freethreading_mutex});")
2910
+ self.putln("#endif")
2911
+
2912
+ def put_acquire_gil(self, variable=None, unknown_gil_state=True):
2913
+ """
2914
+ Acquire the GIL. The thread's thread state must have been initialized
2915
+ by a previous `put_release_gil`
2916
+ """
2917
+ self.use_fast_gil_utility_code()
2918
+ self.putln("__Pyx_FastGIL_Forget();")
2919
+ if variable:
2920
+ self.putln('_save = %s;' % variable)
2921
+ if unknown_gil_state:
2922
+ self.putln("if (_save) {")
2923
+ self.putln("Py_BLOCK_THREADS")
2924
+ if unknown_gil_state:
2925
+ self.putln("}")
2926
+ self.putln("#if CYTHON_COMPILING_IN_LIMITED_API")
2927
+ self.put_release_ensured_gil()
2928
+ self.putln("#endif")
2929
+
2930
+ def put_release_gil(self, variable=None, unknown_gil_state=True):
2931
+ "Release the GIL, corresponds to `put_acquire_gil`."
2932
+ self.use_fast_gil_utility_code()
2933
+ self.putln("PyThreadState *_save;")
2934
+ self.putln("_save = NULL;")
2935
+ if unknown_gil_state:
2936
+ # We don't *know* that we don't have the GIL (since we may be inside a nogil function,
2937
+ # and Py_UNBLOCK_THREADS is unsafe without the GIL).
2938
+ self.putln("#if CYTHON_COMPILING_IN_LIMITED_API")
2939
+ # In the Limited API we can't check whether we have the GIL.
2940
+ # Therefore the only way to be sure that we can release it is to acquire it first.
2941
+ self.put_ensure_gil()
2942
+ self.putln("#else")
2943
+ self.putln("if (PyGILState_Check())")
2944
+ self.putln("#endif")
2945
+ self.putln("{")
2946
+ self.putln("Py_UNBLOCK_THREADS")
2947
+ if unknown_gil_state:
2948
+ self.putln("}")
2949
+ if variable:
2950
+ self.putln('%s = _save;' % variable)
2951
+ self.putln("__Pyx_FastGIL_Remember();")
2952
+
2953
+ def declare_gilstate(self):
2954
+ self.putln("PyGILState_STATE __pyx_gilstate_save;")
2955
+
2956
+ # error handling
2957
+
2958
+ def put_error_if_neg(self, pos, value):
2959
+ # TODO this path is almost _never_ taken, yet this macro makes is slower!
2960
+ # return self.putln("if (unlikely(%s < 0)) %s" % (value, self.error_goto(pos)))
2961
+ return self.putln("if (%s < 0) %s" % (value, self.error_goto(pos)))
2962
+
2963
+ def put_error_if_unbound(self, pos, entry, in_nogil_context=False, unbound_check_code=None):
2964
+ nogil_tag = "Nogil" if in_nogil_context else ""
2965
+ if entry.from_closure:
2966
+ func = "RaiseClosureNameError"
2967
+ elif entry.type.is_cpp_class and entry.is_cglobal:
2968
+ func = "RaiseCppGlobalNameError"
2969
+ elif entry.type.is_cpp_class and entry.is_variable and not entry.is_member and entry.scope.is_c_class_scope:
2970
+ # there doesn't seem to be a good way to detecting an instance-attribute of a C class
2971
+ # (is_member is only set for class attributes)
2972
+ func = "RaiseCppAttributeError"
2973
+ else:
2974
+ func = "RaiseUnboundLocalError"
2975
+
2976
+ self.globalstate.use_utility_code(
2977
+ UtilityCode.load_cached(f"{func}{nogil_tag}", "ObjectHandling.c"))
2978
+
2979
+ if not unbound_check_code:
2980
+ unbound_check_code = entry.type.check_for_null_code(entry.cname)
2981
+ self.putln('if (unlikely(!%s)) { %s(%s); %s }' % (
2982
+ unbound_check_code,
2983
+ f"__Pyx_{func}{nogil_tag}",
2984
+ entry.name.as_c_string_literal(),
2985
+ self.error_goto(pos)))
2986
+
2987
+ def set_error_info(self, pos, used=False):
2988
+ self.funcstate.should_declare_error_indicator = True
2989
+ if used:
2990
+ self.funcstate.uses_error_indicator = True
2991
+ return "__PYX_MARK_ERR_POS(%s, %s)" % (
2992
+ self.lookup_filename(pos[0]),
2993
+ pos[1])
2994
+
2995
+ def error_goto(self, pos, used=True):
2996
+ lbl = self.funcstate.error_label
2997
+ self.funcstate.use_label(lbl)
2998
+ if pos is None:
2999
+ return 'goto %s;' % lbl
3000
+ self.funcstate.should_declare_error_indicator = True
3001
+ if used:
3002
+ self.funcstate.uses_error_indicator = True
3003
+ return "__PYX_ERR(%s, %s, %s)" % (
3004
+ self.lookup_filename(pos[0]),
3005
+ pos[1],
3006
+ lbl)
3007
+
3008
+ def error_goto_if(self, cond, pos):
3009
+ return "if (%s) %s" % (self.unlikely(cond), self.error_goto(pos))
3010
+
3011
+ def error_goto_if_null(self, cname, pos):
3012
+ return self.error_goto_if("!%s" % cname, pos)
3013
+
3014
+ def error_goto_if_neg(self, cname, pos):
3015
+ # Add extra parentheses to silence clang warnings about constant conditions.
3016
+ return self.error_goto_if("(%s < 0)" % cname, pos)
3017
+
3018
+ def error_goto_if_PyErr(self, pos):
3019
+ return self.error_goto_if("PyErr_Occurred()", pos)
3020
+
3021
+ def lookup_filename(self, filename):
3022
+ return self.globalstate.lookup_filename(filename)
3023
+
3024
+ def put_declare_refcount_context(self):
3025
+ self.putln('__Pyx_RefNannyDeclarations')
3026
+
3027
+ def put_setup_refcount_context(self, name, acquire_gil=False):
3028
+ name = name.as_c_string_literal() # handle unicode names
3029
+ if acquire_gil:
3030
+ self.globalstate.use_utility_code(
3031
+ UtilityCode.load_cached("ForceInitThreads", "ModuleSetupCode.c"))
3032
+ self.putln('__Pyx_RefNannySetupContext(%s, %d);' % (name, acquire_gil and 1 or 0))
3033
+
3034
+ def put_finish_refcount_context(self, nogil=False):
3035
+ self.putln("__Pyx_RefNannyFinishContextNogil()" if nogil else "__Pyx_RefNannyFinishContext();")
3036
+
3037
+ def put_add_traceback(self, qualified_name, include_cline=True):
3038
+ """
3039
+ Build a Python traceback for propagating exceptions.
3040
+
3041
+ qualified_name should be the qualified name of the function.
3042
+ """
3043
+ qualified_name = qualified_name.as_c_string_literal() # handle unicode names
3044
+ format_tuple = (
3045
+ qualified_name,
3046
+ Naming.clineno_cname if include_cline else 0,
3047
+ Naming.lineno_cname,
3048
+ Naming.filename_cname,
3049
+ )
3050
+
3051
+ self.funcstate.uses_error_indicator = True
3052
+ self.putln('__Pyx_AddTraceback(%s, %s, %s, %s);' % format_tuple)
3053
+
3054
+ def put_unraisable(self, qualified_name, nogil=False):
3055
+ """
3056
+ Generate code to print a Python warning for an unraisable exception.
3057
+
3058
+ qualified_name should be the qualified name of the function.
3059
+ """
3060
+ format_tuple = (
3061
+ qualified_name,
3062
+ Naming.clineno_cname,
3063
+ Naming.lineno_cname,
3064
+ Naming.filename_cname,
3065
+ self.globalstate.directives['unraisable_tracebacks'],
3066
+ nogil,
3067
+ )
3068
+ self.funcstate.uses_error_indicator = True
3069
+ self.putln('__Pyx_WriteUnraisable("%s", %s, %s, %s, %d, %d);' % format_tuple)
3070
+ self.globalstate.use_utility_code(
3071
+ UtilityCode.load_cached("WriteUnraisableException", "Exceptions.c"))
3072
+
3073
+ def is_tracing(self):
3074
+ return self.globalstate.directives['profile'] or self.globalstate.directives['linetrace']
3075
+
3076
+ def pos_to_offset(self, pos):
3077
+ """
3078
+ Calculate a fake 'instruction offset' from a node position as 31 bit int (32 bit signed).
3079
+ """
3080
+ scope = self.funcstate.scope
3081
+ while scope and pos not in scope.node_positions_to_offset:
3082
+ scope = scope.parent_scope
3083
+ return scope.node_positions_to_offset[pos] if scope else 0
3084
+
3085
+ def put_trace_declarations(self, is_generator=False):
3086
+ self.putln('__Pyx_TraceDeclarationsGen' if is_generator else '__Pyx_TraceDeclarationsFunc')
3087
+
3088
+ def put_trace_frame_init(self, codeobj=None):
3089
+ if codeobj:
3090
+ self.putln('__Pyx_TraceFrameInit(%s)' % codeobj)
3091
+
3092
+ def put_trace_start(self, name, pos, nogil=False, is_generator=False, is_cpdef_func=False):
3093
+ trace_func = "__Pyx_TraceStartGen" if is_generator else "__Pyx_TraceStartFunc"
3094
+ self.putln(
3095
+ f'{trace_func}('
3096
+ f'{name.as_c_string_literal()}, '
3097
+ f'{Naming.filetable_cname}[{self.lookup_filename(pos[0])}], '
3098
+ f'{pos[1]}, '
3099
+ f'{self.pos_to_offset(pos):d}, '
3100
+ f'{nogil:d}, '
3101
+ f'{Naming.skip_dispatch_cname if is_cpdef_func else "0"}, '
3102
+ f'{self.error_goto(pos)}'
3103
+ ');'
3104
+ )
3105
+
3106
+ def put_trace_exit(self, nogil=False):
3107
+ self.putln(f"__Pyx_PyMonitoring_ExitScope({bool(nogil):d});")
3108
+
3109
+ def put_trace_yield(self, retvalue_cname, pos):
3110
+ error_goto = self.error_goto(pos)
3111
+ self.putln(f"__Pyx_TraceYield({retvalue_cname}, {self.pos_to_offset(pos)}, {error_goto});")
3112
+
3113
+ def put_trace_resume(self, pos):
3114
+ scope = self.funcstate.scope
3115
+ # pos[1] is probably not the first line, so try to find the first line of the generator function.
3116
+ first_line = scope.scope_class.pos[1] if scope.scope_class else pos[1]
3117
+ name = scope.name.as_c_string_literal()
3118
+ filename_index = self.lookup_filename(pos[0])
3119
+ error_goto = self.error_goto(pos)
3120
+ self.putln(
3121
+ '__Pyx_TraceResumeGen('
3122
+ f'{name}, '
3123
+ f'{Naming.filetable_cname}[{filename_index}], '
3124
+ f'{first_line}, '
3125
+ f'{self.pos_to_offset(pos)}, '
3126
+ f'{error_goto}'
3127
+ ');'
3128
+ )
3129
+
3130
+ def put_trace_exception(self, pos, reraise=False, fresh=False):
3131
+ self.putln(f"__Pyx_TraceException({self.pos_to_offset(pos)}, {bool(reraise):d}, {bool(fresh):d});")
3132
+
3133
+ def put_trace_exception_propagating(self):
3134
+ self.putln(f"__Pyx_TraceException({Naming.lineno_cname}, 0, 0);")
3135
+
3136
+ def put_trace_exception_handled(self, pos):
3137
+ self.putln(f"__Pyx_TraceExceptionHandled({self.pos_to_offset(pos)});")
3138
+
3139
+ def put_trace_unwind(self, pos, nogil=False):
3140
+ self.putln(f"__Pyx_TraceExceptionUnwind({self.pos_to_offset(pos)}, {bool(nogil):d});")
3141
+
3142
+ def put_trace_stopiteration(self, pos, value):
3143
+ error_goto = self.error_goto(pos)
3144
+ self.putln(f"__Pyx_TraceStopIteration({value}, {self.pos_to_offset(pos)}, {error_goto});")
3145
+
3146
+ def put_trace_return(self, retvalue_cname, pos, return_type=None, nogil=False):
3147
+ extra_arg = ""
3148
+ trace_func = "__Pyx_TraceReturnValue"
3149
+
3150
+ if return_type is None:
3151
+ pass
3152
+ elif return_type.is_pyobject:
3153
+ retvalue_cname = return_type.as_pyobject(retvalue_cname)
3154
+ elif return_type.is_void:
3155
+ retvalue_cname = 'Py_None'
3156
+ elif return_type.is_string:
3157
+ # We don't know if the C string is 0-terminated, but we cannot convert if it's not.
3158
+ retvalue_cname = 'Py_None'
3159
+ elif return_type.to_py_function:
3160
+ trace_func = "__Pyx_TraceReturnCValue"
3161
+ extra_arg = f", {return_type.to_py_function}"
3162
+ else:
3163
+ # We don't have a Python visible return value but we still need to report that we returned.
3164
+ # 'None' may not be a misleading (it's false, for one), but it's hopefully better than nothing.
3165
+ retvalue_cname = 'Py_None'
3166
+
3167
+ error_handling = self.error_goto(pos)
3168
+ self.putln(f"{trace_func}({retvalue_cname}{extra_arg}, {self.pos_to_offset(pos)}, {bool(nogil):d}, {error_handling});")
3169
+
3170
+ def put_cpp_placement_new(self, target,
3171
+ _utility_code=UtilityCode.load("DefaultPlacementNew", "CppSupport.cpp")):
3172
+ self.globalstate.use_utility_code(_utility_code)
3173
+ self.putln(f"__Pyx_default_placement_construct(&({target}));")
3174
+
3175
+ def putln_openmp(self, string):
3176
+ self.putln("#ifdef _OPENMP")
3177
+ self.putln(string)
3178
+ self.putln("#endif /* _OPENMP */")
3179
+
3180
+ def undef_builtin_expect(self, cond):
3181
+ """
3182
+ Redefine the macros likely() and unlikely to no-ops, depending on
3183
+ condition 'cond'
3184
+ """
3185
+ self.putln("#if %s" % cond)
3186
+ self.putln(" #undef likely")
3187
+ self.putln(" #undef unlikely")
3188
+ self.putln(" #define likely(x) (x)")
3189
+ self.putln(" #define unlikely(x) (x)")
3190
+ self.putln("#endif")
3191
+
3192
+ def redef_builtin_expect(self, cond):
3193
+ self.putln("#if %s" % cond)
3194
+ self.putln(" #undef likely")
3195
+ self.putln(" #undef unlikely")
3196
+ self.putln(" #define likely(x) __builtin_expect(!!(x), 1)")
3197
+ self.putln(" #define unlikely(x) __builtin_expect(!!(x), 0)")
3198
+ self.putln("#endif")
3199
+
3200
+
3201
+ class PyrexCodeWriter:
3202
+ # f file output file
3203
+ # level int indentation level
3204
+
3205
+ def __init__(self, outfile_name):
3206
+ self.f = Utils.open_new_file(outfile_name)
3207
+ self.level = 0
3208
+
3209
+ def putln(self, code):
3210
+ self.f.write("%s%s\n" % (" " * self.level, code))
3211
+
3212
+ def indent(self):
3213
+ self.level += 1
3214
+
3215
+ def dedent(self):
3216
+ self.level -= 1
3217
+
3218
+
3219
+ class PyxCodeWriter:
3220
+ """
3221
+ Can be used for writing out some Cython code.
3222
+ """
3223
+
3224
+ def __init__(self, buffer=None, indent_level=0, context=None, encoding='ascii'):
3225
+ self.buffer = buffer or StringIOTree()
3226
+ self.level = indent_level
3227
+ self.original_level = indent_level
3228
+ self.context = context
3229
+ self.encoding = encoding
3230
+ self._insertion_points = {}
3231
+
3232
+ def indent(self, levels=1):
3233
+ self.level += levels
3234
+ return True
3235
+
3236
+ def dedent(self, levels=1):
3237
+ self.level -= levels
3238
+
3239
+ @contextmanager
3240
+ def indenter(self, line):
3241
+ """
3242
+ with pyx_code.indenter("for i in range(10):"):
3243
+ pyx_code.putln("print i")
3244
+ """
3245
+ self.putln(line)
3246
+ self.indent()
3247
+ yield
3248
+ self.dedent()
3249
+
3250
+ def empty(self):
3251
+ return self.buffer.empty()
3252
+
3253
+ def getvalue(self):
3254
+ result = self.buffer.getvalue()
3255
+ if isinstance(result, bytes):
3256
+ result = result.decode(self.encoding)
3257
+ return result
3258
+
3259
+ def putln(self, line, context=None):
3260
+ if context is None:
3261
+ if self.context is not None:
3262
+ context = self.context
3263
+ if context is not None:
3264
+ line = sub_tempita(line, context)
3265
+ # Avoid indenting empty lines.
3266
+ self.buffer.write(f"{self.level * ' '}{line}\n" if line else "\n")
3267
+
3268
+ def put_chunk(self, chunk, context=None):
3269
+ if context is None:
3270
+ if self.context is not None:
3271
+ context = self.context
3272
+ if context is not None:
3273
+ chunk = sub_tempita(chunk, context)
3274
+
3275
+ chunk = _indent_chunk(chunk, self.level * 4)
3276
+ self.buffer.write(chunk)
3277
+
3278
+ def insertion_point(self):
3279
+ return type(self)(self.buffer.insertion_point(), self.level, self.context)
3280
+
3281
+ def reset(self):
3282
+ # resets the buffer so that nothing gets written. Most useful
3283
+ # for abandoning all work in a specific insertion point
3284
+ self.buffer.reset()
3285
+ self.level = self.original_level
3286
+
3287
+ def named_insertion_point(self, name):
3288
+ self._insertion_points[name] = self.insertion_point()
3289
+
3290
+ def __getitem__(self, name):
3291
+ return self._insertion_points[name]
3292
+
3293
+
3294
+ @cython.final
3295
+ @cython.ccall
3296
+ def _indent_chunk(chunk: str, indentation_length: cython.int) -> str:
3297
+ """Normalise leading space to the intended indentation and strip empty lines.
3298
+ """
3299
+ assert '\t' not in chunk
3300
+ lines = chunk.splitlines(keepends=True)
3301
+ if not lines:
3302
+ return chunk
3303
+ last_line = lines[-1].rstrip(' ')
3304
+ if last_line:
3305
+ lines[-1] = last_line
3306
+ else:
3307
+ del lines[-1]
3308
+ if not lines:
3309
+ return '\n'
3310
+
3311
+ # Count minimal (non-empty) indentation and strip empty lines.
3312
+ min_indentation: cython.int = len(chunk) + 1
3313
+ line_indentation: cython.int
3314
+ line: str
3315
+ i: cython.int
3316
+ for i, line in enumerate(lines):
3317
+ line_indentation = _count_indentation(line)
3318
+ if line_indentation + 1 == len(line):
3319
+ lines[i] = '\n'
3320
+ elif line_indentation < min_indentation:
3321
+ min_indentation = line_indentation
3322
+
3323
+ if min_indentation > len(chunk):
3324
+ # All empty lines.
3325
+ min_indentation = 0
3326
+
3327
+ if min_indentation < indentation_length:
3328
+ add_indent = ' ' * (indentation_length - min_indentation)
3329
+ lines = [
3330
+ add_indent + line if line != '\n' else '\n'
3331
+ for line in lines
3332
+ ]
3333
+ elif min_indentation > indentation_length:
3334
+ start: cython.int = min_indentation - indentation_length
3335
+ lines = [
3336
+ line[start:] if line != '\n' else '\n'
3337
+ for line in lines
3338
+ ]
3339
+
3340
+ return ''.join(lines)
3341
+
3342
+
3343
+ @cython.exceptval(-1)
3344
+ @cython.cfunc
3345
+ def _count_indentation(s: str) -> cython.int:
3346
+ i: cython.int = 0
3347
+ ch: cython.Py_UCS4
3348
+ for i, ch in enumerate(s):
3349
+ if ch != ' ':
3350
+ break
3351
+ return i
3352
+
3353
+
3354
+ class ClosureTempAllocator:
3355
+ def __init__(self, klass):
3356
+ self.klass = klass
3357
+ self.temps_allocated = {}
3358
+ self.temps_free = {}
3359
+ self.temps_count = 0
3360
+
3361
+ def reset(self):
3362
+ for type, cnames in self.temps_allocated.items():
3363
+ self.temps_free[type] = list(cnames)
3364
+
3365
+ def allocate_temp(self, type):
3366
+ if type not in self.temps_allocated:
3367
+ self.temps_allocated[type] = []
3368
+ self.temps_free[type] = []
3369
+ elif self.temps_free[type]:
3370
+ return self.temps_free[type].pop(0)
3371
+ cname = '%s%d' % (Naming.codewriter_temp_prefix, self.temps_count)
3372
+ self.klass.declare_var(pos=None, name=cname, cname=cname, type=type, is_cdef=True)
3373
+ self.temps_allocated[type].append(cname)
3374
+ self.temps_count += 1
3375
+ return cname