This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add support for Python 3.12 #27
Merged
Christopher-Chianelli
merged 6 commits into
TimefoldAI:main
from
Christopher-Chianelli:python3.12
Apr 2, 2024
Merged
feat: Add support for Python 3.12 #27
Christopher-Chianelli
merged 6 commits into
TimefoldAI:main
from
Christopher-Chianelli:python3.12
Apr 2, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* 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
reviewed
Apr 2, 2024
There was a problem hiding this 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.
jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/PythonBytecodeInstruction.java
Show resolved
Hide resolved
...rpreter/src/main/java/ai/timefold/jpyinterpreter/PythonBytecodeToJavaBytecodeTranslator.java
Show resolved
Hide resolved
...rpreter/src/main/java/ai/timefold/jpyinterpreter/PythonBytecodeToJavaBytecodeTranslator.java
Show resolved
Hide resolved
.../src/main/java/ai/timefold/jpyinterpreter/opcodes/controlflow/ReturnConstantValueOpcode.java
Outdated
Show resolved
Hide resolved
...erpreter/src/main/java/ai/timefold/jpyinterpreter/opcodes/descriptor/ObjectOpDescriptor.java
Outdated
Show resolved
Hide resolved
...erpreter/src/main/java/ai/timefold/jpyinterpreter/opcodes/descriptor/ObjectOpDescriptor.java
Outdated
Show resolved
Hide resolved
jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/opcodes/dunder/GetSliceOpcode.java
Show resolved
Hide resolved
...nterpreter/src/main/java/ai/timefold/jpyinterpreter/opcodes/meta/UnaryIntrinsicFunction.java
Show resolved
Hide resolved
jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/PythonSlice.java
Show resolved
Hide resolved
triceo
reviewed
Apr 2, 2024
...nterpreter/src/main/java/ai/timefold/jpyinterpreter/opcodes/meta/UnaryIntrinsicFunction.java
Show resolved
Hide resolved
triceo
approved these changes
Apr 2, 2024
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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]
anda[start:end] = iterable
(previously,BUILD_SLICE was used followed by
GET_ITEM
orSET_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).
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