Cython 3.1.0__py3-none-any.whl

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