Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

feat: Add support for Python 3.12 #27

Merged
merged 6 commits into from
Apr 2, 2024
Merged

feat: Add support for Python 3.12 #27

merged 6 commits into from
Apr 2, 2024

Conversation

Christopher-Chianelli
Copy link
Collaborator

  • Python 3.12 changed the meaning of the arg for both COMPARE_OP
    and JUMP instructions:

    • For COMPARE_OP, the index now starts at the fifth-lowest bit.

    • For JUMP instructions,
      "The argument of a jump is the offset of the target instruction relative to the instruction that appears immediately after the jump instruction’s CACHE entries.
      As a consequence, the presence of the CACHE instructions is transparent for forward jumps but needs to be taken into account when reasoning about backward jumps."

      For both of these, we parse the argrepr. In particular:

    • For COMPARE_OP, it be "<", ">", "==", "!=", ">=" or "<=".

    • For JUMP instructions, it be "to BYTECODE_OFFSET", which is exactly
      twice the instruction index.

  • Python 3.12 changed the meaning of FOR_ITER to leave the iterator on
    the stack (and push an undocumented object) when exhausted,
    and added the instruction END_FOR to pop them both off. However, since
    the target for FOR_ITER is always END_FOR (see https://github.com/python/cpython/blob/17a82a1d16a46b6c008240bcc698619419ce5554/Python/bytecodes.c#L2289-L2293),
    we can "optimize" it by using the old implementation and making END_FOR a no-op.

  • Python 3.12 merged LOAD_ATTR and LOAD_METHOD into one opcode.

  • Python 3.12 added BINARY_SLICE and STORE_SLICE opcodes to implement
    a[start:end] and a[start:end] = iterable (previously,
    BUILD_SLICE was used followed by GET_ITEM or SET_ITEM).

  • Python 3.12 added END_SEND for generator cleanup, which we
    implement as a NOOP.

  • Python 3.12 added CLEANUP_THROW and INTRINSIC_STOPITERATION_ERROR,
    which replace TOS with its value if TOS a StopIteration,
    else reraise the error on TOS. CLEANUP_THROW additionally pops
    two additional values off the stack.

  • Python 3.12 added LOAD_FAST_CHECK and LOAD_FAST_AND_CLEAR.
    LOAD_FAST now will never raise unbound local error,
    LOAD_FAST_CHECK might raise an unbound local error,
    and LOAD_FAST_AND_CLEAR will load NONE when the local is unbound
    (and additionally sets the local to null).

    Since Java rightfully says "???" when it sees code that can access
    an unbound local, we need to set locals used by
    LOAD_FAST_AND_CLEAR to null before generating code
    for opcodes.
    
  • Python 3.12 added RETURN_CONST to return a constant.

  • Python 3.12 changed UNARY_POS from a "fast" opcode to a
    slow "intrinsic" opcode since "+a" is rarely used. This
    does not affect us (other than having a new alternative
    way of writing "UNARY_POS" as
    "INTRINSIC_1_INTRINSIC_UNARY_POSITIVE".

  • Simplified and fix some bugs in PythonSlice code

  • Add a workaround for PyTest failure in Python 3.11 and above caused by calling a method that throws in a generated class jpype-project/jpype#1178

* Python 3.12 changed the meaning of the arg for both COMPARE_OP
  and JUMP instructions:

** For COMPARE_OP, the index now starts at the fifth-lowest bit.
** For JUMP instructions,
   "The argument of a jump is the offset of the target instruction relative to the instruction that appears immediately after the jump instruction’s CACHE entries.
   As a consequence, the presence of the CACHE instructions is transparent for forward jumps but needs to be taken into account when reasoning about backward jumps."

  For both of these, we parse the argrepr. In particular:

** For COMPARE_OP, it be "<", ">", "==", "!=", ">=" or "<=".
** For JUMP instructions, it be "to BYTECODE_OFFSET", which is exactly
   twice the instruction index.

* Python 3.12 changed the meaning of FOR_ITER to leave the iterator on
  the stack, and added the instruction END_FOR to pop it off.

* Python 3.12 merged LOAD_ATTR and LOAD_METHOD into one opcode.

* Python 3.12 added BINARY_SLICE and STORE_SLICE opcodes to implement
  `a[start:end]` and `a[start:end] = iterable` (previously,
  BUILD_SLICE was used followed by `GET_ITEM` or `SET_ITEM`).

* Python 3.12 added END_SEND for generator cleanup, which we
  implement as a NOOP.

* Python 3.12 added CLEANUP_THROW and INTRINSIC_STOPITERATION_ERROR,
  which replace TOS with its value if TOS a StopIteration,
  else reraise the error on TOS. CLEANUP_THROW additionally pops
  two additional values off the stack.

* Python 3.12 added LOAD_FAST_CHECK and LOAD_FAST_AND_CLEAR.
  LOAD_FAST now will never raise unbound local error,
  LOAD_FAST_CHECK might raise an unbound local error,
  and LOAD_FAST_AND_CLEAR will load NONE when the local is unbound
  (and additionally sets the local to null).

  Since Java rightfully says "???" when it sees code that can access
  an unbound local, we need to set locals used by
  LOAD_FAST_AND_CLEAR to null before generating code
  for opcodes.

* Python 3.12 added RETURN_CONST to return a constant.

* Python 3.12 changed UNARY_POS from a "fast" opcode to a
  slow "intrinsic" opcode since "+a" is rarely used. This
  does not affect us (other than having a new alternative
  way of writing "UNARY_POS" as
  "INTRINSIC_1_INTRINSIC_UNARY_POSITIVE".

* Simplified and fix some bugs in PythonSlice code

* Add a workaround for jpype-project/jpype#1178

* These changes make the majority of tests for Python 3.12 pass, but
  there still some unimplemented new Python 3.12 opcodes and
  some issues with comprehensions.
From https://github.com/python/cpython/blob/17a82a1d16a46b6c008240bcc698619419ce5554/Python/bytecodes.c#L2289-L2293,
we know the jump target for FOR_ITER will always be END_FOR, which
means we can combine FOR_ITER and END_FOR into the same opcode,
which means the behaviour of FOR_ITER did not actually change, just
the "syntax".

(In particular, FOR_ITER apparently has a stack effect of 1,
 but what it pushes is undocumented, and is popped off by END_FOR)
All tests now pass for Python 3.12
@triceo triceo linked an issue Apr 1, 2024 that may be closed by this pull request
Copy link
Contributor

@triceo triceo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but leaving some comments.
Will approve when resolved.

@Christopher-Chianelli Christopher-Chianelli merged commit 5f4c79d into TimefoldAI:main Apr 2, 2024
3 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jpyinterpreter - Python 3.12 support
2 participants