optapy / optapy

OptaPy is an AI constraint solver for Python to optimize planning and scheduling problems.

Home Page:https://www.optapy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jpyinterpreter - Python 3.11 support

Christopher-Chianelli opened this issue · comments

Python 3.11 made significant changes to exception handling in the bytecode:

  • Instead of using SETUP_FINALLY opcode to create try...except blocks, it now has an exception table stored in co_exceptiontable to make entering try blocks zero-costs like Java
  • Exception representation on the stack now consist of one (exception), not three (traceback, exception, type), items.

It also removed CALL_FUNCTION_KW, CALL_METHOD and CALL_FUNCTION with a single CALL function that uses internal variables to decide what form to use.

New opcodes:

  • COPY(i) - Push a copy of the ith item in the stack to the top of the stack.
  • SWAP(i) - Swap TOS with the item at position i
  • CACHE (Not an instruction and not shown by dis by default, use for adaptive bytecode for Python JIT) (i.e. we can ignore this)
  • BINARY_OP(op) - perform a binary operation (depending on op). Replaces the many BINARY_OP and INPLACE_BINARY_OP opcode, and is similar to what our interpreter already does.
  • PUSH_EXC_INFO - Pops a value from the stack. Pushes the current exception to the top of the stack. Pushes the value originally popped back to the stack. Used in exception handlers. Need to look at actual bytecode to see what this actually does
  • CHECK_EXC_MATCH - Performs exception matching for except. Tests whether the TOS1 is an exception matching TOS. Pops TOS and pushes the boolean result of the test.
  • Performs exception matching for except*. Applies split(TOS) on the exception group representing TOS1. In case of a match, pops two items from the stack and pushes the non-matching subgroup (None in case of full match) followed by the matching subgroup. When there is no match, pops one item (the match type) and pushes None.
  • PREP_RERAISE_STAR - Combines the raised and reraised exceptions list from TOS, into an exception group to propagate from a try-except* block. Uses the original exception group from TOS1 to reconstruct the structure of reraised exceptions. Pops two items from the stack and pushes the exception to reraise or None if there isn’t one.
  • BEFORE_WITH(delta) - This opcode performs several operations before a with block starts. First, it loads exit() from the context manager and pushes it onto the stack for later use by WITH_EXCEPT_START. Then, enter() is called. Finally, the result of calling the enter() method is pushed onto the stack.
  • A bunch of jump instructions, which now all use relative jumps
  • MAKE_CELL(i) -Creates a new cell in slot i. If that slot is empty then that value is stored into the new cell.
  • COPY_FREE_VARS(n) - Copies the n free variables from the closure into the frame. Removes the need for special code on the caller’s side when calling closures.
  • CALL(argc) - described above
  • PRECALL(argc) - Prefixes CALL, a NO-OP for us; it exists to specialize instructions for JIT
  • PUSH_NULL Pushes a NULL to the stack. Used in the call sequence to match the NULL pushed by LOAD_METHOD for non-method calls.
  • KW_NAMES(i) - prefixes PRECALL, Stores a reference to co_consts[consti] into an internal variable for use by CALL. co_consts[consti] must be a tuple of strings.
  • RESUME(where) - a no-op, resumes a generator and does internal tracing debugging and optimization checks
  • RETURN_GENERATOR - Create a generator, coroutine, or async generator from the current frame. Clear the current frame and return the newly created generator.
  • SEND - Sends None to the sub-generator of this generator. Used in yield from and await statements.
  • ASYNC_GEN_WRAP -Wraps the value on top of the stack in an async_generator_wrapped_value. Used to yield in async generators.

Changed instructions:

  • GET_AWAITABLE - Got an argument, which if not zero, indicates where the instruction occurs. It can probably be ignored.
  • END_ASYNC_FOR, POP_EXCEPT, RERAISE, WITH_EXCEPT_START - changed stack representation for exceptions
  • MATCH_KEYS, MATCH_CLASS - In 3.10, in addition to the tuple with the matched values, it pushed a boolean value indicating success (True) or failure (False). In 3.11, only the tuple is pushed.
  • JUMP_IF_TRUE_OR_POP, JUMP_IF_FALSE_OR_POP - argument is now a relative delta (previously was absolute)
  • LOAD_GLOBAL: If the low bit of namei is set, then a NULL is push to the stack before the global variable.
  • LOAD_CLOSURE, LOAD_DEREF, LOAD_CLASSDEREF, STORE_DEREF, DELETE_DEREF - i is no longer offset by the length of co_varnames.