From 78f8a7d3a943db0b0cb6472fb075a54d52ed4132 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 15 Jan 2016 17:12:31 +0100 Subject: [PATCH] PEP 511: reformat to send by email --- pep-0511.txt | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/pep-0511.txt b/pep-0511.txt index 6141a180b..edc8e9bc8 100644 --- a/pep-0511.txt +++ b/pep-0511.txt @@ -32,8 +32,8 @@ builtin ``compile()`` function. There are even more options to hook a code transformer. Python 3.4 added a ``compile_source()`` method to -``importlib.abc.SourceLoader``. But code transformation is wider than just -importing modules, see described use cases below. +``importlib.abc.SourceLoader``. But code transformation is wider than +just importing modules, see described use cases below. Writing an optimizer or a preprocessor is out of the scope of this PEP. @@ -104,8 +104,8 @@ preprocessor has various and different usages. Some examples: -* Remove debug code like assertions and logs to make the code faster to run - it for production. +* Remove debug code like assertions and logs to make the code faster to + run it for production. * `Tail-call Optimization `_ * Add profiling code * `Lazy evaluation `_: @@ -116,12 +116,13 @@ Some examples: * Declare constants: see `@asconstants of codetransformer `_ * Domain Specific Language (DSL) like SQL queries. The - Python language itself doesn't need to be modified. Previous attempts to - implement DSL for SQL like `PEP 335 - Overloadable Boolean Operators - `_ was rejected. + Python language itself doesn't need to be modified. Previous attempts + to implement DSL for SQL like `PEP 335 - Overloadable Boolean + Operators `_ was rejected. * Pattern Matching of functional languages * String Interpolation, but `PEP 498 -- Literal String Interpolation - `_ was merged into Python 3.6. + `_ was merged into Python + 3.6. `MacroPy `_ has a long list of examples and use cases. @@ -276,7 +277,8 @@ Parameters: * *code*: the bytecode (``bytes``) * *consts*: a sequence of constants * *names*: tuple of variable names -* *lnotab*: table mapping instruction offsets to line numbers (``bytes``) +* *lnotab*: table mapping instruction offsets to line numbers + (``bytes``) The code transformer is run after the compilation to bytecode @@ -368,8 +370,9 @@ be compiled to import a module:: def transformers_tag(): transformers = sys.get_code_transformers() if not transformers: - return 'opt' - return '-'.join(transformer.name for transformer in transformers) + return 'noopt' + return '-'.join(transformer.name + for transformer in transformers) def use_py(): return (transformers_tag() == sys.implementation.optim_tag) @@ -456,7 +459,6 @@ Scary bytecode transformer replacing all strings with import sys - class BytecodeTransformer: name = "knights_who_say_ni" @@ -465,11 +467,10 @@ Scary bytecode transformer replacing all strings with for const in consts] return (code, consts, names, lnotab) - - # replace existing code transformers with our bytecode transformer + # replace existing code transformers with the new bytecode transformer sys.set_code_transformers([BytecodeTransformer()]) - # execute code which will be transformed by ast_transformer() + # execute code which will be transformed by code_transformer() exec("print('Hello World!')") Output:: @@ -486,13 +487,11 @@ replaces all strings with ``"Ni! Ni! Ni!"``:: import ast import sys - class KnightsWhoSayNi(ast.NodeTransformer): def visit_Str(self, node): node.s = 'Ni! Ni! Ni!' return node - class ASTTransformer: name = "knights_who_say_ni" @@ -503,7 +502,6 @@ replaces all strings with ``"Ni! Ni! Ni!"``:: self.transformer.visit(tree) return tree - # replace existing code transformers with the new AST transformer sys.set_code_transformers([ASTTransformer()])