Skip to content

Commit

Permalink
Merge pull request #142 from colinxu2020/patch-1
Browse files Browse the repository at this point in the history
Fix the document for python3.12
  • Loading branch information
MatthieuDartiailh committed Feb 7, 2024
2 parents 8cb56dc + c158b64 commit 2a81d1b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ Example executing ``print('Hello World!')``:
from bytecode import Instr, Bytecode
bytecode = Bytecode([Instr("LOAD_NAME", 'print'),
bytecode = Bytecode([Instr("LOAD_GLOBAL", (True, 'print')),
Instr("LOAD_CONST", 'Hello World!'),
Instr("CALL_FUNCTION", 1),
Instr("CALL", 1),
Instr("POP_TOP"),
Instr("LOAD_CONST", None),
Instr("RETURN_VALUE")])
Expand Down
24 changes: 12 additions & 12 deletions doc/cfg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ Dump the control flow graph of the :ref:`conditional jump example

label_else = Label()
label_print = Label()
bytecode = Bytecode([Instr('LOAD_NAME', 'print'),
bytecode = Bytecode([Instr('LOAD_GLOBAL', (True, 'print')),
Instr('LOAD_NAME', 'test'),
Instr('POP_JUMP_IF_FALSE', label_else),
Instr('LOAD_CONST', 'yes'),
Instr('JUMP_FORWARD', label_print),
label_else,
Instr('LOAD_CONST', 'no'),
label_print,
Instr('CALL_FUNCTION', 1),
Instr('CALL', 1),
Instr('LOAD_CONST', None),
Instr('RETURN_VALUE')])

Expand All @@ -38,7 +38,7 @@ Dump the control flow graph of the :ref:`conditional jump example
Output::

block1:
LOAD_NAME 'print'
LOAD_GLOBAL (True, 'print')
LOAD_NAME 'test'
POP_JUMP_IF_FALSE <block3>
-> block2
Expand All @@ -52,7 +52,7 @@ Output::
-> block4

block4:
CALL_FUNCTION 1
CALL 1
LOAD_CONST None
RETURN_VALUE

Expand Down Expand Up @@ -107,15 +107,15 @@ Example of a ``display_blocks()`` function::

label_else = Label()
label_print = Label()
bytecode = Bytecode([Instr('LOAD_NAME', 'print'),
bytecode = Bytecode([Instr('LOAD_GLOBAL', (True, 'print')),
Instr('LOAD_NAME', 'test'),
Instr('POP_JUMP_IF_FALSE', label_else),
Instr('LOAD_CONST', 'yes'),
Instr('JUMP_FORWARD', label_print),
label_else,
Instr('LOAD_CONST', 'no'),
label_print,
Instr('CALL_FUNCTION', 1),
Instr('CALL', 1),
Instr('LOAD_CONST', None),
Instr('RETURN_VALUE')])

Expand All @@ -125,7 +125,7 @@ Example of a ``display_blocks()`` function::
Output::

Block #1
LOAD_NAME 'print'
LOAD_GLOBAL (True, 'print')
LOAD_NAME 'test'
POP_JUMP_IF_FALSE <block #3>
=> <block #2>
Expand All @@ -139,7 +139,7 @@ Output::
=> <block #4>

Block #4
CALL_FUNCTION 1
CALL 1
LOAD_CONST None
RETURN_VALUE

Expand Down Expand Up @@ -193,15 +193,15 @@ Example to a recursive ``display_block()`` function::

label_else = Label()
label_print = Label()
bytecode = Bytecode([Instr('LOAD_NAME', 'print'),
bytecode = Bytecode([Instr('LOAD_GLOBAL', (True, 'print')),
Instr('LOAD_NAME', 'test'),
Instr('POP_JUMP_IF_FALSE', label_else),
Instr('LOAD_CONST', 'yes'),
Instr('JUMP_FORWARD', label_print),
label_else,
Instr('LOAD_CONST', 'no'),
label_print,
Instr('CALL_FUNCTION', 1),
Instr('CALL', 1),
Instr('LOAD_CONST', None),
Instr('RETURN_VALUE')])

Expand All @@ -211,7 +211,7 @@ Example to a recursive ``display_block()`` function::
Output::

Block #1
LOAD_NAME 'print'
LOAD_GLOBAL (True, 'print')
LOAD_NAME 'test'
POP_JUMP_IF_FALSE <block #3>
=> <block #2>
Expand All @@ -221,7 +221,7 @@ Output::
JUMP_FORWARD <block #4>

Block #4
CALL_FUNCTION 1
CALL 1
LOAD_CONST None
RETURN_VALUE

Expand Down
19 changes: 10 additions & 9 deletions doc/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Example using abstract bytecode to execute ``print('Hello World!')``::

from bytecode import Instr, Bytecode

bytecode = Bytecode([Instr("LOAD_NAME", 'print'),
bytecode = Bytecode([Instr("LOAD_GLOBAL", (True, 'print')),
Instr("LOAD_CONST", 'Hello World!'),
Instr("CALL_FUNCTION", 1),
Instr("CALL", 1),
Instr("POP_TOP"),
Instr("LOAD_CONST", None),
Instr("RETURN_VALUE")])
Expand All @@ -46,9 +46,9 @@ Example using concrete bytecode to execute ``print('Hello World!')``::
bytecode = ConcreteBytecode()
bytecode.names = ['print']
bytecode.consts = ['Hello World!', None]
bytecode.extend([ConcreteInstr("LOAD_NAME", 0),
bytecode.extend([ConcreteInstr("LOAD_GLOBAL", 1),
ConcreteInstr("LOAD_CONST", 0),
ConcreteInstr("CALL_FUNCTION", 1),
ConcreteInstr("CALL", 1),
ConcreteInstr("POP_TOP"),
ConcreteInstr("LOAD_CONST", 1),
ConcreteInstr("RETURN_VALUE")])
Expand Down Expand Up @@ -110,14 +110,15 @@ Bytecode of ``for x in (1, 2, 3): print(x)``:
loop_start,
Instr("FOR_ITER", loop_exit),
Instr("STORE_NAME", "x"),
Instr("LOAD_NAME", "print"),
Instr("LOAD_GLOBAL", (True, "print")),
Instr("LOAD_NAME", "x"),
Instr("CALL_FUNCTION", 1),
Instr("CALL", 1),
Instr("POP_TOP"),
Instr("JUMP_ABSOLUTE", loop_start),
Instr("JUMP_BACKWARD", loop_start),
# Python 3.8 removed the need to manually manage blocks in loops
# This is now handled internally by the interpreter
loop_exit,
Instr("END_FOR"),
Instr("LOAD_CONST", None),
Instr("RETURN_VALUE"),
]
Expand Down Expand Up @@ -146,15 +147,15 @@ Bytecode of the Python code ``print('yes' if test else 'no')``::

label_else = Label()
label_print = Label()
bytecode = Bytecode([Instr('LOAD_NAME', 'print'),
bytecode = Bytecode([Instr('LOAD_GLOBAL', (True, 'print')),
Instr('LOAD_NAME', 'test'),
Instr('POP_JUMP_IF_FALSE', label_else),
Instr('LOAD_CONST', 'yes'),
Instr('JUMP_FORWARD', label_print),
label_else,
Instr('LOAD_CONST', 'no'),
label_print,
Instr('CALL_FUNCTION', 1),
Instr('CALL', 1),
Instr('LOAD_CONST', None),
Instr('RETURN_VALUE')])
code = bytecode.to_code()
Expand Down

0 comments on commit 2a81d1b

Please sign in to comment.