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