PEP 511: reformat to send by email
This commit is contained in:
parent
77c59a0cdd
commit
78f8a7d3a9
34
pep-0511.txt
34
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 <https://en.wikipedia.org/wiki/Tail_call>`_
|
||||
* Add profiling code
|
||||
* `Lazy evaluation <https://en.wikipedia.org/wiki/Lazy_evaluation>`_:
|
||||
|
@ -116,12 +116,13 @@ Some examples:
|
|||
* Declare constants: see `@asconstants of codetransformer
|
||||
<https://pypi.python.org/pypi/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
|
||||
<https://www.python.org/dev/peps/pep-0335/>`_ was rejected.
|
||||
Python language itself doesn't need to be modified. Previous attempts
|
||||
to implement DSL for SQL like `PEP 335 - Overloadable Boolean
|
||||
Operators <https://www.python.org/dev/peps/pep-0335/>`_ was rejected.
|
||||
* Pattern Matching of functional languages
|
||||
* 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
|
||||
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()])
|
||||
|
||||
|
|
Loading…
Reference in New Issue