PEP 511: reformat to send by email

This commit is contained in:
Victor Stinner 2016-01-15 17:12:31 +01:00
parent 77c59a0cdd
commit 78f8a7d3a9
1 changed files with 16 additions and 18 deletions

View File

@ -32,8 +32,8 @@ builtin ``compile()`` function. There are even more options to
hook a code transformer. hook a code transformer.
Python 3.4 added a ``compile_source()`` method to Python 3.4 added a ``compile_source()`` method to
``importlib.abc.SourceLoader``. But code transformation is wider than just ``importlib.abc.SourceLoader``. But code transformation is wider than
importing modules, see described use cases below. just importing modules, see described use cases below.
Writing an optimizer or a preprocessor is out of the scope of this PEP. 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: Some examples:
* Remove debug code like assertions and logs to make the code faster to run * Remove debug code like assertions and logs to make the code faster to
it for production. run it for production.
* `Tail-call Optimization <https://en.wikipedia.org/wiki/Tail_call>`_ * `Tail-call Optimization <https://en.wikipedia.org/wiki/Tail_call>`_
* Add profiling code * Add profiling code
* `Lazy evaluation <https://en.wikipedia.org/wiki/Lazy_evaluation>`_: * `Lazy evaluation <https://en.wikipedia.org/wiki/Lazy_evaluation>`_:
@ -116,12 +116,13 @@ Some examples:
* Declare constants: see `@asconstants of codetransformer * Declare constants: see `@asconstants of codetransformer
<https://pypi.python.org/pypi/codetransformer>`_ <https://pypi.python.org/pypi/codetransformer>`_
* Domain Specific Language (DSL) like SQL queries. The * Domain Specific Language (DSL) like SQL queries. The
Python language itself doesn't need to be modified. Previous attempts to Python language itself doesn't need to be modified. Previous attempts
implement DSL for SQL like `PEP 335 - Overloadable Boolean Operators to implement DSL for SQL like `PEP 335 - Overloadable Boolean
<https://www.python.org/dev/peps/pep-0335/>`_ was rejected. Operators <https://www.python.org/dev/peps/pep-0335/>`_ was rejected.
* Pattern Matching of functional languages * Pattern Matching of functional languages
* String Interpolation, but `PEP 498 -- Literal String Interpolation * String Interpolation, but `PEP 498 -- Literal String Interpolation
<https://www.python.org/dev/peps/pep-0498/>`_ was merged into Python 3.6. <https://www.python.org/dev/peps/pep-0498/>`_ was merged into Python
3.6.
`MacroPy <https://github.com/lihaoyi/macropy>`_ has a long list of `MacroPy <https://github.com/lihaoyi/macropy>`_ has a long list of
examples and use cases. examples and use cases.
@ -276,7 +277,8 @@ Parameters:
* *code*: the bytecode (``bytes``) * *code*: the bytecode (``bytes``)
* *consts*: a sequence of constants * *consts*: a sequence of constants
* *names*: tuple of variable names * *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 The code transformer is run after the compilation to bytecode
@ -368,8 +370,9 @@ be compiled to import a module::
def transformers_tag(): def transformers_tag():
transformers = sys.get_code_transformers() transformers = sys.get_code_transformers()
if not transformers: if not transformers:
return 'opt' return 'noopt'
return '-'.join(transformer.name for transformer in transformers) return '-'.join(transformer.name
for transformer in transformers)
def use_py(): def use_py():
return (transformers_tag() == sys.implementation.optim_tag) return (transformers_tag() == sys.implementation.optim_tag)
@ -456,7 +459,6 @@ Scary bytecode transformer replacing all strings with
import sys import sys
class BytecodeTransformer: class BytecodeTransformer:
name = "knights_who_say_ni" name = "knights_who_say_ni"
@ -465,11 +467,10 @@ Scary bytecode transformer replacing all strings with
for const in consts] for const in consts]
return (code, consts, names, lnotab) return (code, consts, names, lnotab)
# replace existing code transformers with the new bytecode transformer
# replace existing code transformers with our bytecode transformer
sys.set_code_transformers([BytecodeTransformer()]) 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!')") exec("print('Hello World!')")
Output:: Output::
@ -486,13 +487,11 @@ replaces all strings with ``"Ni! Ni! Ni!"``::
import ast import ast
import sys import sys
class KnightsWhoSayNi(ast.NodeTransformer): class KnightsWhoSayNi(ast.NodeTransformer):
def visit_Str(self, node): def visit_Str(self, node):
node.s = 'Ni! Ni! Ni!' node.s = 'Ni! Ni! Ni!'
return node return node
class ASTTransformer: class ASTTransformer:
name = "knights_who_say_ni" name = "knights_who_say_ni"
@ -503,7 +502,6 @@ replaces all strings with ``"Ni! Ni! Ni!"``::
self.transformer.visit(tree) self.transformer.visit(tree)
return tree return tree
# replace existing code transformers with the new AST transformer # replace existing code transformers with the new AST transformer
sys.set_code_transformers([ASTTransformer()]) sys.set_code_transformers([ASTTransformer()])