Cython 3.2.0__cp39-abi3-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (333) hide show
  1. Cython/Build/BuildExecutable.py +169 -0
  2. Cython/Build/Cache.py +199 -0
  3. Cython/Build/Cythonize.py +350 -0
  4. Cython/Build/Dependencies.py +1314 -0
  5. Cython/Build/Distutils.py +1 -0
  6. Cython/Build/Inline.py +463 -0
  7. Cython/Build/IpythonMagic.py +560 -0
  8. Cython/Build/SharedModule.py +94 -0
  9. Cython/Build/Tests/TestCyCache.py +194 -0
  10. Cython/Build/Tests/TestCythonizeArgsParser.py +481 -0
  11. Cython/Build/Tests/TestDependencies.py +133 -0
  12. Cython/Build/Tests/TestInline.py +177 -0
  13. Cython/Build/Tests/TestIpythonMagic.py +287 -0
  14. Cython/Build/Tests/TestRecythonize.py +212 -0
  15. Cython/Build/Tests/TestStripLiterals.py +155 -0
  16. Cython/Build/Tests/__init__.py +1 -0
  17. Cython/Build/__init__.py +11 -0
  18. Cython/CodeWriter.py +815 -0
  19. Cython/Compiler/AnalysedTreeTransforms.py +97 -0
  20. Cython/Compiler/Annotate.py +328 -0
  21. Cython/Compiler/AutoDocTransforms.py +320 -0
  22. Cython/Compiler/Buffer.py +680 -0
  23. Cython/Compiler/Builtin.py +984 -0
  24. Cython/Compiler/CmdLine.py +263 -0
  25. Cython/Compiler/Code.pxd +149 -0
  26. Cython/Compiler/Code.py +3746 -0
  27. Cython/Compiler/Code.pyd +0 -0
  28. Cython/Compiler/CodeGeneration.py +33 -0
  29. Cython/Compiler/CythonScope.py +191 -0
  30. Cython/Compiler/Dataclass.py +864 -0
  31. Cython/Compiler/DebugFlags.py +24 -0
  32. Cython/Compiler/Errors.py +297 -0
  33. Cython/Compiler/ExprNodes.py +15562 -0
  34. Cython/Compiler/FlowControl.pxd +97 -0
  35. Cython/Compiler/FlowControl.py +1451 -0
  36. Cython/Compiler/FlowControl.pyd +0 -0
  37. Cython/Compiler/FusedNode.py +971 -0
  38. Cython/Compiler/FusedNode.pyd +0 -0
  39. Cython/Compiler/Future.py +16 -0
  40. Cython/Compiler/Interpreter.py +57 -0
  41. Cython/Compiler/Lexicon.py +421 -0
  42. Cython/Compiler/LineTable.py +114 -0
  43. Cython/Compiler/LineTable.pyd +0 -0
  44. Cython/Compiler/Main.py +857 -0
  45. Cython/Compiler/MatchCaseNodes.py +259 -0
  46. Cython/Compiler/MemoryView.py +905 -0
  47. Cython/Compiler/ModuleNode.py +4235 -0
  48. Cython/Compiler/Naming.py +363 -0
  49. Cython/Compiler/Nodes.py +10831 -0
  50. Cython/Compiler/Optimize.py +5288 -0
  51. Cython/Compiler/Options.py +843 -0
  52. Cython/Compiler/ParseTreeTransforms.pxd +78 -0
  53. Cython/Compiler/ParseTreeTransforms.py +4638 -0
  54. Cython/Compiler/Parsing.pxd +9 -0
  55. Cython/Compiler/Parsing.py +4775 -0
  56. Cython/Compiler/Parsing.pyd +0 -0
  57. Cython/Compiler/Pipeline.py +439 -0
  58. Cython/Compiler/PyrexTypes.py +5870 -0
  59. Cython/Compiler/Pythran.py +232 -0
  60. Cython/Compiler/Scanning.pxd +48 -0
  61. Cython/Compiler/Scanning.py +701 -0
  62. Cython/Compiler/Scanning.pyd +0 -0
  63. Cython/Compiler/StringEncoding.py +298 -0
  64. Cython/Compiler/Symtab.py +3073 -0
  65. Cython/Compiler/Tests/TestBuffer.py +105 -0
  66. Cython/Compiler/Tests/TestBuiltin.py +72 -0
  67. Cython/Compiler/Tests/TestCmdLine.py +586 -0
  68. Cython/Compiler/Tests/TestCode.py +144 -0
  69. Cython/Compiler/Tests/TestFlowControl.py +65 -0
  70. Cython/Compiler/Tests/TestGrammar.py +202 -0
  71. Cython/Compiler/Tests/TestMemView.py +71 -0
  72. Cython/Compiler/Tests/TestParseTreeTransforms.py +285 -0
  73. Cython/Compiler/Tests/TestScanning.py +134 -0
  74. Cython/Compiler/Tests/TestSignatureMatching.py +73 -0
  75. Cython/Compiler/Tests/TestStringEncoding.py +21 -0
  76. Cython/Compiler/Tests/TestTreeFragment.py +63 -0
  77. Cython/Compiler/Tests/TestTreePath.py +103 -0
  78. Cython/Compiler/Tests/TestTypes.py +75 -0
  79. Cython/Compiler/Tests/TestUtilityLoad.py +112 -0
  80. Cython/Compiler/Tests/TestVisitor.py +61 -0
  81. Cython/Compiler/Tests/Utils.py +36 -0
  82. Cython/Compiler/Tests/__init__.py +1 -0
  83. Cython/Compiler/TreeFragment.py +278 -0
  84. Cython/Compiler/TreePath.py +303 -0
  85. Cython/Compiler/TypeInference.py +591 -0
  86. Cython/Compiler/TypeSlots.py +1174 -0
  87. Cython/Compiler/UFuncs.py +311 -0
  88. Cython/Compiler/UtilNodes.py +389 -0
  89. Cython/Compiler/UtilityCode.py +344 -0
  90. Cython/Compiler/Version.py +8 -0
  91. Cython/Compiler/Visitor.pxd +53 -0
  92. Cython/Compiler/Visitor.py +861 -0
  93. Cython/Compiler/Visitor.pyd +0 -0
  94. Cython/Compiler/__init__.py +1 -0
  95. Cython/Coverage.py +448 -0
  96. Cython/Debugger/Cygdb.py +177 -0
  97. Cython/Debugger/DebugWriter.py +82 -0
  98. Cython/Debugger/Tests/TestLibCython.py +275 -0
  99. Cython/Debugger/Tests/__init__.py +1 -0
  100. Cython/Debugger/Tests/cfuncs.c +8 -0
  101. Cython/Debugger/Tests/codefile +49 -0
  102. Cython/Debugger/Tests/test_libcython_in_gdb.py +578 -0
  103. Cython/Debugger/Tests/test_libpython_in_gdb.py +90 -0
  104. Cython/Debugger/__init__.py +1 -0
  105. Cython/Debugger/libcython.py +1548 -0
  106. Cython/Debugger/libpython.py +2821 -0
  107. Cython/Debugging.py +20 -0
  108. Cython/Distutils/__init__.py +2 -0
  109. Cython/Distutils/build_ext.py +139 -0
  110. Cython/Distutils/extension.py +96 -0
  111. Cython/Distutils/old_build_ext.py +351 -0
  112. Cython/Includes/cpython/__init__.pxd +173 -0
  113. Cython/Includes/cpython/array.pxd +178 -0
  114. Cython/Includes/cpython/bool.pxd +37 -0
  115. Cython/Includes/cpython/buffer.pxd +112 -0
  116. Cython/Includes/cpython/bytearray.pxd +33 -0
  117. Cython/Includes/cpython/bytes.pxd +200 -0
  118. Cython/Includes/cpython/cellobject.pxd +35 -0
  119. Cython/Includes/cpython/ceval.pxd +8 -0
  120. Cython/Includes/cpython/codecs.pxd +121 -0
  121. Cython/Includes/cpython/complex.pxd +60 -0
  122. Cython/Includes/cpython/contextvars.pxd +145 -0
  123. Cython/Includes/cpython/conversion.pxd +36 -0
  124. Cython/Includes/cpython/datetime.pxd +395 -0
  125. Cython/Includes/cpython/descr.pxd +26 -0
  126. Cython/Includes/cpython/dict.pxd +187 -0
  127. Cython/Includes/cpython/exc.pxd +263 -0
  128. Cython/Includes/cpython/fileobject.pxd +57 -0
  129. Cython/Includes/cpython/float.pxd +47 -0
  130. Cython/Includes/cpython/function.pxd +65 -0
  131. Cython/Includes/cpython/genobject.pxd +25 -0
  132. Cython/Includes/cpython/getargs.pxd +12 -0
  133. Cython/Includes/cpython/instance.pxd +25 -0
  134. Cython/Includes/cpython/iterator.pxd +36 -0
  135. Cython/Includes/cpython/iterobject.pxd +24 -0
  136. Cython/Includes/cpython/list.pxd +92 -0
  137. Cython/Includes/cpython/long.pxd +149 -0
  138. Cython/Includes/cpython/longintrepr.pxd +14 -0
  139. Cython/Includes/cpython/mapping.pxd +63 -0
  140. Cython/Includes/cpython/marshal.pxd +66 -0
  141. Cython/Includes/cpython/mem.pxd +120 -0
  142. Cython/Includes/cpython/memoryview.pxd +50 -0
  143. Cython/Includes/cpython/method.pxd +49 -0
  144. Cython/Includes/cpython/module.pxd +208 -0
  145. Cython/Includes/cpython/number.pxd +258 -0
  146. Cython/Includes/cpython/object.pxd +433 -0
  147. Cython/Includes/cpython/pycapsule.pxd +143 -0
  148. Cython/Includes/cpython/pylifecycle.pxd +68 -0
  149. Cython/Includes/cpython/pyport.pxd +8 -0
  150. Cython/Includes/cpython/pystate.pxd +95 -0
  151. Cython/Includes/cpython/pythread.pxd +53 -0
  152. Cython/Includes/cpython/ref.pxd +141 -0
  153. Cython/Includes/cpython/sequence.pxd +134 -0
  154. Cython/Includes/cpython/set.pxd +119 -0
  155. Cython/Includes/cpython/slice.pxd +70 -0
  156. Cython/Includes/cpython/time.pxd +129 -0
  157. Cython/Includes/cpython/tuple.pxd +72 -0
  158. Cython/Includes/cpython/type.pxd +53 -0
  159. Cython/Includes/cpython/unicode.pxd +639 -0
  160. Cython/Includes/cpython/version.pxd +32 -0
  161. Cython/Includes/cpython/weakref.pxd +78 -0
  162. Cython/Includes/libc/__init__.pxd +1 -0
  163. Cython/Includes/libc/complex.pxd +35 -0
  164. Cython/Includes/libc/errno.pxd +127 -0
  165. Cython/Includes/libc/float.pxd +43 -0
  166. Cython/Includes/libc/limits.pxd +28 -0
  167. Cython/Includes/libc/locale.pxd +46 -0
  168. Cython/Includes/libc/math.pxd +209 -0
  169. Cython/Includes/libc/setjmp.pxd +10 -0
  170. Cython/Includes/libc/signal.pxd +64 -0
  171. Cython/Includes/libc/stddef.pxd +9 -0
  172. Cython/Includes/libc/stdint.pxd +105 -0
  173. Cython/Includes/libc/stdio.pxd +80 -0
  174. Cython/Includes/libc/stdlib.pxd +72 -0
  175. Cython/Includes/libc/string.pxd +50 -0
  176. Cython/Includes/libc/threads.pxd +234 -0
  177. Cython/Includes/libc/time.pxd +52 -0
  178. Cython/Includes/libcpp/__init__.pxd +4 -0
  179. Cython/Includes/libcpp/algorithm.pxd +320 -0
  180. Cython/Includes/libcpp/any.pxd +16 -0
  181. Cython/Includes/libcpp/atomic.pxd +59 -0
  182. Cython/Includes/libcpp/barrier.pxd +22 -0
  183. Cython/Includes/libcpp/bit.pxd +29 -0
  184. Cython/Includes/libcpp/cast.pxd +12 -0
  185. Cython/Includes/libcpp/cmath.pxd +518 -0
  186. Cython/Includes/libcpp/complex.pxd +106 -0
  187. Cython/Includes/libcpp/condition_variable.pxd +322 -0
  188. Cython/Includes/libcpp/deque.pxd +165 -0
  189. Cython/Includes/libcpp/exception.pxd +86 -0
  190. Cython/Includes/libcpp/execution.pxd +15 -0
  191. Cython/Includes/libcpp/forward_list.pxd +63 -0
  192. Cython/Includes/libcpp/functional.pxd +26 -0
  193. Cython/Includes/libcpp/future.pxd +103 -0
  194. Cython/Includes/libcpp/iterator.pxd +34 -0
  195. Cython/Includes/libcpp/latch.pxd +17 -0
  196. Cython/Includes/libcpp/limits.pxd +61 -0
  197. Cython/Includes/libcpp/list.pxd +117 -0
  198. Cython/Includes/libcpp/map.pxd +252 -0
  199. Cython/Includes/libcpp/memory.pxd +115 -0
  200. Cython/Includes/libcpp/mutex.pxd +387 -0
  201. Cython/Includes/libcpp/numbers.pxd +15 -0
  202. Cython/Includes/libcpp/numeric.pxd +131 -0
  203. Cython/Includes/libcpp/optional.pxd +34 -0
  204. Cython/Includes/libcpp/pair.pxd +1 -0
  205. Cython/Includes/libcpp/queue.pxd +25 -0
  206. Cython/Includes/libcpp/random.pxd +166 -0
  207. Cython/Includes/libcpp/semaphore.pxd +43 -0
  208. Cython/Includes/libcpp/set.pxd +228 -0
  209. Cython/Includes/libcpp/shared_mutex.pxd +96 -0
  210. Cython/Includes/libcpp/span.pxd +87 -0
  211. Cython/Includes/libcpp/stack.pxd +11 -0
  212. Cython/Includes/libcpp/stop_token.pxd +117 -0
  213. Cython/Includes/libcpp/string.pxd +355 -0
  214. Cython/Includes/libcpp/string_view.pxd +183 -0
  215. Cython/Includes/libcpp/typeindex.pxd +15 -0
  216. Cython/Includes/libcpp/typeinfo.pxd +10 -0
  217. Cython/Includes/libcpp/unordered_map.pxd +193 -0
  218. Cython/Includes/libcpp/unordered_set.pxd +152 -0
  219. Cython/Includes/libcpp/utility.pxd +30 -0
  220. Cython/Includes/libcpp/vector.pxd +186 -0
  221. Cython/Includes/numpy/math.pxd +150 -0
  222. Cython/Includes/openmp.pxd +50 -0
  223. Cython/Includes/posix/__init__.pxd +1 -0
  224. Cython/Includes/posix/dlfcn.pxd +14 -0
  225. Cython/Includes/posix/fcntl.pxd +86 -0
  226. Cython/Includes/posix/ioctl.pxd +4 -0
  227. Cython/Includes/posix/mman.pxd +101 -0
  228. Cython/Includes/posix/resource.pxd +57 -0
  229. Cython/Includes/posix/select.pxd +21 -0
  230. Cython/Includes/posix/signal.pxd +73 -0
  231. Cython/Includes/posix/stat.pxd +98 -0
  232. Cython/Includes/posix/stdio.pxd +37 -0
  233. Cython/Includes/posix/stdlib.pxd +29 -0
  234. Cython/Includes/posix/strings.pxd +9 -0
  235. Cython/Includes/posix/time.pxd +71 -0
  236. Cython/Includes/posix/types.pxd +30 -0
  237. Cython/Includes/posix/uio.pxd +26 -0
  238. Cython/Includes/posix/unistd.pxd +271 -0
  239. Cython/Includes/posix/wait.pxd +38 -0
  240. Cython/Plex/Actions.pxd +24 -0
  241. Cython/Plex/Actions.py +119 -0
  242. Cython/Plex/Actions.pyd +0 -0
  243. Cython/Plex/DFA.pxd +14 -0
  244. Cython/Plex/DFA.py +164 -0
  245. Cython/Plex/DFA.pyd +0 -0
  246. Cython/Plex/Errors.py +48 -0
  247. Cython/Plex/Lexicons.py +178 -0
  248. Cython/Plex/Machines.pxd +36 -0
  249. Cython/Plex/Machines.py +238 -0
  250. Cython/Plex/Machines.pyd +0 -0
  251. Cython/Plex/Regexps.py +535 -0
  252. Cython/Plex/Scanners.pxd +45 -0
  253. Cython/Plex/Scanners.py +328 -0
  254. Cython/Plex/Scanners.pyd +0 -0
  255. Cython/Plex/Transitions.pxd +14 -0
  256. Cython/Plex/Transitions.py +239 -0
  257. Cython/Plex/Transitions.pyd +0 -0
  258. Cython/Plex/__init__.py +34 -0
  259. Cython/Runtime/__init__.py +1 -0
  260. Cython/Runtime/refnanny.pyd +0 -0
  261. Cython/Runtime/refnanny.pyx +237 -0
  262. Cython/Shadow.py +690 -0
  263. Cython/Shadow.pyi +521 -0
  264. Cython/StringIOTree.py +170 -0
  265. Cython/StringIOTree.pyd +0 -0
  266. Cython/Tempita/__init__.py +4 -0
  267. Cython/Tempita/_looper.py +154 -0
  268. Cython/Tempita/_tempita.py +1091 -0
  269. Cython/Tempita/_tempita.pyd +0 -0
  270. Cython/TestUtils.py +422 -0
  271. Cython/Tests/TestCodeWriter.py +128 -0
  272. Cython/Tests/TestCythonUtils.py +202 -0
  273. Cython/Tests/TestJediTyper.py +223 -0
  274. Cython/Tests/TestShadow.py +114 -0
  275. Cython/Tests/TestStringIOTree.py +67 -0
  276. Cython/Tests/TestTestUtils.py +90 -0
  277. Cython/Tests/__init__.py +1 -0
  278. Cython/Tests/xmlrunner.py +390 -0
  279. Cython/Utility/AsyncGen.c +1031 -0
  280. Cython/Utility/Buffer.c +865 -0
  281. Cython/Utility/BufferFormatFromTypeInfo.pxd +2 -0
  282. Cython/Utility/Builtins.c +810 -0
  283. Cython/Utility/CConvert.pyx +134 -0
  284. Cython/Utility/CMath.c +104 -0
  285. Cython/Utility/CommonStructures.c +226 -0
  286. Cython/Utility/Complex.c +378 -0
  287. Cython/Utility/Coroutine.c +2300 -0
  288. Cython/Utility/CpdefEnums.pyx +103 -0
  289. Cython/Utility/CppConvert.pyx +282 -0
  290. Cython/Utility/CppSupport.cpp +151 -0
  291. Cython/Utility/CythonFunction.c +1832 -0
  292. Cython/Utility/Dataclasses.c +101 -0
  293. Cython/Utility/Embed.c +121 -0
  294. Cython/Utility/Exceptions.c +1016 -0
  295. Cython/Utility/ExtensionTypes.c +996 -0
  296. Cython/Utility/FunctionArguments.c +1043 -0
  297. Cython/Utility/FusedFunction.pyx +44 -0
  298. Cython/Utility/ImportExport.c +907 -0
  299. Cython/Utility/MemoryView.pxd +188 -0
  300. Cython/Utility/MemoryView.pyx +1482 -0
  301. Cython/Utility/MemoryView_C.c +927 -0
  302. Cython/Utility/ModuleSetupCode.c +3203 -0
  303. Cython/Utility/NumpyImportArray.c +46 -0
  304. Cython/Utility/ObjectHandling.c +3273 -0
  305. Cython/Utility/Optimize.c +1603 -0
  306. Cython/Utility/Overflow.c +384 -0
  307. Cython/Utility/Printing.c +86 -0
  308. Cython/Utility/Profile.c +732 -0
  309. Cython/Utility/StringTools.c +1379 -0
  310. Cython/Utility/Synchronization.c +399 -0
  311. Cython/Utility/TString.c +356 -0
  312. Cython/Utility/TestCyUtilityLoader.pyx +8 -0
  313. Cython/Utility/TestCythonScope.pyx +75 -0
  314. Cython/Utility/TestUtilityLoader.c +12 -0
  315. Cython/Utility/TypeConversion.c +1385 -0
  316. Cython/Utility/UFuncs.pyx +50 -0
  317. Cython/Utility/UFuncs_C.c +89 -0
  318. Cython/Utility/__init__.py +28 -0
  319. Cython/Utility/arrayarray.h +167 -0
  320. Cython/Utils.py +687 -0
  321. Cython/Utils.pyd +0 -0
  322. Cython/__init__.py +10 -0
  323. Cython/__init__.pyi +7 -0
  324. Cython/py.typed +0 -0
  325. cython-3.2.0.dist-info/METADATA +85 -0
  326. cython-3.2.0.dist-info/RECORD +333 -0
  327. cython-3.2.0.dist-info/WHEEL +5 -0
  328. cython-3.2.0.dist-info/entry_points.txt +4 -0
  329. cython-3.2.0.dist-info/top_level.txt +3 -0
  330. cython.py +29 -0
  331. pyximport/__init__.py +4 -0
  332. pyximport/pyxbuild.py +160 -0
  333. pyximport/pyximport.py +482 -0
@@ -0,0 +1,984 @@
1
+ #
2
+ # Builtin Definitions
3
+ #
4
+
5
+
6
+ from .StringEncoding import EncodedString
7
+ from .Symtab import BuiltinScope, CClassScope, StructOrUnionScope, ModuleScope, Entry
8
+ from .Code import UtilityCode, TempitaUtilityCode, KNOWN_PYTHON_BUILTINS, uncachable_builtins
9
+ from .TypeSlots import Signature
10
+ from . import PyrexTypes
11
+
12
+
13
+ # C-level implementations of builtin types, functions and methods
14
+
15
+ iter_next_utility_code = UtilityCode.load("IterNext", "ObjectHandling.c")
16
+ getattr_utility_code = UtilityCode.load("GetAttr", "ObjectHandling.c")
17
+ getattr3_utility_code = UtilityCode.load("GetAttr3", "Builtins.c")
18
+ pyexec_utility_code = UtilityCode.load("PyExec", "Builtins.c")
19
+ pyexec_globals_utility_code = UtilityCode.load("PyExecGlobals", "Builtins.c")
20
+ globals_utility_code = UtilityCode.load("Globals", "Builtins.c")
21
+ range_utility_code = UtilityCode.load("PyRange_Check", "Builtins.c")
22
+ include_std_lib_h_utility_code = UtilityCode.load("IncludeStdlibH", "ModuleSetupCode.c")
23
+ pysequence_multiply_utility_code = UtilityCode.load("PySequenceMultiply", "ObjectHandling.c")
24
+ slice_accessor_utility_code = UtilityCode.load("PySliceAccessors", "Builtins.c")
25
+
26
+ # mapping from builtins to their C-level equivalents
27
+
28
+ class _BuiltinOverride:
29
+ def __init__(self, py_name, args, ret_type, cname, py_equiv="*",
30
+ utility_code=None, sig=None, func_type=None,
31
+ is_strict_signature=False, builtin_return_type=None,
32
+ nogil=None, specialiser=None):
33
+ self.py_name, self.cname, self.py_equiv = py_name, cname, py_equiv
34
+ self.args, self.ret_type = args, ret_type
35
+ self.func_type, self.sig = func_type, sig
36
+ self.builtin_return_type = builtin_return_type
37
+ self.is_strict_signature = is_strict_signature
38
+ self.utility_code = utility_code
39
+ self.nogil = nogil
40
+ self.specialiser = specialiser
41
+
42
+ def build_func_type(self, sig=None, self_arg=None):
43
+ if sig is None:
44
+ sig = Signature(self.args, self.ret_type, nogil=self.nogil)
45
+ sig.exception_check = False # not needed for the current builtins
46
+ func_type = sig.function_type(self_arg)
47
+ if self.is_strict_signature:
48
+ func_type.is_strict_signature = True
49
+ if self.builtin_return_type:
50
+ func_type.return_type = builtin_types[self.builtin_return_type]
51
+ return func_type
52
+
53
+
54
+ class BuiltinAttribute:
55
+ def __init__(self, py_name, cname=None, field_type=None, field_type_name=None):
56
+ self.py_name = py_name
57
+ self.cname = cname or py_name
58
+ self.field_type_name = field_type_name # can't do the lookup before the type is declared!
59
+ self.field_type = field_type
60
+
61
+ def declare_in_type(self, self_type):
62
+ if self.field_type_name is not None:
63
+ # lazy type lookup
64
+ field_type = builtin_scope.lookup(self.field_type_name).type
65
+ else:
66
+ field_type = self.field_type or PyrexTypes.py_object_type
67
+ entry = self_type.scope.declare(self.py_name, self.cname, field_type, None, 'private')
68
+ entry.is_variable = True
69
+
70
+
71
+ class BuiltinFunction(_BuiltinOverride):
72
+ def declare_in_scope(self, scope):
73
+ func_type, sig = self.func_type, self.sig
74
+ if func_type is None:
75
+ func_type = self.build_func_type(sig)
76
+ scope.declare_builtin_cfunction(
77
+ self.py_name, func_type, self.cname, self.py_equiv, self.utility_code,
78
+ specialiser=self.specialiser,
79
+ )
80
+
81
+
82
+ class BuiltinMethod(_BuiltinOverride):
83
+ def declare_in_type(self, self_type):
84
+ method_type, sig = self.func_type, self.sig
85
+ if method_type is None:
86
+ # override 'self' type (first argument)
87
+ self_arg = PyrexTypes.CFuncTypeArg("", self_type, None)
88
+ self_arg.not_none = True
89
+ self_arg.accept_builtin_subtypes = True
90
+ method_type = self.build_func_type(sig, self_arg)
91
+ self_type.scope.declare_builtin_cfunction(
92
+ self.py_name, method_type, self.cname, utility_code=self.utility_code)
93
+
94
+
95
+ class BuiltinProperty:
96
+ # read only for now
97
+ def __init__(self, py_name, property_type, call_cname,
98
+ exception_value=None, exception_check=None, utility_code=None):
99
+ self.py_name = py_name
100
+ self.property_type = property_type
101
+ self.call_cname = call_cname
102
+ self.utility_code = utility_code
103
+ self.exception_value = exception_value
104
+ self.exception_check = exception_check
105
+
106
+ def declare_in_type(self, self_type):
107
+ self_type.scope.declare_cproperty(
108
+ self.py_name,
109
+ self.property_type,
110
+ self.call_cname,
111
+ exception_value=self.exception_value,
112
+ exception_check=self.exception_check,
113
+ utility_code=self.utility_code
114
+ )
115
+
116
+
117
+ ### Special builtin implementations generated at runtime.
118
+
119
+ def _generate_divmod_function(scope, argument_types):
120
+ if len(argument_types) != 2:
121
+ return None
122
+ type_op1, type_op2 = argument_types
123
+
124
+ # Resolve internal typedefs to avoid useless code duplication.
125
+ if type_op1.is_typedef:
126
+ type_op1 = type_op1.resolve_known_type()
127
+ if type_op2.is_typedef:
128
+ type_op2 = type_op2.resolve_known_type()
129
+
130
+ if type_op1.is_float or type_op1 is float_type or type_op2.is_float and (type_op1.is_int or type_op1 is int_type):
131
+ impl = "float"
132
+ # TODO: support 'long double'? Currently fails to handle the error return value.
133
+ number_type = PyrexTypes.c_double_type
134
+ elif type_op1.is_int and type_op2.is_int:
135
+ impl = "int"
136
+ number_type = type_op1 if type_op1.rank >= type_op2.rank else type_op2
137
+ else:
138
+ return None
139
+
140
+ nogil = scope.nogil
141
+ cfunc_suffix = f"{'nogil_' if nogil else ''}{impl}_{'td_' if number_type.is_typedef else ''}{number_type.specialization_name()}"
142
+ function_cname = f"__Pyx_divmod_{cfunc_suffix}"
143
+
144
+ # Reuse an existing specialisation, if available.
145
+ builtin_scope = scope.builtin_scope()
146
+ existing_entry = builtin_scope.lookup_here("divmod")
147
+ if existing_entry is not None:
148
+ for entry in existing_entry.all_alternatives():
149
+ if entry.cname == function_cname:
150
+ return entry
151
+
152
+ # Generate a new specialisation.
153
+ ctuple_entry = scope.declare_tuple_type(None, [number_type]*2)
154
+ ctuple_entry.used = True
155
+ return_type = ctuple_entry.type
156
+
157
+ function_type = PyrexTypes.CFuncType(
158
+ return_type, [
159
+ PyrexTypes.CFuncTypeArg("a", number_type, None),
160
+ PyrexTypes.CFuncTypeArg("b", number_type, None),
161
+ ],
162
+ exception_value=f"__Pyx_divmod_ERROR_VALUE_{cfunc_suffix}",
163
+ exception_check=True,
164
+ is_strict_signature=True,
165
+ nogil=nogil,
166
+ )
167
+
168
+ utility_code = TempitaUtilityCode.load(
169
+ f"divmod_{impl}", "Builtins.c", context={
170
+ 'CFUNC_SUFFIX': cfunc_suffix,
171
+ 'MATH_SUFFIX': number_type.math_h_modifier if number_type.is_float else '',
172
+ 'TYPE': number_type.empty_declaration_code(),
173
+ 'RETURN_TYPE': return_type.empty_declaration_code(),
174
+ 'NOGIL': nogil,
175
+ })
176
+
177
+ entry = builtin_scope.declare_builtin_cfunction(
178
+ "divmod", function_type, function_cname, utility_code=utility_code)
179
+
180
+ return entry
181
+
182
+
183
+ ### List of builtin functions and their implementation.
184
+
185
+ builtin_function_table = [
186
+ # name, args, return, C API func, py equiv = "*"
187
+ BuiltinFunction('abs', "d", "d", "fabs",
188
+ is_strict_signature=True, nogil=True,
189
+ utility_code=include_std_lib_h_utility_code),
190
+ BuiltinFunction('abs', "f", "f", "fabsf",
191
+ is_strict_signature=True, nogil=True,
192
+ utility_code=include_std_lib_h_utility_code),
193
+ BuiltinFunction('abs', "i", "i", "abs",
194
+ is_strict_signature=True, nogil=True,
195
+ utility_code=include_std_lib_h_utility_code),
196
+ BuiltinFunction('abs', "l", "l", "labs",
197
+ is_strict_signature=True, nogil=True,
198
+ utility_code=include_std_lib_h_utility_code),
199
+ BuiltinFunction('abs', None, None, "__Pyx_abs_longlong",
200
+ utility_code = UtilityCode.load("abs_longlong", "Builtins.c"),
201
+ func_type = PyrexTypes.CFuncType(
202
+ PyrexTypes.c_longlong_type, [
203
+ PyrexTypes.CFuncTypeArg("arg", PyrexTypes.c_longlong_type, None)
204
+ ],
205
+ is_strict_signature = True, nogil=True)),
206
+ ] + list(
207
+ BuiltinFunction('abs', None, None, "/*abs_{}*/".format(t.specialization_name()),
208
+ func_type = PyrexTypes.CFuncType(
209
+ t,
210
+ [PyrexTypes.CFuncTypeArg("arg", t, None)],
211
+ is_strict_signature = True, nogil=True))
212
+ for t in (PyrexTypes.c_uint_type, PyrexTypes.c_ulong_type, PyrexTypes.c_ulonglong_type)
213
+ ) + list(
214
+ BuiltinFunction('abs', None, None, "__Pyx_c_abs{}".format(t.funcsuffix),
215
+ func_type = PyrexTypes.CFuncType(
216
+ t.real_type, [
217
+ PyrexTypes.CFuncTypeArg("arg", t, None)
218
+ ],
219
+ is_strict_signature = True, nogil=True))
220
+ for t in (PyrexTypes.c_float_complex_type,
221
+ PyrexTypes.c_double_complex_type,
222
+ PyrexTypes.c_longdouble_complex_type)
223
+ ) + [
224
+ BuiltinFunction('abs', "O", "O", "__Pyx_PyNumber_Absolute",
225
+ utility_code=UtilityCode.load("py_abs", "Builtins.c")),
226
+ #('all', "", "", ""),
227
+ #('any', "", "", ""),
228
+ #('aiter', "", "", ""),
229
+ #('anext', "", "", ""),
230
+ BuiltinFunction('ascii', "O", "O", "PyObject_ASCII", builtin_return_type='str'),
231
+ BuiltinFunction('bin', "O", "O", "__Pyx_PyNumber_Bin", builtin_return_type='str',
232
+ utility_code=UtilityCode(
233
+ proto="#define __Pyx_PyNumber_Bin(obj) PyNumber_ToBase((obj), 2)",
234
+ name="PyNumber_Bin")),
235
+ #('breakpoint', "", "", ""),
236
+ BuiltinFunction('callable', "O", "b", "__Pyx_PyCallable_Check",
237
+ utility_code = UtilityCode.load("CallableCheck", "ObjectHandling.c")),
238
+ BuiltinFunction('chr', "i", "O", "PyUnicode_FromOrdinal", builtin_return_type='str'),
239
+ #('compile', "", "", ""), # PyObject* Py_CompileString( char *str, char *filename, int start)
240
+ BuiltinFunction('delattr', "OO", "r", "__Pyx_PyObject_DelAttr",
241
+ utility_code=UtilityCode.load("PyObjectDelAttr", "ObjectHandling.c")),
242
+ BuiltinFunction('dir', "O", "O", "PyObject_Dir"),
243
+ BuiltinFunction('divmod', "OO", "O", "PyNumber_Divmod",
244
+ specialiser=_generate_divmod_function),
245
+ #('enumerate', "", "", ""),
246
+ #('eval', "", "", ""),
247
+ BuiltinFunction('exec', "O", "O", "__Pyx_PyExecGlobals",
248
+ utility_code = pyexec_globals_utility_code),
249
+ BuiltinFunction('exec', "OO", "O", "__Pyx_PyExec2",
250
+ utility_code = pyexec_utility_code),
251
+ BuiltinFunction('exec', "OOO", "O", "__Pyx_PyExec3",
252
+ utility_code = pyexec_utility_code),
253
+ #('filter', "", "", ""),
254
+ BuiltinFunction('format', "OO", "O", "PyObject_Format", builtin_return_type='str'),
255
+ BuiltinFunction('format', "O", "O", "__Pyx_PyObject_Format1", builtin_return_type='str',
256
+ utility_code=UtilityCode(
257
+ proto="#define __Pyx_PyObject_Format1(obj) PyObject_Format((obj), NULL)",
258
+ name="PyObject_Format1")),
259
+ BuiltinFunction('getattr3', "OOO", "O", "__Pyx_GetAttr3", "getattr",
260
+ utility_code=getattr3_utility_code), # Pyrex legacy
261
+ BuiltinFunction('getattr', "OOO", "O", "__Pyx_GetAttr3",
262
+ utility_code=getattr3_utility_code),
263
+ BuiltinFunction('getattr', "OO", "O", "__Pyx_GetAttr",
264
+ utility_code=getattr_utility_code),
265
+ BuiltinFunction('hasattr', "OO", "b", "__Pyx_HasAttr",
266
+ utility_code = UtilityCode.load("HasAttr", "Builtins.c")),
267
+ BuiltinFunction('hash', "O", "h", "PyObject_Hash"),
268
+ #('help', "", "", ""),
269
+ BuiltinFunction('hex', "O", "O", "__Pyx_PyNumber_Hex", builtin_return_type='str',
270
+ utility_code=UtilityCode(
271
+ proto="#define __Pyx_PyNumber_Hex(obj) PyNumber_ToBase((obj), 16)",
272
+ name="PyNumber_Hex")),
273
+ #('id', "", "", ""),
274
+ #('input', "", "", ""),
275
+ BuiltinFunction('intern', "O", "O", "__Pyx_Intern", # Py2 legacy
276
+ utility_code = UtilityCode.load("Intern", "Builtins.c")),
277
+ BuiltinFunction('isinstance', "OO", "b", "PyObject_IsInstance"),
278
+ BuiltinFunction('issubclass', "OO", "b", "PyObject_IsSubclass"),
279
+ BuiltinFunction('iter', "OO", "O", "PyCallIter_New"),
280
+ BuiltinFunction('iter', "O", "O", "PyObject_GetIter"),
281
+ BuiltinFunction('len', "O", "z", "PyObject_Length"),
282
+ BuiltinFunction('locals', "", "O", "__pyx_locals"),
283
+ #('map', "", "", ""),
284
+ #('max', "", "", ""),
285
+ #('min', "", "", ""),
286
+ BuiltinFunction('next', "O", "O", "__Pyx_PyIter_Next",
287
+ utility_code = iter_next_utility_code),
288
+ BuiltinFunction('next', "OO", "O", "__Pyx_PyIter_Next2",
289
+ utility_code = iter_next_utility_code),
290
+ BuiltinFunction('oct', "O", "O", "__Pyx_PyNumber_Oct", builtin_return_type='str',
291
+ utility_code=UtilityCode(
292
+ proto="#define __Pyx_PyNumber_Oct(obj) PyNumber_ToBase((obj), 8)",
293
+ name="PyNumber_Oct")),
294
+ #('open', "ss", "O", ""), # no C-API equivalent in Py3
295
+ ] + [
296
+ BuiltinFunction('ord', None, None, "__Pyx_long_cast",
297
+ func_type=PyrexTypes.CFuncType(
298
+ PyrexTypes.c_long_type, [PyrexTypes.CFuncTypeArg("c", c_type, None)],
299
+ is_strict_signature=True))
300
+ for c_type in [PyrexTypes.c_py_ucs4_type, PyrexTypes.c_py_unicode_type]
301
+ ] + [
302
+ BuiltinFunction('ord', None, None, "__Pyx_uchar_cast",
303
+ func_type=PyrexTypes.CFuncType(
304
+ PyrexTypes.c_uchar_type, [PyrexTypes.CFuncTypeArg("c", c_type, None)],
305
+ is_strict_signature=True))
306
+ for c_type in [PyrexTypes.c_char_type, PyrexTypes.c_schar_type, PyrexTypes.c_uchar_type]
307
+ ] + [
308
+ BuiltinFunction('ord', None, None, "__Pyx_PyObject_Ord",
309
+ utility_code=UtilityCode.load_cached("object_ord", "Builtins.c"),
310
+ func_type=PyrexTypes.CFuncType(
311
+ PyrexTypes.c_long_type, [
312
+ PyrexTypes.CFuncTypeArg("c", PyrexTypes.py_object_type, None)
313
+ ],
314
+ exception_value="(long)(Py_UCS4)-1")),
315
+ BuiltinFunction('pow', "OOO", "O", "PyNumber_Power"),
316
+ BuiltinFunction('pow', "OO", "O", "__Pyx_PyNumber_Power2",
317
+ utility_code = UtilityCode.load("pow2", "Builtins.c")),
318
+ #('print', "", "", ""),
319
+ #('property', "", "", ""),
320
+ BuiltinFunction('reload', "O", "O", "PyImport_ReloadModule"), # legacy Py2
321
+ BuiltinFunction('repr', "O", "O", "PyObject_Repr", builtin_return_type='str'),
322
+ #('reversed', "", "", ""),
323
+ #('round', "", "", ""),
324
+ BuiltinFunction('setattr', "OOO", "r", "PyObject_SetAttr"),
325
+ #('sorted', "", "", ""),
326
+ #('sum', "", "", ""),
327
+ #('type', "O", "O", "PyObject_Type"),
328
+ BuiltinFunction('unichr', "i", "O", "PyUnicode_FromOrdinal", builtin_return_type='str'), # legacy Py2
329
+ #('vars', "", "", ""),
330
+ #('zip', "", "", ""),
331
+
332
+ # Put in namespace append optimization.
333
+ BuiltinFunction('__Pyx_PyObject_Append', "OO", "O", "__Pyx_PyObject_Append"),
334
+
335
+ # This is conditionally looked up based on a compiler directive.
336
+ BuiltinFunction('__Pyx_Globals', "", "O", "__Pyx_Globals",
337
+ utility_code=globals_utility_code),
338
+ ]
339
+
340
+
341
+ # Builtin types
342
+ # bool
343
+ # bytearray
344
+ # bytes
345
+ # classmethod
346
+ # complex
347
+ # dict
348
+ # enumerate
349
+ # float
350
+ # frozenset
351
+ # int
352
+ # list
353
+ # long
354
+ # memoryview
355
+ # object
356
+ # property
357
+ # range
358
+ # set
359
+ # slice
360
+ # staticmethod
361
+ # str
362
+ # super
363
+ # tuple
364
+ # type
365
+
366
+ builtin_types_table = [
367
+
368
+ ("type", "&PyType_Type", []),
369
+
370
+ ("bool", "&PyBool_Type", []),
371
+
372
+ ("int", "&PyLong_Type", []),
373
+ ("float", "&PyFloat_Type", []),
374
+
375
+ ("complex", "&PyComplex_Type", [BuiltinAttribute('cval', field_type_name = 'Py_complex'),
376
+ BuiltinAttribute('real', 'cval.real', field_type = PyrexTypes.c_double_type),
377
+ BuiltinAttribute('imag', 'cval.imag', field_type = PyrexTypes.c_double_type),
378
+ ]),
379
+
380
+ ("bytearray", "&PyByteArray_Type", [
381
+ BuiltinMethod("__mul__", "Tz", "T", "__Pyx_PySequence_Multiply",
382
+ utility_code=pysequence_multiply_utility_code),
383
+ ]),
384
+ ("bytes", "&PyBytes_Type", [BuiltinMethod("join", "TO", "T", "__Pyx_PyBytes_Join",
385
+ utility_code=UtilityCode.load("StringJoin", "StringTools.c")),
386
+ BuiltinMethod("__mul__", "Tz", "T", "__Pyx_PySequence_Multiply",
387
+ utility_code=pysequence_multiply_utility_code),
388
+ ]),
389
+ ("str", "&PyUnicode_Type", [BuiltinMethod("__contains__", "TO", "b", "PyUnicode_Contains"),
390
+ BuiltinMethod("join", "TO", "T", "PyUnicode_Join"),
391
+ BuiltinMethod("__mul__", "Tz", "T", "__Pyx_PySequence_Multiply",
392
+ utility_code=pysequence_multiply_utility_code),
393
+ ]),
394
+
395
+ ("tuple", "&PyTuple_Type", [BuiltinMethod("__mul__", "Tz", "T", "__Pyx_PySequence_Multiply",
396
+ utility_code=pysequence_multiply_utility_code),
397
+ ]),
398
+
399
+ ("list", "&PyList_Type", [BuiltinMethod("insert", "TzO", "r", "PyList_Insert"),
400
+ BuiltinMethod("reverse", "T", "r", "PyList_Reverse"),
401
+ BuiltinMethod("append", "TO", "r", "__Pyx_PyList_Append",
402
+ utility_code=UtilityCode.load("ListAppend", "Optimize.c")),
403
+ BuiltinMethod("extend", "TO", "r", "__Pyx_PyList_Extend",
404
+ utility_code=UtilityCode.load("ListExtend", "Optimize.c")),
405
+ BuiltinMethod("__mul__", "Tz", "T", "__Pyx_PySequence_Multiply",
406
+ utility_code=pysequence_multiply_utility_code),
407
+ ]),
408
+
409
+ ("dict", "&PyDict_Type", [BuiltinMethod("__contains__", "TO", "b", "PyDict_Contains"),
410
+ BuiltinMethod("has_key", "TO", "b", "PyDict_Contains"),
411
+ BuiltinMethod("items", "T", "O", "__Pyx_PyDict_Items",
412
+ utility_code=UtilityCode.load("py_dict_items", "Builtins.c")),
413
+ BuiltinMethod("keys", "T", "O", "__Pyx_PyDict_Keys",
414
+ utility_code=UtilityCode.load("py_dict_keys", "Builtins.c")),
415
+ BuiltinMethod("values", "T", "O", "__Pyx_PyDict_Values",
416
+ utility_code=UtilityCode.load("py_dict_values", "Builtins.c")),
417
+ BuiltinMethod("iteritems", "T", "O", "__Pyx_PyDict_IterItems",
418
+ utility_code=UtilityCode.load("py_dict_iteritems", "Builtins.c")),
419
+ BuiltinMethod("iterkeys", "T", "O", "__Pyx_PyDict_IterKeys",
420
+ utility_code=UtilityCode.load("py_dict_iterkeys", "Builtins.c")),
421
+ BuiltinMethod("itervalues", "T", "O", "__Pyx_PyDict_IterValues",
422
+ utility_code=UtilityCode.load("py_dict_itervalues", "Builtins.c")),
423
+ BuiltinMethod("viewitems", "T", "O", "__Pyx_PyDict_ViewItems",
424
+ utility_code=UtilityCode.load("py_dict_viewitems", "Builtins.c")),
425
+ BuiltinMethod("viewkeys", "T", "O", "__Pyx_PyDict_ViewKeys",
426
+ utility_code=UtilityCode.load("py_dict_viewkeys", "Builtins.c")),
427
+ BuiltinMethod("viewvalues", "T", "O", "__Pyx_PyDict_ViewValues",
428
+ utility_code=UtilityCode.load("py_dict_viewvalues", "Builtins.c")),
429
+ BuiltinMethod("clear", "T", "r", "__Pyx_PyDict_Clear",
430
+ utility_code=UtilityCode.load("py_dict_clear", "Optimize.c")),
431
+ BuiltinMethod("copy", "T", "T", "PyDict_Copy")]),
432
+
433
+ ("range", "&PyRange_Type", []),
434
+
435
+ ("slice", "&PySlice_Type", [BuiltinProperty("start", PyrexTypes.py_object_type, '__Pyx_PySlice_Start',
436
+ utility_code=slice_accessor_utility_code),
437
+ BuiltinProperty("stop", PyrexTypes.py_object_type, '__Pyx_PySlice_Stop',
438
+ utility_code=slice_accessor_utility_code),
439
+ BuiltinProperty("step", PyrexTypes.py_object_type, '__Pyx_PySlice_Step',
440
+ utility_code=slice_accessor_utility_code),
441
+ ]),
442
+
443
+ ("set", "&PySet_Type", [BuiltinMethod("clear", "T", "r", "PySet_Clear"),
444
+ # discard() and remove() have a special treatment for unhashable values
445
+ BuiltinMethod("discard", "TO", "r", "__Pyx_PySet_Discard",
446
+ utility_code=UtilityCode.load("py_set_discard", "Optimize.c")),
447
+ BuiltinMethod("remove", "TO", "r", "__Pyx_PySet_Remove",
448
+ utility_code=UtilityCode.load("py_set_remove", "Optimize.c")),
449
+ # update is actually variadic (see Github issue #1645)
450
+ # BuiltinMethod("update", "TO", "r", "__Pyx_PySet_Update",
451
+ # utility_code=UtilityCode.load_cached("PySet_Update", "Builtins.c")),
452
+ BuiltinMethod("add", "TO", "r", "PySet_Add"),
453
+ BuiltinMethod("pop", "T", "O", "PySet_Pop")]),
454
+ ("frozenset", "&PyFrozenSet_Type", []),
455
+ ("BaseException", "((PyTypeObject*)PyExc_BaseException)", []),
456
+ ("Exception", "((PyTypeObject*)PyExc_Exception)", []),
457
+ ("memoryview", "&PyMemoryView_Type", [
458
+ # TODO - format would be nice, but hard to get
459
+ # __len__ can be accessed through a direct lookup of the buffer (but probably in Optimize.c)
460
+ # error checking would ideally be limited api only
461
+ BuiltinProperty("ndim", PyrexTypes.c_int_type, '__Pyx_PyMemoryView_Get_ndim',
462
+ exception_value=-1, exception_check=True,
463
+ utility_code=TempitaUtilityCode.load_cached(
464
+ "memoryview_get_from_buffer", "Builtins.c",
465
+ context=dict(name="ndim")
466
+ )
467
+ ),
468
+ BuiltinProperty("readonly", PyrexTypes.c_bint_type, '__Pyx_PyMemoryView_Get_readonly',
469
+ exception_value=-1, exception_check=True,
470
+ utility_code=TempitaUtilityCode.load_cached(
471
+ "memoryview_get_from_buffer", "Builtins.c",
472
+ context=dict(name="readonly")
473
+ )
474
+ ),
475
+ BuiltinProperty("itemsize", PyrexTypes.c_py_ssize_t_type, '__Pyx_PyMemoryView_Get_itemsize',
476
+ exception_value=-1, exception_check=True,
477
+ utility_code=TempitaUtilityCode.load_cached(
478
+ "memoryview_get_from_buffer", "Builtins.c",
479
+ context=dict(name="itemsize")
480
+ )
481
+ )]
482
+ )
483
+ ]
484
+
485
+
486
+ types_that_construct_their_instance = frozenset({
487
+ # Some builtin types do not always return an instance of
488
+ # themselves - these do:
489
+ 'type', 'bool', 'int', 'float', 'complex',
490
+ 'bytes', 'unicode', 'bytearray', 'str',
491
+ 'tuple', 'list', 'dict', 'set', 'frozenset',
492
+ 'memoryview', 'range',
493
+ # All builtin exception types create their own instance.
494
+ *filter(PyrexTypes.is_exception_type_name, KNOWN_PYTHON_BUILTINS),
495
+ })
496
+
497
+
498
+ # When updating this mapping, also update "unsafe_compile_time_methods" below
499
+ # if methods are added that are not safe to evaluate at compile time.
500
+ inferred_method_return_types = {
501
+ 'complex': dict(
502
+ conjugate='complex',
503
+ ),
504
+ 'int': dict(
505
+ as_integer_ratio='tuple[int,int]',
506
+ bit_count='T',
507
+ bit_length='T',
508
+ conjugate='T',
509
+ from_bytes='T', # classmethod
510
+ is_integer='bint',
511
+ to_bytes='bytes',
512
+ ),
513
+ 'float': dict(
514
+ as_integer_ratio='tuple[int,int]',
515
+ conjugate='T',
516
+ fromhex='T', # classmethod
517
+ hex='str',
518
+ is_integer='bint',
519
+ ),
520
+ 'list': dict(
521
+ copy='T',
522
+ count='Py_ssize_t',
523
+ index='Py_ssize_t',
524
+ ),
525
+ 'tuple': dict(
526
+ count='Py_ssize_t',
527
+ index='Py_ssize_t',
528
+ ),
529
+ 'str': dict(
530
+ capitalize='T',
531
+ casefold='T',
532
+ center='T',
533
+ count='Py_ssize_t',
534
+ encode='bytes',
535
+ endswith='bint',
536
+ expandtabs='T',
537
+ find='Py_ssize_t',
538
+ format='T',
539
+ format_map='T',
540
+ index='Py_ssize_t',
541
+ isalnum='bint',
542
+ isalpha='bint',
543
+ isascii='bint',
544
+ isdecimal='bint',
545
+ isdigit='bint',
546
+ isidentifier='bint',
547
+ islower='bint',
548
+ isnumeric='bint',
549
+ isprintable='bint',
550
+ isspace='bint',
551
+ istitle='bint',
552
+ isupper='bint',
553
+ join='T',
554
+ ljust='T',
555
+ lower='T',
556
+ lstrip='T',
557
+ maketrans='dict[int,object]', # staticmethod
558
+ partition='tuple[T,T,T]',
559
+ removeprefix='T',
560
+ removesuffix='T',
561
+ replace='T',
562
+ rfind='Py_ssize_t',
563
+ rindex='Py_ssize_t',
564
+ rjust='T',
565
+ rpartition='tuple[T,T,T]',
566
+ rsplit='list[T]',
567
+ rstrip='T',
568
+ split='list[T]',
569
+ splitlines='list[T]',
570
+ startswith='bint',
571
+ strip='T',
572
+ swapcase='T',
573
+ title='T',
574
+ translate='T',
575
+ upper='T',
576
+ zfill='T',
577
+ ),
578
+ 'bytes': dict(
579
+ capitalize='T',
580
+ center='T',
581
+ count='Py_ssize_t',
582
+ decode='str',
583
+ endswith='bint',
584
+ expandtabs='T',
585
+ find='Py_ssize_t',
586
+ fromhex='T', # classmethod
587
+ hex='str',
588
+ index='Py_ssize_t',
589
+ isalnum='bint',
590
+ isalpha='bint',
591
+ isascii='bint',
592
+ isdigit='bint',
593
+ islower='bint',
594
+ isspace='bint',
595
+ istitle='bint',
596
+ isupper='bint',
597
+ join='T',
598
+ ljust='T',
599
+ lower='T',
600
+ lstrip='T',
601
+ maketrans='bytes', # staticmethod
602
+ partition='tuple[T,T,T]',
603
+ removeprefix='T',
604
+ removesuffix='T',
605
+ replace='T',
606
+ rfind='Py_ssize_t',
607
+ rindex='Py_ssize_t',
608
+ rjust='T',
609
+ rpartition='tuple[T,T,T]',
610
+ rsplit='list[T]',
611
+ rstrip='T',
612
+ split='list[T]',
613
+ splitlines='list[T]',
614
+ startswith='bint',
615
+ strip='T',
616
+ swapcase='T',
617
+ title='T',
618
+ translate='T',
619
+ upper='T',
620
+ zfill='T',
621
+ ),
622
+ 'bytearray': dict(
623
+ # Inherited from 'bytes' below.
624
+ ),
625
+ 'memoryview': dict(
626
+ cast='T',
627
+ hex='str',
628
+ tobytes='bytes',
629
+ tolist='list',
630
+ toreadonly='T',
631
+ ),
632
+ 'set': dict(
633
+ copy='T',
634
+ difference='T',
635
+ intersection='T',
636
+ isdisjoint='bint',
637
+ issubset='bint',
638
+ issuperset='bint',
639
+ symmetric_difference='T',
640
+ union='T',
641
+ ),
642
+ 'frozenset': dict(
643
+ # Inherited from 'set' below.
644
+ ),
645
+ 'dict': dict(
646
+ copy='T',
647
+ fromkeys='T', # classmethod
648
+ popitem='tuple',
649
+ ),
650
+ }
651
+
652
+ inferred_method_return_types['bytearray'].update(inferred_method_return_types['bytes'])
653
+ inferred_method_return_types['frozenset'].update(inferred_method_return_types['set'])
654
+
655
+
656
+ def find_return_type_of_builtin_method(builtin_type, method_name):
657
+ type_name = builtin_type.name
658
+ if type_name in inferred_method_return_types:
659
+ methods = inferred_method_return_types[type_name]
660
+ if method_name in methods:
661
+ return_type_name = methods[method_name]
662
+ if '[' in return_type_name:
663
+ # TODO: Keep the "[...]" part when we add support for generics.
664
+ return_type_name = return_type_name.partition('[')[0]
665
+ if return_type_name == 'T':
666
+ return builtin_type
667
+ if 'T' in return_type_name:
668
+ return_type_name = return_type_name.replace('T', builtin_type.name)
669
+ if return_type_name == 'bint':
670
+ return PyrexTypes.c_bint_type
671
+ elif return_type_name == 'Py_ssize_t':
672
+ return PyrexTypes.c_py_ssize_t_type
673
+ return builtin_scope.lookup(return_type_name).type
674
+ return PyrexTypes.py_object_type
675
+
676
+
677
+ unsafe_compile_time_methods = {
678
+ # We name here only unsafe and non-portable methods if:
679
+ # - the type has a literal representation, allowing for constant folding.
680
+ # - the return type is not None (thus excluding modifier methods)
681
+ # and is listed in 'inferred_method_return_types' above.
682
+ #
683
+ # See the consistency check in TestBuiltin.py.
684
+ #
685
+ 'complex': set(),
686
+ 'int': {
687
+ 'bit_count', # Py3.10+
688
+ 'from_bytes', # classmethod
689
+ 'is_integer', # Py3.12+
690
+ 'to_bytes', # changed in Py3.11
691
+ },
692
+ 'float': {
693
+ 'fromhex', # classmethod
694
+ },
695
+ 'list': {
696
+ 'copy',
697
+ },
698
+ 'tuple': set(),
699
+ 'str': {
700
+ 'replace', # changed in Py3.13+
701
+ 'maketrans', # staticmethod
702
+ 'removeprefix', # Py3.9+
703
+ 'removesuffix', # Py3.9+
704
+ },
705
+ 'bytes': {
706
+ 'fromhex', # classmethod
707
+ 'maketrans', # staticmethod
708
+ 'removeprefix', # Py3.9+
709
+ 'removesuffix', # Py3.9+
710
+ },
711
+ 'set': set(),
712
+ }
713
+
714
+
715
+ def is_safe_compile_time_method(builtin_type_name: str, method_name: str):
716
+ unsafe_methods = unsafe_compile_time_methods.get(builtin_type_name)
717
+ if unsafe_methods is None:
718
+ # Not a literal type.
719
+ return False
720
+ if method_name in unsafe_methods:
721
+ # Not a safe method.
722
+ return False
723
+ known_methods = inferred_method_return_types.get(builtin_type_name)
724
+ if known_methods is None or method_name not in known_methods:
725
+ # Not a known method.
726
+ return False
727
+ return True
728
+
729
+
730
+ builtin_structs_table = [
731
+ ('Py_buffer', 'Py_buffer',
732
+ [("buf", PyrexTypes.c_void_ptr_type),
733
+ ("obj", PyrexTypes.py_object_type),
734
+ ("len", PyrexTypes.c_py_ssize_t_type),
735
+ ("itemsize", PyrexTypes.c_py_ssize_t_type),
736
+ ("readonly", PyrexTypes.c_bint_type),
737
+ ("ndim", PyrexTypes.c_int_type),
738
+ ("format", PyrexTypes.c_char_ptr_type),
739
+ ("shape", PyrexTypes.c_py_ssize_t_ptr_type),
740
+ ("strides", PyrexTypes.c_py_ssize_t_ptr_type),
741
+ ("suboffsets", PyrexTypes.c_py_ssize_t_ptr_type),
742
+ ("internal", PyrexTypes.c_void_ptr_type),
743
+ ]),
744
+ ('Py_complex', 'Py_complex',
745
+ [('real', PyrexTypes.c_double_type),
746
+ ('imag', PyrexTypes.c_double_type),
747
+ ])
748
+ ]
749
+
750
+ # set up builtin scope
751
+
752
+ builtin_scope = BuiltinScope()
753
+
754
+ def init_builtin_funcs():
755
+ for bf in builtin_function_table:
756
+ bf.declare_in_scope(builtin_scope)
757
+
758
+ builtin_types = {}
759
+
760
+ def init_builtin_types():
761
+ global builtin_types
762
+ for name, cname, methods in builtin_types_table:
763
+ if name == 'frozenset':
764
+ objstruct_cname = 'PySetObject'
765
+ elif name == 'bytearray':
766
+ objstruct_cname = 'PyByteArrayObject'
767
+ elif name == 'int':
768
+ objstruct_cname = 'PyLongObject'
769
+ elif name == 'str':
770
+ objstruct_cname = 'PyUnicodeObject'
771
+ elif name == 'bool':
772
+ objstruct_cname = 'PyLongObject'
773
+ elif name == 'BaseException':
774
+ objstruct_cname = "PyBaseExceptionObject"
775
+ elif name == 'Exception':
776
+ objstruct_cname = "PyBaseExceptionObject"
777
+ else:
778
+ objstruct_cname = 'Py%sObject' % name.capitalize()
779
+
780
+ utility_code = None
781
+ type_class = PyrexTypes.BuiltinObjectType
782
+ if name in ['dict', 'list', 'set', 'frozenset']:
783
+ type_class = PyrexTypes.BuiltinTypeConstructorObjectType
784
+ elif name == 'tuple':
785
+ type_class = PyrexTypes.PythonTupleTypeConstructor
786
+ elif name == 'range':
787
+ utility_code = range_utility_code
788
+ the_type = builtin_scope.declare_builtin_type(
789
+ name, cname, objstruct_cname=objstruct_cname, type_class=type_class, utility_code=utility_code)
790
+ builtin_types[name] = the_type
791
+ for method in methods:
792
+ method.declare_in_type(the_type)
793
+
794
+
795
+ def init_builtin_exceptions():
796
+ """Declare known builtin Python exceptions as types.
797
+ """
798
+ for name in KNOWN_PYTHON_BUILTINS:
799
+ if name in uncachable_builtins:
800
+ # Exclude builtins specific to later Python versions or platforms.
801
+ continue
802
+ if not PyrexTypes.is_exception_type_name(name):
803
+ continue
804
+ if builtin_scope.lookup_here(name) is not None:
805
+ # Already declared as builtin type above in a more specialised way.
806
+ continue
807
+ utility_code = UtilityCode(
808
+ proto=f"#define __Pyx_PyExc_{name}_Check(obj) __Pyx_TypeCheck(obj, PyExc_{name})",
809
+ name=f"Py{name}_Check",
810
+ )
811
+ builtin_types[name] = builtin_scope.declare_builtin_type(
812
+ name, f"((PyTypeObject*)PyExc_{name})", utility_code=utility_code)
813
+
814
+
815
+ def init_builtin_structs():
816
+ for name, cname, attribute_types in builtin_structs_table:
817
+ scope = StructOrUnionScope(name)
818
+ for attribute_name, attribute_type in attribute_types:
819
+ scope.declare_var(attribute_name, attribute_type, None,
820
+ attribute_name, allow_pyobject=True)
821
+ builtin_scope.declare_struct_or_union(
822
+ name, "struct", scope, 1, None, cname = cname)
823
+
824
+
825
+ def init_builtins():
826
+ #Errors.init_thread() # hopefully not needed - we should not emit warnings ourselves
827
+ init_builtin_structs()
828
+ init_builtin_types()
829
+ init_builtin_exceptions()
830
+ init_builtin_funcs()
831
+
832
+ entry = builtin_scope.declare_var(
833
+ '__debug__', PyrexTypes.c_const_type(PyrexTypes.c_bint_type),
834
+ pos=None, cname='__pyx_assertions_enabled()', is_cdef=True)
835
+ entry.utility_code = UtilityCode.load_cached("AssertionsEnabled", "Exceptions.c")
836
+
837
+ global type_type, list_type, tuple_type, dict_type, set_type, frozenset_type
838
+ global slice_type, range_type
839
+ global bytes_type, unicode_type, bytearray_type
840
+ global float_type, int_type, bool_type, complex_type
841
+ global memoryview_type, py_buffer_type
842
+ global sequence_types
843
+ type_type = builtin_scope.lookup('type').type
844
+ list_type = builtin_scope.lookup('list').type
845
+ tuple_type = builtin_scope.lookup('tuple').type
846
+ dict_type = builtin_scope.lookup('dict').type
847
+ set_type = builtin_scope.lookup('set').type
848
+ frozenset_type = builtin_scope.lookup('frozenset').type
849
+ slice_type = builtin_scope.lookup('slice').type
850
+ range_type = builtin_scope.lookup('range').type
851
+
852
+ bytes_type = builtin_scope.lookup('bytes').type
853
+ unicode_type = builtin_scope.lookup('str').type
854
+ bytearray_type = builtin_scope.lookup('bytearray').type
855
+ memoryview_type = builtin_scope.lookup('memoryview').type
856
+
857
+ float_type = builtin_scope.lookup('float').type
858
+ int_type = builtin_scope.lookup('int').type
859
+ bool_type = builtin_scope.lookup('bool').type
860
+ complex_type = builtin_scope.lookup('complex').type
861
+
862
+ sequence_types = (
863
+ list_type,
864
+ tuple_type,
865
+ bytes_type,
866
+ unicode_type,
867
+ bytearray_type,
868
+ memoryview_type,
869
+ )
870
+
871
+ # Set up type inference links between equivalent Python/C types
872
+ assert bool_type.name == 'bool', bool_type.name
873
+ bool_type.equivalent_type = PyrexTypes.c_bint_type
874
+ PyrexTypes.c_bint_type.equivalent_type = bool_type
875
+
876
+ assert float_type.name == 'float', float_type.name
877
+ float_type.equivalent_type = PyrexTypes.c_double_type
878
+ PyrexTypes.c_double_type.equivalent_type = float_type
879
+
880
+ assert complex_type.name == 'complex', complex_type.name
881
+ complex_type.equivalent_type = PyrexTypes.c_double_complex_type
882
+ PyrexTypes.c_double_complex_type.equivalent_type = complex_type
883
+
884
+ py_buffer_type = builtin_scope.lookup('Py_buffer').type
885
+
886
+
887
+ init_builtins()
888
+
889
+ ##############################
890
+ # Support for a few standard library modules that Cython understands (currently typing and dataclasses)
891
+ ##############################
892
+ _known_module_scopes = {}
893
+
894
+ def get_known_standard_library_module_scope(module_name):
895
+ mod = _known_module_scopes.get(module_name)
896
+ if mod:
897
+ return mod
898
+
899
+ if module_name == "typing":
900
+ mod = ModuleScope(module_name, None, None)
901
+ for name, tp in [
902
+ ('Dict', dict_type),
903
+ ('List', list_type),
904
+ ('Tuple', tuple_type),
905
+ ('Set', set_type),
906
+ ('FrozenSet', frozenset_type),
907
+ ]:
908
+ name = EncodedString(name)
909
+ entry = mod.declare_type(name, tp, pos = None)
910
+ var_entry = Entry(name, None, PyrexTypes.py_object_type)
911
+ var_entry.is_pyglobal = True
912
+ var_entry.is_variable = True
913
+ var_entry.scope = mod
914
+ entry.as_variable = var_entry
915
+ entry.known_standard_library_import = "%s.%s" % (module_name, name)
916
+
917
+ for name in ['ClassVar', 'Optional', 'Union']:
918
+ name = EncodedString(name)
919
+ indexed_type = PyrexTypes.SpecialPythonTypeConstructor(EncodedString("typing."+name))
920
+ entry = mod.declare_type(name, indexed_type, pos = None)
921
+ var_entry = Entry(name, None, PyrexTypes.py_object_type)
922
+ var_entry.is_pyglobal = True
923
+ var_entry.is_variable = True
924
+ var_entry.scope = mod
925
+ entry.as_variable = var_entry
926
+ entry.known_standard_library_import = "%s.%s" % (module_name, name)
927
+ _known_module_scopes[module_name] = mod
928
+ elif module_name == "dataclasses":
929
+ mod = ModuleScope(module_name, None, None)
930
+ indexed_type = PyrexTypes.SpecialPythonTypeConstructor(EncodedString("dataclasses.InitVar"))
931
+ initvar_string = EncodedString("InitVar")
932
+ entry = mod.declare_type(initvar_string, indexed_type, pos = None)
933
+ var_entry = Entry(initvar_string, None, PyrexTypes.py_object_type)
934
+ var_entry.is_pyglobal = True
935
+ var_entry.scope = mod
936
+ entry.as_variable = var_entry
937
+ entry.known_standard_library_import = "%s.InitVar" % module_name
938
+ for name in ["dataclass", "field"]:
939
+ mod.declare_var(EncodedString(name), PyrexTypes.py_object_type, pos=None)
940
+ _known_module_scopes[module_name] = mod
941
+ elif module_name == "functools":
942
+ mod = ModuleScope(module_name, None, None)
943
+ for name in ["total_ordering"]:
944
+ mod.declare_var(EncodedString(name), PyrexTypes.py_object_type, pos=None)
945
+ _known_module_scopes[module_name] = mod
946
+
947
+ return mod
948
+
949
+
950
+ def get_known_standard_library_entry(qualified_name):
951
+ name_parts = qualified_name.split(".")
952
+ module_name = EncodedString(name_parts[0])
953
+ rest = name_parts[1:]
954
+
955
+ if len(rest) > 1: # for now, we don't know how to deal with any nested modules
956
+ return None
957
+
958
+ mod = get_known_standard_library_module_scope(module_name)
959
+
960
+ # eventually handle more sophisticated multiple lookups if needed
961
+ if mod and rest:
962
+ return mod.lookup_here(rest[0])
963
+ return None
964
+
965
+
966
+ def exprnode_to_known_standard_library_name(node, env):
967
+ qualified_name_parts = []
968
+ known_name = None
969
+ while node.is_attribute:
970
+ qualified_name_parts.append(node.attribute)
971
+ node = node.obj
972
+ if node.is_name:
973
+ entry = env.lookup(node.name)
974
+ if entry and entry.known_standard_library_import:
975
+ if get_known_standard_library_entry(
976
+ entry.known_standard_library_import):
977
+ known_name = entry.known_standard_library_import
978
+ else:
979
+ standard_env = get_known_standard_library_module_scope(
980
+ entry.known_standard_library_import)
981
+ if standard_env:
982
+ qualified_name_parts.append(standard_env.name)
983
+ known_name = ".".join(reversed(qualified_name_parts))
984
+ return known_name