Git Product home page Git Product logo

Comments (3)

kwrooijen avatar kwrooijen commented on July 19, 2024 1

Figured it out. I had to add an argcount which defaults to 0.

from bytecode import ConcreteBytecode, ConcreteInstr

bytecode_fn = ConcreteBytecode()
bytecode_fn.varnames = ["x"]
bytecode_fn.consts = [10]
bytecode_fn.argcount = 1
bytecode_fn.extend([ConcreteInstr("LOAD_FAST", 0),  # var x
                    ConcreteInstr("LOAD_CONST", 0),
                    ConcreteInstr("BINARY_ADD"),    # Add 10
                    ConcreteInstr("RETURN_VALUE")])
fn_code_obj = bytecode_fn.to_code()


bytecode = ConcreteBytecode()
bytecode.names = ["some_fn"]
bytecode.consts = [fn_code_obj, "some_fn", None]
bytecode.extend([ConcreteInstr("LOAD_CONST", 0),    # fn_code_obj
                 ConcreteInstr("LOAD_CONST", 1),    # "some_fn"
                 ConcreteInstr("MAKE_FUNCTION", 0),
                 ConcreteInstr("STORE_NAME", 0),    # "some_fn"
                 ConcreteInstr("LOAD_CONST", 2),    # None
                 ConcreteInstr("RETURN_VALUE")])

# Execute bytecode
code = bytecode.to_code()
exec(code)

some_fn(1) # Returns 11

I guess I overlooked the fact that ConcreteBytecode inherits from BaseBytecode. Would you like this (or similar "howto create a function") in the docs? If so I can make a PR and otherwise this issue can just be closed.

from bytecode.

MatthieuDartiailh avatar MatthieuDartiailh commented on July 19, 2024

This could be an interesting example to have in the docs. Feel free to make a PR.

from bytecode.

P403n1x87 avatar P403n1x87 commented on July 19, 2024

Just as a note that one can also create functions with FunctionType: https://github.com/P403n1x87/surgery/blob/792748fb7497d8e5d0998a54231fc16742250c55/surgery.py#L56

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.