PEP 511: update bytecode example

This commit is contained in:
Victor Stinner 2016-02-13 01:29:05 +01:00
parent e2bf937396
commit 4da7e41f94
1 changed files with 18 additions and 3 deletions

View File

@ -443,14 +443,29 @@ Scary bytecode transformer replacing all strings with
``"Ni! Ni! Ni!"``::
import sys
import types
class BytecodeTransformer:
name = "knights_who_say_ni"
def code_transformer(self, code, consts, names, lnotab, context):
def code_transformer(self, code, context):
consts = ['Ni! Ni! Ni!' if isinstance(const, str) else const
for const in consts]
return (code, consts, names, lnotab)
for const in code.co_consts]
return types.CodeType(code.co_argcount,
code.co_kwonlyargcount,
code.co_nlocals,
code.co_stacksize,
code.co_flags,
code.co_code,
tuple(consts),
code.co_names,
code.co_varnames,
code.co_filename,
code.co_name,
code.co_firstlineno,
code.co_lnotab,
code.co_freevars,
code.co_cellvars)
# replace existing code transformers with the new bytecode transformer
sys.set_code_transformers([BytecodeTransformer()])