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,3121 @@
1
+ #
2
+ # Symbol Table
3
+ #
4
+
5
+ # uses @try_finally_contextmanager
6
+ # cython: binding=True
7
+
8
+ import re
9
+ import copy
10
+ import operator
11
+
12
+ from ..Utils import try_finally_contextmanager
13
+ from .Errors import warning, error, InternalError, performance_hint
14
+ from .StringEncoding import EncodedString
15
+ from . import Options, Naming
16
+ from . import PyrexTypes
17
+ from .PyrexTypes import py_object_type, unspecified_type
18
+ from .TypeSlots import (
19
+ pyfunction_signature, pymethod_signature, richcmp_special_methods,
20
+ get_slot_table, get_property_accessor_signature)
21
+ from . import DebugFlags
22
+
23
+ from . import Code
24
+
25
+
26
+ def c_safe_identifier(cname):
27
+ # There are some C limitations on struct entry names.
28
+ if ((cname[:2] == '__' and not (cname.startswith(Naming.pyrex_prefix)
29
+ or cname in ('__weakref__', '__dict__')))
30
+ or cname in Naming.reserved_cnames):
31
+ cname = Naming.pyrex_prefix + cname
32
+ return cname
33
+
34
+
35
+ def punycodify_name(cname, mangle_with=None):
36
+ # if passed the mangle_with should be a byte string
37
+ # modified from PEP489
38
+ if cname.isascii():
39
+ return cname
40
+
41
+ cname = cname.encode('punycode').replace(b'-', b'_').decode('ascii')
42
+ if mangle_with:
43
+ # sometimes it necessary to mangle unicode names alone where
44
+ # they'll be inserted directly into C, because the punycode
45
+ # transformation can turn them into invalid identifiers
46
+ cname = "%s_%s" % (mangle_with, cname)
47
+ elif cname.startswith(Naming.pyrex_prefix):
48
+ # a punycode name could also be a valid ascii variable name so
49
+ # change the prefix to distinguish
50
+ cname = cname.replace(Naming.pyrex_prefix,
51
+ Naming.pyunicode_identifier_prefix, 1)
52
+
53
+ return cname
54
+
55
+
56
+ class BufferAux:
57
+ writable_needed = False
58
+
59
+ def __init__(self, buflocal_nd_var, rcbuf_var):
60
+ self.buflocal_nd_var = buflocal_nd_var
61
+ self.rcbuf_var = rcbuf_var
62
+
63
+ def __repr__(self):
64
+ return "<BufferAux %r>" % self.__dict__
65
+
66
+
67
+ class Entry:
68
+ # A symbol table entry in a Scope or ModuleNamespace.
69
+ #
70
+ # name string Python name of entity
71
+ # cname string C name of entity
72
+ # type PyrexType Type of entity
73
+ # doc string Doc string
74
+ # annotation ExprNode PEP 484/526 annotation
75
+ # init string Initial value
76
+ # visibility 'private' or 'public' or 'extern'
77
+ # is_builtin boolean Is an entry in the Python builtins dict
78
+ # is_cglobal boolean Is a C global variable
79
+ # is_pyglobal boolean Is a Python module-level variable
80
+ # or class attribute during
81
+ # class construction
82
+ # is_member boolean Is an assigned class member
83
+ # is_pyclass_attr boolean Is a name in a Python class namespace
84
+ # is_variable boolean Is a variable
85
+ # is_cfunction boolean Is a C function
86
+ # is_cmethod boolean Is a C method of an extension type
87
+ # is_builtin_cmethod boolean Is a C method of a builtin type (implies is_cmethod)
88
+ # is_unbound_cmethod boolean Is an unbound C method of an extension type
89
+ # is_final_cmethod boolean Is non-overridable C method
90
+ # is_inline_cmethod boolean Is inlined C method
91
+ # is_anonymous boolean Is a anonymous pyfunction entry
92
+ # is_type boolean Is a type definition
93
+ # is_cclass boolean Is an extension class
94
+ # is_cclass_var_rentry boolean Is a var entry of an extension type
95
+ # (Hack! Only needed because most C globals are
96
+ # static variables while these live in the module scope.
97
+ # Remove when fixed.)
98
+ # is_cpp_class boolean Is a C++ class
99
+ # is_const boolean Is a constant
100
+ # is_property boolean Is a property of an extension type:
101
+ # doc_cname string or None C const holding the docstring
102
+ # getter_cname string C func for getting property
103
+ # setter_cname string C func for setting or deleting property
104
+ # is_cproperty boolean Is an inline property of an external type
105
+ # is_self_arg boolean Is the "self" arg of an exttype method
106
+ # is_arg boolean Is the arg of a method
107
+ # is_local boolean Is a local variable
108
+ # in_closure boolean Is referenced in an inner scope
109
+ # in_subscope boolean Belongs to a generator expression scope
110
+ # is_readonly boolean Can't be assigned to
111
+ # func_cname string C func implementing Python func
112
+ # wrapperbase_cname [string] C wrapperbase object name
113
+ # func_modifiers [string] C function modifiers ('inline')
114
+ # pos position Source position where declared
115
+ # namespace_cname string If is_pyglobal, the C variable
116
+ # holding its home namespace
117
+ # pymethdef_cname string PyMethodDef structure
118
+ # signature Signature Arg & return types for Python func
119
+ # as_variable Entry Alternative interpretation of extension
120
+ # type name or builtin C function as a variable
121
+ # xdecref_cleanup boolean Use Py_XDECREF for error cleanup
122
+ # in_cinclude boolean Suppress C declaration code
123
+ # enum_values [Entry] For enum types, list of values
124
+ # qualified_name string "modname.funcname" or "modname.classname"
125
+ # or "modname.classname.funcname"
126
+ # is_declared_generic boolean Is declared as PyObject * even though its
127
+ # type is an extension type
128
+ # as_module None Module scope, if a cimported module
129
+ # is_inherited boolean Is an inherited attribute of an extension type
130
+ # pystring_cname string C name of Python version of string literal
131
+ # is_interned boolean For string const entries, value is interned
132
+ # is_identifier boolean For string const entries, value is an identifier
133
+ # used boolean
134
+ # is_special boolean Is a special method or property accessor
135
+ # of an extension type
136
+ # defined_in_pxd boolean Is defined in a .pxd file (not just declared)
137
+ # api boolean Generate C API for C class or function
138
+ # utility_code string Utility code needed when this entry is used
139
+ #
140
+ # buffer_aux BufferAux or None Extra information needed for buffer variables
141
+ # inline_func_in_pxd boolean Hacky special case for inline function in pxd file.
142
+ # Ideally this should not be necessary.
143
+ # might_overflow boolean In an arithmetic expression that could cause
144
+ # overflow (used for type inference).
145
+ # utility_code_definition For some Cython builtins, the utility code
146
+ # which contains the definition of the entry.
147
+ # Currently only supported for CythonScope entries.
148
+ # error_on_uninitialized Have Control Flow issue an error when this entry is
149
+ # used uninitialized
150
+ # cf_used boolean Entry is used
151
+ # is_fused_specialized boolean Whether this entry of a cdef or def function
152
+ # is a specialization
153
+ # is_cgetter boolean Is a c-level getter function
154
+ # is_cpp_optional boolean Entry should be declared as std::optional (cpp_locals directive)
155
+ # known_standard_library_import Either None (default), an empty string (definitely can't be determined)
156
+ # or a string of "modulename.something.attribute"
157
+ # Used for identifying imports from typing/dataclasses etc
158
+ # pytyping_modifiers Python type modifiers like "typing.ClassVar" but also "dataclasses.InitVar"
159
+ # enum_int_value None or int If known, the int that corresponds to this enum value
160
+ # specialiser function or None Callable to specialise a function to specific C arguments.
161
+
162
+ # TODO: utility_code and utility_code_definition serves the same purpose...
163
+
164
+ inline_func_in_pxd = False
165
+ borrowed = 0
166
+ init = ""
167
+ annotation = None
168
+ visibility = 'private'
169
+ is_builtin = 0
170
+ is_cglobal = 0
171
+ is_pyglobal = 0
172
+ is_member = 0
173
+ is_pyclass_attr = 0
174
+ is_variable = 0
175
+ is_cfunction = 0
176
+ is_cmethod = 0
177
+ is_builtin_cmethod = False
178
+ is_unbound_cmethod = 0
179
+ is_final_cmethod = 0
180
+ is_inline_cmethod = 0
181
+ is_anonymous = 0
182
+ is_type = 0
183
+ is_cclass = 0
184
+ is_cclass_var_entry = False # Remove when other cglobals are in the module scope
185
+ is_cpp_class = 0
186
+ is_const = 0
187
+ is_property = 0
188
+ is_cproperty = 0
189
+ doc_cname = None
190
+ getter_cname = None
191
+ setter_cname = None
192
+ is_self_arg = 0
193
+ is_arg = 0
194
+ is_local = 0
195
+ in_closure = 0
196
+ from_closure = 0
197
+ in_subscope = 0
198
+ is_declared_generic = 0
199
+ is_readonly = 0
200
+ pyfunc_cname = None
201
+ func_cname = None
202
+ func_modifiers = []
203
+ final_func_cname = None
204
+ doc = None
205
+ as_variable = None
206
+ xdecref_cleanup = 0
207
+ in_cinclude = 0
208
+ as_module = None
209
+ is_inherited = 0
210
+ pystring_cname = None
211
+ is_identifier = 0
212
+ is_interned = 0
213
+ used = 0
214
+ is_special = 0
215
+ defined_in_pxd = 0
216
+ is_implemented = 0
217
+ api = 0
218
+ utility_code = None
219
+ specialiser = None
220
+ is_overridable = 0
221
+ buffer_aux = None
222
+ prev_entry = None
223
+ might_overflow = 0
224
+ fused_cfunction = None
225
+ is_fused_specialized = False
226
+ utility_code_definition = None
227
+ needs_property = False
228
+ in_with_gil_block = 0
229
+ from_cython_utility_code = None
230
+ error_on_uninitialized = False
231
+ cf_used = True
232
+ outer_entry = None
233
+ is_cgetter = False
234
+ is_cpp_optional = False
235
+ known_standard_library_import = None
236
+ pytyping_modifiers = None
237
+ enum_int_value = None
238
+ vtable_type = None
239
+
240
+ def __init__(self, name, cname, type, pos = None, init = None):
241
+ self.name = name
242
+ self.cname = cname
243
+ self.type = type
244
+ self.pos = pos
245
+ self.init = init
246
+ self.overloaded_alternatives = []
247
+ self.cf_assignments = []
248
+ self.cf_references = []
249
+ self.inner_entries = []
250
+ self.defining_entry = self
251
+
252
+ # Debug helper to find places where entry types are assigned.
253
+ if DebugFlags.debug_verbose_entry_types:
254
+ @property
255
+ def type(self):
256
+ return self.__dict__['type']
257
+
258
+ @type.setter
259
+ def type(self, new_type):
260
+ print(f"ENTRY {self.name}[{self.cname}] TYPE: {self.__dict__.get('type')} -> {new_type}")
261
+ self.__dict__['type'] = new_type
262
+
263
+ def __repr__(self):
264
+ return "%s(<%x>, name=%s, type=%s)" % (type(self).__name__, id(self), self.name, self.type)
265
+
266
+ def already_declared_here(self):
267
+ error(self.pos, "Previous declaration is here")
268
+
269
+ def redeclared(self, pos):
270
+ error(pos, "'%s' does not match previous declaration" % self.name)
271
+ self.already_declared_here()
272
+
273
+ def all_alternatives(self):
274
+ return [self] + self.overloaded_alternatives
275
+
276
+ def best_function_match(self, scope, arg_types, fail_if_empty=False, arg_is_lvalue_array=None):
277
+ func_entry = None
278
+ if self.specialiser is not None:
279
+ func_entry = self.specialiser(scope, arg_types)
280
+ if func_entry is None:
281
+ if self.type.is_fused:
282
+ functypes = self.type.get_all_specialized_function_types()
283
+ alternatives = [f.entry for f in functypes]
284
+ else:
285
+ alternatives = self.all_alternatives()
286
+ func_entry = PyrexTypes.best_match(
287
+ arg_types, alternatives, fail_if_empty=fail_if_empty, arg_is_lvalue_array=arg_is_lvalue_array)
288
+ return func_entry
289
+
290
+ def all_entries(self):
291
+ return [self] + self.inner_entries
292
+
293
+ def __lt__(left, right):
294
+ if isinstance(left, Entry) and isinstance(right, Entry):
295
+ return (left.name, left.cname) < (right.name, right.cname)
296
+ else:
297
+ return NotImplemented
298
+
299
+ @property
300
+ def cf_is_reassigned(self):
301
+ return len(self.cf_assignments) > 1
302
+
303
+ def make_cpp_optional(self):
304
+ assert self.type.is_cpp_class
305
+ self.is_cpp_optional = True
306
+ assert not self.utility_code # we're not overwriting anything?
307
+ self.utility_code_definition = Code.UtilityCode.load_cached("OptionalLocals", "CppSupport.cpp")
308
+
309
+ def declared_with_pytyping_modifier(self, modifier_name):
310
+ return modifier_name in self.pytyping_modifiers if self.pytyping_modifiers else False
311
+
312
+
313
+ class InnerEntry(Entry):
314
+ """
315
+ An entry in a closure scope that represents the real outer Entry.
316
+ """
317
+ from_closure = True
318
+
319
+ def __init__(self, outer_entry, scope):
320
+ Entry.__init__(self, outer_entry.name,
321
+ outer_entry.cname,
322
+ outer_entry.type,
323
+ outer_entry.pos)
324
+ self.outer_entry = outer_entry
325
+ self.scope = scope
326
+
327
+ # share state with (outermost) defining entry
328
+ outermost_entry = outer_entry
329
+ while outermost_entry.outer_entry:
330
+ outermost_entry = outermost_entry.outer_entry
331
+ self.defining_entry = outermost_entry
332
+ self.inner_entries = outermost_entry.inner_entries
333
+ self.cf_assignments = outermost_entry.cf_assignments
334
+ self.cf_references = outermost_entry.cf_references
335
+ self.overloaded_alternatives = outermost_entry.overloaded_alternatives
336
+ self.is_cpp_optional = outermost_entry.is_cpp_optional
337
+ self.inner_entries.append(self)
338
+
339
+ def __getattr__(self, name):
340
+ if name.startswith('__'):
341
+ # we wouldn't have been called if it was there
342
+ raise AttributeError(name)
343
+ return getattr(self.defining_entry, name)
344
+
345
+ def all_entries(self):
346
+ return self.defining_entry.all_entries()
347
+
348
+
349
+ class Scope:
350
+ # name string Unqualified name
351
+ # outer_scope Scope or None Enclosing scope
352
+ # entries {string : Entry} Python name to entry, non-types
353
+ # const_entries [Entry] Constant entries
354
+ # type_entries [Entry] Struct/union/enum/typedef/exttype entries
355
+ # sue_entries [Entry] Struct/union/enum entries
356
+ # arg_entries [Entry] Function argument entries
357
+ # var_entries [Entry] User-defined variable entries
358
+ # pyfunc_entries [Entry] Python function entries
359
+ # cfunc_entries [Entry] C function entries
360
+ # c_class_entries [Entry] All extension type entries
361
+ # cname_to_entry {string : Entry} Temp cname to entry mapping
362
+ # return_type PyrexType or None Return type of function owning scope
363
+ # is_builtin_scope boolean Is the builtin scope of Python/Cython
364
+ # is_py_class_scope boolean Is a Python class scope
365
+ # is_c_class_scope boolean Is an extension type scope
366
+ # is_local_scope boolean Is a local (i.e. function/method/generator) scope
367
+ # is_closure_scope boolean Is a closure scope
368
+ # is_generator_expression_scope boolean A subset of closure scope used for generator expressions
369
+ # is_passthrough boolean Outer scope is passed directly
370
+ # is_cpp_class_scope boolean Is a C++ class scope
371
+ # is_property_scope boolean Is a extension type property scope
372
+ # is_c_dataclass_scope boolean or "frozen" is a cython.dataclasses.dataclass
373
+ # scope_prefix string Disambiguator for C names
374
+ # in_cinclude boolean Suppress C declaration code
375
+ # qualified_name string "modname" or "modname.classname"
376
+ # Python strings in this scope
377
+ # nogil boolean In a nogil section
378
+ # directives dict Helper variable for the recursive
379
+ # analysis, contains directive values.
380
+ # is_internal boolean Is only used internally (simpler setup)
381
+ # scope_predefined_names list of str Class variable containing special names defined by
382
+ # this type of scope (e.g. __builtins__, __qualname__)
383
+ # node_positions_to_offset {pos: offset} Mapping from node positions to line table offsets
384
+
385
+ is_builtin_scope = 0
386
+ is_py_class_scope = 0
387
+ is_c_class_scope = 0
388
+ is_closure_scope = 0
389
+ is_local_scope = False
390
+ is_generator_expression_scope = 0
391
+ is_comprehension_scope = 0
392
+ is_passthrough = 0
393
+ is_cpp_class_scope = 0
394
+ is_property_scope = 0
395
+ is_module_scope = 0
396
+ is_c_dataclass_scope = False
397
+ is_internal = 0
398
+ scope_prefix = ""
399
+ in_cinclude = 0
400
+ nogil = 0
401
+ fused_to_specific = None
402
+ return_type = None
403
+ scope_predefined_names = []
404
+ # Do ambiguous type names like 'int' and 'float' refer to the C types? (Otherwise, Python types.)
405
+ in_c_type_context = True
406
+ node_positions_to_offset = {} # read-only fallback dict
407
+
408
+ def __init__(self, name, outer_scope, parent_scope):
409
+ # The outer_scope is the next scope in the lookup chain.
410
+ # The parent_scope is used to derive the qualified name of this scope.
411
+ self.name = name
412
+ self.outer_scope = outer_scope
413
+ self.parent_scope = parent_scope
414
+ mangled_name = "%d%s_" % (len(name), name.replace('.', '_dot_'))
415
+ qual_scope = self.qualifying_scope()
416
+ if qual_scope:
417
+ self.qualified_name = qual_scope.qualify_name(name)
418
+ self.scope_prefix = qual_scope.scope_prefix + mangled_name
419
+ else:
420
+ self.qualified_name = EncodedString(name)
421
+ self.scope_prefix = mangled_name
422
+ self.entries = {}
423
+ self.subscopes = set()
424
+ self.const_entries = []
425
+ self.type_entries = []
426
+ self.sue_entries = []
427
+ self.arg_entries = []
428
+ self.var_entries = []
429
+ self.pyfunc_entries = []
430
+ self.cfunc_entries = []
431
+ self.c_class_entries = []
432
+ self.defined_c_classes = []
433
+ self.imported_c_classes = {}
434
+ self.cname_to_entry = {}
435
+ self.identifier_to_entry = {}
436
+ self.num_to_entry = {}
437
+ self.obj_to_entry = {}
438
+ self.buffer_entries = []
439
+ self.lambda_defs = []
440
+ self.id_counters = {}
441
+ for var_name in self.scope_predefined_names:
442
+ self.declare_var(EncodedString(var_name), py_object_type, pos=None)
443
+
444
+ def __deepcopy__(self, memo):
445
+ return self
446
+
447
+ def merge_in(self, other, merge_unused=True, allowlist=None):
448
+ # Use with care...
449
+ entries = []
450
+ for name, entry in other.entries.items():
451
+ if not allowlist or name in allowlist:
452
+ if entry.used or merge_unused:
453
+ entries.append((name, entry))
454
+
455
+ self.entries.update(entries)
456
+
457
+ for attr in ('const_entries',
458
+ 'type_entries',
459
+ 'sue_entries',
460
+ 'arg_entries',
461
+ 'var_entries',
462
+ 'pyfunc_entries',
463
+ 'cfunc_entries',
464
+ 'c_class_entries'):
465
+ self_entries = getattr(self, attr)
466
+ names = {e.name for e in self_entries}
467
+ for entry in getattr(other, attr):
468
+ if (entry.used or merge_unused) and entry.name not in names:
469
+ self_entries.append(entry)
470
+
471
+ def __str__(self):
472
+ return "<%s %s>" % (self.__class__.__name__, self.qualified_name)
473
+
474
+ def qualifying_scope(self):
475
+ return self.parent_scope
476
+
477
+ def mangle(self, prefix, name = None):
478
+ if name:
479
+ return punycodify_name("%s%s%s" % (prefix, self.scope_prefix, name))
480
+ else:
481
+ return self.parent_scope.mangle(prefix, self.name)
482
+
483
+ def mangle_internal(self, name):
484
+ # Mangle an internal name so as not to clash with any
485
+ # user-defined name in this scope.
486
+ prefix = "%s%s_" % (Naming.pyrex_prefix, name)
487
+ return self.mangle(prefix)
488
+ #return self.parent_scope.mangle(prefix, self.name)
489
+
490
+ def mangle_class_private_name(self, name):
491
+ if self.parent_scope:
492
+ return self.parent_scope.mangle_class_private_name(name)
493
+ return name
494
+
495
+ def next_id(self, name=None):
496
+ # Return a cname fragment that is unique for this module
497
+ counters = self.global_scope().id_counters
498
+ try:
499
+ count = counters[name] + 1
500
+ except KeyError:
501
+ count = 0
502
+ counters[name] = count
503
+ if name:
504
+ if not count:
505
+ # unique names don't need a suffix, reoccurrences will get one
506
+ return name
507
+ return '%s%d' % (name, count)
508
+ else:
509
+ return '%d' % count
510
+
511
+ @property
512
+ def context(self):
513
+ return self.global_scope().context
514
+
515
+ def global_scope(self):
516
+ """ Return the module-level scope containing this scope. """
517
+ return self.outer_scope.global_scope()
518
+
519
+ def builtin_scope(self):
520
+ """ Return the module-level scope containing this scope. """
521
+ return self.outer_scope.builtin_scope()
522
+
523
+ def iter_local_scopes(self):
524
+ yield self
525
+ if self.subscopes:
526
+ yield from sorted(self.subscopes, key=operator.attrgetter('scope_prefix'))
527
+
528
+ @try_finally_contextmanager
529
+ def new_c_type_context(self, in_c_type_context=None):
530
+ old_c_type_context = self.in_c_type_context
531
+ if in_c_type_context is not None:
532
+ self.in_c_type_context = in_c_type_context
533
+ yield
534
+ self.in_c_type_context = old_c_type_context
535
+
536
+ def handle_already_declared_name(self, name, cname, type, pos, visibility, copy_entry=False):
537
+ """
538
+ Returns an entry or None
539
+
540
+ If it returns an entry, it makes sense for "declare" to keep using that
541
+ entry and not to declare its own.
542
+
543
+ May be overridden (e.g. for builtin scope,
544
+ which always allows redeclarations)
545
+ """
546
+ entry = None
547
+ entries = self.entries
548
+ old_entry = entries[name]
549
+
550
+ # Reject redeclared C++ functions only if they have a compatible type signature.
551
+ cpp_override_allowed = False
552
+ if type.is_cfunction and old_entry.type.is_cfunction and self.is_cpp():
553
+ # If we redefine a C++ class method which is either inherited
554
+ # or automatically generated (base constructor), then it's fine.
555
+ # Otherwise, we shout.
556
+ for alt_entry in old_entry.all_alternatives():
557
+ if type.compatible_signature_with(alt_entry.type):
558
+ if name == '<init>' and not type.args:
559
+ # Cython pre-declares the no-args constructor - allow later user definitions.
560
+ cpp_override_allowed = True
561
+ elif alt_entry.is_inherited:
562
+ # Note that we can override an inherited method with a compatible but not exactly equal signature, as in C++.
563
+ cpp_override_allowed = True
564
+ if cpp_override_allowed:
565
+ entry = alt_entry
566
+ if copy_entry:
567
+ entry = copy.copy(alt_entry)
568
+
569
+ # A compatible signature doesn't mean the exact same signature,
570
+ # so we're taking the new signature for the entry.
571
+ entry.type = type
572
+ entry.is_inherited = False
573
+ # Updating the entry attributes which can be modified in the method redefinition.
574
+ entry.cname = cname
575
+ entry.pos = pos
576
+ break
577
+ else:
578
+ cpp_override_allowed = True
579
+
580
+ if cpp_override_allowed:
581
+ # C++ function/method overrides with different signatures are ok.
582
+ pass
583
+ elif entries[name].is_inherited:
584
+ # Likewise ignore inherited classes.
585
+ pass
586
+ else:
587
+ if visibility == 'extern':
588
+ # Silenced outside of "cdef extern" blocks, until we have a safe way to
589
+ # prevent pxd-defined cpdef functions from ending up here.
590
+ warning(pos, "'%s' redeclared " % name, 1 if self.in_cinclude else 0)
591
+ elif visibility != 'ignore':
592
+ error(pos, "'%s' redeclared " % name)
593
+ self.entries[name].already_declared_here()
594
+ return None
595
+
596
+ return entry
597
+
598
+
599
+ def declare(self, name, cname, type, pos, visibility, shadow = 0, is_type = 0, create_wrapper = 0):
600
+ # Create new entry, and add to dictionary if
601
+ # name is not None. Reports a warning if already
602
+ # declared.
603
+ if type.is_buffer and not isinstance(self, LocalScope): # and not is_type:
604
+ error(pos, 'Buffer types only allowed as function local variables')
605
+ if not self.in_cinclude and cname and re.match("^_[_A-Z]+$", cname):
606
+ # See https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html#Reserved-Names
607
+ warning(pos, "'%s' is a reserved name in C." % cname, -1)
608
+
609
+ entries = self.entries
610
+ entry = None
611
+ if name and name in entries and not shadow:
612
+ entry = self.handle_already_declared_name(name, cname, type, pos, visibility)
613
+
614
+ if not entry:
615
+ entry = Entry(name, cname, type, pos = pos)
616
+ entry.in_cinclude = self.in_cinclude
617
+ entry.create_wrapper = create_wrapper
618
+
619
+ if name:
620
+ entry.qualified_name = self.qualify_name(name)
621
+ if not shadow:
622
+ if name in entries and self.is_cpp() and type.is_cfunction and not entries[name].is_cmethod:
623
+ # Which means: function or cppclass method is already present
624
+ entries[name].overloaded_alternatives.append(entry)
625
+ else:
626
+ entries[name] = entry
627
+
628
+ if type.is_memoryviewslice:
629
+ entry.init = type.default_value
630
+
631
+ entry.scope = self
632
+ entry.visibility = visibility
633
+ return entry
634
+
635
+ def qualify_name(self, name):
636
+ return EncodedString("%s.%s" % (self.qualified_name, name))
637
+
638
+ def declare_const(self, name, type, value, pos, cname = None, visibility = 'private', api = 0, create_wrapper = 0):
639
+ # Add an entry for a named constant.
640
+ if not cname:
641
+ if self.in_cinclude or (visibility == 'public' or api):
642
+ cname = name
643
+ else:
644
+ cname = self.mangle(Naming.enum_prefix, name)
645
+ entry = self.declare(name, cname, type, pos, visibility, create_wrapper = create_wrapper)
646
+ entry.is_const = 1
647
+ entry.value_node = value
648
+ return entry
649
+
650
+ def declare_type(self, name, type, pos,
651
+ cname = None, visibility = 'private', api = 0, defining = 1,
652
+ shadow = 0, template = 0):
653
+ # Add an entry for a type definition.
654
+ if not cname:
655
+ cname = name
656
+ entry = self.declare(name, cname, type, pos, visibility, shadow,
657
+ is_type=True)
658
+ entry.is_type = 1
659
+ entry.api = api
660
+ if defining:
661
+ self.type_entries.append(entry)
662
+
663
+ # don't replace an entry that's already set
664
+ if not template and getattr(type, "entry", None) is None:
665
+ type.entry = entry
666
+
667
+ # here we would set as_variable to an object representing this type
668
+ return entry
669
+
670
+ def declare_typedef(self, name, base_type, pos, cname = None,
671
+ visibility = 'private', api = 0):
672
+ if not cname:
673
+ if self.in_cinclude or (visibility != 'private' or api):
674
+ cname = name
675
+ else:
676
+ cname = self.mangle(Naming.type_prefix, name)
677
+ try:
678
+ if self.is_cpp_class_scope:
679
+ namespace = self.outer_scope.lookup(self.name).type
680
+ else:
681
+ namespace = None
682
+ type = PyrexTypes.create_typedef_type(name, base_type, cname,
683
+ (visibility == 'extern'),
684
+ namespace)
685
+ except ValueError as e:
686
+ error(pos, e.args[0])
687
+ type = PyrexTypes.error_type
688
+ entry = self.declare_type(name, type, pos, cname,
689
+ visibility = visibility, api = api)
690
+ type.qualified_name = entry.qualified_name
691
+ return entry
692
+
693
+ def declare_struct_or_union(self, name, kind, scope,
694
+ typedef_flag, pos, cname = None,
695
+ visibility = 'private', api = 0,
696
+ packed = False):
697
+ # Add an entry for a struct or union definition.
698
+ if not cname:
699
+ if self.in_cinclude or (visibility == 'public' or api):
700
+ cname = name
701
+ else:
702
+ cname = self.mangle(Naming.type_prefix, name)
703
+ entry = self.lookup_here(name)
704
+ if not entry:
705
+ in_cpp = self.is_cpp()
706
+ type = PyrexTypes.CStructOrUnionType(
707
+ name, kind, scope, typedef_flag, cname, packed,
708
+ in_cpp = in_cpp)
709
+ entry = self.declare_type(name, type, pos, cname,
710
+ visibility = visibility, api = api,
711
+ defining = scope is not None)
712
+ self.sue_entries.append(entry)
713
+ type.entry = entry
714
+ else:
715
+ if not (entry.is_type and entry.type.is_struct_or_union
716
+ and entry.type.kind == kind):
717
+ warning(pos, "'%s' redeclared " % name, 0)
718
+ elif scope and entry.type.scope:
719
+ warning(pos, "'%s' already defined (ignoring second definition)" % name, 0)
720
+ else:
721
+ self.check_previous_typedef_flag(entry, typedef_flag, pos)
722
+ self.check_previous_visibility(entry, visibility, pos)
723
+ if scope:
724
+ entry.type.scope = scope
725
+ self.type_entries.append(entry)
726
+ if self.is_cpp_class_scope:
727
+ entry.type.namespace = self.outer_scope.lookup(self.name).type
728
+ return entry
729
+
730
+ def declare_cpp_class(self, name, scope,
731
+ pos, cname = None, base_classes = (),
732
+ visibility = 'extern', templates = None):
733
+ if cname is None:
734
+ if self.in_cinclude or (visibility != 'private'):
735
+ cname = name
736
+ else:
737
+ cname = self.mangle(Naming.type_prefix, name)
738
+ base_classes = list(base_classes)
739
+ entry = self.lookup_here(name)
740
+ if not entry:
741
+ type = PyrexTypes.CppClassType(
742
+ name, scope, cname, base_classes, templates = templates)
743
+ entry = self.declare_type(name, type, pos, cname,
744
+ visibility = visibility, defining = scope is not None)
745
+ self.sue_entries.append(entry)
746
+ else:
747
+ if not (entry.is_type and entry.type.is_cpp_class):
748
+ error(pos, "'%s' redeclared " % name)
749
+ entry.already_declared_here()
750
+ return None
751
+ elif scope and entry.type.scope:
752
+ warning(pos, "'%s' already defined (ignoring second definition)" % name, 0)
753
+ else:
754
+ if scope:
755
+ entry.type.scope = scope
756
+ self.type_entries.append(entry)
757
+ if base_classes:
758
+ if entry.type.base_classes and entry.type.base_classes != base_classes:
759
+ error(pos, "Base type does not match previous declaration")
760
+ entry.already_declared_here()
761
+ else:
762
+ entry.type.base_classes = base_classes
763
+ if templates or entry.type.templates:
764
+ if templates != entry.type.templates:
765
+ error(pos, "Template parameters do not match previous declaration")
766
+ entry.already_declared_here()
767
+
768
+ def declare_inherited_attributes(entry, base_classes):
769
+ for base_class in base_classes:
770
+ if base_class is PyrexTypes.error_type:
771
+ continue
772
+ if base_class.scope is None:
773
+ error(pos, "Cannot inherit from incomplete type")
774
+ else:
775
+ declare_inherited_attributes(entry, base_class.base_classes)
776
+ entry.type.scope.declare_inherited_cpp_attributes(base_class)
777
+ if scope:
778
+ declare_inherited_attributes(entry, base_classes)
779
+ scope.declare_var(name="this", cname="this", type=PyrexTypes.CPtrType(entry.type), pos=entry.pos)
780
+ if self.is_cpp_class_scope:
781
+ entry.type.namespace = self.outer_scope.lookup(self.name).type
782
+ return entry
783
+
784
+ def check_previous_typedef_flag(self, entry, typedef_flag, pos):
785
+ if typedef_flag != entry.type.typedef_flag:
786
+ error(pos, "'%s' previously declared using '%s'" % (
787
+ entry.name, ("cdef", "ctypedef")[entry.type.typedef_flag]))
788
+
789
+ def check_previous_visibility(self, entry, visibility, pos):
790
+ if entry.visibility != visibility:
791
+ error(pos, "'%s' previously declared as '%s'" % (
792
+ entry.name, entry.visibility))
793
+
794
+ def declare_enum(self, name, pos, cname, scoped, typedef_flag,
795
+ visibility='private', api=0, create_wrapper=0, doc=None):
796
+ if name:
797
+ if not cname:
798
+ if (self.in_cinclude or visibility == 'public'
799
+ or visibility == 'extern' or api):
800
+ cname = name
801
+ else:
802
+ cname = self.mangle(Naming.type_prefix, name)
803
+ if self.is_cpp_class_scope:
804
+ namespace = self.outer_scope.lookup(self.name).type
805
+ else:
806
+ namespace = None
807
+
808
+ if scoped:
809
+ type = PyrexTypes.CppScopedEnumType(name, cname, namespace, doc=doc)
810
+ else:
811
+ type = PyrexTypes.CEnumType(name, cname, typedef_flag, namespace, doc=doc)
812
+ else:
813
+ type = PyrexTypes.c_anon_enum_type
814
+ entry = self.declare_type(name, type, pos, cname = cname,
815
+ visibility = visibility, api = api)
816
+ if scoped:
817
+ entry.utility_code = Code.UtilityCode.load_cached("EnumClassDecl", "CppSupport.cpp")
818
+ self.use_entry_utility_code(entry)
819
+ entry.create_wrapper = create_wrapper
820
+ entry.enum_values = []
821
+
822
+ self.sue_entries.append(entry)
823
+ return entry
824
+
825
+ def declare_tuple_type(self, pos, components):
826
+ return self.outer_scope.declare_tuple_type(pos, components)
827
+
828
+ def declare_var(self, name, type, pos,
829
+ cname=None, visibility='private',
830
+ api=False, in_pxd=False, is_cdef=False, pytyping_modifiers=None):
831
+ # Add an entry for a variable.
832
+ if not cname:
833
+ if visibility != 'private' or api:
834
+ cname = name
835
+ else:
836
+ cname = self.mangle(Naming.var_prefix, name)
837
+ entry = self.declare(name, cname, type, pos, visibility)
838
+ entry.is_variable = 1
839
+ if type.is_cpp_class and visibility != 'extern':
840
+ if self.directives['cpp_locals']:
841
+ entry.make_cpp_optional()
842
+ else:
843
+ type.check_nullary_constructor(pos)
844
+ if in_pxd and visibility != 'extern':
845
+ entry.defined_in_pxd = 1
846
+ entry.used = 1
847
+ if api:
848
+ entry.api = 1
849
+ entry.used = 1
850
+ if pytyping_modifiers:
851
+ entry.pytyping_modifiers = pytyping_modifiers
852
+ return entry
853
+
854
+ def _reject_pytyping_modifiers(self, pos, modifiers, allowed=()):
855
+ if not modifiers:
856
+ return
857
+ for modifier in modifiers:
858
+ if modifier not in allowed:
859
+ error(pos, "Modifier '%s' is not allowed here." % modifier)
860
+
861
+ def declare_assignment_expression_target(self, name, type, pos):
862
+ # In most cases declares the variable as normal.
863
+ # For generator expressions and comprehensions the variable is declared in their parent
864
+ return self.declare_var(name, type, pos)
865
+
866
+ def declare_builtin(self, name, pos):
867
+ name = self.mangle_class_private_name(name)
868
+ return self.outer_scope.declare_builtin(name, pos)
869
+
870
+ def _declare_pyfunction(self, name, pos, visibility='extern', entry=None):
871
+ if entry and not entry.type.is_cfunction:
872
+ error(pos, "'%s' already declared" % name)
873
+ error(entry.pos, "Previous declaration is here")
874
+ entry = self.declare_var(name, py_object_type, pos, visibility=visibility)
875
+ entry.signature = pyfunction_signature
876
+ self.pyfunc_entries.append(entry)
877
+ return entry
878
+
879
+ def declare_pyfunction(self, name, pos, allow_redefine=False, visibility='extern'):
880
+ # Add an entry for a Python function.
881
+ entry = self.lookup_here(name)
882
+ if not allow_redefine:
883
+ return self._declare_pyfunction(name, pos, visibility=visibility, entry=entry)
884
+ if entry:
885
+ if entry.type.is_unspecified:
886
+ entry.type = py_object_type
887
+ elif entry.type is not py_object_type:
888
+ return self._declare_pyfunction(name, pos, visibility=visibility, entry=entry)
889
+ else: # declare entry stub
890
+ self.declare_var(name, py_object_type, pos, visibility=visibility)
891
+ entry = self.declare_var(None, py_object_type, pos,
892
+ cname=name, visibility='private')
893
+ entry.name = EncodedString(name)
894
+ entry.qualified_name = self.qualify_name(name)
895
+ entry.signature = pyfunction_signature
896
+ entry.is_anonymous = True
897
+ return entry
898
+
899
+ def declare_lambda_function(self, lambda_name, pos):
900
+ # Add an entry for an anonymous Python function.
901
+ func_cname = self.mangle(Naming.lambda_func_prefix + 'funcdef_', lambda_name)
902
+ pymethdef_cname = self.mangle(Naming.lambda_func_prefix + 'methdef_', lambda_name)
903
+ qualified_name = self.qualify_name(lambda_name)
904
+
905
+ entry = self.declare(None, func_cname, py_object_type, pos, 'private')
906
+ entry.name = EncodedString(lambda_name)
907
+ entry.qualified_name = qualified_name
908
+ entry.pymethdef_cname = pymethdef_cname
909
+ entry.func_cname = func_cname
910
+ entry.signature = pyfunction_signature
911
+ entry.is_anonymous = True
912
+ return entry
913
+
914
+ def add_lambda_def(self, def_node):
915
+ self.lambda_defs.append(def_node)
916
+
917
+ def register_pyfunction(self, entry):
918
+ self.pyfunc_entries.append(entry)
919
+
920
+ def declare_cfunction(self, name, type, pos,
921
+ cname=None, visibility='private', api=0, in_pxd=0,
922
+ defining=0, modifiers=(), utility_code=None, overridable=False):
923
+ # Add an entry for a C function.
924
+ if not cname:
925
+ if visibility != 'private' or api:
926
+ cname = name
927
+ else:
928
+ cname = self.mangle(Naming.func_prefix, name)
929
+ inline_in_pxd = 'inline' in modifiers and in_pxd and defining
930
+ if inline_in_pxd:
931
+ # in_pxd does special things that we don't want to apply to inline functions
932
+ in_pxd = False
933
+ entry = self.lookup_here(name)
934
+ if entry:
935
+ if not in_pxd and visibility != entry.visibility and visibility == 'extern':
936
+ # Previously declared, but now extern => treat this
937
+ # as implementing the function, using the new cname
938
+ defining = True
939
+ visibility = entry.visibility
940
+ entry.cname = cname
941
+ entry.func_cname = cname
942
+ if visibility != 'private' and visibility != entry.visibility:
943
+ warning(pos, "Function '%s' previously declared as '%s', now as '%s'" % (
944
+ name, entry.visibility, visibility), 1)
945
+ if overridable != entry.is_overridable:
946
+ warning(pos, "Function '%s' previously declared as '%s'" % (
947
+ name, 'cpdef' if overridable else 'cdef'), 1)
948
+ if entry.type.same_as(type):
949
+ # Fix with_gil vs nogil.
950
+ entry.type = entry.type.with_with_gil(type.with_gil)
951
+ else:
952
+ if visibility == 'extern' and entry.visibility == 'extern':
953
+ can_override = self.is_builtin_scope
954
+ if self.is_cpp():
955
+ can_override = True
956
+ elif cname and not can_override:
957
+ # if all alternatives have different cnames,
958
+ # it's safe to allow signature overrides
959
+ for alt_entry in entry.all_alternatives():
960
+ if not alt_entry.cname or cname == alt_entry.cname:
961
+ break # cname not unique!
962
+ else:
963
+ can_override = True
964
+ if can_override:
965
+ temp = self.add_cfunction(name, type, pos, cname, visibility, modifiers)
966
+ temp.overloaded_alternatives = entry.all_alternatives()
967
+ if entry.specialiser is not None:
968
+ temp.specialiser = entry.specialiser
969
+ entry = temp
970
+ else:
971
+ warning(pos, "Function signature does not match previous declaration", 1)
972
+ entry.type = type
973
+ elif not in_pxd and entry.defined_in_pxd and type.compatible_signature_with(entry.type):
974
+ # TODO: check that this was done by a signature optimisation and not a user error.
975
+ #warning(pos, "Function signature does not match previous declaration", 1)
976
+
977
+ # Cython can't assume anything about cimported functions declared without
978
+ # an exception value. This is a performance problem mainly for nogil functions.
979
+ if entry.type.nogil and entry.type.exception_value is None and type.exception_value:
980
+ performance_hint(
981
+ entry.pos,
982
+ f"No exception value declared for '{entry.name}' in pxd file.\n"
983
+ "Users cimporting this function and calling it without the gil "
984
+ "will always require an exception check.\n"
985
+ "Suggest adding an explicit exception value.",
986
+ self)
987
+ entry.type = type
988
+ else:
989
+ error(pos, "Function signature does not match previous declaration")
990
+ else:
991
+ entry = self.add_cfunction(name, type, pos, cname, visibility, modifiers)
992
+ entry.func_cname = cname
993
+ entry.is_overridable = overridable
994
+ if inline_in_pxd:
995
+ entry.inline_func_in_pxd = True
996
+ if in_pxd and visibility != 'extern':
997
+ entry.defined_in_pxd = 1
998
+ if api:
999
+ entry.api = 1
1000
+ if not defining and not in_pxd and visibility != 'extern':
1001
+ error(pos, "Non-extern C function '%s' declared but not defined" % name)
1002
+ if defining:
1003
+ entry.is_implemented = True
1004
+ if modifiers:
1005
+ entry.func_modifiers = modifiers
1006
+ if utility_code:
1007
+ assert not entry.utility_code, "duplicate utility code definition in entry %s (%s)" % (name, cname)
1008
+ entry.utility_code = utility_code
1009
+ if overridable:
1010
+ # names of cpdef functions can be used as variables and can be assigned to
1011
+ var_entry = Entry(name, cname, py_object_type) # FIXME: cname?
1012
+ var_entry.qualified_name = self.qualify_name(name)
1013
+ var_entry.is_variable = 1
1014
+ var_entry.is_pyglobal = 1
1015
+ var_entry.scope = entry.scope
1016
+ entry.as_variable = var_entry
1017
+ type.entry = entry
1018
+ if (type.exception_check and type.exception_value is None and type.nogil and
1019
+ not pos[0].in_utility_code and
1020
+ # don't warn about external functions here - the user likely can't do anything
1021
+ defining and not in_pxd and not inline_in_pxd):
1022
+ PyrexTypes.write_noexcept_performance_hint(
1023
+ pos, self, function_name=name, void_return=type.return_type.is_void)
1024
+ return entry
1025
+
1026
+ def declare_cgetter(self, name, return_type, pos=None, cname=None,
1027
+ visibility="private", modifiers=(), defining=False, **cfunc_type_config):
1028
+ assert all(
1029
+ k in ('exception_value', 'exception_check', 'nogil', 'with_gil', 'is_const_method', 'is_static_method')
1030
+ for k in cfunc_type_config
1031
+ )
1032
+ cfunc_type = PyrexTypes.CFuncType(
1033
+ return_type,
1034
+ [PyrexTypes.CFuncTypeArg("self", self.parent_type, None)],
1035
+ **cfunc_type_config)
1036
+ entry = self.declare_cfunction(
1037
+ name, cfunc_type, pos, cname=None, visibility=visibility, modifiers=modifiers, defining=defining)
1038
+ entry.is_cgetter = True
1039
+ if cname is not None:
1040
+ entry.func_cname = cname
1041
+ return entry
1042
+
1043
+ def add_cfunction(self, name, type, pos, cname, visibility, modifiers, inherited=False):
1044
+ # Add a C function entry without giving it a func_cname.
1045
+ entry = self.declare(name, cname, type, pos, visibility)
1046
+ entry.is_cfunction = 1
1047
+ if modifiers:
1048
+ entry.func_modifiers = modifiers
1049
+ if inherited or type.is_fused:
1050
+ self.cfunc_entries.append(entry)
1051
+ else:
1052
+ # For backwards compatibility reasons, we must keep all non-fused methods
1053
+ # before all fused methods, but separately for each type.
1054
+ i = len(self.cfunc_entries)
1055
+ for cfunc_entry in reversed(self.cfunc_entries):
1056
+ if cfunc_entry.is_inherited or not cfunc_entry.type.is_fused:
1057
+ break
1058
+ i -= 1
1059
+ self.cfunc_entries.insert(i, entry)
1060
+ return entry
1061
+
1062
+ def find(self, name, pos):
1063
+ # Look up name, report error if not found.
1064
+ entry = self.lookup(name)
1065
+ if entry:
1066
+ return entry
1067
+ else:
1068
+ error(pos, "'%s' is not declared" % name)
1069
+
1070
+ def find_imported_module(self, path, pos):
1071
+ # Look up qualified name, must be a module, report error if not found.
1072
+ # Path is a list of names.
1073
+ scope = self
1074
+ for name in path:
1075
+ entry = scope.find(name, pos)
1076
+ if not entry:
1077
+ return None
1078
+ if entry.as_module:
1079
+ scope = entry.as_module
1080
+ else:
1081
+ error(pos, "'%s' is not a cimported module" % '.'.join(path))
1082
+ return None
1083
+ return scope
1084
+
1085
+ def lookup(self, name):
1086
+ # Look up name in this scope or an enclosing one.
1087
+ # Return None if not found.
1088
+
1089
+ mangled_name = self.mangle_class_private_name(name)
1090
+ entry = (self.lookup_here(name) # lookup here also does mangling
1091
+ or (self.outer_scope and self.outer_scope.lookup(mangled_name))
1092
+ or None)
1093
+ if entry:
1094
+ return entry
1095
+
1096
+ # look up the original name in the outer scope
1097
+ # Not strictly Python behaviour but see https://github.com/cython/cython/issues/3544
1098
+ entry = (self.outer_scope and self.outer_scope.lookup(name)) or None
1099
+ if entry and entry.is_pyglobal:
1100
+ self._emit_class_private_warning(entry.pos, name)
1101
+ return entry
1102
+
1103
+ def lookup_here(self, name):
1104
+ # Look up in this scope only, return None if not found.
1105
+
1106
+ entry = self.entries.get(self.mangle_class_private_name(name), None)
1107
+ if entry:
1108
+ return entry
1109
+ # Also check the unmangled name in the current scope
1110
+ # (even if mangling should give us something else).
1111
+ # This is to support things like global __foo which makes a declaration for __foo
1112
+ return self.entries.get(name, None)
1113
+
1114
+ def lookup_here_unmangled(self, name):
1115
+ return self.entries.get(name, None)
1116
+
1117
+ def lookup_assignment_expression_target(self, name):
1118
+ # For most cases behaves like "lookup_here".
1119
+ # However, it does look outwards for comprehension and generator expression scopes
1120
+ return self.lookup_here(name)
1121
+
1122
+ def lookup_target(self, name):
1123
+ # Look up name in this scope only. Declare as Python
1124
+ # variable if not found.
1125
+ entry = self.lookup_here(name)
1126
+ if not entry:
1127
+ entry = self.lookup_here_unmangled(name)
1128
+ if entry and entry.is_pyglobal:
1129
+ self._emit_class_private_warning(entry.pos, name)
1130
+ if not entry:
1131
+ entry = self.declare_var(name, py_object_type, None)
1132
+ return entry
1133
+
1134
+ def _type_or_specialized_type_from_entry(self, entry):
1135
+ if entry and entry.is_type:
1136
+ if entry.type.is_fused and self.fused_to_specific:
1137
+ return entry.type.specialize(self.fused_to_specific)
1138
+ return entry.type
1139
+ return None
1140
+
1141
+ def lookup_type(self, name):
1142
+ entry = self.lookup(name)
1143
+ # The logic here is:
1144
+ # 1. if entry is a type then return it (and maybe specialize it)
1145
+ # 2. if the entry comes from a known standard library import then follow that
1146
+ # 3. repeat step 1 with the (possibly) updated entry
1147
+
1148
+ tp = self._type_or_specialized_type_from_entry(entry)
1149
+ if tp:
1150
+ return tp
1151
+ # allow us to find types from the "typing" module and similar
1152
+ if entry and entry.known_standard_library_import:
1153
+ from .Builtin import get_known_standard_library_entry
1154
+ entry = get_known_standard_library_entry(entry.known_standard_library_import)
1155
+ return self._type_or_specialized_type_from_entry(entry)
1156
+
1157
+ def lookup_operator(self, operator, operands):
1158
+ if operands[0].type.is_cpp_class:
1159
+ obj_type = operands[0].type
1160
+ method = obj_type.scope.lookup("operator%s" % operator)
1161
+ if method is not None:
1162
+ arg_types = [arg.type for arg in operands[1:]]
1163
+ res = PyrexTypes.best_match(arg_types, method.all_alternatives())
1164
+ if res is not None:
1165
+ return res
1166
+ function = self.lookup("operator%s" % operator)
1167
+ function_alternatives = []
1168
+ if function is not None:
1169
+ function_alternatives = function.all_alternatives()
1170
+
1171
+ # look-up nonmember methods listed within a class
1172
+ method_alternatives = []
1173
+ if len(operands) == 2: # binary operators only
1174
+ for n in range(2):
1175
+ if operands[n].type.is_cpp_class:
1176
+ obj_type = operands[n].type
1177
+ method = obj_type.scope.lookup("operator%s" % operator)
1178
+ if method is not None:
1179
+ method_alternatives += method.all_alternatives()
1180
+
1181
+ if (not method_alternatives) and (not function_alternatives):
1182
+ return None
1183
+
1184
+ # select the unique alternatives
1185
+ all_alternatives = list(set(method_alternatives + function_alternatives))
1186
+
1187
+ return PyrexTypes.best_match([arg.type for arg in operands],
1188
+ all_alternatives)
1189
+
1190
+ def lookup_operator_for_types(self, pos, operator, types):
1191
+ from .Nodes import Node
1192
+ class FakeOperand(Node):
1193
+ pass
1194
+ operands = [FakeOperand(pos, type=type) for type in types]
1195
+ return self.lookup_operator(operator, operands)
1196
+
1197
+ def _emit_class_private_warning(self, pos, name):
1198
+ warning(pos, "Global name %s matched from within class scope "
1199
+ "in contradiction to Python 'class private name' rules. "
1200
+ "This may change in a future release." % name, 1)
1201
+
1202
+ def use_utility_code(self, new_code):
1203
+ self.global_scope().use_utility_code(new_code)
1204
+
1205
+ def use_entry_utility_code(self, entry):
1206
+ self.global_scope().use_entry_utility_code(entry)
1207
+
1208
+ def defines_any(self, names):
1209
+ # Test whether any of the given names are defined in this scope.
1210
+ for name in names:
1211
+ if name in self.entries:
1212
+ return 1
1213
+ return 0
1214
+
1215
+ def defines_any_special(self, names):
1216
+ # Test whether any of the given names are defined as special methods in this scope.
1217
+ for name in names:
1218
+ if name in self.entries and self.entries[name].is_special:
1219
+ return 1
1220
+ return 0
1221
+
1222
+ def infer_types(self):
1223
+ from .TypeInference import get_type_inferer
1224
+ get_type_inferer().infer_types(self)
1225
+
1226
+ def is_cpp(self):
1227
+ outer = self.outer_scope
1228
+ if outer is None:
1229
+ return False
1230
+ else:
1231
+ return outer.is_cpp()
1232
+
1233
+ def add_include_file(self, filename, verbatim_include=None, late=False):
1234
+ self.outer_scope.add_include_file(filename, verbatim_include, late)
1235
+
1236
+ def name_in_module_state(self, cname):
1237
+ # TODO - override to give more choices depending on the type of scope
1238
+ # e.g. slot, function, method
1239
+ return f"{Naming.modulestateglobal_cname}->{cname}"
1240
+
1241
+ def find_shared_usages_of_type(self, type_to_find, _seen_scopes=None):
1242
+ if _seen_scopes is None:
1243
+ _seen_scopes = set()
1244
+ include_all_entries = not self.is_module_scope
1245
+ for entry in self.entries.values():
1246
+ if not (include_all_entries or entry.defined_in_pxd or entry.visibility == "public" or entry.api):
1247
+ continue
1248
+ entry_subtypes = PyrexTypes.get_all_subtypes(entry.type)
1249
+ if any(type_to_find == sub_tp for sub_tp in entry_subtypes):
1250
+ return True
1251
+ type_scope = getattr(entry.type, "scope", None)
1252
+ if type_scope is None or type_scope in _seen_scopes:
1253
+ continue
1254
+ _seen_scopes.add(type_scope)
1255
+ if type_scope.find_shared_usages_of_type(type_to_find, _seen_scopes):
1256
+ return True
1257
+ return False
1258
+
1259
+
1260
+ class PreImportScope(Scope):
1261
+
1262
+ namespace_cname = Naming.preimport_cname
1263
+
1264
+ def __init__(self):
1265
+ Scope.__init__(self, Options.pre_import, None, None)
1266
+
1267
+ def declare_builtin(self, name, pos):
1268
+ entry = self.declare(name, name, py_object_type, pos, 'private')
1269
+ entry.is_variable = True
1270
+ entry.is_pyglobal = True
1271
+ return entry
1272
+
1273
+
1274
+ class BuiltinScope(Scope):
1275
+ # The builtin namespace.
1276
+
1277
+ is_builtin_scope = True
1278
+
1279
+ def __init__(self):
1280
+ if Options.pre_import is None:
1281
+ Scope.__init__(self, "__builtin__", None, None)
1282
+ else:
1283
+ Scope.__init__(self, "__builtin__", PreImportScope(), None)
1284
+ self.type_names = {}
1285
+
1286
+ def lookup(self, name, language_level=None):
1287
+ # 'language_level' is passed by ModuleScope
1288
+ if name == 'unicode' or name == 'basestring':
1289
+ # Keep recognising 'unicode' and 'basestring' in legacy code but map them to 'str'.
1290
+ name = 'str'
1291
+ elif name == 'long' and language_level == 2:
1292
+ # Keep recognising 'long' in legacy Py2 code but map it to 'int'.
1293
+ name = 'int'
1294
+ return Scope.lookup(self, name)
1295
+
1296
+ def declare_builtin(self, name, pos):
1297
+ if name not in PyrexTypes.KNOWN_PYTHON_BUILTINS:
1298
+ if self.outer_scope is not None:
1299
+ return self.outer_scope.declare_builtin(name, pos)
1300
+ else:
1301
+ if Options.error_on_unknown_names:
1302
+ error(pos, "undeclared name not builtin: %s" % name)
1303
+ else:
1304
+ warning(pos, "undeclared name not builtin: %s" % name, 2)
1305
+
1306
+ def declare_builtin_cfunction(self, name, type, cname, python_equiv=None, utility_code=None, specialiser=None):
1307
+ # If python_equiv == "*", the Python equivalent has the same name
1308
+ # as the entry, otherwise it has the name specified by python_equiv.
1309
+ name = EncodedString(name)
1310
+ entry = self.declare_cfunction(name, type, None, cname, visibility='extern', utility_code=utility_code)
1311
+ if specialiser is not None:
1312
+ entry.specialiser = specialiser
1313
+ if python_equiv:
1314
+ if python_equiv == "*":
1315
+ python_equiv = name
1316
+ else:
1317
+ python_equiv = EncodedString(python_equiv)
1318
+ var_entry = Entry(python_equiv, python_equiv, py_object_type)
1319
+ var_entry.qualified_name = self.qualify_name(name)
1320
+ var_entry.is_variable = 1
1321
+ var_entry.is_builtin = 1
1322
+ var_entry.utility_code = utility_code
1323
+ var_entry.scope = entry.scope
1324
+ entry.as_variable = var_entry
1325
+ return entry
1326
+
1327
+ def declare_builtin_type(self, name, cname,
1328
+ objstruct_cname=None, type_class=PyrexTypes.BuiltinObjectType,
1329
+ utility_code=None):
1330
+ name = EncodedString(name)
1331
+ type = type_class(name, cname, objstruct_cname, type_check_utility_code=utility_code)
1332
+ scope = CClassScope(name, outer_scope=None, visibility='extern', parent_type=type)
1333
+ scope.directives = {}
1334
+ type.set_scope(scope)
1335
+ self.type_names[name] = 1
1336
+
1337
+ entry = self.declare_type(name, type, None, visibility='extern')
1338
+ if utility_code:
1339
+ entry.utility_code = utility_code
1340
+ if name == 'range' and 'xrange' not in self.entries:
1341
+ # Keep supporting legacy Py2 'xrange' because it's still in use.
1342
+ self.entries['xrange'] = entry
1343
+
1344
+ var_entry = Entry(
1345
+ name=entry.name,
1346
+ type=self.lookup('type').type, # make sure "type" is the first type declared...
1347
+ pos=entry.pos,
1348
+ cname=entry.type.typeptr_cname,
1349
+ )
1350
+ var_entry.qualified_name = self.qualify_name(name)
1351
+ var_entry.is_variable = 1
1352
+ var_entry.is_cglobal = 1
1353
+ var_entry.is_readonly = 1
1354
+ var_entry.is_builtin = 1
1355
+ var_entry.scope = self
1356
+ if Options.cache_builtins:
1357
+ var_entry.is_const = True
1358
+ if utility_code:
1359
+ var_entry.utility_code = utility_code
1360
+ entry.as_variable = var_entry
1361
+
1362
+ return type
1363
+
1364
+ def builtin_scope(self):
1365
+ return self
1366
+
1367
+ def handle_already_declared_name(self, name, cname, type, pos, visibility, copy_entry=False):
1368
+ # Overriding is OK in the builtin scope
1369
+ return None
1370
+
1371
+
1372
+ const_counter = 1 # As a temporary solution for compiling code in pxds
1373
+
1374
+ class ModuleScope(Scope):
1375
+ # module_name string Python name of the module
1376
+ # module_cname string C name of Python module object
1377
+ # #module_dict_cname string C name of module dict object
1378
+ # method_table_cname string C name of method table
1379
+ # doc string Module doc string
1380
+ # doc_cname string C name of module doc string
1381
+ # utility_code_list [UtilityCode] Queuing utility codes for forwarding to Code.py
1382
+ # c_includes {key: IncludeCode} C headers or verbatim code to be generated
1383
+ # See process_include() for more documentation
1384
+ # identifier_to_entry {string : Entry} Map identifier string const to entry
1385
+ # context Context
1386
+ # parent_module Scope Parent in the import namespace
1387
+ # module_entries {string : Entry} For cimport statements
1388
+ # type_names {string : 1} Set of type names (used during parsing)
1389
+ # included_files [string] Cython sources included with 'include'
1390
+ # pxd_file_loaded boolean Corresponding .pxd file has been processed
1391
+ # cimported_modules [ModuleScope] Modules imported with cimport
1392
+ # types_imported {PyrexType} Set of types for which import code generated
1393
+ # has_import_star boolean Module contains import *
1394
+ # cpp boolean Compiling a C++ file
1395
+ # is_cython_builtin boolean Is this the Cython builtin scope (or a child scope)
1396
+ # is_package boolean Is this a package module? (__init__)
1397
+
1398
+ is_module_scope = 1
1399
+ has_import_star = 0
1400
+ is_cython_builtin = 0
1401
+ old_style_globals = 0
1402
+ cpp = False
1403
+ namespace_cname_is_type = False
1404
+ scope_predefined_names = [
1405
+ '__builtins__', '__name__', '__file__', '__doc__', '__path__',
1406
+ '__spec__', '__loader__', '__package__', '__cached__',
1407
+ ]
1408
+
1409
+ def __init__(self, name, parent_module, context, is_package=False):
1410
+ from . import Builtin
1411
+ self.parent_module = parent_module
1412
+ outer_scope = Builtin.builtin_scope
1413
+ Scope.__init__(self, name, outer_scope, parent_module)
1414
+ self.is_package = is_package
1415
+ self.module_name = EncodedString(name)
1416
+ self._context = context
1417
+ self.module_cname = Naming.module_cname
1418
+ self.module_dict_cname = Naming.moddict_cname
1419
+ self.method_table_cname = Naming.methtable_cname
1420
+ self.doc = ""
1421
+ self.doc_cname = Naming.moddoc_cname
1422
+ self.utility_code_list = []
1423
+ self.module_entries = {}
1424
+ self.c_includes = {}
1425
+ self.type_names = dict(outer_scope.type_names)
1426
+ self.pxd_file_loaded = 0
1427
+ self.cimported_modules = []
1428
+ self.types_imported = set()
1429
+ self.included_files = []
1430
+ self.has_extern_class = 0
1431
+ self.cached_builtins = []
1432
+ self.undeclared_cached_builtins = []
1433
+ self.namespace_cname = self.module_cname
1434
+ self._cached_tuple_types = {}
1435
+ self._cached_defaults_c_class_entries = {}
1436
+ self.process_include(Code.IncludeCode("Python.h", initial=True))
1437
+
1438
+ def qualifying_scope(self):
1439
+ return self.parent_module
1440
+
1441
+ @property
1442
+ def context(self):
1443
+ return self._context
1444
+
1445
+ def global_scope(self):
1446
+ return self
1447
+
1448
+ def lookup(self, name, language_level=None):
1449
+ entry = self.lookup_here(name)
1450
+ if entry is not None:
1451
+ return entry
1452
+
1453
+ if language_level is None:
1454
+ language_level = self.context.language_level if self.context is not None else 3
1455
+ return self.outer_scope.lookup(name, language_level=language_level)
1456
+
1457
+ def declare_tuple_type(self, pos, components):
1458
+ components = tuple(components)
1459
+ try:
1460
+ ttype = self._cached_tuple_types[components]
1461
+ except KeyError:
1462
+ ttype = self._cached_tuple_types[components] = PyrexTypes.c_tuple_type(components)
1463
+ cname = ttype.cname
1464
+ entry = self.lookup_here(cname)
1465
+ if not entry:
1466
+ scope = StructOrUnionScope(cname)
1467
+ for ix, component in enumerate(components):
1468
+ scope.declare_var(name="f%s" % ix, type=component, pos=pos)
1469
+ struct_entry = self.declare_struct_or_union(
1470
+ cname + '_struct', 'struct', scope, typedef_flag=True, pos=pos, cname=cname)
1471
+ self.type_entries.remove(struct_entry)
1472
+ ttype.struct_entry = struct_entry
1473
+ entry = self.declare_type(cname, ttype, pos, cname)
1474
+ ttype.entry = entry
1475
+ return entry
1476
+
1477
+ def declare_defaults_c_class(self, pos, components):
1478
+ # returns an entry (for the c-class)
1479
+ components = tuple(components)
1480
+ try:
1481
+ return self._cached_defaults_c_class_entries[components]
1482
+ except KeyError:
1483
+ pass
1484
+
1485
+ cname = self.next_id(Naming.defaults_struct_prefix)
1486
+ cname = EncodedString(cname)
1487
+ entry = self._cached_defaults_c_class_entries[components] = self.declare_c_class(
1488
+ cname, pos, defining=True, implementing=True,
1489
+ objstruct_cname=cname)
1490
+ self.check_c_class(entry)
1491
+ entry.type.is_final_type = True
1492
+ scope = entry.type.scope
1493
+ scope.is_internal = True
1494
+ scope.is_defaults_class_scope = True
1495
+
1496
+ # zero pad the argument number so they can be sorted
1497
+ num_zeros = len(str(len(components)))
1498
+ build_argname = ("arg{:0>%dd}" % num_zeros).format
1499
+ for n, type_ in enumerate(components):
1500
+ arg_name = EncodedString(build_argname(n))
1501
+ scope.declare_var(arg_name, type_, pos=None, is_cdef=True)
1502
+ return entry
1503
+
1504
+ def declare_builtin(self, name, pos):
1505
+ if name not in PyrexTypes.KNOWN_PYTHON_BUILTINS \
1506
+ and name not in Code.renamed_py2_builtins_map \
1507
+ and name not in PyrexTypes.uncachable_builtins:
1508
+ if self.has_import_star:
1509
+ entry = self.declare_var(name, py_object_type, pos)
1510
+ return entry
1511
+ else:
1512
+ if Options.error_on_unknown_names:
1513
+ error(pos, "undeclared name not builtin: %s" % name)
1514
+ else:
1515
+ warning(pos, "undeclared name not builtin: %s" % name, 2)
1516
+ # unknown - assume it's builtin and look it up at runtime
1517
+ entry = self.declare(name, None, py_object_type, pos, 'private')
1518
+ entry.is_builtin = 1
1519
+ return entry
1520
+ if Options.cache_builtins:
1521
+ for entry in self.cached_builtins:
1522
+ if entry.name == name:
1523
+ return entry
1524
+ if name == 'globals' and not self.old_style_globals:
1525
+ return self.outer_scope.lookup('__Pyx_Globals')
1526
+ else:
1527
+ entry = self.declare(None, None, py_object_type, pos, 'private')
1528
+ if Options.cache_builtins and name not in PyrexTypes.uncachable_builtins:
1529
+ entry.is_builtin = 1
1530
+ entry.is_const = 1 # cached
1531
+ entry.name = name
1532
+ entry.cname = Naming.builtin_prefix + name
1533
+ self.cached_builtins.append(entry)
1534
+ self.undeclared_cached_builtins.append(entry)
1535
+ else:
1536
+ entry.is_builtin = 1
1537
+ entry.name = name
1538
+ entry.qualified_name = self.builtin_scope().qualify_name(name)
1539
+ return entry
1540
+
1541
+ def find_module(self, module_name, pos, relative_level=-1):
1542
+ # Find a module in the import namespace, interpreting
1543
+ # relative imports relative to this module's parent.
1544
+ # Finds and parses the module's .pxd file if the module
1545
+ # has not been referenced before.
1546
+ is_relative_import = relative_level is not None and relative_level > 0
1547
+ from_module = None
1548
+ absolute_fallback = False
1549
+ if relative_level is not None and relative_level > 0:
1550
+ # explicit relative cimport
1551
+ # error of going beyond top-level is handled in cimport node
1552
+ from_module = self
1553
+
1554
+ top_level = 1 if self.is_package else 0
1555
+ # * top_level == 1 when file is __init__.pyx, current package (from_module) is the current module
1556
+ # i.e. dot in `from . import ...` points to the current package
1557
+ # * top_level == 0 when file is regular module, current package (from_module) is parent module
1558
+ # i.e. dot in `from . import ...` points to the package where module is placed
1559
+ while relative_level > top_level and from_module:
1560
+ from_module = from_module.parent_module
1561
+ relative_level -= 1
1562
+
1563
+ elif relative_level != 0:
1564
+ # -1 or None: try relative cimport first, then absolute
1565
+ from_module = self.parent_module
1566
+ absolute_fallback = True
1567
+
1568
+ module_scope = self.global_scope()
1569
+ return module_scope.context.find_module(
1570
+ module_name, from_module=from_module, pos=pos, absolute_fallback=absolute_fallback, relative_import=is_relative_import)
1571
+
1572
+ def find_submodule(self, name, as_package=False):
1573
+ # Find and return scope for a submodule of this module,
1574
+ # creating a new empty one if necessary. Doesn't parse .pxd.
1575
+ if '.' in name:
1576
+ name, submodule = name.split('.', 1)
1577
+ else:
1578
+ submodule = None
1579
+ scope = self.lookup_submodule(name)
1580
+ if not scope:
1581
+ scope = ModuleScope(name, parent_module=self, context=self.context, is_package=True if submodule else as_package)
1582
+ self.module_entries[name] = scope
1583
+ if submodule:
1584
+ scope = scope.find_submodule(submodule, as_package=as_package)
1585
+ return scope
1586
+
1587
+ def lookup_submodule(self, name):
1588
+ # Return scope for submodule of this module, or None.
1589
+ if '.' in name:
1590
+ name, submodule = name.split('.', 1)
1591
+ else:
1592
+ submodule = None
1593
+ module = self.module_entries.get(name, None)
1594
+ if submodule and module is not None:
1595
+ module = module.lookup_submodule(submodule)
1596
+ return module
1597
+
1598
+ def add_include_file(self, filename, verbatim_include=None, late=False):
1599
+ """
1600
+ Add `filename` as include file. Add `verbatim_include` as
1601
+ verbatim text in the C file.
1602
+ Both `filename` and `verbatim_include` can be `None` or empty.
1603
+ """
1604
+ inc = Code.IncludeCode(filename, verbatim_include, late=late)
1605
+ self.process_include(inc)
1606
+
1607
+ def process_include(self, inc):
1608
+ """
1609
+ Add `inc`, which is an instance of `IncludeCode`, to this
1610
+ `ModuleScope`. This either adds a new element to the
1611
+ `c_includes` dict or it updates an existing entry.
1612
+
1613
+ In detail: the values of the dict `self.c_includes` are
1614
+ instances of `IncludeCode` containing the code to be put in the
1615
+ generated C file. The keys of the dict are needed to ensure
1616
+ uniqueness in two ways: if an include file is specified in
1617
+ multiple "cdef extern" blocks, only one `#include` statement is
1618
+ generated. Second, the same include might occur multiple times
1619
+ if we find it through multiple "cimport" paths. So we use the
1620
+ generated code (of the form `#include "header.h"`) as dict key.
1621
+
1622
+ If verbatim code does not belong to any include file (i.e. it
1623
+ was put in a `cdef extern from *` block), then we use a unique
1624
+ dict key: namely, the `sortkey()`.
1625
+
1626
+ One `IncludeCode` object can contain multiple pieces of C code:
1627
+ one optional "main piece" for the include file and several other
1628
+ pieces for the verbatim code. The `IncludeCode.dict_update`
1629
+ method merges the pieces of two different `IncludeCode` objects
1630
+ if needed.
1631
+ """
1632
+ key = inc.mainpiece()
1633
+ if key is None:
1634
+ key = inc.sortkey()
1635
+ inc.dict_update(self.c_includes, key)
1636
+ inc = self.c_includes[key]
1637
+
1638
+ def add_imported_module(self, scope):
1639
+ if scope not in self.cimported_modules:
1640
+ for inc in scope.c_includes.values():
1641
+ self.process_include(inc)
1642
+ self.cimported_modules.append(scope)
1643
+ for m in scope.cimported_modules:
1644
+ self.add_imported_module(m)
1645
+
1646
+ def add_imported_entry(self, name, entry, pos):
1647
+ if entry.is_pyglobal:
1648
+ # Allow cimports to follow imports.
1649
+ entry.is_variable = True
1650
+ if entry not in self.entries:
1651
+ self.entries[name] = entry
1652
+ else:
1653
+ warning(pos, "'%s' redeclared " % name, 0)
1654
+
1655
+ def declare_module(self, name, scope, pos):
1656
+ # Declare a cimported module. This is represented as a
1657
+ # Python module-level variable entry with a module
1658
+ # scope attached to it. Reports an error and returns
1659
+ # None if previously declared as something else.
1660
+ entry = self.lookup_here(name)
1661
+ if entry:
1662
+ if entry.is_pyglobal and entry.as_module is scope:
1663
+ return entry # Already declared as the same module
1664
+ if not (entry.is_pyglobal and not entry.as_module):
1665
+ # SAGE -- I put this here so Pyrex
1666
+ # cimport's work across directories.
1667
+ # Currently it tries to multiply define
1668
+ # every module appearing in an import list.
1669
+ # It shouldn't be an error for a module
1670
+ # name to appear again, and indeed the generated
1671
+ # code compiles fine.
1672
+ return entry
1673
+ else:
1674
+ entry = self.declare_var(name, py_object_type, pos)
1675
+ entry.is_variable = 0
1676
+ entry.as_module = scope
1677
+ self.add_imported_module(scope)
1678
+ return entry
1679
+
1680
+ def declare_var(self, name, type, pos,
1681
+ cname=None, visibility='private',
1682
+ api=False, in_pxd=False, is_cdef=False, pytyping_modifiers=None):
1683
+ # Add an entry for a global variable. If it is a Python
1684
+ # object type, and not declared with cdef, it will live
1685
+ # in the module dictionary, otherwise it will be a C
1686
+ # global variable.
1687
+ if visibility not in ('private', 'public', 'extern'):
1688
+ error(pos, "Module-level variable cannot be declared %s" % visibility)
1689
+ self._reject_pytyping_modifiers(pos, pytyping_modifiers, ('typing.Optional',)) # let's allow at least this one
1690
+ if not is_cdef:
1691
+ if type is unspecified_type:
1692
+ type = py_object_type
1693
+ if not (type.is_pyobject and not type.is_extension_type):
1694
+ raise InternalError(
1695
+ "Non-cdef global variable is not a generic Python object")
1696
+ if (is_cdef and visibility != "extern"
1697
+ and self.directives['subinterpreters_compatible'] != "no"):
1698
+ extra_warning = ""
1699
+ pyobject_warning = ""
1700
+ if type.is_pyobject:
1701
+ extra_warning = "\nPython objects should not be shared between interpreters"
1702
+ pyobject_warning = "Python "
1703
+ warning(
1704
+ pos,
1705
+ f"Global cdef {pyobject_warning}variable used with subinterpreter support enabled.\n"
1706
+ "This variable is not currently in the per-interpreter module state "
1707
+ "but this will likely change in future releases." +
1708
+ extra_warning,
1709
+ 2+(1 if extra_warning else 0))
1710
+
1711
+ if not cname:
1712
+ defining = not in_pxd
1713
+ if visibility == 'extern' or (visibility == 'public' and defining):
1714
+ cname = name
1715
+ else:
1716
+ cname = self.mangle(Naming.var_prefix, name)
1717
+
1718
+ entry = self.lookup_here(name)
1719
+ if entry and entry.defined_in_pxd:
1720
+ #if visibility != 'private' and visibility != entry.visibility:
1721
+ # warning(pos, "Variable '%s' previously declared as '%s'" % (name, entry.visibility), 1)
1722
+ if not entry.type.same_as(type):
1723
+ if visibility == 'extern' and entry.visibility == 'extern':
1724
+ warning(pos, "Variable '%s' type does not match previous declaration" % name, 1)
1725
+ entry.type = type
1726
+ #else:
1727
+ # error(pos, "Variable '%s' type does not match previous declaration" % name)
1728
+ if entry.visibility != "private":
1729
+ mangled_cname = self.mangle(Naming.var_prefix, name)
1730
+ if entry.cname == mangled_cname:
1731
+ cname = name
1732
+ entry.cname = name
1733
+ if not entry.is_implemented:
1734
+ entry.is_implemented = True
1735
+ return entry
1736
+
1737
+ entry = Scope.declare_var(self, name, type, pos,
1738
+ cname=cname, visibility=visibility,
1739
+ api=api, in_pxd=in_pxd, is_cdef=is_cdef, pytyping_modifiers=pytyping_modifiers)
1740
+ if is_cdef:
1741
+ entry.is_cglobal = 1
1742
+ if entry.type.declaration_value:
1743
+ entry.init = entry.type.declaration_value
1744
+ self.var_entries.append(entry)
1745
+ else:
1746
+ entry.is_pyglobal = 1
1747
+ if Options.cimport_from_pyx:
1748
+ entry.used = 1
1749
+ return entry
1750
+
1751
+ def declare_cfunction(self, name, type, pos,
1752
+ cname=None, visibility='private', api=0, in_pxd=0,
1753
+ defining=0, modifiers=(), utility_code=None, overridable=False):
1754
+ if not defining and 'inline' in modifiers:
1755
+ # TODO(github/1736): Make this an error.
1756
+ warning(pos, "Declarations should not be declared inline.", 1)
1757
+ # Add an entry for a C function.
1758
+ if not cname:
1759
+ if visibility == 'extern' or (visibility == 'public' and defining):
1760
+ cname = name
1761
+ else:
1762
+ cname = self.mangle(Naming.func_prefix, name)
1763
+ if visibility == 'extern' and type.optional_arg_count:
1764
+ error(pos, "Extern functions cannot have default arguments values.")
1765
+ entry = self.lookup_here(name)
1766
+ if entry and entry.defined_in_pxd:
1767
+ if entry.visibility != "private":
1768
+ mangled_cname = self.mangle(Naming.func_prefix, name)
1769
+ if entry.cname == mangled_cname:
1770
+ cname = name
1771
+ entry.cname = cname
1772
+ entry.func_cname = cname
1773
+ entry = Scope.declare_cfunction(
1774
+ self, name, type, pos,
1775
+ cname=cname, visibility=visibility, api=api, in_pxd=in_pxd,
1776
+ defining=defining, modifiers=modifiers, utility_code=utility_code,
1777
+ overridable=overridable)
1778
+ return entry
1779
+
1780
+ def declare_global(self, name, pos):
1781
+ entry = self.lookup_here(name)
1782
+ if not entry:
1783
+ self.declare_var(name, py_object_type, pos)
1784
+
1785
+ def use_utility_code(self, new_code):
1786
+ if new_code is not None:
1787
+ self.utility_code_list.append(new_code)
1788
+
1789
+ def use_entry_utility_code(self, entry):
1790
+ if entry is None:
1791
+ return
1792
+ if entry.utility_code:
1793
+ self.utility_code_list.append(entry.utility_code)
1794
+ if entry.utility_code_definition:
1795
+ self.utility_code_list.append(entry.utility_code_definition)
1796
+ for tp in PyrexTypes.get_all_subtypes(entry.type):
1797
+ if hasattr(tp, "entry") and tp.entry is not entry:
1798
+ self.use_entry_utility_code(tp.entry)
1799
+
1800
+ def declare_c_class(self, name, pos, defining=0, implementing=0,
1801
+ module_name=None, base_type=None, objstruct_cname=None,
1802
+ typeobj_cname=None, typeptr_cname=None, visibility='private',
1803
+ typedef_flag=0, api=0, check_size=None,
1804
+ buffer_defaults=None, shadow=0):
1805
+ # If this is a non-extern typedef class, expose the typedef, but use
1806
+ # the non-typedef struct internally to avoid needing forward
1807
+ # declarations for anonymous structs.
1808
+ if typedef_flag and visibility != 'extern':
1809
+ if not (visibility == 'public' or api):
1810
+ warning(pos, "ctypedef only valid for 'extern' , 'public', and 'api'", 2)
1811
+ objtypedef_cname = objstruct_cname
1812
+ typedef_flag = 0
1813
+ else:
1814
+ objtypedef_cname = None
1815
+ #
1816
+ # Look for previous declaration as a type
1817
+ #
1818
+ entry = self.lookup_here(name)
1819
+ if entry and not shadow:
1820
+ type = entry.type
1821
+ if not (entry.is_type and type.is_extension_type):
1822
+ entry = None # Will cause redeclaration and produce an error
1823
+ else:
1824
+ scope = type.scope
1825
+ if typedef_flag and (not scope or scope.defined):
1826
+ self.check_previous_typedef_flag(entry, typedef_flag, pos)
1827
+ if (scope and scope.defined) or (base_type and type.base_type):
1828
+ if base_type and base_type is not type.base_type:
1829
+ error(pos, "Base type does not match previous declaration")
1830
+ if base_type and not type.base_type:
1831
+ type.base_type = base_type
1832
+ #
1833
+ # Make a new entry if needed
1834
+ #
1835
+ if not entry or shadow:
1836
+ type = PyrexTypes.PyExtensionType(
1837
+ name, typedef_flag, base_type, visibility == 'extern', check_size=check_size)
1838
+ type.pos = pos
1839
+ type.buffer_defaults = buffer_defaults
1840
+ if objtypedef_cname is not None:
1841
+ type.objtypedef_cname = objtypedef_cname
1842
+ if visibility == 'extern':
1843
+ type.module_name = module_name
1844
+ else:
1845
+ type.module_name = self.qualified_name
1846
+ if typeptr_cname:
1847
+ type.typeptr_cname = typeptr_cname
1848
+ else:
1849
+ type.typeptr_cname = self.mangle(Naming.typeptr_prefix, name)
1850
+ entry = self.declare_type(name, type, pos, visibility = visibility,
1851
+ defining = 0, shadow = shadow)
1852
+ entry.is_cclass = True
1853
+ if objstruct_cname:
1854
+ type.objstruct_cname = objstruct_cname
1855
+ elif not entry.in_cinclude:
1856
+ type.objstruct_cname = self.mangle(Naming.objstruct_prefix, name)
1857
+ else:
1858
+ error(entry.pos,
1859
+ "Object name required for 'public' or 'extern' C class")
1860
+ self.attach_var_entry_to_c_class(entry)
1861
+ self.c_class_entries.append(entry)
1862
+ #
1863
+ # Check for re-definition and create scope if needed
1864
+ #
1865
+ if not type.scope:
1866
+ if defining or implementing:
1867
+ scope = CClassScope(name = name, outer_scope = self,
1868
+ visibility=visibility,
1869
+ parent_type=type)
1870
+ scope.directives = self.directives.copy()
1871
+ if base_type and base_type.scope:
1872
+ scope.declare_inherited_c_attributes(base_type.scope)
1873
+ type.set_scope(scope)
1874
+ self.type_entries.append(entry)
1875
+ else:
1876
+ if defining and type.scope.defined:
1877
+ error(pos, "C class '%s' already defined" % name)
1878
+ elif implementing and type.scope.implemented:
1879
+ error(pos, "C class '%s' already implemented" % name)
1880
+ #
1881
+ # Fill in options, checking for compatibility with any previous declaration
1882
+ #
1883
+ if defining:
1884
+ entry.defined_in_pxd = 1
1885
+ if implementing: # So that filenames in runtime exceptions refer to
1886
+ entry.pos = pos # the .pyx file and not the .pxd file
1887
+ if visibility != 'private' and entry.visibility != visibility:
1888
+ error(pos, "Class '%s' previously declared as '%s'"
1889
+ % (name, entry.visibility))
1890
+ if api:
1891
+ entry.api = 1
1892
+ if objstruct_cname:
1893
+ if type.objstruct_cname and type.objstruct_cname != objstruct_cname:
1894
+ error(pos, "Object struct name differs from previous declaration")
1895
+ type.objstruct_cname = objstruct_cname
1896
+ if typeobj_cname:
1897
+ if type.typeobj_cname and type.typeobj_cname != typeobj_cname:
1898
+ error(pos, "Type object name differs from previous declaration")
1899
+ type.typeobj_cname = typeobj_cname
1900
+
1901
+ if self.directives.get('final'):
1902
+ entry.type.is_final_type = True
1903
+ collection_type = self.directives.get('collection_type')
1904
+ if collection_type == 'sequence':
1905
+ entry.type.has_sequence_flag = True
1906
+ elif collection_type == 'mapping':
1907
+ entry.type.has_mapping_flag = True
1908
+
1909
+ # cdef classes are always exported, but we need to set it to
1910
+ # distinguish between unused Cython utility code extension classes
1911
+ entry.used = True
1912
+
1913
+ #
1914
+ # Return new or existing entry
1915
+ #
1916
+ return entry
1917
+
1918
+ def allocate_vtable_names(self, entry):
1919
+ # If extension type has a vtable, allocate vtable struct and
1920
+ # slot names for it.
1921
+ type = entry.type
1922
+ if type.base_type and type.base_type.vtabslot_cname:
1923
+ #print "...allocating vtabslot_cname because base type has one" ###
1924
+ type.vtabslot_cname = "%s.%s" % (
1925
+ Naming.obj_base_cname, type.base_type.vtabslot_cname)
1926
+ elif type.scope and type.scope.cfunc_entries:
1927
+ # one special case here: when inheriting from builtin
1928
+ # types, the methods may also be built-in, in which
1929
+ # case they won't need a vtable
1930
+ entry_count = len(type.scope.cfunc_entries)
1931
+ base_type = type.base_type
1932
+ while base_type:
1933
+ # FIXME: this will break if we ever get non-inherited C methods
1934
+ if not base_type.scope or entry_count > len(base_type.scope.cfunc_entries):
1935
+ break
1936
+ if base_type.is_builtin_type:
1937
+ # builtin base type defines all methods => no vtable needed
1938
+ return
1939
+ base_type = base_type.base_type
1940
+ #print "...allocating vtabslot_cname because there are C methods" ###
1941
+ type.vtabslot_cname = Naming.vtabslot_cname
1942
+ if type.vtabslot_cname:
1943
+ #print "...allocating other vtable related cnames" ###
1944
+ type.vtabstruct_cname = self.mangle(Naming.vtabstruct_prefix, entry.name)
1945
+ type.vtabptr_cname = self.mangle(Naming.vtabptr_prefix, entry.name)
1946
+
1947
+ def check_c_classes_pxd(self):
1948
+ # Performs post-analysis checking and finishing up of extension types
1949
+ # being implemented in this module. This is called only for the .pxd.
1950
+ #
1951
+ # Checks all extension types declared in this scope to
1952
+ # make sure that:
1953
+ #
1954
+ # * The extension type is fully declared
1955
+ #
1956
+ # Also allocates a name for the vtable if needed.
1957
+ #
1958
+ for entry in self.c_class_entries:
1959
+ # Check defined
1960
+ if not entry.type.scope:
1961
+ error(entry.pos, "C class '%s' is declared but not defined" % entry.name)
1962
+
1963
+ def check_c_class(self, entry):
1964
+ type = entry.type
1965
+ name = entry.name
1966
+ visibility = entry.visibility
1967
+ # Check defined
1968
+ if not type.scope:
1969
+ error(entry.pos, "C class '%s' is declared but not defined" % name)
1970
+ # Generate typeobj_cname
1971
+ if visibility != 'extern' and not type.typeobj_cname:
1972
+ type.typeobj_cname = self.mangle(Naming.typeobj_prefix, name)
1973
+ ## Generate typeptr_cname
1974
+ #type.typeptr_cname = self.mangle(Naming.typeptr_prefix, name)
1975
+ # Check C methods defined
1976
+ if type.scope:
1977
+ for method_entry in type.scope.cfunc_entries:
1978
+ if not method_entry.is_inherited and not method_entry.func_cname:
1979
+ error(method_entry.pos, "C method '%s' is declared but not defined" %
1980
+ method_entry.name)
1981
+ # Allocate vtable name if necessary
1982
+ if type.vtabslot_cname:
1983
+ #print "ModuleScope.check_c_classes: allocating vtable cname for", self ###
1984
+ type.vtable_cname = self.mangle(Naming.vtable_prefix, entry.name)
1985
+
1986
+ def check_c_classes(self):
1987
+ # Performs post-analysis checking and finishing up of extension types
1988
+ # being implemented in this module. This is called only for the main
1989
+ # .pyx file scope, not for cimported .pxd scopes.
1990
+ #
1991
+ # Checks all extension types declared in this scope to
1992
+ # make sure that:
1993
+ #
1994
+ # * The extension type is implemented
1995
+ # * All required object and type names have been specified or generated
1996
+ # * All non-inherited C methods are implemented
1997
+ #
1998
+ # Also allocates a name for the vtable if needed.
1999
+ #
2000
+ debug_check_c_classes = 0
2001
+ if debug_check_c_classes:
2002
+ print("Scope.check_c_classes: checking scope " + self.qualified_name)
2003
+ for entry in self.c_class_entries:
2004
+ if debug_check_c_classes:
2005
+ print("...entry %s %s" % (entry.name, entry))
2006
+ print("......type = ", entry.type)
2007
+ print("......visibility = ", entry.visibility)
2008
+ self.check_c_class(entry)
2009
+
2010
+ def check_c_functions(self):
2011
+ # Performs post-analysis checking making sure all
2012
+ # defined c functions are actually implemented.
2013
+ for name, entry in self.entries.items():
2014
+ if entry.is_cfunction:
2015
+ if (entry.defined_in_pxd
2016
+ and entry.scope is self
2017
+ and entry.visibility != 'extern'
2018
+ and not entry.in_cinclude
2019
+ and not entry.is_implemented):
2020
+ error(entry.pos, "Non-extern C function '%s' declared but not defined" % name)
2021
+
2022
+ def attach_var_entry_to_c_class(self, entry):
2023
+ # The name of an extension class has to serve as both a type
2024
+ # name and a variable name holding the type object. It is
2025
+ # represented in the symbol table by a type entry with a
2026
+ # variable entry attached to it. For the variable entry,
2027
+ # we use a read-only C global variable whose name is an
2028
+ # expression that refers to the type object.
2029
+ from . import Builtin
2030
+ var_entry = Entry(name = entry.name,
2031
+ type = Builtin.type_type,
2032
+ pos = entry.pos,
2033
+ cname = entry.type.typeptr_cname)
2034
+ var_entry.qualified_name = entry.qualified_name
2035
+ var_entry.is_variable = 1
2036
+ var_entry.is_cglobal = 1
2037
+ var_entry.is_readonly = 1
2038
+ var_entry.is_cclass_var_entry = True
2039
+ var_entry.scope = entry.scope
2040
+ entry.as_variable = var_entry
2041
+
2042
+ def is_cpp(self):
2043
+ return self.cpp
2044
+
2045
+ def infer_types(self):
2046
+ from .TypeInference import PyObjectTypeInferer
2047
+ PyObjectTypeInferer().infer_types(self)
2048
+
2049
+
2050
+ class LocalScope(Scope):
2051
+ is_local_scope = True
2052
+
2053
+ # Does the function have a 'with gil:' block?
2054
+ has_with_gil_block = False
2055
+
2056
+ # Transient attribute, used for symbol table variable declarations
2057
+ _in_with_gil_block = False
2058
+
2059
+ def __init__(self, name, outer_scope, parent_scope = None):
2060
+ if parent_scope is None:
2061
+ parent_scope = outer_scope
2062
+ Scope.__init__(self, name, outer_scope, parent_scope)
2063
+
2064
+ def mangle(self, prefix, name):
2065
+ return punycodify_name(prefix + name)
2066
+
2067
+ def declare_arg(self, name, type, pos):
2068
+ # Add an entry for an argument of a function.
2069
+ name = self.mangle_class_private_name(name)
2070
+ cname = self.mangle(Naming.var_prefix, name)
2071
+ entry = self.declare(name, cname, type, pos, 'private')
2072
+ entry.is_variable = 1
2073
+ if type.is_pyobject:
2074
+ entry.init = "0"
2075
+ entry.is_arg = 1
2076
+ #entry.borrowed = 1 # Not using borrowed arg refs for now
2077
+ self.arg_entries.append(entry)
2078
+ return entry
2079
+
2080
+ def declare_var(self, name, type, pos,
2081
+ cname=None, visibility='private',
2082
+ api=False, in_pxd=False, is_cdef=False, pytyping_modifiers=None):
2083
+ name = self.mangle_class_private_name(name)
2084
+ # Add an entry for a local variable.
2085
+ if visibility in ('public', 'readonly'):
2086
+ error(pos, "Local variable cannot be declared %s" % visibility)
2087
+ entry = Scope.declare_var(self, name, type, pos,
2088
+ cname=cname, visibility=visibility,
2089
+ api=api, in_pxd=in_pxd, is_cdef=is_cdef, pytyping_modifiers=pytyping_modifiers)
2090
+ if entry.type.declaration_value:
2091
+ entry.init = entry.type.declaration_value
2092
+ entry.is_local = 1
2093
+
2094
+ entry.in_with_gil_block = self._in_with_gil_block
2095
+ self.var_entries.append(entry)
2096
+ return entry
2097
+
2098
+ def declare_global(self, name, pos):
2099
+ # Pull entry from global scope into local scope.
2100
+ if self.lookup_here(name):
2101
+ warning(pos, "'%s' redeclared ", 0)
2102
+ else:
2103
+ entry = self.global_scope().lookup_target(name)
2104
+ self.entries[name] = entry
2105
+
2106
+ def declare_nonlocal(self, name, pos):
2107
+ # Pull entry from outer scope into local scope
2108
+ orig_entry = self.lookup_here(name)
2109
+ if orig_entry and orig_entry.scope is self and not orig_entry.from_closure:
2110
+ error(pos, "'%s' redeclared as nonlocal" % name)
2111
+ orig_entry.already_declared_here()
2112
+ else:
2113
+ entry = self.lookup(name)
2114
+ if entry is None or not entry.from_closure:
2115
+ error(pos, "no binding for nonlocal '%s' found" % name)
2116
+
2117
+ def _create_inner_entry_for_closure(self, name, entry):
2118
+ entry.in_closure = True
2119
+ inner_entry = InnerEntry(entry, self)
2120
+ inner_entry.is_variable = True
2121
+ self.entries[name] = inner_entry
2122
+ return inner_entry
2123
+
2124
+ def lookup(self, name):
2125
+ # Look up name in this scope or an enclosing one.
2126
+ # Return None if not found.
2127
+
2128
+ entry = Scope.lookup(self, name)
2129
+ if entry is not None:
2130
+ entry_scope = entry.scope
2131
+ while entry_scope.is_comprehension_scope:
2132
+ entry_scope = entry_scope.outer_scope
2133
+ if entry_scope is not self and entry_scope.is_closure_scope:
2134
+ if hasattr(entry.scope, "scope_class"):
2135
+ raise InternalError("lookup() after scope class created.")
2136
+ # The actual c fragment for the different scopes differs
2137
+ # on the outside and inside, so we make a new entry
2138
+ return self._create_inner_entry_for_closure(name, entry)
2139
+ return entry
2140
+
2141
+ def mangle_closure_cnames(self, outer_scope_cname):
2142
+ for scope in self.iter_local_scopes():
2143
+ for entry in scope.entries.values():
2144
+ if entry.from_closure:
2145
+ cname = entry.outer_entry.cname
2146
+ if self.is_passthrough:
2147
+ entry.cname = cname
2148
+ else:
2149
+ if cname.startswith(Naming.cur_scope_cname):
2150
+ cname = cname[len(Naming.cur_scope_cname)+2:]
2151
+ entry.cname = "%s->%s" % (outer_scope_cname, cname)
2152
+ elif entry.in_closure:
2153
+ entry.original_cname = entry.cname
2154
+ entry.cname = "%s->%s" % (Naming.cur_scope_cname, entry.cname)
2155
+ if entry.type.is_cpp_class and entry.scope.directives['cpp_locals']:
2156
+ entry.make_cpp_optional()
2157
+
2158
+
2159
+ class ComprehensionScope(Scope):
2160
+ """Scope for comprehensions (but not generator expressions, which use ClosureScope).
2161
+ As opposed to generators, these can be easily inlined in some cases, so all
2162
+ we really need is a scope that holds the loop variable(s).
2163
+ """
2164
+ is_comprehension_scope = True
2165
+
2166
+ def __init__(self, outer_scope):
2167
+ parent_scope = outer_scope
2168
+ # TODO: also ignore class scopes?
2169
+ while parent_scope.is_comprehension_scope:
2170
+ parent_scope = parent_scope.parent_scope
2171
+ name = parent_scope.global_scope().next_id(Naming.genexpr_id_ref)
2172
+ Scope.__init__(self, name, outer_scope, parent_scope)
2173
+ self.directives = outer_scope.directives
2174
+ self.genexp_prefix = "%s%d%s" % (Naming.pyrex_prefix, len(name), name)
2175
+
2176
+ # Class/ExtType scopes are filled at class creation time, i.e. from the
2177
+ # module init function or surrounding function.
2178
+ while outer_scope.is_comprehension_scope or outer_scope.is_c_class_scope or outer_scope.is_py_class_scope:
2179
+ outer_scope = outer_scope.outer_scope
2180
+ self.var_entries = outer_scope.var_entries # keep declarations outside
2181
+ outer_scope.subscopes.add(self)
2182
+
2183
+ def mangle(self, prefix, name):
2184
+ return '%s%s' % (self.genexp_prefix, self.parent_scope.mangle(prefix, name))
2185
+
2186
+ def declare_var(self, name, type, pos,
2187
+ cname=None, visibility='private',
2188
+ api=False, in_pxd=False, is_cdef=True, pytyping_modifiers=None):
2189
+ if type is unspecified_type:
2190
+ # if the outer scope defines a type for this variable, inherit it
2191
+ outer_entry = self.outer_scope.lookup(name)
2192
+ if outer_entry and outer_entry.is_variable:
2193
+ type = outer_entry.type # may still be 'unspecified_type' !
2194
+ self._reject_pytyping_modifiers(pos, pytyping_modifiers)
2195
+ # the parent scope needs to generate code for the variable, but
2196
+ # this scope must hold its name exclusively
2197
+ cname = '%s%s' % (self.genexp_prefix, self.parent_scope.mangle(Naming.var_prefix, name or self.next_id()))
2198
+ entry = self.declare(name, cname, type, pos, visibility)
2199
+ entry.is_variable = True
2200
+ if self.parent_scope.is_module_scope:
2201
+ entry.is_cglobal = True
2202
+ else:
2203
+ entry.is_local = True
2204
+ entry.in_subscope = True
2205
+ self.var_entries.append(entry)
2206
+ self.entries[name] = entry
2207
+ return entry
2208
+
2209
+ def declare_assignment_expression_target(self, name, type, pos):
2210
+ # should be declared in the parent scope instead
2211
+ return self.parent_scope.declare_var(name, type, pos)
2212
+
2213
+ def declare_pyfunction(self, name, pos, allow_redefine=False):
2214
+ return self.outer_scope.declare_pyfunction(
2215
+ name, pos, allow_redefine)
2216
+
2217
+ def declare_lambda_function(self, func_cname, pos):
2218
+ return self.outer_scope.declare_lambda_function(func_cname, pos)
2219
+
2220
+ def add_lambda_def(self, def_node):
2221
+ return self.outer_scope.add_lambda_def(def_node)
2222
+
2223
+ def lookup_assignment_expression_target(self, name):
2224
+ entry = self.lookup_here(name)
2225
+ if not entry:
2226
+ entry = self.parent_scope.lookup_assignment_expression_target(name)
2227
+ return entry
2228
+
2229
+
2230
+ class ClosureScope(LocalScope):
2231
+
2232
+ is_closure_scope = True
2233
+ is_pure_generator_scope = False
2234
+
2235
+ def __init__(self, name, scope_name, outer_scope, parent_scope=None,
2236
+ is_pure_generator_scope=False):
2237
+ LocalScope.__init__(self, name, outer_scope, parent_scope)
2238
+ self.is_pure_generator_scope = is_pure_generator_scope
2239
+ self.closure_cname = "%s%s" % (Naming.closure_scope_prefix, scope_name)
2240
+
2241
+ # def mangle_closure_cnames(self, scope_var):
2242
+ # for entry in self.entries.values() + self.temp_entries:
2243
+ # entry.in_closure = 1
2244
+ # LocalScope.mangle_closure_cnames(self, scope_var)
2245
+
2246
+ # def mangle(self, prefix, name):
2247
+ # return "%s->%s" % (self.cur_scope_cname, name)
2248
+ # return "%s->%s" % (self.closure_cname, name)
2249
+
2250
+ def declare_pyfunction(self, name, pos, allow_redefine=False):
2251
+ return LocalScope.declare_pyfunction(self, name, pos, allow_redefine, visibility='private')
2252
+
2253
+ def declare_assignment_expression_target(self, name, type, pos):
2254
+ return self.declare_var(name, type, pos)
2255
+
2256
+
2257
+ class GeneratorExpressionScope(ClosureScope):
2258
+ is_generator_expression_scope = True
2259
+
2260
+ def declare_assignment_expression_target(self, name, type, pos):
2261
+ entry = self.parent_scope.declare_var(name, type, pos)
2262
+ return self._create_inner_entry_for_closure(name, entry)
2263
+
2264
+ def lookup_assignment_expression_target(self, name):
2265
+ entry = self.lookup_here(name)
2266
+ if not entry:
2267
+ entry = self.parent_scope.lookup_assignment_expression_target(name)
2268
+ if entry:
2269
+ return self._create_inner_entry_for_closure(name, entry)
2270
+ return entry
2271
+
2272
+
2273
+ class StructOrUnionScope(Scope):
2274
+ # Namespace of a C struct or union.
2275
+
2276
+ def __init__(self, name="?"):
2277
+ Scope.__init__(self, name, outer_scope=None, parent_scope=None)
2278
+
2279
+ def declare_var(self, name, type, pos,
2280
+ cname=None, visibility='private',
2281
+ api=False, in_pxd=False, is_cdef=False, pytyping_modifiers=None,
2282
+ allow_pyobject=False, allow_memoryview=False, allow_refcounted=False):
2283
+ # Add an entry for an attribute.
2284
+ if not cname:
2285
+ cname = name
2286
+ if visibility == 'private':
2287
+ cname = c_safe_identifier(cname)
2288
+ if type.is_cfunction:
2289
+ type = PyrexTypes.CPtrType(type)
2290
+ self._reject_pytyping_modifiers(pos, pytyping_modifiers)
2291
+ entry = self.declare(name, cname, type, pos, visibility)
2292
+ entry.is_variable = 1
2293
+ self.var_entries.append(entry)
2294
+ if type.is_pyobject:
2295
+ if not allow_pyobject:
2296
+ error(pos, "C struct/union member cannot be a Python object")
2297
+ elif type.is_memoryviewslice:
2298
+ if not allow_memoryview:
2299
+ # Memory views wrap their buffer owner as a Python object.
2300
+ error(pos, "C struct/union member cannot be a memory view")
2301
+ elif type.needs_refcounting:
2302
+ if not allow_refcounted:
2303
+ error(pos, "C struct/union member cannot be reference-counted type '%s'" % type)
2304
+ return entry
2305
+
2306
+ def declare_cfunction(self, name, type, pos,
2307
+ cname=None, visibility='private', api=0, in_pxd=0,
2308
+ defining=0, modifiers=(), overridable=False): # currently no utility code ...
2309
+ if overridable:
2310
+ error(pos, "C struct/union member cannot be declared 'cpdef'")
2311
+ return self.declare_var(name, type, pos,
2312
+ cname=cname, visibility=visibility)
2313
+
2314
+
2315
+ class ClassScope(Scope):
2316
+ # Abstract base class for namespace of
2317
+ # Python class or extension type.
2318
+ #
2319
+ # class_name string Python name of the class
2320
+ # scope_prefix string Additional prefix for names
2321
+ # declared in the class
2322
+ # doc string or None Doc string
2323
+
2324
+ scope_predefined_names = ['__module__', '__qualname__']
2325
+
2326
+ def mangle_class_private_name(self, name):
2327
+ # a few utilitycode names need to specifically be ignored
2328
+ if name and name.lower().startswith("__pyx_"):
2329
+ return name
2330
+ if name and name.startswith('__') and not name.endswith('__'):
2331
+ name = EncodedString('_%s%s' % (self.class_name.lstrip('_'), name))
2332
+ return name
2333
+
2334
+ def __init__(self, name, outer_scope):
2335
+ Scope.__init__(self, name, outer_scope, outer_scope)
2336
+ self.class_name = name
2337
+ self.doc = None
2338
+
2339
+ def lookup(self, name):
2340
+ entry = Scope.lookup(self, name)
2341
+ if entry:
2342
+ return entry
2343
+ if name == "classmethod":
2344
+ # We don't want to use the builtin classmethod here 'cause it won't do the
2345
+ # right thing in this scope (as the class members aren't still functions).
2346
+ # Don't want to add a cfunction to this scope 'cause that would mess with
2347
+ # the type definition, so we just return the right entry.
2348
+ entry = Entry(
2349
+ "classmethod",
2350
+ "__Pyx_Method_ClassMethod",
2351
+ PyrexTypes.CFuncType(
2352
+ py_object_type,
2353
+ [PyrexTypes.CFuncTypeArg("", py_object_type, None)], 0, 0))
2354
+ entry.utility_code_definition = Code.UtilityCode.load_cached("ClassMethod", "CythonFunction.c")
2355
+ self.use_entry_utility_code(entry)
2356
+ entry.is_cfunction = 1
2357
+ entry.scope = self.builtin_scope()
2358
+ return entry
2359
+
2360
+
2361
+ class PyClassScope(ClassScope):
2362
+ # Namespace of a Python class.
2363
+
2364
+ is_py_class_scope = 1
2365
+ namespace_cname_is_type = False
2366
+
2367
+ def declare_var(self, name, type, pos,
2368
+ cname=None, visibility='private',
2369
+ api=False, in_pxd=False, is_cdef=False, pytyping_modifiers=None):
2370
+ name = self.mangle_class_private_name(name)
2371
+ if type is unspecified_type:
2372
+ type = py_object_type
2373
+ # Add an entry for a class attribute.
2374
+ entry = Scope.declare_var(self, name, type, pos,
2375
+ cname=cname, visibility=visibility,
2376
+ api=api, in_pxd=in_pxd, is_cdef=is_cdef, pytyping_modifiers=pytyping_modifiers)
2377
+ entry.is_pyglobal = 1
2378
+ entry.is_pyclass_attr = 1
2379
+ return entry
2380
+
2381
+ def declare_nonlocal(self, name, pos):
2382
+ # Pull entry from outer scope into local scope
2383
+ orig_entry = self.lookup_here(name)
2384
+ if orig_entry and orig_entry.scope is self and not orig_entry.from_closure:
2385
+ error(pos, "'%s' redeclared as nonlocal" % name)
2386
+ orig_entry.already_declared_here()
2387
+ else:
2388
+ entry = self.lookup(name)
2389
+ if entry is None:
2390
+ error(pos, "no binding for nonlocal '%s' found" % name)
2391
+ else:
2392
+ # FIXME: this works, but it's unclear if it's the
2393
+ # right thing to do
2394
+ self.entries[name] = entry
2395
+
2396
+ def declare_global(self, name, pos):
2397
+ # Pull entry from global scope into local scope.
2398
+ if self.lookup_here(name):
2399
+ warning(pos, "'%s' redeclared ", 0)
2400
+ else:
2401
+ entry = self.global_scope().lookup_target(name)
2402
+ self.entries[name] = entry
2403
+
2404
+ def add_default_value(self, type):
2405
+ return self.outer_scope.add_default_value(type)
2406
+
2407
+
2408
+ class CClassScope(ClassScope):
2409
+ # Namespace of an extension type.
2410
+ #
2411
+ # parent_type PyExtensionType
2412
+ # #typeobj_cname string or None
2413
+ # #objstruct_cname string
2414
+ # method_table_cname string
2415
+ # getset_table_cname string
2416
+ # has_pyobject_attrs boolean Any PyObject attributes?
2417
+ # has_memoryview_attrs boolean Any memory view attributes?
2418
+ # has_explicitly_constructable_class_attrs boolean Any attributes that
2419
+ # need an explicit constructor (e.g. C++ class non-pointers)?
2420
+ # has_cyclic_pyobject_attrs boolean Any PyObject attributes that may need GC?
2421
+ # property_entries [Entry]
2422
+ # defined boolean Defined in .pxd file
2423
+ # implemented boolean Defined in .pyx file
2424
+ # inherited_var_entries [Entry] Adapted var entries from base class
2425
+
2426
+ is_c_class_scope = 1
2427
+ is_closure_class_scope = False
2428
+ is_defaults_class_scope = False
2429
+
2430
+ has_pyobject_attrs = False
2431
+ has_memoryview_attrs = False
2432
+ has_explicitly_constructable_attrs = False
2433
+ has_cyclic_pyobject_attrs = False
2434
+ defined = False
2435
+ implemented = False
2436
+
2437
+ def __init__(self, name, outer_scope, visibility, parent_type):
2438
+ ClassScope.__init__(self, name, outer_scope)
2439
+ if visibility != 'extern':
2440
+ self.method_table_cname = outer_scope.mangle(Naming.methtab_prefix, name)
2441
+ self.getset_table_cname = outer_scope.mangle(Naming.gstab_prefix, name)
2442
+ self.property_entries = []
2443
+ self.inherited_var_entries = []
2444
+ self.parent_type = parent_type
2445
+ # Usually parent_type will be an extension type and so the typeptr_cname
2446
+ # can be used to calculate the namespace_cname. Occasionally other types
2447
+ # are used (e.g. numeric/complex types) and in these cases the typeptr
2448
+ # isn't relevant.
2449
+ if ((parent_type.is_builtin_type or parent_type.is_extension_type)
2450
+ and parent_type.typeptr_cname):
2451
+ self.namespace_cname = self.parent_type.typeptr_cname
2452
+ self.namespace_cname_is_type = True
2453
+
2454
+ def needs_gc(self):
2455
+ # If the type or any of its base types have Python-valued
2456
+ # C attributes, then it needs to participate in GC.
2457
+ if self.has_cyclic_pyobject_attrs and not self.directives.get('no_gc', False):
2458
+ return True
2459
+ if self.parent_type.is_external and not self.parent_type.is_builtin_type:
2460
+ # It's impossible to really know - external types are often incomplete.
2461
+ return True
2462
+ base_type = self.parent_type.base_type
2463
+ if base_type and base_type.scope is not None:
2464
+ return base_type.scope.needs_gc()
2465
+ elif self.parent_type.is_builtin_type:
2466
+ return not self.parent_type.is_gc_simple
2467
+ return False
2468
+
2469
+ def needs_trashcan(self):
2470
+ # If the trashcan directive is explicitly set to False,
2471
+ # unconditionally disable the trashcan.
2472
+ directive = self.directives.get('trashcan')
2473
+ if directive is False:
2474
+ return False
2475
+ # If the directive is set to True and the class has Python-valued
2476
+ # C attributes, then it should use the trashcan in tp_dealloc.
2477
+ if directive and self.has_cyclic_pyobject_attrs:
2478
+ return True
2479
+ # Use the trashcan if the base class uses it
2480
+ base_type = self.parent_type.base_type
2481
+ if base_type and base_type.scope is not None:
2482
+ return base_type.scope.needs_trashcan()
2483
+ return self.parent_type.builtin_trashcan
2484
+
2485
+ def needs_tp_clear(self):
2486
+ """
2487
+ Do we need to generate an implementation for the tp_clear slot? Can
2488
+ be disabled to keep references for the __dealloc__ cleanup function.
2489
+ """
2490
+ return self.needs_gc() and not self.directives.get('no_gc_clear', False)
2491
+
2492
+ def may_have_finalize(self):
2493
+ """
2494
+ This covers cases where we definitely have a __del__ function
2495
+ and also cases where one of the base classes could have a __del__
2496
+ function but we don't know.
2497
+ """
2498
+ current_type_scope = self
2499
+ while current_type_scope:
2500
+ del_entry = current_type_scope.lookup_here("__del__")
2501
+ if del_entry and del_entry.is_special:
2502
+ return True
2503
+ if (current_type_scope.parent_type.is_external or not current_type_scope.implemented or
2504
+ current_type_scope.parent_type.multiple_bases):
2505
+ # we don't know if we have __del__, so assume we do and call it
2506
+ return True
2507
+ current_base_type = current_type_scope.parent_type.base_type
2508
+ current_type_scope = current_base_type.scope if current_base_type else None
2509
+ return False
2510
+
2511
+ def get_refcounted_entries(self, include_weakref=False,
2512
+ include_gc_simple=True):
2513
+ py_attrs = []
2514
+ py_buffers = []
2515
+ memoryview_slices = []
2516
+
2517
+ for entry in self.var_entries:
2518
+ if entry.type.is_pyobject:
2519
+ if include_weakref or (self.is_closure_class_scope or entry.name != "__weakref__"):
2520
+ if include_gc_simple or not entry.type.is_gc_simple:
2521
+ py_attrs.append(entry)
2522
+ elif entry.type == PyrexTypes.c_py_buffer_type:
2523
+ py_buffers.append(entry)
2524
+ elif entry.type.is_memoryviewslice:
2525
+ memoryview_slices.append(entry)
2526
+
2527
+ have_entries = py_attrs or py_buffers or memoryview_slices
2528
+ return have_entries, (py_attrs, py_buffers, memoryview_slices)
2529
+
2530
+ def declare_var(self, name, type, pos,
2531
+ cname=None, visibility='private',
2532
+ api=False, in_pxd=False, is_cdef=False, pytyping_modifiers=None):
2533
+ name = self.mangle_class_private_name(name)
2534
+
2535
+ if pytyping_modifiers:
2536
+ if "typing.ClassVar" in pytyping_modifiers:
2537
+ is_cdef = 0
2538
+ if not type.is_pyobject:
2539
+ if not type.equivalent_type:
2540
+ warning(pos, "ClassVar[] requires the type to be a Python object type. Found '%s', using object instead." % type)
2541
+ type = py_object_type
2542
+ else:
2543
+ type = type.equivalent_type
2544
+ if "dataclasses.InitVar" in pytyping_modifiers and not self.is_c_dataclass_scope:
2545
+ error(pos, "Use of cython.dataclasses.InitVar does not make sense outside a dataclass")
2546
+
2547
+ if is_cdef:
2548
+ # Add an entry for an attribute.
2549
+ if self.defined:
2550
+ error(pos,
2551
+ "C attributes cannot be added in implementation part of"
2552
+ " extension type defined in a pxd")
2553
+ if (not self.is_closure_class_scope and
2554
+ get_slot_table(self.directives).get_special_method_signature(name)):
2555
+ error(pos,
2556
+ "The name '%s' is reserved for a special method."
2557
+ % name)
2558
+ if not cname:
2559
+ cname = name
2560
+ if not (self.parent_type.is_external or self.parent_type.entry.api or
2561
+ self.parent_type.entry.visibility == "public"):
2562
+ cname = c_safe_identifier(cname)
2563
+ cname = punycodify_name(cname, Naming.unicode_structmember_prefix)
2564
+ entry = self.declare(name, cname, type, pos, visibility)
2565
+ entry.is_variable = 1
2566
+ self.var_entries.append(entry)
2567
+ entry.pytyping_modifiers = pytyping_modifiers
2568
+ if type.is_cpp_class and visibility != 'extern':
2569
+ if self.directives['cpp_locals']:
2570
+ entry.make_cpp_optional()
2571
+ else:
2572
+ type.check_nullary_constructor(pos)
2573
+ if type.is_memoryviewslice:
2574
+ self.has_memoryview_attrs = True
2575
+ elif type.needs_explicit_construction(self):
2576
+ self.has_explicitly_constructable_attrs = True
2577
+ elif type.is_pyobject and (self.is_closure_class_scope or name != '__weakref__'):
2578
+ self.has_pyobject_attrs = True
2579
+ if (not type.is_builtin_type
2580
+ or not type.scope or type.scope.needs_gc()):
2581
+ self.has_cyclic_pyobject_attrs = True
2582
+ if visibility not in ('private', 'public', 'readonly'):
2583
+ error(pos,
2584
+ "Attribute of extension type cannot be declared %s" % visibility)
2585
+ if visibility in ('public', 'readonly'):
2586
+ # If the field is an external typedef, we cannot be sure about the type,
2587
+ # so do conversion ourself rather than rely on the CPython mechanism (through
2588
+ # a property; made in AnalyseDeclarationsTransform).
2589
+ entry.needs_property = True
2590
+ if not self.is_closure_class_scope and name in ("__dict__", "__weakref__"):
2591
+ error(pos, f"Special attribute {name} cannot be exposed to Python")
2592
+ if not (type.is_pyobject or type.can_coerce_to_pyobject(self)):
2593
+ # we're not testing for coercion *from* Python here - that would fail later
2594
+ error(pos, "C attribute of type '%s' cannot be accessed from Python" % type)
2595
+ else:
2596
+ entry.needs_property = False
2597
+ return entry
2598
+ else:
2599
+ if type is unspecified_type:
2600
+ type = py_object_type
2601
+ # Add an entry for a class attribute.
2602
+ entry = Scope.declare_var(self, name, type, pos,
2603
+ cname=cname, visibility=visibility,
2604
+ api=api, in_pxd=in_pxd, is_cdef=is_cdef, pytyping_modifiers=pytyping_modifiers)
2605
+ entry.is_member = 1
2606
+ # xxx: is_pyglobal changes behaviour in so many places that I keep it in for now.
2607
+ # is_member should be enough later on
2608
+ entry.is_pyglobal = 1
2609
+
2610
+ return entry
2611
+
2612
+ def declare_pyfunction(self, name, pos, allow_redefine=False):
2613
+ # Add an entry for a method.
2614
+ if name in richcmp_special_methods:
2615
+ if self.lookup_here('__richcmp__'):
2616
+ error(pos, "Cannot define both % and __richcmp__" % name)
2617
+ elif name == '__richcmp__':
2618
+ for n in richcmp_special_methods:
2619
+ if self.lookup_here(n):
2620
+ error(pos, "Cannot define both % and __richcmp__" % n)
2621
+ if name == "__new__":
2622
+ error(pos, "__new__ method of extension type will change semantics "
2623
+ "in a future version of Pyrex and Cython. Use __cinit__ instead.")
2624
+ entry = self.declare_var(name, py_object_type, pos,
2625
+ visibility='extern')
2626
+ special_sig = get_slot_table(self.directives).get_special_method_signature(name)
2627
+ if special_sig:
2628
+ # Special methods get put in the method table with a particular
2629
+ # signature declared in advance.
2630
+ entry.signature = special_sig
2631
+ entry.is_special = 1
2632
+ else:
2633
+ entry.signature = pymethod_signature
2634
+ entry.is_special = 0
2635
+
2636
+ self.pyfunc_entries.append(entry)
2637
+ return entry
2638
+
2639
+ def lookup_here(self, name):
2640
+ if not self.is_closure_class_scope and name == "__new__":
2641
+ name = EncodedString("__cinit__")
2642
+ entry = ClassScope.lookup_here(self, name)
2643
+ if entry and entry.is_builtin_cmethod:
2644
+ if not self.parent_type.is_builtin_type:
2645
+ # For subtypes of builtin types, we can only return
2646
+ # optimised C methods if the type if final.
2647
+ # Otherwise, subtypes may choose to override the
2648
+ # method, but the optimisation would prevent the
2649
+ # subtype method from being called.
2650
+ if not self.parent_type.is_final_type:
2651
+ return None
2652
+ return entry
2653
+
2654
+ def declare_cfunction(self, name, type, pos,
2655
+ cname=None, visibility='private', api=0, in_pxd=0,
2656
+ defining=0, modifiers=(), utility_code=None, overridable=False):
2657
+ name = self.mangle_class_private_name(name)
2658
+ if (get_slot_table(self.directives).get_special_method_signature(name)
2659
+ and not self.parent_type.is_builtin_type):
2660
+ error(pos, "Special methods must be declared with 'def', not 'cdef'")
2661
+ args = type.args
2662
+ if not type.is_static_method:
2663
+ if not args:
2664
+ error(pos, "C method has no self argument")
2665
+ elif not self.parent_type.assignable_from(args[0].type):
2666
+ error(pos, "Self argument (%s) of C method '%s' does not match parent type (%s)" %
2667
+ (args[0].type, name, self.parent_type))
2668
+ entry = self.lookup_here(name)
2669
+ if cname is None:
2670
+ cname = punycodify_name(c_safe_identifier(name), Naming.unicode_vtabentry_prefix)
2671
+ if entry:
2672
+ if not entry.is_cfunction:
2673
+ error(pos, "'%s' redeclared " % name)
2674
+ entry.already_declared_here()
2675
+ else:
2676
+ if defining and entry.func_cname:
2677
+ error(pos, "'%s' already defined" % name)
2678
+ #print "CClassScope.declare_cfunction: checking signature" ###
2679
+ if entry.is_final_cmethod and entry.is_inherited:
2680
+ error(pos, "Overriding final methods is not allowed")
2681
+ elif type.same_c_signature_as(entry.type, as_cmethod = 1) and type.nogil == entry.type.nogil:
2682
+ # Fix with_gil vs nogil.
2683
+ entry.type = entry.type.with_with_gil(type.with_gil)
2684
+ elif type.compatible_signature_with(entry.type, as_cmethod = 1) and type.nogil == entry.type.nogil:
2685
+ if (self.defined and not in_pxd
2686
+ and not type.same_c_signature_as_resolved_type(
2687
+ entry.type, as_cmethod=1, as_pxd_definition=1)):
2688
+ # TODO(robertwb): Make this an error.
2689
+ warning(pos,
2690
+ "Compatible but non-identical C method '%s' not redeclared "
2691
+ "in definition part of extension type '%s'. "
2692
+ "This may cause incorrect vtables to be generated." % (
2693
+ name, self.class_name), 2)
2694
+ warning(entry.pos, "Previous declaration is here", 2)
2695
+ entry = self.add_cfunction(name, type, pos, cname, visibility='ignore', modifiers=modifiers)
2696
+ else:
2697
+ error(pos, "Signature not compatible with previous declaration")
2698
+ error(entry.pos, "Previous declaration is here")
2699
+ else:
2700
+ if self.defined:
2701
+ error(pos,
2702
+ "C method '%s' not previously declared in definition part of"
2703
+ " extension type '%s'" % (name, self.class_name))
2704
+ entry = self.add_cfunction(name, type, pos, cname, visibility, modifiers)
2705
+ if defining:
2706
+ entry.func_cname = self.mangle(Naming.func_prefix, name)
2707
+ entry.utility_code = utility_code
2708
+ type.entry = entry
2709
+
2710
+ if 'inline' in modifiers:
2711
+ entry.is_inline_cmethod = True
2712
+
2713
+ if self.parent_type.is_final_type or entry.is_inline_cmethod or self.directives.get('final'):
2714
+ entry.is_final_cmethod = True
2715
+ entry.final_func_cname = entry.func_cname
2716
+ if not type.is_fused:
2717
+ entry.vtable_type = entry.type
2718
+ entry.type = type
2719
+
2720
+ return entry
2721
+
2722
+ def add_cfunction(self, name, type, pos, cname, visibility, modifiers, inherited=False):
2723
+ # Add a cfunction entry without giving it a func_cname.
2724
+ prev_entry = self.lookup_here(name)
2725
+ entry = ClassScope.add_cfunction(
2726
+ self, name, type, pos, cname, visibility, modifiers, inherited=inherited)
2727
+ entry.is_cmethod = 1
2728
+ entry.prev_entry = prev_entry
2729
+ return entry
2730
+
2731
+ def declare_builtin_cfunction(self, name, type, cname, utility_code = None):
2732
+ # overridden methods of builtin types still have their Python
2733
+ # equivalent that must be accessible to support bound methods
2734
+ name = EncodedString(name)
2735
+ entry = self.declare_cfunction(
2736
+ name, type, pos=None, cname=cname, visibility='extern', utility_code=utility_code)
2737
+ var_entry = Entry(name, name, py_object_type)
2738
+ var_entry.qualified_name = name
2739
+ var_entry.is_variable = 1
2740
+ var_entry.is_builtin = 1
2741
+ var_entry.utility_code = utility_code
2742
+ var_entry.scope = entry.scope
2743
+ entry.as_variable = var_entry
2744
+ return entry
2745
+
2746
+ def declare_property(self, name, doc, pos, ctype=None, property_scope=None):
2747
+ entry = self.lookup_here(name)
2748
+ if entry is None:
2749
+ entry = self.declare(name, name, py_object_type if ctype is None else ctype, pos, 'private')
2750
+ entry.is_property = True
2751
+ if ctype is not None:
2752
+ entry.is_cproperty = True
2753
+ entry.doc = doc
2754
+ if property_scope is None:
2755
+ entry.scope = PropertyScope(name, class_scope=self)
2756
+ else:
2757
+ entry.scope = property_scope
2758
+ self.property_entries.append(entry)
2759
+ return entry
2760
+
2761
+ def declare_cproperty(self, name, type, cfunc_name, setter_cfunc_name=None,
2762
+ doc=None, pos=None, visibility='extern',
2763
+ nogil=False, with_gil=False,
2764
+ exception_value=None, exception_check=False, setter_raises=True,
2765
+ utility_code=None, setter_utility_code=None):
2766
+ """Internal convenience method to declare a C property function in one go.
2767
+ """
2768
+ property_entry = self.declare_property(name, doc=doc, ctype=type, pos=pos)
2769
+ getter_cfunc_entry = property_entry.scope.declare_cfunction(
2770
+ name="__get__",
2771
+ type=PyrexTypes.CFuncType(
2772
+ type,
2773
+ [PyrexTypes.CFuncTypeArg("self", self.parent_type, pos=None)],
2774
+ nogil=nogil,
2775
+ with_gil=with_gil,
2776
+ exception_value=exception_value,
2777
+ exception_check=exception_check,
2778
+ ),
2779
+ cname=cfunc_name,
2780
+ utility_code=utility_code,
2781
+ visibility=visibility,
2782
+ pos=pos,
2783
+ )
2784
+ setter_cfunc_entry = None
2785
+ if setter_cfunc_name:
2786
+ setter_cfunc_entry = property_entry.scope.declare_cfunction(
2787
+ name="__set__",
2788
+ type=PyrexTypes.CFuncType(
2789
+ PyrexTypes.c_returncode_type if setter_raises else PyrexTypes.c_void_type,
2790
+ [PyrexTypes.CFuncTypeArg("self", self.parent_type, pos=None),
2791
+ PyrexTypes.CFuncTypeArg("value", type, pos=None)],
2792
+ nogil=nogil,
2793
+ with_gil=with_gil,
2794
+ exception_value=-1 if setter_raises else None,
2795
+ ),
2796
+ cname=setter_cfunc_name,
2797
+ # Allow getter+setter in one utility code section.
2798
+ utility_code=setter_utility_code or utility_code,
2799
+ visibility=visibility,
2800
+ pos=pos,
2801
+ )
2802
+ return property_entry, getter_cfunc_entry, setter_cfunc_entry
2803
+
2804
+ def declare_inherited_c_attributes(self, base_scope):
2805
+ # Declare entries for all the C attributes of an
2806
+ # inherited type, with cnames modified appropriately
2807
+ # to work with this type.
2808
+ def adapt(cname):
2809
+ return "%s.%s" % (Naming.obj_base_cname, base_entry.cname)
2810
+
2811
+ entries = base_scope.inherited_var_entries + base_scope.var_entries
2812
+ for base_entry in entries:
2813
+ entry = self.declare(
2814
+ base_entry.name, adapt(base_entry.cname),
2815
+ base_entry.type, None, 'private')
2816
+ entry.is_variable = 1
2817
+ entry.is_inherited = True
2818
+ entry.annotation = base_entry.annotation
2819
+ self.inherited_var_entries.append(entry)
2820
+
2821
+ # If the class defined in a pxd, specific entries have not been added.
2822
+ # Ensure now that the parent (base) scope has specific entries
2823
+ # Iterate over a copy as get_all_specialized_function_types() will mutate
2824
+ for base_entry in base_scope.cfunc_entries[:]:
2825
+ if base_entry.type.is_fused:
2826
+ base_entry.type.get_all_specialized_function_types()
2827
+
2828
+ for base_entry in base_scope.cfunc_entries:
2829
+ cname = base_entry.cname
2830
+ var_entry = base_entry.as_variable
2831
+ is_builtin = var_entry and var_entry.is_builtin
2832
+ if not is_builtin:
2833
+ cname = adapt(cname)
2834
+ entry = self.add_cfunction(
2835
+ base_entry.name, base_entry.type, base_entry.pos, cname,
2836
+ base_entry.visibility, base_entry.func_modifiers, inherited=True)
2837
+ entry.is_inherited = 1
2838
+ if base_entry.is_final_cmethod:
2839
+ entry.is_final_cmethod = True
2840
+ entry.is_inline_cmethod = base_entry.is_inline_cmethod
2841
+ if (self.parent_scope == base_scope.parent_scope or
2842
+ entry.is_inline_cmethod):
2843
+ entry.final_func_cname = base_entry.final_func_cname
2844
+ if is_builtin:
2845
+ entry.is_builtin_cmethod = True
2846
+ entry.as_variable = var_entry
2847
+ if base_entry.utility_code:
2848
+ entry.utility_code = base_entry.utility_code
2849
+
2850
+
2851
+ def handle_already_declared_name(self, name, cname, type, pos, visibility, copy_entry=True):
2852
+ # We want to copy the existing entry instead of modifying it, since this is an override.
2853
+ super().handle_already_declared_name(name, cname, type, pos, visibility, copy_entry)
2854
+
2855
+ class CppClassScope(Scope):
2856
+ # Namespace of a C++ class.
2857
+
2858
+ is_cpp_class_scope = 1
2859
+
2860
+ default_constructor = None
2861
+ type = None
2862
+
2863
+ def __init__(self, name, outer_scope, templates=None):
2864
+ Scope.__init__(self, name, outer_scope, None)
2865
+ self.directives = outer_scope.directives
2866
+ self.inherited_var_entries = []
2867
+ if templates is not None:
2868
+ for T in templates:
2869
+ template_entry = self.declare(
2870
+ T, T, PyrexTypes.TemplatePlaceholderType(T), None, 'extern')
2871
+ template_entry.is_type = 1
2872
+
2873
+ def declare_var(self, name, type, pos,
2874
+ cname=None, visibility='extern',
2875
+ api=False, in_pxd=False, is_cdef=False, defining=False, pytyping_modifiers=None):
2876
+ # Add an entry for an attribute.
2877
+ if not cname:
2878
+ cname = name
2879
+ self._reject_pytyping_modifiers(pos, pytyping_modifiers)
2880
+ entry = self.lookup_here(name)
2881
+ if defining and entry is not None:
2882
+ if type.is_cfunction:
2883
+ entry = self.declare(name, cname, type, pos, visibility)
2884
+ elif entry.type.same_as(type):
2885
+ # Fix with_gil vs nogil.
2886
+ entry.type = entry.type.with_with_gil(type.with_gil)
2887
+ else:
2888
+ error(pos, "Function signature does not match previous declaration")
2889
+ else:
2890
+ entry = self.declare(name, cname, type, pos, visibility)
2891
+ if type.is_cfunction and not defining:
2892
+ entry.is_inherited = 1
2893
+ entry.is_variable = 1
2894
+ if type.is_cfunction:
2895
+ entry.is_cfunction = 1
2896
+ if self.type and not self.type.get_fused_types():
2897
+ entry.func_cname = "%s::%s" % (self.type.empty_declaration_code(), cname)
2898
+ if name != "this" and (defining or name != "<init>"):
2899
+ self.var_entries.append(entry)
2900
+ return entry
2901
+
2902
+ def declare_cfunction(self, name, type, pos,
2903
+ cname=None, visibility='extern', api=0, in_pxd=0,
2904
+ defining=0, modifiers=(), utility_code=None, overridable=False):
2905
+ class_name = self.name.split('::')[-1]
2906
+ if name in (class_name, '__init__') and cname is None:
2907
+ cname = "%s__init__%s" % (Naming.func_prefix, class_name)
2908
+ name = EncodedString('<init>')
2909
+ type.return_type = PyrexTypes.CVoidType()
2910
+ # This is called by the actual constructor, but need to support
2911
+ # arguments that cannot by called by value.
2912
+ type.original_args = type.args
2913
+ def maybe_ref(arg):
2914
+ if arg.type.is_cpp_class and not arg.type.is_reference:
2915
+ return PyrexTypes.CFuncTypeArg(
2916
+ arg.name, PyrexTypes.c_ref_type(arg.type), arg.pos)
2917
+ else:
2918
+ return arg
2919
+ type.args = [maybe_ref(arg) for arg in type.args]
2920
+ elif name == '__dealloc__' and cname is None:
2921
+ cname = "%s__dealloc__%s" % (Naming.func_prefix, class_name)
2922
+ name = EncodedString('<del>')
2923
+ type.return_type = PyrexTypes.CVoidType()
2924
+ if name in ('<init>', '<del>') and type.nogil:
2925
+ for base in self.type.base_classes:
2926
+ base_entry = base.scope.lookup(name)
2927
+ if base_entry and not base_entry.type.nogil:
2928
+ error(pos, "Constructor cannot be called without GIL unless all base constructors can also be called without GIL")
2929
+ error(base_entry.pos, "Base constructor defined here.")
2930
+ # The previous entries management is now done directly in Scope.declare
2931
+ entry = self.declare_var(name, type, pos,
2932
+ defining=defining,
2933
+ cname=cname, visibility=visibility)
2934
+ entry.utility_code = utility_code
2935
+ type.entry = entry
2936
+ return entry
2937
+
2938
+ def declare_inherited_cpp_attributes(self, base_class):
2939
+ base_scope = base_class.scope
2940
+ template_type = base_class
2941
+ while getattr(template_type, 'template_type', None):
2942
+ template_type = template_type.template_type
2943
+ if getattr(template_type, 'templates', None):
2944
+ base_templates = [T.name for T in template_type.templates]
2945
+ else:
2946
+ base_templates = ()
2947
+ # Declare entries for all the C++ attributes of an
2948
+ # inherited type, with cnames modified appropriately
2949
+ # to work with this type.
2950
+ for base_entry in base_scope.inherited_var_entries + base_scope.var_entries:
2951
+ #constructor/destructor is not inherited
2952
+ if base_entry.name in ("<init>", "<del>"):
2953
+ continue
2954
+ #print base_entry.name, self.entries
2955
+ if base_entry.name in self.entries:
2956
+ base_entry.name # FIXME: is there anything to do in this case?
2957
+ entry = self.declare(base_entry.name, base_entry.cname,
2958
+ base_entry.type, None, 'extern')
2959
+ entry.is_variable = 1
2960
+ entry.is_inherited = 1
2961
+ if base_entry.is_cfunction:
2962
+ entry.is_cfunction = 1
2963
+ entry.func_cname = base_entry.func_cname
2964
+ self.inherited_var_entries.append(entry)
2965
+ for base_entry in base_scope.cfunc_entries:
2966
+ entry = self.declare_cfunction(base_entry.name, base_entry.type,
2967
+ base_entry.pos, base_entry.cname,
2968
+ base_entry.visibility, api=0,
2969
+ modifiers=base_entry.func_modifiers,
2970
+ utility_code=base_entry.utility_code)
2971
+ entry.is_inherited = 1
2972
+ for base_entry in base_scope.type_entries:
2973
+ if base_entry.name not in base_templates:
2974
+ entry = self.declare_type(base_entry.name, base_entry.type,
2975
+ base_entry.pos, base_entry.cname,
2976
+ base_entry.visibility, defining=False)
2977
+ entry.is_inherited = 1
2978
+
2979
+ def specialize(self, values, type_entry):
2980
+ scope = CppClassScope(self.name, self.outer_scope)
2981
+ scope.type = type_entry
2982
+ for entry in self.entries.values():
2983
+ if entry.is_type:
2984
+ scope.declare_type(entry.name,
2985
+ entry.type.specialize(values),
2986
+ entry.pos,
2987
+ entry.cname,
2988
+ template=1)
2989
+ elif entry.type.is_cfunction:
2990
+ for e in entry.all_alternatives():
2991
+ scope.declare_cfunction(e.name,
2992
+ e.type.specialize(values),
2993
+ e.pos,
2994
+ e.cname,
2995
+ utility_code=e.utility_code)
2996
+ else:
2997
+ scope.declare_var(entry.name,
2998
+ entry.type.specialize(values),
2999
+ entry.pos,
3000
+ entry.cname,
3001
+ entry.visibility)
3002
+
3003
+ return scope
3004
+
3005
+ def lookup_here(self, name):
3006
+ if name == "__init__":
3007
+ name = "<init>"
3008
+ elif name == "__dealloc__":
3009
+ name = "<del>"
3010
+ return super(CppClassScope, self).lookup_here(name)
3011
+
3012
+ def is_cpp(self):
3013
+ # Whatever the global environment, always treat cppclass with C++ rules.
3014
+ # (Cython will emit warnings elsewhere)
3015
+ return True
3016
+
3017
+
3018
+ class CppScopedEnumScope(Scope):
3019
+ # Namespace of a ScopedEnum
3020
+
3021
+ def __init__(self, name, outer_scope):
3022
+ Scope.__init__(self, name, outer_scope, None)
3023
+
3024
+ def declare_var(self, name, type, pos,
3025
+ cname=None, visibility='extern', pytyping_modifiers=None):
3026
+ # Add an entry for an attribute.
3027
+ if not cname:
3028
+ cname = name
3029
+ self._reject_pytyping_modifiers(pos, pytyping_modifiers)
3030
+ entry = self.declare(name, cname, type, pos, visibility)
3031
+ entry.is_variable = True
3032
+ return entry
3033
+
3034
+
3035
+ class PropertyScope(Scope):
3036
+ # Scope holding the __get__, __set__ and __del__ methods for
3037
+ # a property of an extension type.
3038
+ #
3039
+ # parent_type PyExtensionType The type to which the property belongs
3040
+
3041
+ is_property_scope = 1
3042
+
3043
+ def __init__(self, name, class_scope):
3044
+ # outer scope is None for some internal properties
3045
+ outer_scope = class_scope.global_scope() if class_scope.outer_scope else None
3046
+ Scope.__init__(self, name, outer_scope, parent_scope=class_scope)
3047
+ self.parent_type = class_scope.parent_type
3048
+ self.directives = class_scope.directives
3049
+
3050
+ def declare_cfunction(self, name, type, pos, *args, **kwargs):
3051
+ """Declare a C property function.
3052
+ """
3053
+ if name=="__get__":
3054
+ if type.return_type.is_void:
3055
+ error(pos, "C property getter cannot return 'void'")
3056
+ if len(type.args) != 1:
3057
+ error(pos, "C property getter must have a single (self) argument")
3058
+
3059
+ if name=="__set__":
3060
+ # Allow only 'void noexcept' and 'int except(?) -1'.
3061
+ if type.return_type.is_void:
3062
+ if type.exception_check or type.exception_value is not None:
3063
+ error(pos, "C property setter needs 'noexcept' if declared 'void'")
3064
+ elif type.return_type in (PyrexTypes.c_int_type, PyrexTypes.c_returncode_type):
3065
+ if type.exception_value is not None and type.exception_value.python_value != -1:
3066
+ error(pos, f"C property setter with 'int' return needs explicit 'except -1' or 'except? -1'")
3067
+ else:
3068
+ error(pos, "C property setter must return void or an 'int' error code, -1 (error) or 0 (ok)")
3069
+ if len(type.args) != 2:
3070
+ error(pos, "C property setter must have two arguments (self and value)")
3071
+
3072
+ if type.args and not (type.args[0].type.is_pyobject or type.args[0].type is self.parent_scope.parent_type):
3073
+ error(pos, "self argument of C property method must be an object")
3074
+ if type.args and type.args[0].type is py_object_type:
3075
+ # Set 'self' argument type to extension type.
3076
+ type.args[0].type = self.parent_scope.parent_type
3077
+
3078
+ entry = Scope.declare_cfunction(self, name, type, pos, *args, **kwargs)
3079
+ entry.is_cproperty = True
3080
+ return entry
3081
+
3082
+ def declare_pyfunction(self, name, pos, allow_redefine=False):
3083
+ # Add an entry for a method.
3084
+ signature = get_property_accessor_signature(name)
3085
+ if signature:
3086
+ entry = self.declare(name, name, py_object_type, pos, 'private')
3087
+ entry.is_special = 1
3088
+ entry.signature = signature
3089
+ return entry
3090
+ else:
3091
+ error(pos, "Only __get__, __set__ and __del__ methods allowed "
3092
+ "in a property declaration")
3093
+ return None
3094
+
3095
+
3096
+ class CQualifierScope(Scope):
3097
+
3098
+ def __init__(self, base_type_scope, is_const=False, is_volatile=False, is_restrict=False):
3099
+ Scope.__init__(
3100
+ self,
3101
+ 'cv_' + base_type_scope.name,
3102
+ base_type_scope.outer_scope,
3103
+ base_type_scope.parent_scope)
3104
+ self.base_type_scope = base_type_scope
3105
+ self.is_const = is_const
3106
+ self.is_volatile = is_volatile
3107
+ self.is_restrict = is_restrict
3108
+
3109
+ def lookup_here(self, name):
3110
+ entry = self.base_type_scope.lookup_here(name)
3111
+ if entry is not None:
3112
+ entry = copy.copy(entry)
3113
+ entry.type = PyrexTypes.c_qualifier_type(
3114
+ entry.type, self.is_const, self.is_volatile, is_restrict=False)
3115
+ return entry
3116
+
3117
+
3118
+ class TemplateScope(Scope):
3119
+ def __init__(self, name, outer_scope):
3120
+ Scope.__init__(self, name, outer_scope, None)
3121
+ self.directives = outer_scope.directives