Comments (3)
This is a perfectly legitimate use case of bytecode and any issue your encounter doing this would be a bug.
@thautwarm if I remember correctly you use bytecode to modify constants and probably do a lot of round tripping as a consequence, can you comment on this too.
from bytecode.
@MatthieuDartiailh Yes, I did a lot of this sort of stuffs..
I don't see any corner case that would break, if any it must be a bug and should be solved.
@fabioz For your use case, there might be something to be take care.
-
If you're using Python < 3.6, be cautious about
lineno
of anInstr
.Python 3.6 begins to support negative line number delta, so in 3.5 or lower versions, when inserting new instructions, you should guarantee that the
lineno
s are monotonically increasing along instructions. -
Besides, I recommend you to generate new instructions via a generator, instead of performing multiple insertions(it is inefficient if you insert for multiple times).
def gen(instrs): yield from (instrs[i] for i in range(1, i) yield instrs[i] yield Instr("LOAD_GLOBAL", '_pydev_stop_at_break') yield Instr("LOAD_CONST", stop_at_line) yield Instr("CALL_FUNCTION", 1) yield Instr("POP_TOP") yield from (instrs[i] for i in range(i, len(instrs))
-
A global name
_pydev_stop_at_break
might be rare enough and users will not write down such a name, but still, it is not well encapsulated or hidden well from users.If the bytecode modified by debugger will not be dumped into
.pyc
file, you might consider insert a constant function instead of loading it from globals(loading from globals also requires you to initialize the variable in each module).Instr("LOAD_GLOBAL", '_pydev_stop_at_break')
->
def pydev_stop_at_break(...): ... Instr("LOAD_CONST", pydev_stop_at_break)
I used to have this demand so I asked for this feature, thanks to @MatthieuDartiailh for kindly reviewing and accepting this.
from bytecode.
Thank you for the feedback.
@thautwarm I'm actually using that only with Python 3.5 onwards and I'm converting the list to a double linked list (so that multiple insertions are efficient and to have a nicer API as iterating/changing items at the same time is a bit more straightforward), so, 1 and 2 are already ok, and thanks for the tip on the LOAD_CONST
, I'll take a look into using that.
from bytecode.
Related Issues (20)
- The `Compare` enum is broken starting from Python 3.9
- Python 3.11 support HOT 5
- EXTENDED_ARG + NOP Error HOT 3
- except Exception as e fails on Python 3.11 HOT 1
- read .pyc HOT 3
- Possible "hysteresis" in bytecode recompilation with 3.12 HOT 7
- `ValueError: ('TryBegin target must a BasicBlock, got %s', 'Label')` when using `Label` with `TryBegin` HOT 14
- `LOAD_METHOD` is still a valid opname but doesn't seem to be fully supported HOT 1
- `_encode_varint` cannot handle 0 HOT 1
- Running pytest segfaults with Python 3.12 on armhf HOT 11
- Can't call function with no arguments (py312) HOT 2
- Cannot decompile code with empty try block HOT 1
- Missing tag on 0.12.0 release HOT 1
- Update jump address handling for 3.10 HOT 1
- Bytecode doesn't properly set linenumbers (in corner case in Python 3.10rc1) HOT 2
- Errors in compute stacksize on 3.10 HOT 7
- Please clarify the copyright HOT 4
- Remove `Compare.EXC_MATCH` on Python>=3.9 HOT 1
- Update pre_and_post_stack_effect HOT 1
- Labels don't seem to be handled correctly with Python<3.9 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bytecode.