Cython 3.2.0__cp39-abi3-win32.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 +169 -0
- Cython/Build/Cache.py +199 -0
- Cython/Build/Cythonize.py +350 -0
- Cython/Build/Dependencies.py +1314 -0
- Cython/Build/Distutils.py +1 -0
- Cython/Build/Inline.py +463 -0
- Cython/Build/IpythonMagic.py +560 -0
- Cython/Build/SharedModule.py +94 -0
- Cython/Build/Tests/TestCyCache.py +194 -0
- Cython/Build/Tests/TestCythonizeArgsParser.py +481 -0
- Cython/Build/Tests/TestDependencies.py +133 -0
- Cython/Build/Tests/TestInline.py +177 -0
- Cython/Build/Tests/TestIpythonMagic.py +287 -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 +984 -0
- Cython/Compiler/CmdLine.py +263 -0
- Cython/Compiler/Code.pxd +149 -0
- Cython/Compiler/Code.py +3746 -0
- Cython/Compiler/Code.pyd +0 -0
- Cython/Compiler/CodeGeneration.py +33 -0
- Cython/Compiler/CythonScope.py +191 -0
- Cython/Compiler/Dataclass.py +864 -0
- Cython/Compiler/DebugFlags.py +24 -0
- Cython/Compiler/Errors.py +297 -0
- Cython/Compiler/ExprNodes.py +15562 -0
- Cython/Compiler/FlowControl.pxd +97 -0
- Cython/Compiler/FlowControl.py +1451 -0
- Cython/Compiler/FlowControl.pyd +0 -0
- Cython/Compiler/FusedNode.py +971 -0
- Cython/Compiler/FusedNode.pyd +0 -0
- Cython/Compiler/Future.py +16 -0
- Cython/Compiler/Interpreter.py +57 -0
- Cython/Compiler/Lexicon.py +421 -0
- Cython/Compiler/LineTable.py +114 -0
- Cython/Compiler/LineTable.pyd +0 -0
- Cython/Compiler/Main.py +857 -0
- Cython/Compiler/MatchCaseNodes.py +259 -0
- Cython/Compiler/MemoryView.py +905 -0
- Cython/Compiler/ModuleNode.py +4235 -0
- Cython/Compiler/Naming.py +363 -0
- Cython/Compiler/Nodes.py +10831 -0
- Cython/Compiler/Optimize.py +5288 -0
- Cython/Compiler/Options.py +843 -0
- Cython/Compiler/ParseTreeTransforms.pxd +78 -0
- Cython/Compiler/ParseTreeTransforms.py +4638 -0
- Cython/Compiler/Parsing.pxd +9 -0
- Cython/Compiler/Parsing.py +4775 -0
- Cython/Compiler/Parsing.pyd +0 -0
- Cython/Compiler/Pipeline.py +439 -0
- Cython/Compiler/PyrexTypes.py +5870 -0
- Cython/Compiler/Pythran.py +232 -0
- Cython/Compiler/Scanning.pxd +48 -0
- Cython/Compiler/Scanning.py +701 -0
- Cython/Compiler/Scanning.pyd +0 -0
- Cython/Compiler/StringEncoding.py +298 -0
- Cython/Compiler/Symtab.py +3073 -0
- Cython/Compiler/Tests/TestBuffer.py +105 -0
- Cython/Compiler/Tests/TestBuiltin.py +72 -0
- Cython/Compiler/Tests/TestCmdLine.py +586 -0
- Cython/Compiler/Tests/TestCode.py +144 -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 +134 -0
- Cython/Compiler/Tests/TestSignatureMatching.py +73 -0
- Cython/Compiler/Tests/TestStringEncoding.py +21 -0
- Cython/Compiler/Tests/TestTreeFragment.py +63 -0
- Cython/Compiler/Tests/TestTreePath.py +103 -0
- Cython/Compiler/Tests/TestTypes.py +75 -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 +591 -0
- Cython/Compiler/TypeSlots.py +1174 -0
- Cython/Compiler/UFuncs.py +311 -0
- Cython/Compiler/UtilNodes.py +389 -0
- Cython/Compiler/UtilityCode.py +344 -0
- Cython/Compiler/Version.py +8 -0
- Cython/Compiler/Visitor.pxd +53 -0
- Cython/Compiler/Visitor.py +861 -0
- Cython/Compiler/Visitor.pyd +0 -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 +275 -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 +578 -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 +178 -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 +187 -0
- Cython/Includes/cpython/exc.pxd +263 -0
- Cython/Includes/cpython/fileobject.pxd +57 -0
- Cython/Includes/cpython/float.pxd +47 -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 +92 -0
- Cython/Includes/cpython/long.pxd +149 -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 +433 -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/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 +53 -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 +86 -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/Plex/Actions.pxd +24 -0
- Cython/Plex/Actions.py +119 -0
- Cython/Plex/Actions.pyd +0 -0
- Cython/Plex/DFA.pxd +14 -0
- Cython/Plex/DFA.py +164 -0
- Cython/Plex/DFA.pyd +0 -0
- Cython/Plex/Errors.py +48 -0
- Cython/Plex/Lexicons.py +178 -0
- Cython/Plex/Machines.pxd +36 -0
- Cython/Plex/Machines.py +238 -0
- Cython/Plex/Machines.pyd +0 -0
- Cython/Plex/Regexps.py +535 -0
- Cython/Plex/Scanners.pxd +45 -0
- Cython/Plex/Scanners.py +328 -0
- Cython/Plex/Scanners.pyd +0 -0
- Cython/Plex/Transitions.pxd +14 -0
- Cython/Plex/Transitions.py +239 -0
- Cython/Plex/Transitions.pyd +0 -0
- Cython/Plex/__init__.py +34 -0
- Cython/Runtime/__init__.py +1 -0
- Cython/Runtime/refnanny.pyd +0 -0
- Cython/Runtime/refnanny.pyx +237 -0
- Cython/Shadow.py +690 -0
- Cython/Shadow.pyi +521 -0
- Cython/StringIOTree.py +170 -0
- Cython/StringIOTree.pyd +0 -0
- Cython/Tempita/__init__.py +4 -0
- Cython/Tempita/_looper.py +154 -0
- Cython/Tempita/_tempita.py +1091 -0
- Cython/Tempita/_tempita.pyd +0 -0
- Cython/TestUtils.py +422 -0
- Cython/Tests/TestCodeWriter.py +128 -0
- Cython/Tests/TestCythonUtils.py +202 -0
- Cython/Tests/TestJediTyper.py +223 -0
- Cython/Tests/TestShadow.py +114 -0
- Cython/Tests/TestStringIOTree.py +67 -0
- Cython/Tests/TestTestUtils.py +90 -0
- Cython/Tests/__init__.py +1 -0
- Cython/Tests/xmlrunner.py +390 -0
- Cython/Utility/AsyncGen.c +1031 -0
- Cython/Utility/Buffer.c +865 -0
- Cython/Utility/BufferFormatFromTypeInfo.pxd +2 -0
- Cython/Utility/Builtins.c +810 -0
- Cython/Utility/CConvert.pyx +134 -0
- Cython/Utility/CMath.c +104 -0
- Cython/Utility/CommonStructures.c +226 -0
- Cython/Utility/Complex.c +378 -0
- Cython/Utility/Coroutine.c +2300 -0
- Cython/Utility/CpdefEnums.pyx +103 -0
- Cython/Utility/CppConvert.pyx +282 -0
- Cython/Utility/CppSupport.cpp +151 -0
- Cython/Utility/CythonFunction.c +1832 -0
- Cython/Utility/Dataclasses.c +101 -0
- Cython/Utility/Embed.c +121 -0
- Cython/Utility/Exceptions.c +1016 -0
- Cython/Utility/ExtensionTypes.c +996 -0
- Cython/Utility/FunctionArguments.c +1043 -0
- Cython/Utility/FusedFunction.pyx +44 -0
- Cython/Utility/ImportExport.c +907 -0
- Cython/Utility/MemoryView.pxd +188 -0
- Cython/Utility/MemoryView.pyx +1482 -0
- Cython/Utility/MemoryView_C.c +927 -0
- Cython/Utility/ModuleSetupCode.c +3203 -0
- Cython/Utility/NumpyImportArray.c +46 -0
- Cython/Utility/ObjectHandling.c +3273 -0
- Cython/Utility/Optimize.c +1603 -0
- Cython/Utility/Overflow.c +384 -0
- Cython/Utility/Printing.c +86 -0
- Cython/Utility/Profile.c +732 -0
- Cython/Utility/StringTools.c +1379 -0
- Cython/Utility/Synchronization.c +399 -0
- Cython/Utility/TString.c +356 -0
- Cython/Utility/TestCyUtilityLoader.pyx +8 -0
- Cython/Utility/TestCythonScope.pyx +75 -0
- Cython/Utility/TestUtilityLoader.c +12 -0
- Cython/Utility/TypeConversion.c +1385 -0
- Cython/Utility/UFuncs.pyx +50 -0
- Cython/Utility/UFuncs_C.c +89 -0
- Cython/Utility/__init__.py +28 -0
- Cython/Utility/arrayarray.h +167 -0
- Cython/Utils.py +687 -0
- Cython/Utils.pyd +0 -0
- Cython/__init__.py +10 -0
- Cython/__init__.pyi +7 -0
- Cython/py.typed +0 -0
- cython-3.2.0.dist-info/METADATA +85 -0
- cython-3.2.0.dist-info/RECORD +333 -0
- cython-3.2.0.dist-info/WHEEL +5 -0
- cython-3.2.0.dist-info/entry_points.txt +4 -0
- cython-3.2.0.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,861 @@
|
|
|
1
|
+
# cython: infer_types=True
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
# Tree visitor and transform framework
|
|
5
|
+
#
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
import inspect
|
|
10
|
+
|
|
11
|
+
from . import TypeSlots
|
|
12
|
+
from . import Builtin
|
|
13
|
+
from . import Nodes
|
|
14
|
+
from . import ExprNodes
|
|
15
|
+
from . import Errors
|
|
16
|
+
from . import DebugFlags
|
|
17
|
+
from . import Future
|
|
18
|
+
|
|
19
|
+
import cython
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
_PRINTABLE = cython.declare(tuple, (bytes, str, int, float, complex))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class TreeVisitor:
|
|
26
|
+
"""
|
|
27
|
+
Base class for writing visitors for a Cython tree, contains utilities for
|
|
28
|
+
recursing such trees using visitors. Each node is
|
|
29
|
+
expected to have a child_attrs iterable containing the names of attributes
|
|
30
|
+
containing child nodes or lists of child nodes. Lists are not considered
|
|
31
|
+
part of the tree structure (i.e. contained nodes are considered direct
|
|
32
|
+
children of the parent node).
|
|
33
|
+
|
|
34
|
+
visit_children visits each of the children of a given node (see the visit_children
|
|
35
|
+
documentation). When recursing the tree using visit_children, an attribute
|
|
36
|
+
access_path is maintained which gives information about the current location
|
|
37
|
+
in the tree as a stack of tuples: (parent_node, attrname, index), representing
|
|
38
|
+
the node, attribute and optional list index that was taken in each step in the path to
|
|
39
|
+
the current node.
|
|
40
|
+
|
|
41
|
+
Example:
|
|
42
|
+
|
|
43
|
+
>>> class SampleNode(object):
|
|
44
|
+
... child_attrs = ["head", "body"]
|
|
45
|
+
... def __init__(self, value, head=None, body=None):
|
|
46
|
+
... self.value = value
|
|
47
|
+
... self.head = head
|
|
48
|
+
... self.body = body
|
|
49
|
+
... def __repr__(self): return "SampleNode(%s)" % self.value
|
|
50
|
+
...
|
|
51
|
+
>>> tree = SampleNode(0, SampleNode(1), [SampleNode(2), SampleNode(3)])
|
|
52
|
+
>>> class MyVisitor(TreeVisitor):
|
|
53
|
+
... def visit_SampleNode(self, node):
|
|
54
|
+
... print("in %s %s" % (node.value, self.access_path))
|
|
55
|
+
... self.visitchildren(node)
|
|
56
|
+
... print("out %s" % node.value)
|
|
57
|
+
...
|
|
58
|
+
>>> MyVisitor().visit(tree)
|
|
59
|
+
in 0 []
|
|
60
|
+
in 1 [(SampleNode(0), 'head', None)]
|
|
61
|
+
out 1
|
|
62
|
+
in 2 [(SampleNode(0), 'body', 0)]
|
|
63
|
+
out 2
|
|
64
|
+
in 3 [(SampleNode(0), 'body', 1)]
|
|
65
|
+
out 3
|
|
66
|
+
out 0
|
|
67
|
+
"""
|
|
68
|
+
def __init__(self):
|
|
69
|
+
super().__init__()
|
|
70
|
+
self.dispatch_table = {}
|
|
71
|
+
self.access_path = []
|
|
72
|
+
|
|
73
|
+
def dump_node(self, node):
|
|
74
|
+
ignored = list(node.child_attrs or []) + [
|
|
75
|
+
'child_attrs', 'pos', 'gil_message', 'cpp_message', 'subexprs']
|
|
76
|
+
values = []
|
|
77
|
+
pos = getattr(node, 'pos', None)
|
|
78
|
+
if pos:
|
|
79
|
+
source = pos[0]
|
|
80
|
+
if source:
|
|
81
|
+
import os.path
|
|
82
|
+
source = os.path.basename(source.get_description())
|
|
83
|
+
values.append('%s:%s:%s' % (source, pos[1], pos[2]))
|
|
84
|
+
attribute_names = dir(node)
|
|
85
|
+
for attr in attribute_names:
|
|
86
|
+
if attr in ignored:
|
|
87
|
+
continue
|
|
88
|
+
if attr.startswith('_') or attr.endswith('_'):
|
|
89
|
+
continue
|
|
90
|
+
try:
|
|
91
|
+
value = getattr(node, attr)
|
|
92
|
+
except AttributeError:
|
|
93
|
+
continue
|
|
94
|
+
if value is None or value == 0:
|
|
95
|
+
continue
|
|
96
|
+
elif isinstance(value, list):
|
|
97
|
+
value = '[...]/%d' % len(value)
|
|
98
|
+
elif not isinstance(value, _PRINTABLE):
|
|
99
|
+
continue
|
|
100
|
+
else:
|
|
101
|
+
value = repr(value)
|
|
102
|
+
values.append('%s = %s' % (attr, value))
|
|
103
|
+
return '%s(%s)' % (node.__class__.__name__, ',\n '.join(values))
|
|
104
|
+
|
|
105
|
+
def _find_node_path(self, stacktrace):
|
|
106
|
+
import os.path
|
|
107
|
+
last_traceback = stacktrace
|
|
108
|
+
nodes = []
|
|
109
|
+
while hasattr(stacktrace, 'tb_frame'):
|
|
110
|
+
frame = stacktrace.tb_frame
|
|
111
|
+
node = frame.f_locals.get('self')
|
|
112
|
+
if isinstance(node, Nodes.Node):
|
|
113
|
+
code = frame.f_code
|
|
114
|
+
method_name = code.co_name
|
|
115
|
+
pos = (os.path.basename(code.co_filename),
|
|
116
|
+
frame.f_lineno)
|
|
117
|
+
nodes.append((node, method_name, pos))
|
|
118
|
+
last_traceback = stacktrace
|
|
119
|
+
stacktrace = stacktrace.tb_next
|
|
120
|
+
return (last_traceback, nodes)
|
|
121
|
+
|
|
122
|
+
def _raise_compiler_error(self, child, e):
|
|
123
|
+
trace = ['']
|
|
124
|
+
for parent, attribute, index in self.access_path:
|
|
125
|
+
node = getattr(parent, attribute)
|
|
126
|
+
if index is None:
|
|
127
|
+
index = ''
|
|
128
|
+
else:
|
|
129
|
+
node = node[index]
|
|
130
|
+
index = '[%d]' % index
|
|
131
|
+
trace.append('%s.%s%s = %s' % (
|
|
132
|
+
parent.__class__.__name__, attribute, index,
|
|
133
|
+
self.dump_node(node)))
|
|
134
|
+
stacktrace, called_nodes = self._find_node_path(sys.exc_info()[2])
|
|
135
|
+
last_node = child
|
|
136
|
+
for node, method_name, pos in called_nodes:
|
|
137
|
+
last_node = node
|
|
138
|
+
trace.append("File '%s', line %d, in %s: %s" % (
|
|
139
|
+
pos[0], pos[1], method_name, self.dump_node(node)))
|
|
140
|
+
raise Errors.CompilerCrash(
|
|
141
|
+
getattr(last_node, 'pos', None), self.__class__.__name__,
|
|
142
|
+
'\n'.join(trace), e, stacktrace)
|
|
143
|
+
|
|
144
|
+
@cython.final
|
|
145
|
+
def find_handler(self, obj):
|
|
146
|
+
# to resolve, try entire hierarchy
|
|
147
|
+
cls = type(obj)
|
|
148
|
+
mro = inspect.getmro(cls)
|
|
149
|
+
for mro_cls in mro:
|
|
150
|
+
handler_method = getattr(self, "visit_" + mro_cls.__name__, None)
|
|
151
|
+
if handler_method is not None:
|
|
152
|
+
return handler_method
|
|
153
|
+
|
|
154
|
+
print(type(self), cls)
|
|
155
|
+
if self.access_path:
|
|
156
|
+
print(self.access_path)
|
|
157
|
+
print(self.access_path[-1][0].pos)
|
|
158
|
+
print(self.access_path[-1][0].__dict__)
|
|
159
|
+
raise RuntimeError("Visitor %r does not accept object: %s" % (self, obj))
|
|
160
|
+
|
|
161
|
+
def visit(self, obj):
|
|
162
|
+
# generic def entry point for calls from Python subclasses
|
|
163
|
+
return self._visit(obj)
|
|
164
|
+
|
|
165
|
+
@cython.final
|
|
166
|
+
def _visit(self, obj):
|
|
167
|
+
# fast cdef entry point for calls from Cython subclasses
|
|
168
|
+
try:
|
|
169
|
+
try:
|
|
170
|
+
handler_method = self.dispatch_table[type(obj)]
|
|
171
|
+
except KeyError:
|
|
172
|
+
handler_method = self.find_handler(obj)
|
|
173
|
+
self.dispatch_table[type(obj)] = handler_method
|
|
174
|
+
return handler_method(obj)
|
|
175
|
+
except Errors.CompileError:
|
|
176
|
+
raise
|
|
177
|
+
except Errors.AbortError:
|
|
178
|
+
raise
|
|
179
|
+
except Exception as e:
|
|
180
|
+
if DebugFlags.debug_no_exception_intercept:
|
|
181
|
+
raise
|
|
182
|
+
self._raise_compiler_error(obj, e)
|
|
183
|
+
|
|
184
|
+
@cython.final
|
|
185
|
+
def _visitchild(self, child, parent, attrname, idx):
|
|
186
|
+
# fast cdef entry point for calls from Cython subclasses
|
|
187
|
+
self.access_path.append((parent, attrname, idx))
|
|
188
|
+
result = self._visit(child)
|
|
189
|
+
self.access_path.pop()
|
|
190
|
+
return result
|
|
191
|
+
|
|
192
|
+
def visitchildren(self, parent, attrs=None, exclude=None):
|
|
193
|
+
# generic def entry point for calls from Python subclasses
|
|
194
|
+
return self._visitchildren(parent, attrs, exclude)
|
|
195
|
+
|
|
196
|
+
@cython.final
|
|
197
|
+
def _visitchildren(self, parent, attrs, exclude):
|
|
198
|
+
# fast cdef entry point for calls from Cython subclasses
|
|
199
|
+
"""
|
|
200
|
+
Visits the children of the given parent. If parent is None, returns
|
|
201
|
+
immediately (returning None).
|
|
202
|
+
|
|
203
|
+
The return value is a dictionary giving the results for each
|
|
204
|
+
child (mapping the attribute name to either the return value
|
|
205
|
+
or a list of return values (in the case of multiple children
|
|
206
|
+
in an attribute)).
|
|
207
|
+
"""
|
|
208
|
+
idx: cython.Py_ssize_t
|
|
209
|
+
|
|
210
|
+
if parent is None: return None
|
|
211
|
+
result = {}
|
|
212
|
+
for attr in parent.child_attrs:
|
|
213
|
+
if attrs is not None and attr not in attrs: continue
|
|
214
|
+
if exclude is not None and attr in exclude: continue
|
|
215
|
+
child = getattr(parent, attr)
|
|
216
|
+
if child is not None:
|
|
217
|
+
if type(child) is list:
|
|
218
|
+
childretval = [self._visitchild(x, parent, attr, idx) for idx, x in enumerate(child)]
|
|
219
|
+
else:
|
|
220
|
+
childretval = self._visitchild(child, parent, attr, None)
|
|
221
|
+
assert not isinstance(childretval, list), 'Cannot insert list here: %s in %r' % (attr, parent)
|
|
222
|
+
result[attr] = childretval
|
|
223
|
+
return result
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
class VisitorTransform(TreeVisitor):
|
|
227
|
+
"""
|
|
228
|
+
A tree transform is a base class for visitors that wants to do stream
|
|
229
|
+
processing of the structure (rather than attributes etc.) of a tree.
|
|
230
|
+
|
|
231
|
+
It implements __call__ to simply visit the argument node.
|
|
232
|
+
|
|
233
|
+
It requires the visitor methods to return the nodes which should take
|
|
234
|
+
the place of the visited node in the result tree (which can be the same
|
|
235
|
+
or one or more replacement). Specifically, if the return value from
|
|
236
|
+
a visitor method is:
|
|
237
|
+
|
|
238
|
+
- [] or None; the visited node will be removed (set to None if an attribute and
|
|
239
|
+
removed if in a list)
|
|
240
|
+
- A single node; the visited node will be replaced by the returned node.
|
|
241
|
+
- A list of nodes; the visited nodes will be replaced by all the nodes in the
|
|
242
|
+
list. This will only work if the node was already a member of a list; if it
|
|
243
|
+
was not, an exception will be raised. (Typically you want to ensure that you
|
|
244
|
+
are within a StatListNode or similar before doing this.)
|
|
245
|
+
"""
|
|
246
|
+
def visitchildren(self, parent, attrs=None, exclude=None):
|
|
247
|
+
# generic def entry point for calls from Python subclasses
|
|
248
|
+
return self._process_children(parent, attrs, exclude)
|
|
249
|
+
|
|
250
|
+
@cython.final
|
|
251
|
+
def _process_children(self, parent, attrs=None, exclude=None):
|
|
252
|
+
# fast cdef entry point for calls from Cython subclasses
|
|
253
|
+
result = self._visitchildren(parent, attrs, exclude)
|
|
254
|
+
for attr, newnode in result.items():
|
|
255
|
+
if type(newnode) is list:
|
|
256
|
+
newnode = self._flatten_list(newnode)
|
|
257
|
+
setattr(parent, attr, newnode)
|
|
258
|
+
return result
|
|
259
|
+
|
|
260
|
+
@cython.final
|
|
261
|
+
def _flatten_list(self, orig_list):
|
|
262
|
+
# Flatten the list one level and remove any None
|
|
263
|
+
newlist = []
|
|
264
|
+
for x in orig_list:
|
|
265
|
+
if x is not None:
|
|
266
|
+
if type(x) is list:
|
|
267
|
+
newlist.extend(x)
|
|
268
|
+
else:
|
|
269
|
+
newlist.append(x)
|
|
270
|
+
return newlist
|
|
271
|
+
|
|
272
|
+
def visitchild(self, parent, attr, idx=0):
|
|
273
|
+
# Helper to visit specific children from Python subclasses
|
|
274
|
+
child = getattr(parent, attr)
|
|
275
|
+
if child is not None:
|
|
276
|
+
node = self._visitchild(child, parent, attr, idx)
|
|
277
|
+
if node is not child:
|
|
278
|
+
setattr(parent, attr, node)
|
|
279
|
+
child = node
|
|
280
|
+
return child
|
|
281
|
+
|
|
282
|
+
def recurse_to_children(self, node):
|
|
283
|
+
self._process_children(node)
|
|
284
|
+
return node
|
|
285
|
+
|
|
286
|
+
def __call__(self, root):
|
|
287
|
+
return self._visit(root)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
class CythonTransform(VisitorTransform):
|
|
291
|
+
"""
|
|
292
|
+
Certain common conventions and utilities for Cython transforms.
|
|
293
|
+
|
|
294
|
+
- Sets up the context of the pipeline in self.context
|
|
295
|
+
- Tracks directives in effect in self.current_directives
|
|
296
|
+
"""
|
|
297
|
+
def __init__(self, context):
|
|
298
|
+
super().__init__()
|
|
299
|
+
self.context = context
|
|
300
|
+
|
|
301
|
+
def __call__(self, node):
|
|
302
|
+
from .ModuleNode import ModuleNode
|
|
303
|
+
if isinstance(node, ModuleNode):
|
|
304
|
+
self.current_directives = node.directives
|
|
305
|
+
return super().__call__(node)
|
|
306
|
+
|
|
307
|
+
def visit_CompilerDirectivesNode(self, node):
|
|
308
|
+
old = self.current_directives
|
|
309
|
+
self.current_directives = node.directives
|
|
310
|
+
self._process_children(node)
|
|
311
|
+
self.current_directives = old
|
|
312
|
+
return node
|
|
313
|
+
|
|
314
|
+
def visit_Node(self, node):
|
|
315
|
+
self._process_children(node)
|
|
316
|
+
return node
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
class ScopeTrackingTransform(CythonTransform):
|
|
320
|
+
# Keeps track of type of scopes
|
|
321
|
+
#scope_type: can be either of 'module', 'function', 'cclass', 'pyclass', 'struct'
|
|
322
|
+
#scope_node: the node that owns the current scope
|
|
323
|
+
|
|
324
|
+
def visit_ModuleNode(self, node):
|
|
325
|
+
self.scope_type = 'module'
|
|
326
|
+
self.scope_node = node
|
|
327
|
+
self._process_children(node)
|
|
328
|
+
return node
|
|
329
|
+
|
|
330
|
+
def visit_scope(self, node, scope_type):
|
|
331
|
+
prev = self.scope_type, self.scope_node
|
|
332
|
+
self.scope_type = scope_type
|
|
333
|
+
self.scope_node = node
|
|
334
|
+
self._process_children(node)
|
|
335
|
+
self.scope_type, self.scope_node = prev
|
|
336
|
+
return node
|
|
337
|
+
|
|
338
|
+
def visit_CClassDefNode(self, node):
|
|
339
|
+
return self.visit_scope(node, 'cclass')
|
|
340
|
+
|
|
341
|
+
def visit_PyClassDefNode(self, node):
|
|
342
|
+
return self.visit_scope(node, 'pyclass')
|
|
343
|
+
|
|
344
|
+
def visit_FuncDefNode(self, node):
|
|
345
|
+
return self.visit_scope(node, 'function')
|
|
346
|
+
|
|
347
|
+
def visit_CStructOrUnionDefNode(self, node):
|
|
348
|
+
return self.visit_scope(node, 'struct')
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
class EnvTransform(CythonTransform):
|
|
352
|
+
"""
|
|
353
|
+
This transformation keeps a stack of the environments.
|
|
354
|
+
"""
|
|
355
|
+
def __call__(self, root):
|
|
356
|
+
self.env_stack = []
|
|
357
|
+
self.enter_scope(root, root.scope)
|
|
358
|
+
return super().__call__(root)
|
|
359
|
+
|
|
360
|
+
def current_env(self):
|
|
361
|
+
return self.env_stack[-1][1]
|
|
362
|
+
|
|
363
|
+
def current_scope_node(self):
|
|
364
|
+
return self.env_stack[-1][0]
|
|
365
|
+
|
|
366
|
+
def global_scope(self):
|
|
367
|
+
return self.current_env().global_scope()
|
|
368
|
+
|
|
369
|
+
def enter_scope(self, node, scope):
|
|
370
|
+
self.env_stack.append((node, scope))
|
|
371
|
+
|
|
372
|
+
def exit_scope(self):
|
|
373
|
+
self.env_stack.pop()
|
|
374
|
+
|
|
375
|
+
def visit_FuncDefNode(self, node):
|
|
376
|
+
self.visit_func_outer_attrs(node)
|
|
377
|
+
self.enter_scope(node, node.local_scope)
|
|
378
|
+
self.visitchildren(node, attrs=None, exclude=node.outer_attrs)
|
|
379
|
+
self.exit_scope()
|
|
380
|
+
return node
|
|
381
|
+
|
|
382
|
+
def visit_func_outer_attrs(self, node):
|
|
383
|
+
self.visitchildren(node, attrs=node.outer_attrs)
|
|
384
|
+
|
|
385
|
+
def visit_GeneratorBodyDefNode(self, node):
|
|
386
|
+
self._process_children(node)
|
|
387
|
+
return node
|
|
388
|
+
|
|
389
|
+
def visit_ClassDefNode(self, node):
|
|
390
|
+
self.enter_scope(node, node.scope)
|
|
391
|
+
self._process_children(node)
|
|
392
|
+
self.exit_scope()
|
|
393
|
+
return node
|
|
394
|
+
|
|
395
|
+
def visit_CStructOrUnionDefNode(self, node):
|
|
396
|
+
self.enter_scope(node, node.scope)
|
|
397
|
+
self._process_children(node)
|
|
398
|
+
self.exit_scope()
|
|
399
|
+
return node
|
|
400
|
+
|
|
401
|
+
def visit_ScopedExprNode(self, node):
|
|
402
|
+
if node.expr_scope:
|
|
403
|
+
self.enter_scope(node, node.expr_scope)
|
|
404
|
+
self._process_children(node)
|
|
405
|
+
self.exit_scope()
|
|
406
|
+
else:
|
|
407
|
+
self._process_children(node)
|
|
408
|
+
return node
|
|
409
|
+
|
|
410
|
+
def visit_CArgDeclNode(self, node):
|
|
411
|
+
# default arguments are evaluated in the outer scope
|
|
412
|
+
if node.default:
|
|
413
|
+
attrs = [attr for attr in node.child_attrs if attr != 'default']
|
|
414
|
+
self._process_children(node, attrs)
|
|
415
|
+
self.enter_scope(node, self.current_env().outer_scope)
|
|
416
|
+
self.visitchildren(node, ('default',))
|
|
417
|
+
self.exit_scope()
|
|
418
|
+
else:
|
|
419
|
+
self._process_children(node)
|
|
420
|
+
return node
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
class NodeRefCleanupMixin:
|
|
424
|
+
"""
|
|
425
|
+
Clean up references to nodes that were replaced.
|
|
426
|
+
|
|
427
|
+
NOTE: this implementation assumes that the replacement is
|
|
428
|
+
done first, before hitting any further references during
|
|
429
|
+
normal tree traversal. This needs to be arranged by calling
|
|
430
|
+
"self.visitchildren()" at a proper place in the transform
|
|
431
|
+
and by ordering the "child_attrs" of nodes appropriately.
|
|
432
|
+
"""
|
|
433
|
+
def __init__(self, *args):
|
|
434
|
+
super().__init__(*args)
|
|
435
|
+
self._replacements = {}
|
|
436
|
+
|
|
437
|
+
def visit_CloneNode(self, node):
|
|
438
|
+
arg = node.arg
|
|
439
|
+
if arg not in self._replacements:
|
|
440
|
+
self.visitchildren(arg)
|
|
441
|
+
node.arg = self._replacements.get(arg, arg)
|
|
442
|
+
return node
|
|
443
|
+
|
|
444
|
+
def visit_ResultRefNode(self, node):
|
|
445
|
+
expr = node.expression
|
|
446
|
+
if expr is None or expr not in self._replacements:
|
|
447
|
+
self.visitchildren(node)
|
|
448
|
+
expr = node.expression
|
|
449
|
+
if expr is not None:
|
|
450
|
+
node.expression = self._replacements.get(expr, expr)
|
|
451
|
+
return node
|
|
452
|
+
|
|
453
|
+
def replace(self, node, replacement):
|
|
454
|
+
self._replacements[node] = replacement
|
|
455
|
+
return replacement
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
find_special_method_for_binary_operator = {
|
|
459
|
+
'<': '__lt__',
|
|
460
|
+
'<=': '__le__',
|
|
461
|
+
'==': '__eq__',
|
|
462
|
+
'!=': '__ne__',
|
|
463
|
+
'>=': '__ge__',
|
|
464
|
+
'>': '__gt__',
|
|
465
|
+
'+': '__add__',
|
|
466
|
+
'&': '__and__',
|
|
467
|
+
'/': '__div__',
|
|
468
|
+
'//': '__floordiv__',
|
|
469
|
+
'<<': '__lshift__',
|
|
470
|
+
'%': '__mod__',
|
|
471
|
+
'*': '__mul__',
|
|
472
|
+
'|': '__or__',
|
|
473
|
+
'**': '__pow__',
|
|
474
|
+
'>>': '__rshift__',
|
|
475
|
+
'-': '__sub__',
|
|
476
|
+
'^': '__xor__',
|
|
477
|
+
'in': '__contains__',
|
|
478
|
+
}.get
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
find_special_method_for_unary_operator = {
|
|
482
|
+
'not': '__not__',
|
|
483
|
+
'~': '__inv__',
|
|
484
|
+
'-': '__neg__',
|
|
485
|
+
'+': '__pos__',
|
|
486
|
+
}.get
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
class MethodDispatcherTransform(EnvTransform):
|
|
490
|
+
"""
|
|
491
|
+
Base class for transformations that want to intercept on specific
|
|
492
|
+
builtin functions or methods of builtin types, including special
|
|
493
|
+
methods triggered by Python operators. Must run after declaration
|
|
494
|
+
analysis when entries were assigned.
|
|
495
|
+
|
|
496
|
+
Naming pattern for handler methods is as follows:
|
|
497
|
+
|
|
498
|
+
* builtin functions: _handle_(general|simple|any)_function_NAME
|
|
499
|
+
|
|
500
|
+
* builtin methods: _handle_(general|simple|any)_method_TYPENAME_METHODNAME
|
|
501
|
+
"""
|
|
502
|
+
# only visit call nodes and Python operations
|
|
503
|
+
def visit_GeneralCallNode(self, node):
|
|
504
|
+
self._process_children(node)
|
|
505
|
+
function = node.function
|
|
506
|
+
if not function.type.is_pyobject:
|
|
507
|
+
return node
|
|
508
|
+
arg_tuple = node.positional_args
|
|
509
|
+
if not isinstance(arg_tuple, ExprNodes.TupleNode):
|
|
510
|
+
return node
|
|
511
|
+
keyword_args = node.keyword_args
|
|
512
|
+
if keyword_args and not isinstance(keyword_args, ExprNodes.DictNode):
|
|
513
|
+
# can't handle **kwargs
|
|
514
|
+
return node
|
|
515
|
+
args = arg_tuple.args
|
|
516
|
+
return self._dispatch_to_handler(node, function, args, keyword_args)
|
|
517
|
+
|
|
518
|
+
def visit_SimpleCallNode(self, node):
|
|
519
|
+
self._process_children(node)
|
|
520
|
+
function = node.function
|
|
521
|
+
if function.type.is_pyobject:
|
|
522
|
+
arg_tuple = node.arg_tuple
|
|
523
|
+
if not isinstance(arg_tuple, ExprNodes.TupleNode):
|
|
524
|
+
return node
|
|
525
|
+
args = arg_tuple.args
|
|
526
|
+
else:
|
|
527
|
+
args = node.args
|
|
528
|
+
return self._dispatch_to_handler(node, function, args, None)
|
|
529
|
+
|
|
530
|
+
def visit_PrimaryCmpNode(self, node):
|
|
531
|
+
if node.cascade:
|
|
532
|
+
# not currently handled below
|
|
533
|
+
self._process_children(node)
|
|
534
|
+
return node
|
|
535
|
+
return self._visit_binop_node(node)
|
|
536
|
+
|
|
537
|
+
def visit_BinopNode(self, node):
|
|
538
|
+
return self._visit_binop_node(node)
|
|
539
|
+
|
|
540
|
+
def _visit_binop_node(self, node):
|
|
541
|
+
self._process_children(node)
|
|
542
|
+
# FIXME: could special case 'not_in'
|
|
543
|
+
special_method_name = find_special_method_for_binary_operator(node.operator)
|
|
544
|
+
if special_method_name:
|
|
545
|
+
operand1, operand2 = node.operand1, node.operand2
|
|
546
|
+
if special_method_name == '__contains__':
|
|
547
|
+
operand1, operand2 = operand2, operand1
|
|
548
|
+
elif special_method_name == '__div__':
|
|
549
|
+
if Future.division in self.current_env().context.future_directives:
|
|
550
|
+
special_method_name = '__truediv__'
|
|
551
|
+
obj_type = operand1.type
|
|
552
|
+
if obj_type.is_builtin_type:
|
|
553
|
+
type_name = obj_type.name
|
|
554
|
+
else:
|
|
555
|
+
type_name = "object" # safety measure
|
|
556
|
+
node = self._dispatch_to_method_handler(
|
|
557
|
+
special_method_name, None, False, type_name,
|
|
558
|
+
node, None, [operand1, operand2], None)
|
|
559
|
+
return node
|
|
560
|
+
|
|
561
|
+
def visit_UnopNode(self, node):
|
|
562
|
+
self._process_children(node)
|
|
563
|
+
special_method_name = find_special_method_for_unary_operator(node.operator)
|
|
564
|
+
if special_method_name:
|
|
565
|
+
operand = node.operand
|
|
566
|
+
obj_type = operand.type
|
|
567
|
+
if obj_type.is_builtin_type:
|
|
568
|
+
type_name = obj_type.name
|
|
569
|
+
else:
|
|
570
|
+
type_name = "object" # safety measure
|
|
571
|
+
node = self._dispatch_to_method_handler(
|
|
572
|
+
special_method_name, None, False, type_name,
|
|
573
|
+
node, None, [operand], None)
|
|
574
|
+
return node
|
|
575
|
+
|
|
576
|
+
### dispatch to specific handlers
|
|
577
|
+
|
|
578
|
+
def _find_handler(self, match_name, has_kwargs):
|
|
579
|
+
if not match_name.isascii():
|
|
580
|
+
# Classes with unicode names won't have specific handlers.
|
|
581
|
+
return None
|
|
582
|
+
|
|
583
|
+
call_type = 'general' if has_kwargs else 'simple'
|
|
584
|
+
handler = getattr(self, f'_handle_{call_type}_{match_name}', None)
|
|
585
|
+
if handler is None:
|
|
586
|
+
handler = getattr(self, f'_handle_any_{match_name}', None)
|
|
587
|
+
return handler
|
|
588
|
+
|
|
589
|
+
def _delegate_to_assigned_value(self, node, function, arg_list, kwargs):
|
|
590
|
+
assignment = function.cf_state[0]
|
|
591
|
+
value = assignment.rhs
|
|
592
|
+
if value.is_name:
|
|
593
|
+
if not value.entry or len(value.entry.cf_assignments) > 1:
|
|
594
|
+
# the variable might have been reassigned => play safe
|
|
595
|
+
return node
|
|
596
|
+
elif value.is_attribute and value.obj.is_name:
|
|
597
|
+
if not value.obj.entry or len(value.obj.entry.cf_assignments) > 1:
|
|
598
|
+
# the underlying variable might have been reassigned => play safe
|
|
599
|
+
return node
|
|
600
|
+
else:
|
|
601
|
+
return node
|
|
602
|
+
return self._dispatch_to_handler(
|
|
603
|
+
node, value, arg_list, kwargs)
|
|
604
|
+
|
|
605
|
+
def _dispatch_to_handler(self, node, function, arg_list, kwargs):
|
|
606
|
+
if function.is_name:
|
|
607
|
+
# we only consider functions that are either builtin
|
|
608
|
+
# Python functions or builtins that were already replaced
|
|
609
|
+
# into a C function call (defined in the builtin scope)
|
|
610
|
+
if not function.entry:
|
|
611
|
+
return node
|
|
612
|
+
entry = function.entry
|
|
613
|
+
is_builtin = (
|
|
614
|
+
entry.is_builtin or
|
|
615
|
+
entry is self.current_env().builtin_scope().lookup_here(function.name))
|
|
616
|
+
if not is_builtin:
|
|
617
|
+
if function.cf_state and function.cf_state.is_single:
|
|
618
|
+
# we know the value of the variable
|
|
619
|
+
# => see if it's usable instead
|
|
620
|
+
return self._delegate_to_assigned_value(
|
|
621
|
+
node, function, arg_list, kwargs)
|
|
622
|
+
if arg_list and entry.is_cmethod and entry.scope and entry.scope.parent_type.is_builtin_type:
|
|
623
|
+
if entry.scope.parent_type is arg_list[0].type:
|
|
624
|
+
# Optimised (unbound) method of a builtin type => try to "de-optimise".
|
|
625
|
+
return self._dispatch_to_method_handler(
|
|
626
|
+
entry.name, self_arg=None, is_unbound_method=True,
|
|
627
|
+
type_name=entry.scope.parent_type.name,
|
|
628
|
+
node=node, function=function, arg_list=arg_list, kwargs=kwargs)
|
|
629
|
+
return node
|
|
630
|
+
function_handler = self._find_handler(
|
|
631
|
+
f"function_{function.name}", kwargs)
|
|
632
|
+
if function_handler is None:
|
|
633
|
+
return self._handle_function(node, function.name, function, arg_list, kwargs)
|
|
634
|
+
if kwargs:
|
|
635
|
+
return function_handler(node, function, arg_list, kwargs)
|
|
636
|
+
else:
|
|
637
|
+
return function_handler(node, function, arg_list)
|
|
638
|
+
elif function.is_attribute:
|
|
639
|
+
attr_name = function.attribute
|
|
640
|
+
if function.type.is_pyobject:
|
|
641
|
+
self_arg = function.obj
|
|
642
|
+
elif node.self and function.entry:
|
|
643
|
+
entry = function.entry.as_variable
|
|
644
|
+
if not entry or not entry.is_builtin:
|
|
645
|
+
return node
|
|
646
|
+
# C implementation of a Python builtin method - see if we find further matches
|
|
647
|
+
self_arg = node.self
|
|
648
|
+
arg_list = arg_list[1:] # drop CloneNode of self argument
|
|
649
|
+
else:
|
|
650
|
+
return node
|
|
651
|
+
obj_type = self_arg.type
|
|
652
|
+
is_unbound_method = False
|
|
653
|
+
if obj_type.is_builtin_type:
|
|
654
|
+
if obj_type is Builtin.type_type and self_arg.is_name and arg_list and arg_list[0].type.is_pyobject:
|
|
655
|
+
# calling an unbound method like 'list.append(L,x)'
|
|
656
|
+
# (ignoring 'type.mro()' here ...)
|
|
657
|
+
type_name = self_arg.name
|
|
658
|
+
self_arg = None
|
|
659
|
+
is_unbound_method = True
|
|
660
|
+
else:
|
|
661
|
+
type_name = obj_type.name
|
|
662
|
+
if type_name == 'str':
|
|
663
|
+
# We traditionally used the type name 'unicode' for 'str' dispatch methods.
|
|
664
|
+
type_name = 'unicode'
|
|
665
|
+
else:
|
|
666
|
+
type_name = "object" # safety measure
|
|
667
|
+
return self._dispatch_to_method_handler(
|
|
668
|
+
attr_name, self_arg, is_unbound_method, type_name,
|
|
669
|
+
node, function, arg_list, kwargs)
|
|
670
|
+
else:
|
|
671
|
+
return node
|
|
672
|
+
|
|
673
|
+
def _dispatch_to_method_handler(self, attr_name, self_arg,
|
|
674
|
+
is_unbound_method, type_name,
|
|
675
|
+
node, function, arg_list, kwargs):
|
|
676
|
+
method_handler = self._find_handler(
|
|
677
|
+
f"method_{type_name}_{attr_name}", kwargs)
|
|
678
|
+
if method_handler is None:
|
|
679
|
+
if (attr_name in TypeSlots.special_method_names
|
|
680
|
+
or attr_name in ['__new__', '__class__']):
|
|
681
|
+
method_handler = self._find_handler(
|
|
682
|
+
f"slot{attr_name}", kwargs)
|
|
683
|
+
if method_handler is None:
|
|
684
|
+
return self._handle_method(
|
|
685
|
+
node, type_name, attr_name, function,
|
|
686
|
+
arg_list, is_unbound_method, kwargs)
|
|
687
|
+
if self_arg is not None:
|
|
688
|
+
arg_list = [self_arg] + list(arg_list)
|
|
689
|
+
if kwargs:
|
|
690
|
+
result = method_handler(
|
|
691
|
+
node, function, arg_list, is_unbound_method, kwargs)
|
|
692
|
+
else:
|
|
693
|
+
result = method_handler(
|
|
694
|
+
node, function, arg_list, is_unbound_method)
|
|
695
|
+
return result
|
|
696
|
+
|
|
697
|
+
def _handle_function(self, node, function_name, function, arg_list, kwargs):
|
|
698
|
+
"""Fallback handler"""
|
|
699
|
+
return node
|
|
700
|
+
|
|
701
|
+
def _handle_method(self, node, type_name, attr_name, function,
|
|
702
|
+
arg_list, is_unbound_method, kwargs):
|
|
703
|
+
"""Fallback handler"""
|
|
704
|
+
return node
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
class RecursiveNodeReplacer(VisitorTransform):
|
|
708
|
+
"""
|
|
709
|
+
Recursively replace all occurrences of a node in a subtree by
|
|
710
|
+
another node.
|
|
711
|
+
"""
|
|
712
|
+
def __init__(self, orig_node, new_node):
|
|
713
|
+
super().__init__()
|
|
714
|
+
self.orig_node, self.new_node = orig_node, new_node
|
|
715
|
+
|
|
716
|
+
def visit_CloneNode(self, node):
|
|
717
|
+
if node is self.orig_node:
|
|
718
|
+
return self.new_node
|
|
719
|
+
if node.arg is self.orig_node:
|
|
720
|
+
node.arg = self.new_node
|
|
721
|
+
return node
|
|
722
|
+
|
|
723
|
+
def visit_Node(self, node):
|
|
724
|
+
self._process_children(node)
|
|
725
|
+
if node is self.orig_node:
|
|
726
|
+
return self.new_node
|
|
727
|
+
else:
|
|
728
|
+
return node
|
|
729
|
+
|
|
730
|
+
def recursively_replace_node(tree, old_node, new_node):
|
|
731
|
+
replace_in = RecursiveNodeReplacer(old_node, new_node)
|
|
732
|
+
replace_in(tree)
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
class NodeFinder(TreeVisitor):
|
|
736
|
+
"""
|
|
737
|
+
Find out if a node appears in a subtree.
|
|
738
|
+
"""
|
|
739
|
+
def __init__(self, node):
|
|
740
|
+
super().__init__()
|
|
741
|
+
self.node = node
|
|
742
|
+
self.found = False
|
|
743
|
+
|
|
744
|
+
def visit_Node(self, node):
|
|
745
|
+
if self.found:
|
|
746
|
+
pass # short-circuit
|
|
747
|
+
elif node is self.node:
|
|
748
|
+
self.found = True
|
|
749
|
+
else:
|
|
750
|
+
self._visitchildren(node, None, None)
|
|
751
|
+
|
|
752
|
+
def tree_contains(tree, node):
|
|
753
|
+
finder = NodeFinder(node)
|
|
754
|
+
finder.visit(tree)
|
|
755
|
+
return finder.found
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
# Utils
|
|
759
|
+
def replace_node(ptr, value):
|
|
760
|
+
"""Replaces a node. ptr is of the form used on the access path stack
|
|
761
|
+
(parent, attrname, listidx|None)
|
|
762
|
+
"""
|
|
763
|
+
parent, attrname, listidx = ptr
|
|
764
|
+
if listidx is None:
|
|
765
|
+
setattr(parent, attrname, value)
|
|
766
|
+
else:
|
|
767
|
+
getattr(parent, attrname)[listidx] = value
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
class PrintTree(TreeVisitor):
|
|
771
|
+
"""Prints a representation of the tree to standard output.
|
|
772
|
+
Subclass and override repr_of to provide more information
|
|
773
|
+
about nodes. """
|
|
774
|
+
def __init__(self, start=None, end=None):
|
|
775
|
+
TreeVisitor.__init__(self)
|
|
776
|
+
self._indent = ""
|
|
777
|
+
if start is not None or end is not None:
|
|
778
|
+
self._line_range = (start or 0, end or 2**30)
|
|
779
|
+
else:
|
|
780
|
+
self._line_range = None
|
|
781
|
+
|
|
782
|
+
def indent(self):
|
|
783
|
+
self._indent += " "
|
|
784
|
+
|
|
785
|
+
def unindent(self):
|
|
786
|
+
self._indent = self._indent[:-2]
|
|
787
|
+
|
|
788
|
+
def __call__(self, tree, phase=None):
|
|
789
|
+
print("Parse tree dump at phase '%s'" % phase)
|
|
790
|
+
self.visit(tree)
|
|
791
|
+
return tree
|
|
792
|
+
|
|
793
|
+
# Don't do anything about process_list, the defaults gives
|
|
794
|
+
# nice-looking name[idx] nodes which will visually appear
|
|
795
|
+
# under the parent-node, not displaying the list itself in
|
|
796
|
+
# the hierarchy.
|
|
797
|
+
def visit_Node(self, node):
|
|
798
|
+
self._print_node(node)
|
|
799
|
+
self.indent()
|
|
800
|
+
self.visitchildren(node)
|
|
801
|
+
self.unindent()
|
|
802
|
+
return node
|
|
803
|
+
|
|
804
|
+
def visit_CloneNode(self, node):
|
|
805
|
+
self._print_node(node)
|
|
806
|
+
self.indent()
|
|
807
|
+
line = node.pos[1]
|
|
808
|
+
if self._line_range is None or self._line_range[0] <= line <= self._line_range[1]:
|
|
809
|
+
print("%s- %s: %s" % (self._indent, 'arg', self.repr_of(node.arg)))
|
|
810
|
+
self.indent()
|
|
811
|
+
self.visitchildren(node.arg)
|
|
812
|
+
self.unindent()
|
|
813
|
+
self.unindent()
|
|
814
|
+
return node
|
|
815
|
+
|
|
816
|
+
def _print_node(self, node):
|
|
817
|
+
line = node.pos[1]
|
|
818
|
+
if self._line_range is None or self._line_range[0] <= line <= self._line_range[1]:
|
|
819
|
+
if len(self.access_path) == 0:
|
|
820
|
+
name = "(root)"
|
|
821
|
+
else:
|
|
822
|
+
parent, attr, idx = self.access_path[-1]
|
|
823
|
+
if idx is not None:
|
|
824
|
+
name = "%s[%d]" % (attr, idx)
|
|
825
|
+
else:
|
|
826
|
+
name = attr
|
|
827
|
+
print("%s- %s: %s" % (self._indent, name, self.repr_of(node)))
|
|
828
|
+
|
|
829
|
+
def repr_of(self, node):
|
|
830
|
+
if node is None:
|
|
831
|
+
return "(none)"
|
|
832
|
+
else:
|
|
833
|
+
result = node.__class__.__name__
|
|
834
|
+
if isinstance(node, ExprNodes.NameNode):
|
|
835
|
+
result += "(type=%s, name=\"%s\")" % (repr(node.type), node.name)
|
|
836
|
+
elif isinstance(node, Nodes.DefNode):
|
|
837
|
+
result += "(name=\"%s\")" % node.name
|
|
838
|
+
elif isinstance(node, Nodes.CFuncDefNode):
|
|
839
|
+
result += "(name=\"%s\", type=\"%s\")" % (
|
|
840
|
+
node.declared_name(), getattr(node, "type", None))
|
|
841
|
+
elif isinstance(node, ExprNodes.AttributeNode):
|
|
842
|
+
result += "(type=%s, attribute=\"%s\")" % (repr(node.type), node.attribute)
|
|
843
|
+
elif isinstance(node, (ExprNodes.ConstNode, ExprNodes.PyConstNode)):
|
|
844
|
+
result += "(type=%s, value=%r)" % (repr(node.type), node.value)
|
|
845
|
+
elif isinstance(node, ExprNodes.ExprNode):
|
|
846
|
+
t = node.type
|
|
847
|
+
result += "(type=%s)" % repr(t)
|
|
848
|
+
elif node.pos:
|
|
849
|
+
pos = node.pos
|
|
850
|
+
path = pos[0].get_description()
|
|
851
|
+
if '/' in path:
|
|
852
|
+
path = path.split('/')[-1]
|
|
853
|
+
if '\\' in path:
|
|
854
|
+
path = path.split('\\')[-1]
|
|
855
|
+
result += "(pos=(%s:%s:%s))" % (path, pos[1], pos[2])
|
|
856
|
+
|
|
857
|
+
return result
|
|
858
|
+
|
|
859
|
+
if __name__ == "__main__":
|
|
860
|
+
import doctest
|
|
861
|
+
doctest.testmod()
|