question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any way to get the bytecode offsets along with the line number ?

See original GitHub issue

x = 0

Consider the above code snippet. The dis module outputs the line numbers along with the bytecode offsets.

  1           0 LOAD_CONST               0 (0)
              2 STORE_NAME               0 (x)
              4 LOAD_CONST               1 (None)
              6 RETURN_VALUE

Using the bytecode module I get the following, except the offset=<> part. Is there any way I can get these offsets, similar to the dis module’s output?

<LOAD_CONST arg=0 lineno=1 offset=0>
<STORE_NAME arg='x' lineno=1 offset=2>
<LOAD_CONST arg=None lineno=1 offset=4>
<RETURN_VALUE lineno=1 offset=6>>

I modified the bytecode module source to get the above output. If there is no other way except modifying the source, should I submit a PR?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:23 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
laike9mcommented, Oct 21, 2019

Thanks @MatthieuDartiailh for pointing me to this issue. I’m also interested in having a way to get offset easily, cause right now I have to iterate over Bytecode to calculate the offset, which is inefficient.

I read the above comments, from my understanding, the concern is mostly about whether it is appropriate to coupe [Concrete]Bytecode with the original bytecode. I don’t have enough understanding of the library to give a perfect answer, but perhaps we can consider a different approach.

So, we are concerned about having things coupled, how about create a separete class specifically for handling mapping, and leave [Concrete]Bytecode unchanged.

For example, a class called BytecodeMapper

mapper = BytecodeMapper(my_func.f_code)
bc: Bytecode = mapper.get_bytecode()
cbc: ConcreteBytecode = mapper.get_concrete_bytecode()

# For offset
mapper.find_instr_by_offset(int)  # returns a Instr
mapper.find_concrete_instr_by_offset(int)  # returns a ConcreteInstr
mapper.get_offset(Union[Instr, ConcreteInstr])

# For lineno
mapper.find_instrs_by_lineno(int)  # returns a list of Instr
mapper.find_concrete_instrs_by_lineno(int)  # returns a list of ConcreteInstr
mapper.get_lineno(Union[Instr, ConcreteInstr])

You can do whatever you want with the returned Bytecode object. When get_offset or get_lineno is called, it checks whether the passed in instr actually comes from this mapper(aka is, not ==). If not, just return None.

1reaction
laike9mcommented, Oct 23, 2019

@MatthieuDartiailh By “new offsets” you mean the offset after modifying Bytecode or ConcreteBytecode right? I would suggest not bother doing it, it feels hacky and may have problems? I can not say for other people, maybe someone need the offset to be updated as well, but I just don’t feel this to be a legitimate use.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there any way to get the bytecode offsets along with the line number
Consider the above code snippet. The dis module outputs the line numbers along with the bytecode offsets. 1 0 LOAD_CONST 0 (0) 2...
Read more >
How do I get the line number of the source code from a byte ...
I am manipulating a .class file. I am using the InstrutionHandle package to get the instructions one at a time. I have the...
Read more >
Understanding Python Bytecode - Towards Data Science
This is an array of signed bytes stored in a bytes literal and is used to map the bytecode offsets to the source...
Read more >
Wicked hack: Python bytecode tracing - Ned Batchelder
The two numbers in each pair are a bytecode offset delta and a source line number delta. The firstlineno value of 1 means...
Read more >
32.12. dis — Disassembler for Python bytecode - Read the Docs
The CPython bytecode which this module takes as an input is defined in the file ... If first_line is not None , it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found