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