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