Cython 3.3.0__cp315-cp315-win_amd64.whl

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