GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-43950: include position in dis.Instruction by isidentical · Pull Request #27015 · python/cpython

bpo-43950: include position in dis.Instruction #27015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
miss-islington merged 1 commit into python:main from colnotab:trace-co-positions
Jul 4, 2021

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-43950: include position in dis.Instruction #27015

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
05ce1cc
bpo-43950: include position in dis.Instruction
isidentical Jul 4, 2021
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

53 changes: 44 additions & 9 deletions 53 Lib/dis.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,32 @@ def show_code(co, *, file=None):
"""
print(code_info(co), file=file)

_Instruction = collections.namedtuple("_Instruction",
"opname opcode arg argval argrepr offset starts_line is_jump_target")
Positions = collections.namedtuple(
Comment thread
pablogsal marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

'Positions',
[
'lineno',
'end_lineno',
'col_offset',
'end_col_offset',
],
defaults=[None] * 4
)

_Instruction = collections.namedtuple(
"_Instruction",
[
'opname',
'opcode',
'arg',
'argval',
'argrepr',
'offset',
'starts_line',
'is_jump_target',
'positions'
],
defaults=[None]
)

_Instruction.opname.__doc__ = "Human readable name for operation"
_Instruction.opcode.__doc__ = "Numeric code for operation"
Expand All @@ -202,6 +226,7 @@ def show_code(co, *, file=None):
_Instruction.offset.__doc__ = "Start index of operation within bytecode sequence"
_Instruction.starts_line.__doc__ = "Line started by this opcode (if any), otherwise None"
_Instruction.is_jump_target.__doc__ = "True if other code jumps to here, otherwise False"
_Instruction.positions.__doc__ = "dis.Positions object holding the span of source code covered by this instruction"

_ExceptionTableEntry = collections.namedtuple("_ExceptionTableEntry",
"start end target depth lasti")
Expand All @@ -221,6 +246,8 @@ class Instruction(_Instruction):
offset - start index of operation within bytecode sequence
starts_line - line started by this opcode (if any), otherwise None
is_jump_target - True if other code jumps to here, otherwise False
positions - Optional dis.Positions object holding the span of source code
covered by this instruction
"""

def _disassemble(self, lineno_width=3, mark_as_current=False, offset_width=4):
Expand Down Expand Up @@ -281,7 +308,7 @@ def get_instructions(x, *, first_line=None):
return _get_instructions_bytes(co.co_code,
co._varname_from_oparg,
co.co_names, co.co_consts,
linestarts, line_offset)
linestarts, line_offset, co_positions=co.co_positions())

def _get_const_info(const_index, const_list):
"""Helper to get optional details about const references
Expand Down Expand Up @@ -339,7 +366,7 @@ def parse_exception_table(code):
def _get_instructions_bytes(code, varname_from_oparg=None,
names=None, constants=None,
linestarts=None, line_offset=0,
exception_entries=()):
exception_entries=(), co_positions=None):
"""Iterate over the instructions in a bytecode string.

Generates a sequence of Instruction namedtuples giving the details of each
Expand All @@ -348,6 +375,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
arguments.

"""
co_positions = co_positions or iter(())
get_name = None if names is None else names.__getitem__
labels = set(findlabels(code))
for start, end, target, _, _ in exception_entries:
Expand All @@ -362,6 +390,10 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
is_jump_target = offset in labels
argval = None
argrepr = ''
try:
positions = next(co_positions)
except StopIteration:
positions = None
if arg is not None:
# Set argval to the dereferenced value of the argument when
# available, and argrepr to the string representation of argval.
Expand Down Expand Up @@ -395,7 +427,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
if arg & (1<<i))
yield Instruction(opname[op], op,
arg, argval, argrepr,
offset, starts_line, is_jump_target)
offset, starts_line, is_jump_target, positions)

def disassemble(co, lasti=-1, *, file=None):
"""Disassemble a code object."""
Expand All @@ -404,7 +436,7 @@ def disassemble(co, lasti=-1, *, file=None):
_disassemble_bytes(co.co_code, lasti,
co._varname_from_oparg,
co.co_names, co.co_consts, linestarts, file=file,
exception_entries=exception_entries)
exception_entries=exception_entries, co_positions=co.co_positions())

def _disassemble_recursive(co, *, file=None, depth=None):
disassemble(co, file=file)
Expand All @@ -419,7 +451,8 @@ def _disassemble_recursive(co, *, file=None, depth=None):

def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None,
names=None, constants=None, linestarts=None,
*, file=None, line_offset=0, exception_entries=()):
*, file=None, line_offset=0, exception_entries=(),
co_positions=None):
# Omit the line number column entirely if we have no line number info
show_lineno = bool(linestarts)
if show_lineno:
Expand All @@ -437,7 +470,8 @@ def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None,
offset_width = 4
for instr in _get_instructions_bytes(code, varname_from_oparg, names,
constants, linestarts,
line_offset=line_offset, exception_entries=exception_entries):
line_offset=line_offset, exception_entries=exception_entries,
co_positions=co_positions):
new_source_line = (show_lineno and
instr.starts_line is not None and
instr.offset > 0)
Expand Down Expand Up @@ -562,7 +596,8 @@ def dis(self):
line_offset=self._line_offset,
file=output,
lasti=offset,
exception_entries=self.exception_entries)
exception_entries=self.exception_entries,
co_positions=co.co_positions())
return output.getvalue()


Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.