Is there any way to get the bytecode offsets along with the line number ?
See original GitHub issuex = 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:
- Created 4 years ago
- Reactions:1
- Comments:23 (16 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
You can do whatever you want with the returned
Bytecode
object. Whenget_offset
orget_lineno
is called, it checks whether the passed in instr actually comes from this mapper(akais
, not==
). If not, just returnNone
.@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.