Nuitka-winsvc 2.4.11__cp311-cp311-win_amd64.whl → 2.5.6__cp311-cp311-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.
Potentially problematic release.
This version of Nuitka-winsvc might be problematic. Click here for more details.
- {Nuitka_winsvc-2.4.11.dist-info → Nuitka_winsvc-2.5.6.dist-info}/METADATA +56 -109
- {Nuitka_winsvc-2.4.11.dist-info → Nuitka_winsvc-2.5.6.dist-info}/RECORD +304 -280
- {Nuitka_winsvc-2.4.11.dist-info → Nuitka_winsvc-2.5.6.dist-info}/WHEEL +1 -1
- nuitka/Builtins.py +31 -0
- nuitka/HardImportRegistry.py +8 -0
- nuitka/MainControl.py +9 -4
- nuitka/OptionParsing.py +108 -54
- nuitka/Options.py +69 -13
- nuitka/OutputDirectories.py +7 -2
- nuitka/PostProcessing.py +69 -38
- nuitka/Progress.py +16 -1
- nuitka/PythonVersions.py +2 -1
- nuitka/Serialization.py +4 -2
- nuitka/Tracing.py +69 -1
- nuitka/Version.py +2 -2
- nuitka/build/Backend.scons +45 -5
- nuitka/build/CCompilerVersion.scons +6 -0
- nuitka/build/Onefile.scons +6 -0
- nuitka/build/SconsCaching.py +4 -0
- nuitka/build/SconsCompilerSettings.py +14 -3
- nuitka/build/SconsInterface.py +3 -0
- nuitka/build/SconsUtils.py +5 -0
- nuitka/build/include/nuitka/allocator.h +124 -1
- nuitka/build/include/nuitka/calling.h +6 -6
- nuitka/build/include/nuitka/compiled_asyncgen.h +10 -7
- nuitka/build/include/nuitka/compiled_frame.h +2 -1
- nuitka/build/include/nuitka/constants.h +8 -0
- nuitka/build/include/nuitka/debug_settings.h +60 -0
- nuitka/build/include/nuitka/exceptions.h +264 -121
- nuitka/build/include/nuitka/helper/attributes.h +0 -4
- nuitka/build/include/nuitka/helper/calling_generated.h +52 -52
- nuitka/build/include/nuitka/helper/comparisons_dual_eq.h +47 -0
- nuitka/build/include/nuitka/helper/comparisons_dual_ge.h +39 -0
- nuitka/build/include/nuitka/helper/comparisons_dual_gt.h +39 -0
- nuitka/build/include/nuitka/helper/comparisons_dual_le.h +47 -0
- nuitka/build/include/nuitka/helper/comparisons_dual_lt.h +47 -0
- nuitka/build/include/nuitka/helper/comparisons_dual_ne.h +39 -0
- nuitka/build/include/nuitka/helper/dictionaries.h +75 -31
- nuitka/build/include/nuitka/helper/ints.h +86 -37
- nuitka/build/include/nuitka/helper/iterators.h +82 -34
- nuitka/build/include/nuitka/helper/operations.h +3 -0
- nuitka/build/include/nuitka/helper/operations_binary_add.h +6 -0
- nuitka/build/include/nuitka/helper/operations_binary_dual_add.h +34 -0
- nuitka/build/include/nuitka/helper/operations_binary_sub.h +6 -0
- nuitka/build/include/nuitka/helper/operations_inplace_add.h +3 -0
- nuitka/build/include/nuitka/helper/operations_inplace_sub.h +3 -0
- nuitka/build/include/nuitka/helper/raising.h +47 -28
- nuitka/build/include/nuitka/helper/richcomparisons.h +7 -0
- nuitka/build/include/nuitka/helper/tuples.h +12 -0
- nuitka/build/include/nuitka/helpers.h +12 -2
- nuitka/build/include/nuitka/prelude.h +36 -25
- nuitka/build/include/nuitka/printing.h +9 -7
- nuitka/build/include/nuitka/threading.h +15 -1
- nuitka/build/include/nuitka/type_aliases.h +3 -0
- nuitka/build/inline_copy/stubgen/astunparse.py +938 -0
- nuitka/build/inline_copy/stubgen/six.py +998 -0
- nuitka/build/inline_copy/stubgen/stubgen.py +307 -0
- nuitka/build/static_src/CompiledAsyncgenType.c +45 -39
- nuitka/build/static_src/CompiledCellType.c +6 -4
- nuitka/build/static_src/CompiledCodeHelpers.c +106 -40
- nuitka/build/static_src/CompiledCoroutineType.c +41 -31
- nuitka/build/static_src/CompiledFrameType.c +110 -79
- nuitka/build/static_src/CompiledFunctionType.c +173 -151
- nuitka/build/static_src/CompiledGeneratorType.c +51 -49
- nuitka/build/static_src/CompiledGeneratorTypeUncompiledIntegration.c +433 -46
- nuitka/build/static_src/CompiledMethodType.c +6 -12
- nuitka/build/static_src/HelpersAllocator.c +266 -28
- nuitka/build/static_src/HelpersAttributes.c +0 -21
- nuitka/build/static_src/HelpersBuiltin.c +23 -14
- nuitka/build/static_src/HelpersCalling.c +7 -7
- nuitka/build/static_src/HelpersCallingGenerated.c +70 -70
- nuitka/build/static_src/HelpersComparisonDualEq.c +183 -0
- nuitka/build/static_src/HelpersComparisonDualGe.c +121 -0
- nuitka/build/static_src/HelpersComparisonDualGt.c +121 -0
- nuitka/build/static_src/HelpersComparisonDualLe.c +183 -0
- nuitka/build/static_src/HelpersComparisonDualLt.c +183 -0
- nuitka/build/static_src/HelpersComparisonDualNe.c +121 -0
- nuitka/build/static_src/HelpersComparisonEq.c +18 -4
- nuitka/build/static_src/HelpersComparisonGe.c +18 -4
- nuitka/build/static_src/HelpersComparisonGt.c +18 -4
- nuitka/build/static_src/HelpersComparisonLe.c +18 -4
- nuitka/build/static_src/HelpersComparisonLt.c +18 -4
- nuitka/build/static_src/HelpersComparisonNe.c +18 -4
- nuitka/build/static_src/HelpersConsole.c +34 -8
- nuitka/build/static_src/HelpersConstantsBlob.c +4 -2
- nuitka/build/static_src/HelpersDeepcopy.c +1 -0
- nuitka/build/static_src/HelpersDictionaries.c +195 -36
- nuitka/build/static_src/HelpersDictionariesGenerated.c +12 -107
- nuitka/build/static_src/HelpersExceptions.c +24 -50
- nuitka/build/static_src/HelpersFiles.c +79 -5
- nuitka/build/static_src/HelpersImport.c +13 -9
- nuitka/build/static_src/HelpersImportHard.c +23 -46
- nuitka/build/static_src/HelpersLists.c +98 -9
- nuitka/build/static_src/HelpersOperationBinaryAdd.c +224 -6
- nuitka/build/static_src/HelpersOperationBinaryAddUtils.c +3 -0
- nuitka/build/static_src/HelpersOperationBinaryBitand.c +5 -5
- nuitka/build/static_src/HelpersOperationBinaryBitor.c +5 -5
- nuitka/build/static_src/HelpersOperationBinaryBitxor.c +5 -5
- nuitka/build/static_src/HelpersOperationBinaryDivmod.c +8 -2
- nuitka/build/static_src/HelpersOperationBinaryDualAdd.c +172 -0
- nuitka/build/static_src/HelpersOperationBinaryFloordiv.c +14 -8
- nuitka/build/static_src/HelpersOperationBinaryLshift.c +36 -36
- nuitka/build/static_src/HelpersOperationBinaryMatmult.c +1 -0
- nuitka/build/static_src/HelpersOperationBinaryMod.c +22 -10
- nuitka/build/static_src/HelpersOperationBinaryMult.c +17 -7
- nuitka/build/static_src/HelpersOperationBinaryOlddiv.c +14 -8
- nuitka/build/static_src/HelpersOperationBinaryPow.c +24 -24
- nuitka/build/static_src/HelpersOperationBinaryRshift.c +4 -4
- nuitka/build/static_src/HelpersOperationBinarySub.c +229 -8
- nuitka/build/static_src/HelpersOperationBinaryTruediv.c +8 -2
- nuitka/build/static_src/HelpersOperationInplaceAdd.c +162 -10
- nuitka/build/static_src/HelpersOperationInplaceBitand.c +5 -5
- nuitka/build/static_src/HelpersOperationInplaceBitor.c +5 -5
- nuitka/build/static_src/HelpersOperationInplaceBitxor.c +5 -5
- nuitka/build/static_src/HelpersOperationInplaceFloordiv.c +11 -6
- nuitka/build/static_src/HelpersOperationInplaceLshift.c +20 -20
- nuitka/build/static_src/HelpersOperationInplaceMatmult.c +1 -0
- nuitka/build/static_src/HelpersOperationInplaceMod.c +11 -6
- nuitka/build/static_src/HelpersOperationInplaceMult.c +11 -6
- nuitka/build/static_src/HelpersOperationInplaceOlddiv.c +11 -6
- nuitka/build/static_src/HelpersOperationInplacePow.c +20 -20
- nuitka/build/static_src/HelpersOperationInplaceRshift.c +4 -4
- nuitka/build/static_src/HelpersOperationInplaceSub.c +163 -10
- nuitka/build/static_src/HelpersOperationInplaceTruediv.c +6 -1
- nuitka/build/static_src/HelpersProfiling.c +1 -1
- nuitka/build/static_src/HelpersRaising.c +255 -310
- nuitka/build/static_src/HelpersStrings.c +1 -1
- nuitka/build/static_src/HelpersTuples.c +2 -2
- nuitka/build/static_src/HelpersTypes.c +98 -2
- nuitka/build/static_src/MainProgram.c +132 -28
- nuitka/build/static_src/MetaPathBasedLoader.c +18 -19
- nuitka/build/static_src/MetaPathBasedLoaderResourceReaderFiles.c +134 -11
- nuitka/build/static_src/OnefileBootstrap.c +6 -1
- nuitka/code_generation/AsyncgenCodes.py +2 -6
- nuitka/code_generation/BinaryOperationHelperDefinitions.py +269 -167
- nuitka/code_generation/BuiltinCodes.py +9 -1
- nuitka/code_generation/CallCodes.py +16 -16
- nuitka/code_generation/CodeGeneration.py +14 -2
- nuitka/code_generation/CodeHelperSelection.py +1 -1
- nuitka/code_generation/CodeHelpers.py +4 -1
- nuitka/code_generation/CodeObjectCodes.py +2 -2
- nuitka/code_generation/ComparisonCodes.py +13 -2
- nuitka/code_generation/ComparisonHelperDefinitions.py +23 -8
- nuitka/code_generation/Contexts.py +28 -16
- nuitka/code_generation/CoroutineCodes.py +2 -6
- nuitka/code_generation/DictCodes.py +3 -3
- nuitka/code_generation/ErrorCodes.py +34 -50
- nuitka/code_generation/ExceptionCodes.py +165 -96
- nuitka/code_generation/ExpressionCTypeSelectionHelpers.py +25 -10
- nuitka/code_generation/FrameCodes.py +4 -11
- nuitka/code_generation/FunctionCodes.py +2 -6
- nuitka/code_generation/GeneratorCodes.py +2 -6
- nuitka/code_generation/GlobalConstants.py +8 -2
- nuitka/code_generation/GlobalsLocalsCodes.py +2 -2
- nuitka/code_generation/IteratorCodes.py +67 -54
- nuitka/code_generation/LineNumberCodes.py +1 -3
- nuitka/code_generation/ModuleCodes.py +28 -4
- nuitka/code_generation/OperationCodes.py +51 -20
- nuitka/code_generation/PackageResourceCodes.py +37 -0
- nuitka/code_generation/RaisingCodes.py +164 -90
- nuitka/code_generation/Reports.py +13 -5
- nuitka/code_generation/ReturnCodes.py +2 -16
- nuitka/code_generation/TryCodes.py +12 -24
- nuitka/code_generation/TypeAliasCodes.py +48 -0
- nuitka/code_generation/VariableCodes.py +99 -40
- nuitka/code_generation/VariableDeclarations.py +8 -8
- nuitka/code_generation/YieldCodes.py +1 -1
- nuitka/code_generation/c_types/CTypeBases.py +10 -2
- nuitka/code_generation/c_types/CTypeModuleDictVariables.py +3 -0
- nuitka/code_generation/c_types/CTypeNuitkaBooleans.py +3 -4
- nuitka/code_generation/c_types/CTypeNuitkaInts.py +70 -25
- nuitka/code_generation/c_types/CTypeNuitkaVoids.py +1 -1
- nuitka/code_generation/c_types/CTypePyObjectPointers.py +16 -6
- nuitka/code_generation/templates/CodeTemplatesAsyncgens.py +2 -2
- nuitka/code_generation/templates/CodeTemplatesConstants.py +11 -3
- nuitka/code_generation/templates/CodeTemplatesCoroutines.py +2 -2
- nuitka/code_generation/templates/CodeTemplatesExceptions.py +15 -16
- nuitka/code_generation/templates/CodeTemplatesFrames.py +18 -10
- nuitka/code_generation/templates/CodeTemplatesFunction.py +2 -2
- nuitka/code_generation/templates/CodeTemplatesGeneratorFunction.py +2 -2
- nuitka/code_generation/templates/CodeTemplatesIterators.py +1 -35
- nuitka/code_generation/templates/CodeTemplatesModules.py +34 -4
- nuitka/code_generation/templates/CodeTemplatesVariables.py +76 -0
- nuitka/code_generation/templates_c/CodeTemplateCallsMixed.c.j2 +3 -3
- nuitka/code_generation/templates_c/CodeTemplateCallsPositional.c.j2 +3 -3
- nuitka/code_generation/templates_c/HelperDictionaryCopy.c.j2 +4 -106
- nuitka/code_generation/templates_c/HelperImportHard.c.j2 +1 -2
- nuitka/code_generation/templates_c/HelperLongTools.c.j2 +1 -1
- nuitka/code_generation/templates_c/HelperOperationBinaryDual.c.j2 +115 -0
- nuitka/code_generation/templates_c/HelperOperationComparisonBytes.c.j2 +9 -9
- nuitka/code_generation/templates_c/HelperOperationComparisonDual.c.j2 +86 -0
- nuitka/code_generation/templates_c/HelperOperationComparisonFloat.c.j2 +1 -1
- nuitka/code_generation/templates_c/HelperOperationComparisonInt.c.j2 +1 -1
- nuitka/code_generation/templates_c/HelperOperationComparisonList.c.j2 +9 -9
- nuitka/code_generation/templates_c/HelperOperationComparisonLong.c.j2 +7 -7
- nuitka/code_generation/templates_c/HelperOperationComparisonStr.c.j2 +9 -9
- nuitka/code_generation/templates_c/HelperOperationComparisonTuple.c.j2 +7 -7
- nuitka/code_generation/templates_c/HelperOperationComparisonUnicode.c.j2 +4 -4
- nuitka/code_generation/templates_c/HelperSlotsBinary.c.j2 +1 -1
- nuitka/code_generation/templates_c/HelperSlotsInt.c.j2 +31 -31
- nuitka/code_generation/templates_c/HelperSlotsLong.c.j2 +1 -1
- nuitka/containers/Namedtuples.py +6 -0
- nuitka/containers/OrderedSets.py +105 -5
- nuitka/distutils/Build.py +14 -0
- nuitka/distutils/DistutilCommands.py +50 -10
- nuitka/finalizations/Finalization.py +3 -3
- nuitka/finalizations/FinalizeMarkups.py +10 -14
- nuitka/freezer/DependsExe.py +5 -2
- nuitka/freezer/DllDependenciesMacOS.py +18 -1
- nuitka/freezer/DllDependenciesWin32.py +26 -12
- nuitka/freezer/ImportDetection.py +6 -0
- nuitka/freezer/IncludedDataFiles.py +37 -6
- nuitka/freezer/Onefile.py +1 -0
- nuitka/freezer/Standalone.py +72 -18
- nuitka/importing/Importing.py +33 -6
- nuitka/importing/Recursion.py +5 -2
- nuitka/importing/StandardLibrary.py +57 -59
- nuitka/nodes/AsyncgenNodes.py +3 -6
- nuitka/nodes/BuiltinIteratorNodes.py +2 -2
- nuitka/nodes/BuiltinRefNodes.py +14 -30
- nuitka/nodes/ChildrenHavingMixins.py +439 -46
- nuitka/nodes/ConditionalNodes.py +3 -2
- nuitka/nodes/CoroutineNodes.py +3 -6
- nuitka/nodes/DictionaryNodes.py +1 -1
- nuitka/nodes/ExceptionNodes.py +102 -26
- nuitka/nodes/ExpressionBasesGenerated.py +288 -4
- nuitka/nodes/FrameNodes.py +22 -11
- nuitka/nodes/FunctionNodes.py +3 -3
- nuitka/nodes/GeneratorNodes.py +3 -6
- nuitka/nodes/HardImportNodesGenerated.py +182 -0
- nuitka/nodes/ImportNodes.py +2 -2
- nuitka/nodes/NodeBases.py +28 -1
- nuitka/nodes/NodeMakingHelpers.py +71 -50
- nuitka/nodes/OperatorNodesUnary.py +128 -3
- nuitka/nodes/OsSysNodes.py +37 -6
- nuitka/nodes/OutlineNodes.py +2 -2
- nuitka/nodes/StatementBasesGenerated.py +0 -2
- nuitka/nodes/StatementNodes.py +8 -7
- nuitka/nodes/SubscriptNodes.py +1 -1
- nuitka/nodes/TypeNodes.py +28 -1
- nuitka/nodes/VariableAssignNodes.py +2 -1
- nuitka/nodes/shapes/BuiltinTypeShapes.py +24 -7
- nuitka/nodes/shapes/IteratorShapes.py +71 -0
- nuitka/nodes/shapes/ShapeMixins.py +18 -0
- nuitka/nodes/shapes/StandardShapes.py +47 -44
- nuitka/optimizations/BytecodeDemotion.py +2 -1
- nuitka/plugins/PluginBase.py +64 -22
- nuitka/plugins/Plugins.py +16 -3
- nuitka/plugins/standard/AntiBloatPlugin.py +23 -12
- nuitka/plugins/standard/DllFilesPlugin.py +78 -2
- nuitka/plugins/standard/ImplicitImports.py +5 -15
- nuitka/plugins/standard/KivyPlugin.py +3 -12
- nuitka/plugins/standard/MatplotlibPlugin.py +12 -1
- nuitka/plugins/standard/MultiprocessingPlugin.py +11 -20
- nuitka/plugins/standard/OptionsNannyPlugin.py +5 -4
- nuitka/plugins/standard/PlaywrightPlugin.py +184 -0
- nuitka/plugins/standard/PySidePyQtPlugin.py +8 -0
- nuitka/plugins/standard/TkinterPlugin.py +23 -3
- nuitka/plugins/standard/TransformersPlugin.py +70 -275
- nuitka/plugins/standard/standard.nuitka-package.config.yml +413 -30
- nuitka/plugins/standard/stdlib3.nuitka-package.config.yml +35 -2
- nuitka/reports/CompilationReportReader.py +12 -0
- nuitka/reports/Reports.py +11 -0
- nuitka/specs/BuiltinParameterSpecs.py +5 -1
- nuitka/specs/HardImportSpecs.py +7 -0
- nuitka/specs/ParameterSpecs.py +4 -4
- nuitka/tools/environments/Virtualenv.py +13 -5
- nuitka/tools/general/dll_report/__main__.py +10 -1
- nuitka/tools/onefile_compressor/OnefileCompressor.py +1 -1
- nuitka/tools/podman/__main__.py +4 -2
- nuitka/tools/specialize/CTypeDescriptions.py +158 -9
- nuitka/tools/specialize/SpecializeC.py +218 -56
- nuitka/tools/testing/Common.py +69 -62
- nuitka/tools/testing/compare_with_cpython/__main__.py +3 -1
- nuitka/tools/testing/measure_construct_performance/__main__.py +1 -8
- nuitka/tools/testing/run_nuitka_tests/__main__.py +77 -11
- nuitka/tools/watch/AutoStage.py +1 -0
- nuitka/tree/Building.py +10 -37
- nuitka/tree/ComplexCallHelperFunctions.py +4 -0
- nuitka/tree/ReformulationAssertStatements.py +31 -14
- nuitka/tree/ReformulationAssignmentStatements.py +5 -1
- nuitka/tree/ReformulationClasses3.py +94 -19
- nuitka/tree/ReformulationDictionaryCreation.py +1 -0
- nuitka/tree/ReformulationFunctionStatements.py +1 -1
- nuitka/tree/ReformulationMatchStatements.py +3 -1
- nuitka/tree/ReformulationYieldExpressions.py +23 -5
- nuitka/utils/CommandLineOptions.py +27 -3
- nuitka/utils/Download.py +2 -2
- nuitka/utils/Execution.py +36 -31
- nuitka/utils/FileOperations.py +18 -39
- nuitka/utils/Hashing.py +5 -1
- nuitka/utils/Importing.py +2 -14
- nuitka/utils/InlineCopies.py +52 -0
- nuitka/utils/InstalledPythons.py +9 -4
- nuitka/utils/Json.py +2 -2
- nuitka/utils/PackageResources.py +44 -0
- nuitka/utils/SharedLibraries.py +24 -0
- nuitka/utils/Signing.py +9 -1
- nuitka/utils/Utils.py +1 -0
- {Nuitka_winsvc-2.4.11.data → Nuitka_winsvc-2.5.6.data}/scripts/nuitka-run.cmd +0 -0
- {Nuitka_winsvc-2.4.11.data → Nuitka_winsvc-2.5.6.data}/scripts/nuitka.cmd +0 -0
- {Nuitka_winsvc-2.4.11.dist-info → Nuitka_winsvc-2.5.6.dist-info}/LICENSE.txt +0 -0
- {Nuitka_winsvc-2.4.11.dist-info → Nuitka_winsvc-2.5.6.dist-info}/entry_points.txt +0 -0
- {Nuitka_winsvc-2.4.11.dist-info → Nuitka_winsvc-2.5.6.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,938 @@
|
|
|
1
|
+
"Usage: unparse.py <path to source file>"
|
|
2
|
+
from __future__ import print_function, unicode_literals
|
|
3
|
+
import six
|
|
4
|
+
import sys
|
|
5
|
+
import ast
|
|
6
|
+
import os
|
|
7
|
+
import tokenize
|
|
8
|
+
from six import StringIO
|
|
9
|
+
from six.moves import cStringIO
|
|
10
|
+
|
|
11
|
+
# Large float and imaginary literals get turned into infinities in the AST.
|
|
12
|
+
# We unparse those infinities to INFSTR.
|
|
13
|
+
INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def interleave(inter, f, seq):
|
|
17
|
+
"""Call f on each item in seq, calling inter() in between."""
|
|
18
|
+
seq = iter(seq)
|
|
19
|
+
try:
|
|
20
|
+
f(next(seq))
|
|
21
|
+
except StopIteration:
|
|
22
|
+
pass
|
|
23
|
+
else:
|
|
24
|
+
for x in seq:
|
|
25
|
+
inter()
|
|
26
|
+
f(x)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Unparser:
|
|
30
|
+
"""Methods in this class recursively traverse an AST and
|
|
31
|
+
output source code for the abstract syntax; original formatting
|
|
32
|
+
is disregarded."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, tree, file=sys.stdout):
|
|
35
|
+
"""Unparser(tree, file=sys.stdout) -> None.
|
|
36
|
+
Print the source for tree to file."""
|
|
37
|
+
self.f = file
|
|
38
|
+
self.future_imports = []
|
|
39
|
+
self._indent = 0
|
|
40
|
+
self.dispatch(tree)
|
|
41
|
+
print("", file=self.f)
|
|
42
|
+
self.f.flush()
|
|
43
|
+
|
|
44
|
+
def fill(self, text=""):
|
|
45
|
+
"Indent a piece of text, according to the current indentation level"
|
|
46
|
+
self.f.write("\n" + " " * self._indent + text)
|
|
47
|
+
|
|
48
|
+
def write(self, text):
|
|
49
|
+
"Append a piece of text to the current line."
|
|
50
|
+
self.f.write(six.text_type(text))
|
|
51
|
+
|
|
52
|
+
def enter(self):
|
|
53
|
+
"Print ':', and increase the indentation."
|
|
54
|
+
self.write(":")
|
|
55
|
+
self._indent += 1
|
|
56
|
+
|
|
57
|
+
def leave(self):
|
|
58
|
+
"Decrease the indentation level."
|
|
59
|
+
self._indent -= 1
|
|
60
|
+
|
|
61
|
+
def dispatch(self, tree):
|
|
62
|
+
"Dispatcher function, dispatching tree type T to method _T."
|
|
63
|
+
if isinstance(tree, list):
|
|
64
|
+
for t in tree:
|
|
65
|
+
self.dispatch(t)
|
|
66
|
+
return
|
|
67
|
+
meth = getattr(self, "_" + tree.__class__.__name__)
|
|
68
|
+
meth(tree)
|
|
69
|
+
|
|
70
|
+
############### Unparsing methods ######################
|
|
71
|
+
# There should be one method per concrete grammar type #
|
|
72
|
+
# Constructors should be grouped by sum type. Ideally, #
|
|
73
|
+
# this would follow the order in the grammar, but #
|
|
74
|
+
# currently doesn't. #
|
|
75
|
+
########################################################
|
|
76
|
+
|
|
77
|
+
def _Module(self, tree):
|
|
78
|
+
for stmt in tree.body:
|
|
79
|
+
self.dispatch(stmt)
|
|
80
|
+
|
|
81
|
+
def _Interactive(self, tree):
|
|
82
|
+
for stmt in tree.body:
|
|
83
|
+
self.dispatch(stmt)
|
|
84
|
+
|
|
85
|
+
def _Expression(self, tree):
|
|
86
|
+
self.dispatch(tree.body)
|
|
87
|
+
|
|
88
|
+
# stmt
|
|
89
|
+
def _Expr(self, tree):
|
|
90
|
+
self.fill()
|
|
91
|
+
self.dispatch(tree.value)
|
|
92
|
+
|
|
93
|
+
def _NamedExpr(self, tree):
|
|
94
|
+
self.write("(")
|
|
95
|
+
self.dispatch(tree.target)
|
|
96
|
+
self.write(" := ")
|
|
97
|
+
self.dispatch(tree.value)
|
|
98
|
+
self.write(")")
|
|
99
|
+
|
|
100
|
+
def _Import(self, t):
|
|
101
|
+
self.fill("import ")
|
|
102
|
+
interleave(lambda: self.write(", "), self.dispatch, t.names)
|
|
103
|
+
|
|
104
|
+
def _ImportFrom(self, t):
|
|
105
|
+
# A from __future__ import may affect unparsing, so record it.
|
|
106
|
+
if t.module and t.module == "__future__":
|
|
107
|
+
self.future_imports.extend(n.name for n in t.names)
|
|
108
|
+
|
|
109
|
+
self.fill("from ")
|
|
110
|
+
self.write("." * t.level)
|
|
111
|
+
if t.module:
|
|
112
|
+
self.write(t.module)
|
|
113
|
+
self.write(" import ")
|
|
114
|
+
interleave(lambda: self.write(", "), self.dispatch, t.names)
|
|
115
|
+
|
|
116
|
+
def _Assign(self, t):
|
|
117
|
+
self.fill()
|
|
118
|
+
for target in t.targets:
|
|
119
|
+
self.dispatch(target)
|
|
120
|
+
self.write(" = ")
|
|
121
|
+
self.dispatch(t.value)
|
|
122
|
+
|
|
123
|
+
def _AugAssign(self, t):
|
|
124
|
+
self.fill()
|
|
125
|
+
self.dispatch(t.target)
|
|
126
|
+
self.write(" " + self.binop[t.op.__class__.__name__] + "= ")
|
|
127
|
+
self.dispatch(t.value)
|
|
128
|
+
|
|
129
|
+
def _AnnAssign(self, t):
|
|
130
|
+
self.fill()
|
|
131
|
+
if not t.simple and isinstance(t.target, ast.Name):
|
|
132
|
+
self.write("(")
|
|
133
|
+
self.dispatch(t.target)
|
|
134
|
+
if not t.simple and isinstance(t.target, ast.Name):
|
|
135
|
+
self.write(")")
|
|
136
|
+
self.write(": ")
|
|
137
|
+
self.dispatch(t.annotation)
|
|
138
|
+
if t.value:
|
|
139
|
+
self.write(" = ")
|
|
140
|
+
self.dispatch(t.value)
|
|
141
|
+
|
|
142
|
+
def _Return(self, t):
|
|
143
|
+
self.fill("return")
|
|
144
|
+
if t.value:
|
|
145
|
+
self.write(" ")
|
|
146
|
+
self.dispatch(t.value)
|
|
147
|
+
|
|
148
|
+
def _Pass(self, t):
|
|
149
|
+
self.fill("pass")
|
|
150
|
+
|
|
151
|
+
def _Break(self, t):
|
|
152
|
+
self.fill("break")
|
|
153
|
+
|
|
154
|
+
def _Continue(self, t):
|
|
155
|
+
self.fill("continue")
|
|
156
|
+
|
|
157
|
+
def _Delete(self, t):
|
|
158
|
+
self.fill("del ")
|
|
159
|
+
interleave(lambda: self.write(", "), self.dispatch, t.targets)
|
|
160
|
+
|
|
161
|
+
def _Assert(self, t):
|
|
162
|
+
self.fill("assert ")
|
|
163
|
+
self.dispatch(t.test)
|
|
164
|
+
if t.msg:
|
|
165
|
+
self.write(", ")
|
|
166
|
+
self.dispatch(t.msg)
|
|
167
|
+
|
|
168
|
+
def _Exec(self, t):
|
|
169
|
+
self.fill("exec ")
|
|
170
|
+
self.dispatch(t.body)
|
|
171
|
+
if t.globals:
|
|
172
|
+
self.write(" in ")
|
|
173
|
+
self.dispatch(t.globals)
|
|
174
|
+
if t.locals:
|
|
175
|
+
self.write(", ")
|
|
176
|
+
self.dispatch(t.locals)
|
|
177
|
+
|
|
178
|
+
def _Print(self, t):
|
|
179
|
+
self.fill("print ")
|
|
180
|
+
do_comma = False
|
|
181
|
+
if t.dest:
|
|
182
|
+
self.write(">>")
|
|
183
|
+
self.dispatch(t.dest)
|
|
184
|
+
do_comma = True
|
|
185
|
+
for e in t.values:
|
|
186
|
+
if do_comma:
|
|
187
|
+
self.write(", ")
|
|
188
|
+
else:
|
|
189
|
+
do_comma = True
|
|
190
|
+
self.dispatch(e)
|
|
191
|
+
if not t.nl:
|
|
192
|
+
self.write(",")
|
|
193
|
+
|
|
194
|
+
def _Global(self, t):
|
|
195
|
+
self.fill("global ")
|
|
196
|
+
interleave(lambda: self.write(", "), self.write, t.names)
|
|
197
|
+
|
|
198
|
+
def _Nonlocal(self, t):
|
|
199
|
+
self.fill("nonlocal ")
|
|
200
|
+
interleave(lambda: self.write(", "), self.write, t.names)
|
|
201
|
+
|
|
202
|
+
def _Await(self, t):
|
|
203
|
+
self.write("(")
|
|
204
|
+
self.write("await")
|
|
205
|
+
if t.value:
|
|
206
|
+
self.write(" ")
|
|
207
|
+
self.dispatch(t.value)
|
|
208
|
+
self.write(")")
|
|
209
|
+
|
|
210
|
+
def _Yield(self, t):
|
|
211
|
+
self.write("(")
|
|
212
|
+
self.write("yield")
|
|
213
|
+
if t.value:
|
|
214
|
+
self.write(" ")
|
|
215
|
+
self.dispatch(t.value)
|
|
216
|
+
self.write(")")
|
|
217
|
+
|
|
218
|
+
def _YieldFrom(self, t):
|
|
219
|
+
self.write("(")
|
|
220
|
+
self.write("yield from")
|
|
221
|
+
if t.value:
|
|
222
|
+
self.write(" ")
|
|
223
|
+
self.dispatch(t.value)
|
|
224
|
+
self.write(")")
|
|
225
|
+
|
|
226
|
+
def _Raise(self, t):
|
|
227
|
+
self.fill("raise")
|
|
228
|
+
if six.PY3:
|
|
229
|
+
if not t.exc:
|
|
230
|
+
assert not t.cause
|
|
231
|
+
return
|
|
232
|
+
self.write(" ")
|
|
233
|
+
self.dispatch(t.exc)
|
|
234
|
+
if t.cause:
|
|
235
|
+
self.write(" from ")
|
|
236
|
+
self.dispatch(t.cause)
|
|
237
|
+
else:
|
|
238
|
+
self.write(" ")
|
|
239
|
+
if t.type:
|
|
240
|
+
self.dispatch(t.type)
|
|
241
|
+
if t.inst:
|
|
242
|
+
self.write(", ")
|
|
243
|
+
self.dispatch(t.inst)
|
|
244
|
+
if t.tback:
|
|
245
|
+
self.write(", ")
|
|
246
|
+
self.dispatch(t.tback)
|
|
247
|
+
|
|
248
|
+
def _Try(self, t):
|
|
249
|
+
self.fill("try")
|
|
250
|
+
self.enter()
|
|
251
|
+
self.dispatch(t.body)
|
|
252
|
+
self.leave()
|
|
253
|
+
for ex in t.handlers:
|
|
254
|
+
self.dispatch(ex)
|
|
255
|
+
if t.orelse:
|
|
256
|
+
self.fill("else")
|
|
257
|
+
self.enter()
|
|
258
|
+
self.dispatch(t.orelse)
|
|
259
|
+
self.leave()
|
|
260
|
+
if t.finalbody:
|
|
261
|
+
self.fill("finally")
|
|
262
|
+
self.enter()
|
|
263
|
+
self.dispatch(t.finalbody)
|
|
264
|
+
self.leave()
|
|
265
|
+
|
|
266
|
+
def _TryExcept(self, t):
|
|
267
|
+
self.fill("try")
|
|
268
|
+
self.enter()
|
|
269
|
+
self.dispatch(t.body)
|
|
270
|
+
self.leave()
|
|
271
|
+
|
|
272
|
+
for ex in t.handlers:
|
|
273
|
+
self.dispatch(ex)
|
|
274
|
+
if t.orelse:
|
|
275
|
+
self.fill("else")
|
|
276
|
+
self.enter()
|
|
277
|
+
self.dispatch(t.orelse)
|
|
278
|
+
self.leave()
|
|
279
|
+
|
|
280
|
+
def _TryFinally(self, t):
|
|
281
|
+
if len(t.body) == 1 and isinstance(t.body[0], ast.TryExcept):
|
|
282
|
+
# try-except-finally
|
|
283
|
+
self.dispatch(t.body)
|
|
284
|
+
else:
|
|
285
|
+
self.fill("try")
|
|
286
|
+
self.enter()
|
|
287
|
+
self.dispatch(t.body)
|
|
288
|
+
self.leave()
|
|
289
|
+
|
|
290
|
+
self.fill("finally")
|
|
291
|
+
self.enter()
|
|
292
|
+
self.dispatch(t.finalbody)
|
|
293
|
+
self.leave()
|
|
294
|
+
|
|
295
|
+
def _ExceptHandler(self, t):
|
|
296
|
+
self.fill("except")
|
|
297
|
+
if t.type:
|
|
298
|
+
self.write(" ")
|
|
299
|
+
self.dispatch(t.type)
|
|
300
|
+
if t.name:
|
|
301
|
+
self.write(" as ")
|
|
302
|
+
if six.PY3:
|
|
303
|
+
self.write(t.name)
|
|
304
|
+
else:
|
|
305
|
+
self.dispatch(t.name)
|
|
306
|
+
self.enter()
|
|
307
|
+
self.dispatch(t.body)
|
|
308
|
+
self.leave()
|
|
309
|
+
|
|
310
|
+
def _ClassDef(self, t):
|
|
311
|
+
self.write("\n")
|
|
312
|
+
for deco in t.decorator_list:
|
|
313
|
+
self.fill("@")
|
|
314
|
+
self.dispatch(deco)
|
|
315
|
+
self.fill("class " + t.name)
|
|
316
|
+
if six.PY3:
|
|
317
|
+
self.write("(")
|
|
318
|
+
comma = False
|
|
319
|
+
for e in t.bases:
|
|
320
|
+
if comma:
|
|
321
|
+
self.write(", ")
|
|
322
|
+
else:
|
|
323
|
+
comma = True
|
|
324
|
+
self.dispatch(e)
|
|
325
|
+
for e in t.keywords:
|
|
326
|
+
if comma:
|
|
327
|
+
self.write(", ")
|
|
328
|
+
else:
|
|
329
|
+
comma = True
|
|
330
|
+
self.dispatch(e)
|
|
331
|
+
if sys.version_info[:2] < (3, 5):
|
|
332
|
+
if t.starargs:
|
|
333
|
+
if comma:
|
|
334
|
+
self.write(", ")
|
|
335
|
+
else:
|
|
336
|
+
comma = True
|
|
337
|
+
self.write("*")
|
|
338
|
+
self.dispatch(t.starargs)
|
|
339
|
+
if t.kwargs:
|
|
340
|
+
if comma:
|
|
341
|
+
self.write(", ")
|
|
342
|
+
else:
|
|
343
|
+
comma = True
|
|
344
|
+
self.write("**")
|
|
345
|
+
self.dispatch(t.kwargs)
|
|
346
|
+
self.write(")")
|
|
347
|
+
elif t.bases:
|
|
348
|
+
self.write("(")
|
|
349
|
+
for a in t.bases:
|
|
350
|
+
self.dispatch(a)
|
|
351
|
+
self.write(", ")
|
|
352
|
+
self.write(")")
|
|
353
|
+
self.enter()
|
|
354
|
+
self.dispatch(t.body)
|
|
355
|
+
self.leave()
|
|
356
|
+
|
|
357
|
+
def _FunctionDef(self, t):
|
|
358
|
+
self.__FunctionDef_helper(t, "def")
|
|
359
|
+
|
|
360
|
+
def _AsyncFunctionDef(self, t):
|
|
361
|
+
self.__FunctionDef_helper(t, "async def")
|
|
362
|
+
|
|
363
|
+
def __FunctionDef_helper(self, t, fill_suffix):
|
|
364
|
+
self.write("\n")
|
|
365
|
+
for deco in t.decorator_list:
|
|
366
|
+
self.fill("@")
|
|
367
|
+
self.dispatch(deco)
|
|
368
|
+
def_str = fill_suffix + " " + t.name + "("
|
|
369
|
+
self.fill(def_str)
|
|
370
|
+
self.dispatch(t.args)
|
|
371
|
+
self.write(")")
|
|
372
|
+
if getattr(t, "returns", False):
|
|
373
|
+
self.write(" -> ")
|
|
374
|
+
self.dispatch(t.returns)
|
|
375
|
+
self.enter()
|
|
376
|
+
self.dispatch(t.body)
|
|
377
|
+
self.leave()
|
|
378
|
+
|
|
379
|
+
def _For(self, t):
|
|
380
|
+
self.__For_helper("for ", t)
|
|
381
|
+
|
|
382
|
+
def _AsyncFor(self, t):
|
|
383
|
+
self.__For_helper("async for ", t)
|
|
384
|
+
|
|
385
|
+
def __For_helper(self, fill, t):
|
|
386
|
+
self.fill(fill)
|
|
387
|
+
self.dispatch(t.target)
|
|
388
|
+
self.write(" in ")
|
|
389
|
+
self.dispatch(t.iter)
|
|
390
|
+
self.enter()
|
|
391
|
+
self.dispatch(t.body)
|
|
392
|
+
self.leave()
|
|
393
|
+
if t.orelse:
|
|
394
|
+
self.fill("else")
|
|
395
|
+
self.enter()
|
|
396
|
+
self.dispatch(t.orelse)
|
|
397
|
+
self.leave()
|
|
398
|
+
|
|
399
|
+
def _If(self, t):
|
|
400
|
+
self.fill("if ")
|
|
401
|
+
self.dispatch(t.test)
|
|
402
|
+
self.enter()
|
|
403
|
+
self.dispatch(t.body)
|
|
404
|
+
self.leave()
|
|
405
|
+
# collapse nested ifs into equivalent elifs.
|
|
406
|
+
while t.orelse and len(t.orelse) == 1 and isinstance(t.orelse[0], ast.If):
|
|
407
|
+
t = t.orelse[0]
|
|
408
|
+
self.fill("elif ")
|
|
409
|
+
self.dispatch(t.test)
|
|
410
|
+
self.enter()
|
|
411
|
+
self.dispatch(t.body)
|
|
412
|
+
self.leave()
|
|
413
|
+
# final else
|
|
414
|
+
if t.orelse:
|
|
415
|
+
self.fill("else")
|
|
416
|
+
self.enter()
|
|
417
|
+
self.dispatch(t.orelse)
|
|
418
|
+
self.leave()
|
|
419
|
+
|
|
420
|
+
def _While(self, t):
|
|
421
|
+
self.fill("while ")
|
|
422
|
+
self.dispatch(t.test)
|
|
423
|
+
self.enter()
|
|
424
|
+
self.dispatch(t.body)
|
|
425
|
+
self.leave()
|
|
426
|
+
if t.orelse:
|
|
427
|
+
self.fill("else")
|
|
428
|
+
self.enter()
|
|
429
|
+
self.dispatch(t.orelse)
|
|
430
|
+
self.leave()
|
|
431
|
+
|
|
432
|
+
def _generic_With(self, t, async_=False):
|
|
433
|
+
self.fill("async with " if async_ else "with ")
|
|
434
|
+
if hasattr(t, "items"):
|
|
435
|
+
interleave(lambda: self.write(", "), self.dispatch, t.items)
|
|
436
|
+
else:
|
|
437
|
+
self.dispatch(t.context_expr)
|
|
438
|
+
if t.optional_vars:
|
|
439
|
+
self.write(" as ")
|
|
440
|
+
self.dispatch(t.optional_vars)
|
|
441
|
+
self.enter()
|
|
442
|
+
self.dispatch(t.body)
|
|
443
|
+
self.leave()
|
|
444
|
+
|
|
445
|
+
def _With(self, t):
|
|
446
|
+
self._generic_With(t)
|
|
447
|
+
|
|
448
|
+
def _AsyncWith(self, t):
|
|
449
|
+
self._generic_With(t, async_=True)
|
|
450
|
+
|
|
451
|
+
# expr
|
|
452
|
+
def _Bytes(self, t):
|
|
453
|
+
self.write(repr(t.s))
|
|
454
|
+
|
|
455
|
+
def _Str(self, tree):
|
|
456
|
+
if six.PY3:
|
|
457
|
+
self.write(repr(tree.s))
|
|
458
|
+
else:
|
|
459
|
+
# if from __future__ import unicode_literals is in effect,
|
|
460
|
+
# then we want to output string literals using a 'b' prefix
|
|
461
|
+
# and unicode literals with no prefix.
|
|
462
|
+
if "unicode_literals" not in self.future_imports:
|
|
463
|
+
self.write(repr(tree.s))
|
|
464
|
+
elif isinstance(tree.s, str):
|
|
465
|
+
self.write("b" + repr(tree.s))
|
|
466
|
+
elif isinstance(tree.s, unicode):
|
|
467
|
+
self.write(repr(tree.s).lstrip("u"))
|
|
468
|
+
else:
|
|
469
|
+
assert False, "shouldn't get here"
|
|
470
|
+
|
|
471
|
+
def _JoinedStr(self, t):
|
|
472
|
+
# JoinedStr(expr* values)
|
|
473
|
+
self.write("f")
|
|
474
|
+
string = StringIO()
|
|
475
|
+
self._fstring_JoinedStr(t, string.write)
|
|
476
|
+
# Deviation from `unparse.py`: Try to find an unused quote.
|
|
477
|
+
# This change is made to handle _very_ complex f-strings.
|
|
478
|
+
v = string.getvalue()
|
|
479
|
+
if "\n" in v or "\r" in v:
|
|
480
|
+
quote_types = ["'''", '"""']
|
|
481
|
+
else:
|
|
482
|
+
quote_types = ["'", '"', '"""', "'''"]
|
|
483
|
+
for quote_type in quote_types:
|
|
484
|
+
if quote_type not in v:
|
|
485
|
+
v = "{quote_type}{v}{quote_type}".format(quote_type=quote_type, v=v)
|
|
486
|
+
break
|
|
487
|
+
else:
|
|
488
|
+
v = repr(v)
|
|
489
|
+
self.write(v)
|
|
490
|
+
|
|
491
|
+
def _FormattedValue(self, t):
|
|
492
|
+
# FormattedValue(expr value, int? conversion, expr? format_spec)
|
|
493
|
+
self.write("f")
|
|
494
|
+
string = StringIO()
|
|
495
|
+
self._fstring_JoinedStr(t, string.write)
|
|
496
|
+
self.write(repr(string.getvalue()))
|
|
497
|
+
|
|
498
|
+
def _fstring_JoinedStr(self, t, write):
|
|
499
|
+
for value in t.values:
|
|
500
|
+
meth = getattr(self, "_fstring_" + type(value).__name__)
|
|
501
|
+
meth(value, write)
|
|
502
|
+
|
|
503
|
+
def _fstring_Str(self, t, write):
|
|
504
|
+
value = t.s.replace("{", "{{").replace("}", "}}")
|
|
505
|
+
write(value)
|
|
506
|
+
|
|
507
|
+
def _fstring_Constant(self, t, write):
|
|
508
|
+
assert isinstance(t.value, str)
|
|
509
|
+
value = t.value.replace("{", "{{").replace("}", "}}")
|
|
510
|
+
write(value)
|
|
511
|
+
|
|
512
|
+
def _fstring_FormattedValue(self, t, write):
|
|
513
|
+
write("{")
|
|
514
|
+
expr = StringIO()
|
|
515
|
+
Unparser(t.value, expr)
|
|
516
|
+
expr = expr.getvalue().rstrip("\n")
|
|
517
|
+
if expr.startswith("{"):
|
|
518
|
+
write(" ") # Separate pair of opening brackets as "{ {"
|
|
519
|
+
write(expr)
|
|
520
|
+
if t.conversion != -1:
|
|
521
|
+
conversion = chr(t.conversion)
|
|
522
|
+
assert conversion in "sra"
|
|
523
|
+
write("!{conversion}".format(conversion=conversion))
|
|
524
|
+
if t.format_spec:
|
|
525
|
+
write(":")
|
|
526
|
+
meth = getattr(self, "_fstring_" + type(t.format_spec).__name__)
|
|
527
|
+
meth(t.format_spec, write)
|
|
528
|
+
write("}")
|
|
529
|
+
|
|
530
|
+
def _Name(self, t):
|
|
531
|
+
self.write(t.id)
|
|
532
|
+
|
|
533
|
+
def _NameConstant(self, t):
|
|
534
|
+
self.write(repr(t.value))
|
|
535
|
+
|
|
536
|
+
def _Repr(self, t):
|
|
537
|
+
self.write("`")
|
|
538
|
+
self.dispatch(t.value)
|
|
539
|
+
self.write("`")
|
|
540
|
+
|
|
541
|
+
def _write_constant(self, value):
|
|
542
|
+
if isinstance(value, (float, complex)):
|
|
543
|
+
# Substitute overflowing decimal literal for AST infinities.
|
|
544
|
+
self.write(repr(value).replace("inf", INFSTR))
|
|
545
|
+
else:
|
|
546
|
+
self.write(repr(value))
|
|
547
|
+
|
|
548
|
+
def _Constant(self, t):
|
|
549
|
+
value = t.value
|
|
550
|
+
if isinstance(value, tuple):
|
|
551
|
+
self.write("(")
|
|
552
|
+
if len(value) == 1:
|
|
553
|
+
self._write_constant(value[0])
|
|
554
|
+
self.write(",")
|
|
555
|
+
else:
|
|
556
|
+
interleave(lambda: self.write(", "), self._write_constant, value)
|
|
557
|
+
self.write(")")
|
|
558
|
+
elif value is Ellipsis: # instead of `...` for Py2 compatibility
|
|
559
|
+
self.write("...")
|
|
560
|
+
else:
|
|
561
|
+
if t.kind == "u":
|
|
562
|
+
self.write("u")
|
|
563
|
+
self._write_constant(t.value)
|
|
564
|
+
|
|
565
|
+
def _Num(self, t):
|
|
566
|
+
repr_n = repr(t.n)
|
|
567
|
+
if six.PY3:
|
|
568
|
+
self.write(repr_n.replace("inf", INFSTR))
|
|
569
|
+
else:
|
|
570
|
+
# Parenthesize negative numbers, to avoid turning (-1)**2 into -1**2.
|
|
571
|
+
if repr_n.startswith("-"):
|
|
572
|
+
self.write("(")
|
|
573
|
+
if "inf" in repr_n and repr_n.endswith("*j"):
|
|
574
|
+
repr_n = repr_n.replace("*j", "j")
|
|
575
|
+
# Substitute overflowing decimal literal for AST infinities.
|
|
576
|
+
self.write(repr_n.replace("inf", INFSTR))
|
|
577
|
+
if repr_n.startswith("-"):
|
|
578
|
+
self.write(")")
|
|
579
|
+
|
|
580
|
+
def _List(self, t):
|
|
581
|
+
self.write("[")
|
|
582
|
+
interleave(lambda: self.write(", "), self.dispatch, t.elts)
|
|
583
|
+
self.write("]")
|
|
584
|
+
|
|
585
|
+
def _ListComp(self, t):
|
|
586
|
+
self.write("[")
|
|
587
|
+
self.dispatch(t.elt)
|
|
588
|
+
for gen in t.generators:
|
|
589
|
+
self.dispatch(gen)
|
|
590
|
+
self.write("]")
|
|
591
|
+
|
|
592
|
+
def _GeneratorExp(self, t):
|
|
593
|
+
self.write("(")
|
|
594
|
+
self.dispatch(t.elt)
|
|
595
|
+
for gen in t.generators:
|
|
596
|
+
self.dispatch(gen)
|
|
597
|
+
self.write(")")
|
|
598
|
+
|
|
599
|
+
def _SetComp(self, t):
|
|
600
|
+
self.write("{")
|
|
601
|
+
self.dispatch(t.elt)
|
|
602
|
+
for gen in t.generators:
|
|
603
|
+
self.dispatch(gen)
|
|
604
|
+
self.write("}")
|
|
605
|
+
|
|
606
|
+
def _DictComp(self, t):
|
|
607
|
+
self.write("{")
|
|
608
|
+
self.dispatch(t.key)
|
|
609
|
+
self.write(": ")
|
|
610
|
+
self.dispatch(t.value)
|
|
611
|
+
for gen in t.generators:
|
|
612
|
+
self.dispatch(gen)
|
|
613
|
+
self.write("}")
|
|
614
|
+
|
|
615
|
+
def _comprehension(self, t):
|
|
616
|
+
if getattr(t, "is_async", False):
|
|
617
|
+
self.write(" async for ")
|
|
618
|
+
else:
|
|
619
|
+
self.write(" for ")
|
|
620
|
+
self.dispatch(t.target)
|
|
621
|
+
self.write(" in ")
|
|
622
|
+
self.dispatch(t.iter)
|
|
623
|
+
for if_clause in t.ifs:
|
|
624
|
+
self.write(" if ")
|
|
625
|
+
self.dispatch(if_clause)
|
|
626
|
+
|
|
627
|
+
def _IfExp(self, t):
|
|
628
|
+
self.write("(")
|
|
629
|
+
self.dispatch(t.body)
|
|
630
|
+
self.write(" if ")
|
|
631
|
+
self.dispatch(t.test)
|
|
632
|
+
self.write(" else ")
|
|
633
|
+
self.dispatch(t.orelse)
|
|
634
|
+
self.write(")")
|
|
635
|
+
|
|
636
|
+
def _Set(self, t):
|
|
637
|
+
assert t.elts # should be at least one element
|
|
638
|
+
self.write("{")
|
|
639
|
+
interleave(lambda: self.write(", "), self.dispatch, t.elts)
|
|
640
|
+
self.write("}")
|
|
641
|
+
|
|
642
|
+
def _Dict(self, t):
|
|
643
|
+
self.write("{")
|
|
644
|
+
|
|
645
|
+
def write_key_value_pair(k, v):
|
|
646
|
+
self.dispatch(k)
|
|
647
|
+
self.write(": ")
|
|
648
|
+
self.dispatch(v)
|
|
649
|
+
|
|
650
|
+
def write_item(item):
|
|
651
|
+
k, v = item
|
|
652
|
+
if k is None:
|
|
653
|
+
# for dictionary unpacking operator in dicts {**{'y': 2}}
|
|
654
|
+
# see PEP 448 for details
|
|
655
|
+
self.write("**")
|
|
656
|
+
self.dispatch(v)
|
|
657
|
+
else:
|
|
658
|
+
write_key_value_pair(k, v)
|
|
659
|
+
|
|
660
|
+
interleave(lambda: self.write(", "), write_item, zip(t.keys, t.values))
|
|
661
|
+
self.write("}")
|
|
662
|
+
|
|
663
|
+
def _Tuple(self, t):
|
|
664
|
+
self.write("(")
|
|
665
|
+
if len(t.elts) == 1:
|
|
666
|
+
elt = t.elts[0]
|
|
667
|
+
self.dispatch(elt)
|
|
668
|
+
self.write(",")
|
|
669
|
+
else:
|
|
670
|
+
interleave(lambda: self.write(", "), self.dispatch, t.elts)
|
|
671
|
+
self.write(")")
|
|
672
|
+
|
|
673
|
+
unop = {"Invert": "~", "Not": "not", "UAdd": "+", "USub": "-"}
|
|
674
|
+
|
|
675
|
+
def _UnaryOp(self, t):
|
|
676
|
+
self.write("(")
|
|
677
|
+
self.write(self.unop[t.op.__class__.__name__])
|
|
678
|
+
self.write(" ")
|
|
679
|
+
if six.PY2 and isinstance(t.op, ast.USub) and isinstance(t.operand, ast.Num):
|
|
680
|
+
# If we're applying unary minus to a number, parenthesize the number.
|
|
681
|
+
# This is necessary: -2147483648 is different from -(2147483648) on
|
|
682
|
+
# a 32-bit machine (the first is an int, the second a long), and
|
|
683
|
+
# -7j is different from -(7j). (The first has real part 0.0, the second
|
|
684
|
+
# has real part -0.0.)
|
|
685
|
+
self.write("(")
|
|
686
|
+
self.dispatch(t.operand)
|
|
687
|
+
self.write(")")
|
|
688
|
+
else:
|
|
689
|
+
self.dispatch(t.operand)
|
|
690
|
+
self.write(")")
|
|
691
|
+
|
|
692
|
+
binop = {
|
|
693
|
+
"Add": "+",
|
|
694
|
+
"Sub": "-",
|
|
695
|
+
"Mult": "*",
|
|
696
|
+
"MatMult": "@",
|
|
697
|
+
"Div": "/",
|
|
698
|
+
"Mod": "%",
|
|
699
|
+
"LShift": "<<",
|
|
700
|
+
"RShift": ">>",
|
|
701
|
+
"BitOr": "|",
|
|
702
|
+
"BitXor": "^",
|
|
703
|
+
"BitAnd": "&",
|
|
704
|
+
"FloorDiv": "//",
|
|
705
|
+
"Pow": "**",
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
def _BinOp(self, t):
|
|
709
|
+
self.write("(")
|
|
710
|
+
self.dispatch(t.left)
|
|
711
|
+
self.write(" " + self.binop[t.op.__class__.__name__] + " ")
|
|
712
|
+
self.dispatch(t.right)
|
|
713
|
+
self.write(")")
|
|
714
|
+
|
|
715
|
+
cmpops = {
|
|
716
|
+
"Eq": "==",
|
|
717
|
+
"NotEq": "!=",
|
|
718
|
+
"Lt": "<",
|
|
719
|
+
"LtE": "<=",
|
|
720
|
+
"Gt": ">",
|
|
721
|
+
"GtE": ">=",
|
|
722
|
+
"Is": "is",
|
|
723
|
+
"IsNot": "is not",
|
|
724
|
+
"In": "in",
|
|
725
|
+
"NotIn": "not in",
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
def _Compare(self, t):
|
|
729
|
+
self.write("(")
|
|
730
|
+
self.dispatch(t.left)
|
|
731
|
+
for o, e in zip(t.ops, t.comparators):
|
|
732
|
+
self.write(" " + self.cmpops[o.__class__.__name__] + " ")
|
|
733
|
+
self.dispatch(e)
|
|
734
|
+
self.write(")")
|
|
735
|
+
|
|
736
|
+
boolops = {ast.And: "and", ast.Or: "or"}
|
|
737
|
+
|
|
738
|
+
def _BoolOp(self, t):
|
|
739
|
+
self.write("(")
|
|
740
|
+
s = " %s " % self.boolops[t.op.__class__]
|
|
741
|
+
interleave(lambda: self.write(s), self.dispatch, t.values)
|
|
742
|
+
self.write(")")
|
|
743
|
+
|
|
744
|
+
def _Attribute(self, t):
|
|
745
|
+
self.dispatch(t.value)
|
|
746
|
+
# Special case: 3.__abs__() is a syntax error, so if t.value
|
|
747
|
+
# is an integer literal then we need to either parenthesize
|
|
748
|
+
# it or add an extra space to get 3 .__abs__().
|
|
749
|
+
if isinstance(
|
|
750
|
+
t.value, getattr(ast, "Constant", getattr(ast, "Num", None))
|
|
751
|
+
) and isinstance(t.value.n, int):
|
|
752
|
+
self.write(" ")
|
|
753
|
+
self.write(".")
|
|
754
|
+
self.write(t.attr)
|
|
755
|
+
|
|
756
|
+
def _Call(self, t):
|
|
757
|
+
self.dispatch(t.func)
|
|
758
|
+
self.write("(")
|
|
759
|
+
comma = False
|
|
760
|
+
for e in t.args:
|
|
761
|
+
if comma:
|
|
762
|
+
self.write(", ")
|
|
763
|
+
else:
|
|
764
|
+
comma = True
|
|
765
|
+
self.dispatch(e)
|
|
766
|
+
for e in t.keywords:
|
|
767
|
+
if comma:
|
|
768
|
+
self.write(", ")
|
|
769
|
+
else:
|
|
770
|
+
comma = True
|
|
771
|
+
self.dispatch(e)
|
|
772
|
+
if sys.version_info[:2] < (3, 5):
|
|
773
|
+
if t.starargs:
|
|
774
|
+
if comma:
|
|
775
|
+
self.write(", ")
|
|
776
|
+
else:
|
|
777
|
+
comma = True
|
|
778
|
+
self.write("*")
|
|
779
|
+
self.dispatch(t.starargs)
|
|
780
|
+
if t.kwargs:
|
|
781
|
+
if comma:
|
|
782
|
+
self.write(", ")
|
|
783
|
+
else:
|
|
784
|
+
comma = True
|
|
785
|
+
self.write("**")
|
|
786
|
+
self.dispatch(t.kwargs)
|
|
787
|
+
self.write(")")
|
|
788
|
+
|
|
789
|
+
def _Subscript(self, t):
|
|
790
|
+
self.dispatch(t.value)
|
|
791
|
+
self.write("[")
|
|
792
|
+
self.dispatch(t.slice)
|
|
793
|
+
self.write("]")
|
|
794
|
+
|
|
795
|
+
def _Starred(self, t):
|
|
796
|
+
self.write("*")
|
|
797
|
+
self.dispatch(t.value)
|
|
798
|
+
|
|
799
|
+
# slice
|
|
800
|
+
def _Ellipsis(self, t):
|
|
801
|
+
self.write("...")
|
|
802
|
+
|
|
803
|
+
def _Index(self, t):
|
|
804
|
+
self.dispatch(t.value)
|
|
805
|
+
|
|
806
|
+
def _Slice(self, t):
|
|
807
|
+
if t.lower:
|
|
808
|
+
self.dispatch(t.lower)
|
|
809
|
+
self.write(":")
|
|
810
|
+
if t.upper:
|
|
811
|
+
self.dispatch(t.upper)
|
|
812
|
+
if t.step:
|
|
813
|
+
self.write(":")
|
|
814
|
+
self.dispatch(t.step)
|
|
815
|
+
|
|
816
|
+
def _ExtSlice(self, t):
|
|
817
|
+
interleave(lambda: self.write(", "), self.dispatch, t.dims)
|
|
818
|
+
|
|
819
|
+
# argument
|
|
820
|
+
def _arg(self, t):
|
|
821
|
+
self.write(t.arg)
|
|
822
|
+
if t.annotation:
|
|
823
|
+
self.write(": ")
|
|
824
|
+
self.dispatch(t.annotation)
|
|
825
|
+
|
|
826
|
+
# others
|
|
827
|
+
def _arguments(self, t):
|
|
828
|
+
first = True
|
|
829
|
+
# normal arguments
|
|
830
|
+
all_args = getattr(t, "posonlyargs", []) + t.args
|
|
831
|
+
defaults = [None] * (len(all_args) - len(t.defaults)) + t.defaults
|
|
832
|
+
for index, elements in enumerate(zip(all_args, defaults), 1):
|
|
833
|
+
a, d = elements
|
|
834
|
+
if first:
|
|
835
|
+
first = False
|
|
836
|
+
else:
|
|
837
|
+
self.write(", ")
|
|
838
|
+
self.dispatch(a)
|
|
839
|
+
if d:
|
|
840
|
+
self.write("=")
|
|
841
|
+
self.dispatch(d)
|
|
842
|
+
if index == len(getattr(t, "posonlyargs", ())):
|
|
843
|
+
self.write(", /")
|
|
844
|
+
|
|
845
|
+
# varargs, or bare '*' if no varargs but keyword-only arguments present
|
|
846
|
+
if t.vararg or getattr(t, "kwonlyargs", False):
|
|
847
|
+
if first:
|
|
848
|
+
first = False
|
|
849
|
+
else:
|
|
850
|
+
self.write(", ")
|
|
851
|
+
self.write("*")
|
|
852
|
+
if t.vararg:
|
|
853
|
+
if hasattr(t.vararg, "arg"):
|
|
854
|
+
self.write(t.vararg.arg)
|
|
855
|
+
if t.vararg.annotation:
|
|
856
|
+
self.write(": ")
|
|
857
|
+
self.dispatch(t.vararg.annotation)
|
|
858
|
+
else:
|
|
859
|
+
self.write(t.vararg)
|
|
860
|
+
if getattr(t, "varargannotation", None):
|
|
861
|
+
self.write(": ")
|
|
862
|
+
self.dispatch(t.varargannotation)
|
|
863
|
+
|
|
864
|
+
# keyword-only arguments
|
|
865
|
+
if getattr(t, "kwonlyargs", False):
|
|
866
|
+
for a, d in zip(t.kwonlyargs, t.kw_defaults):
|
|
867
|
+
if first:
|
|
868
|
+
first = False
|
|
869
|
+
else:
|
|
870
|
+
self.write(", ")
|
|
871
|
+
self.dispatch(a),
|
|
872
|
+
if d:
|
|
873
|
+
self.write("=")
|
|
874
|
+
self.dispatch(d)
|
|
875
|
+
|
|
876
|
+
# kwargs
|
|
877
|
+
if t.kwarg:
|
|
878
|
+
if first:
|
|
879
|
+
first = False
|
|
880
|
+
else:
|
|
881
|
+
self.write(", ")
|
|
882
|
+
if hasattr(t.kwarg, "arg"):
|
|
883
|
+
self.write("**" + t.kwarg.arg)
|
|
884
|
+
if t.kwarg.annotation:
|
|
885
|
+
self.write(": ")
|
|
886
|
+
self.dispatch(t.kwarg.annotation)
|
|
887
|
+
else:
|
|
888
|
+
self.write("**" + t.kwarg)
|
|
889
|
+
if getattr(t, "kwargannotation", None):
|
|
890
|
+
self.write(": ")
|
|
891
|
+
self.dispatch(t.kwargannotation)
|
|
892
|
+
|
|
893
|
+
def _keyword(self, t):
|
|
894
|
+
if t.arg is None:
|
|
895
|
+
# starting from Python 3.5 this denotes a kwargs part of the invocation
|
|
896
|
+
self.write("**")
|
|
897
|
+
else:
|
|
898
|
+
self.write(t.arg)
|
|
899
|
+
self.write("=")
|
|
900
|
+
self.dispatch(t.value)
|
|
901
|
+
|
|
902
|
+
def _Lambda(self, t):
|
|
903
|
+
self.write("(")
|
|
904
|
+
self.write("lambda ")
|
|
905
|
+
self.dispatch(t.args)
|
|
906
|
+
self.write(": ")
|
|
907
|
+
self.dispatch(t.body)
|
|
908
|
+
self.write(")")
|
|
909
|
+
|
|
910
|
+
def _alias(self, t):
|
|
911
|
+
self.write(t.name)
|
|
912
|
+
if t.asname:
|
|
913
|
+
self.write(" as " + t.asname)
|
|
914
|
+
|
|
915
|
+
def _withitem(self, t):
|
|
916
|
+
self.dispatch(t.context_expr)
|
|
917
|
+
if t.optional_vars:
|
|
918
|
+
self.write(" as ")
|
|
919
|
+
self.dispatch(t.optional_vars)
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
def roundtrip(filename, output=sys.stdout):
|
|
923
|
+
if six.PY3:
|
|
924
|
+
with open(filename, "rb") as pyfile:
|
|
925
|
+
encoding = tokenize.detect_encoding(pyfile.readline)[0]
|
|
926
|
+
with open(filename, "r", encoding=encoding) as pyfile:
|
|
927
|
+
source = pyfile.read()
|
|
928
|
+
else:
|
|
929
|
+
with open(filename, "r") as pyfile:
|
|
930
|
+
source = pyfile.read()
|
|
931
|
+
tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST, dont_inherit=True)
|
|
932
|
+
Unparser(tree, output)
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
def unparse(tree):
|
|
936
|
+
v = cStringIO()
|
|
937
|
+
Unparser(tree, file=v)
|
|
938
|
+
return v.getvalue()
|