Git Product home page Git Product logo

Comments (3)

MatthieuDartiailh avatar MatthieuDartiailh commented on July 19, 2024

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.

thautwarm avatar thautwarm commented on July 19, 2024

@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.

  1. If you're using Python < 3.6, be cautious about lineno of an Instr.

    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 linenos are monotonically increasing along instructions.

  2. 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))
  3. 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.

fabioz avatar fabioz commented on July 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.