From 75de28deccba873f8c3566aa5c194f052e620903 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 8 Aug 2024 01:39:40 +0300 Subject: [PATCH] Python 3.13 initial support (#497) * Support for JUMP_BACKWARD * Initial support for Python 3.13 * Remove opcode from another old PR, not needed in the current one * Update bytes/python_3_13.cpp Co-authored-by: Michael Hansen * Update bytecode_ops.inl Co-authored-by: Michael Hansen * Update .gitignore Co-authored-by: Michael Hansen * Update bytecode_ops.inl --------- Co-authored-by: Michael Hansen --- CMakeLists.txt | 1 + bytecode.cpp | 2 + bytecode_ops.inl | 87 +++++++++++++++++ bytes/python_3_13.cpp | 216 ++++++++++++++++++++++++++++++++++++++++++ pyc_module.cpp | 6 ++ pyc_module.h | 1 + 6 files changed, 313 insertions(+) create mode 100644 bytes/python_3_13.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dda56b90..f233ca251 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ add_library(pycxx STATIC bytes/python_3_10.cpp bytes/python_3_11.cpp bytes/python_3_12.cpp + bytes/python_3_13.cpp ) add_executable(pycdas pycdas.cpp) diff --git a/bytecode.cpp b/bytecode.cpp index 54598dda1..703255ff1 100644 --- a/bytecode.cpp +++ b/bytecode.cpp @@ -38,6 +38,7 @@ DECLARE_PYTHON(3, 9) DECLARE_PYTHON(3, 10) DECLARE_PYTHON(3, 11) DECLARE_PYTHON(3, 12) +DECLARE_PYTHON(3, 13) const char* Pyc::OpcodeName(int opcode) { @@ -107,6 +108,7 @@ int Pyc::ByteToOpcode(int maj, int min, int opcode) case 10: return python_3_10_map(opcode); case 11: return python_3_11_map(opcode); case 12: return python_3_12_map(opcode); + case 13: return python_3_13_map(opcode); } break; } diff --git a/bytecode_ops.inl b/bytecode_ops.inl index 0a14b3f94..397f83c9e 100644 --- a/bytecode_ops.inl +++ b/bytecode_ops.inl @@ -118,6 +118,12 @@ OPCODE(RESERVED) // Python 3.12 -> OPCODE(BINARY_SLICE) // Python 3.12 -> OPCODE(STORE_SLICE) // Python 3.12 -> OPCODE(CLEANUP_THROW) // Python 3.12 -> +OPCODE(BINARY_OP_INPLACE_ADD_UNICODE) // Python 3.13 -> +OPCODE(EXIT_INIT_CHECK) // Python 3.13 -> +OPCODE(FORMAT_SIMPLE) // Python 3.13 -> +OPCODE(FORMAT_WITH_SPEC) // Python 3.13 -> +OPCODE(MAKE_FUNCTION) // Python 3.13 -> +OPCODE(TO_BOOL) // Python 3.13 -> /* Has parameter word */ OPCODE_A_FIRST(STORE_NAME) // Python 1.0 -> names[A] @@ -252,6 +258,86 @@ OPCODE_A(CALL_INTRINSIC_1) // Python 3.12 -> intrinsi OPCODE_A(CALL_INTRINSIC_2) // Python 3.12 -> intrinsics_2[A] OPCODE_A(LOAD_FROM_DICT_OR_GLOBALS) // Python 3.12 -> names[A] OPCODE_A(LOAD_FROM_DICT_OR_DEREF) // Python 3.12 -> localsplusnames[A] +OPCODE_A(CALL_KW) // Python 3.13 -> +OPCODE_A(CONVERT_VALUE) // Python 3.13 -> +OPCODE_A(ENTER_EXECUTOR) // Python 3.13 -> +OPCODE_A(LOAD_FAST_LOAD_FAST) // Python 3.13 -> +OPCODE_A(SET_FUNCTION_ATTRIBUTE) // Python 3.13 -> +OPCODE_A(STORE_FAST_LOAD_FAST) // Python 3.13 -> +OPCODE_A(STORE_FAST_STORE_FAST) // Python 3.13 -> +OPCODE_A(BINARY_OP_ADD_FLOAT) +OPCODE_A(BINARY_OP_ADD_INT) +OPCODE_A(BINARY_OP_ADD_UNICODE) +OPCODE_A(BINARY_OP_MULTIPLY_FLOAT) +OPCODE_A(BINARY_OP_MULTIPLY_INT) +OPCODE_A(BINARY_OP_SUBTRACT_FLOAT) +OPCODE_A(BINARY_OP_SUBTRACT_INT) +OPCODE_A(BINARY_SUBSCR_DICT) +OPCODE_A(BINARY_SUBSCR_GETITEM) +OPCODE_A(BINARY_SUBSCR_LIST_INT) +OPCODE_A(BINARY_SUBSCR_STR_INT) +OPCODE_A(BINARY_SUBSCR_TUPLE_INT) +OPCODE_A(CALL_ALLOC_AND_ENTER_INIT) +OPCODE_A(CALL_BOUND_METHOD_EXACT_ARGS) +OPCODE_A(CALL_BOUND_METHOD_GENERAL) +OPCODE_A(CALL_BUILTIN_CLASS) +OPCODE_A(CALL_BUILTIN_FAST) +OPCODE_A(CALL_BUILTIN_FAST_WITH_KEYWORDS) +OPCODE_A(CALL_BUILTIN_O) +OPCODE_A(CALL_ISINSTANCE) +OPCODE_A(CALL_LEN) +OPCODE_A(CALL_LIST_APPEND) +OPCODE_A(CALL_METHOD_DESCRIPTOR_FAST) +OPCODE_A(CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS) +OPCODE_A(CALL_METHOD_DESCRIPTOR_NOARGS) +OPCODE_A(CALL_METHOD_DESCRIPTOR_O) +OPCODE_A(CALL_NON_PY_GENERAL) +OPCODE_A(CALL_PY_EXACT_ARGS) +OPCODE_A(CALL_PY_GENERAL) +OPCODE_A(CALL_STR_1) +OPCODE_A(CALL_TUPLE_1) +OPCODE_A(CALL_TYPE_1) +OPCODE_A(COMPARE_OP_FLOAT) +OPCODE_A(COMPARE_OP_INT) +OPCODE_A(COMPARE_OP_STR) +OPCODE_A(CONTAINS_OP_DICT) +OPCODE_A(CONTAINS_OP_SET) +OPCODE_A(FOR_ITER_GEN) +OPCODE_A(FOR_ITER_LIST) +OPCODE_A(FOR_ITER_RANGE) +OPCODE_A(FOR_ITER_TUPLE) +OPCODE_A(LOAD_ATTR_CLASS) +OPCODE_A(LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN) +OPCODE_A(LOAD_ATTR_INSTANCE_VALUE) +OPCODE_A(LOAD_ATTR_METHOD_LAZY_DICT) +OPCODE_A(LOAD_ATTR_METHOD_NO_DICT) +OPCODE_A(LOAD_ATTR_METHOD_WITH_VALUES) +OPCODE_A(LOAD_ATTR_MODULE) +OPCODE_A(LOAD_ATTR_NONDESCRIPTOR_NO_DICT) +OPCODE_A(LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES) +OPCODE_A(LOAD_ATTR_PROPERTY) +OPCODE_A(LOAD_ATTR_SLOT) +OPCODE_A(LOAD_ATTR_WITH_HINT) +OPCODE_A(LOAD_GLOBAL_BUILTIN) +OPCODE_A(LOAD_GLOBAL_MODULE) +OPCODE_A(LOAD_SUPER_ATTR_ATTR) +OPCODE_A(LOAD_SUPER_ATTR_METHOD) +OPCODE_A(RESUME_CHECK) +OPCODE_A(SEND_GEN) +OPCODE_A(STORE_ATTR_INSTANCE_VALUE) +OPCODE_A(STORE_ATTR_SLOT) +OPCODE_A(STORE_ATTR_WITH_HINT) +OPCODE_A(STORE_SUBSCR_DICT) +OPCODE_A(STORE_SUBSCR_LIST_INT) +OPCODE_A(TO_BOOL_ALWAYS_TRUE) +OPCODE_A(TO_BOOL_BOOL) +OPCODE_A(TO_BOOL_INT) +OPCODE_A(TO_BOOL_LIST) +OPCODE_A(TO_BOOL_NONE) +OPCODE_A(TO_BOOL_STR) +OPCODE_A(UNPACK_SEQUENCE_LIST) +OPCODE_A(UNPACK_SEQUENCE_TUPLE) +OPCODE_A(UNPACK_SEQUENCE_TWO_TUPLE) /* Instrumented opcodes */ OPCODE_A(INSTRUMENTED_LOAD_SUPER_ATTR) // Python 3.12 -> (see LOAD_SUPER_ATTR) @@ -272,3 +358,4 @@ OPCODE_A(INSTRUMENTED_END_FOR) // Python 3.12 -> (see END OPCODE_A(INSTRUMENTED_END_SEND) // Python 3.12 -> (see END_SEND) OPCODE_A(INSTRUMENTED_INSTRUCTION) // Python 3.12 -> ??? OPCODE_A(INSTRUMENTED_LINE) // Python 3.12 -> ??? +OPCODE_A(INSTRUMENTED_CALL_KW) // Python 3.13 -> ??? diff --git a/bytes/python_3_13.cpp b/bytes/python_3_13.cpp new file mode 100644 index 000000000..b8a3829f7 --- /dev/null +++ b/bytes/python_3_13.cpp @@ -0,0 +1,216 @@ +#include "bytecode_map.h" + +BEGIN_MAP(3, 13) + MAP_OP(0, CACHE) + MAP_OP(1, BEFORE_ASYNC_WITH) + MAP_OP(2, BEFORE_WITH) + MAP_OP(3, BINARY_OP_INPLACE_ADD_UNICODE) + MAP_OP(4, BINARY_SLICE) + MAP_OP(5, BINARY_SUBSCR) + MAP_OP(6, CHECK_EG_MATCH) + MAP_OP(7, CHECK_EXC_MATCH) + MAP_OP(8, CLEANUP_THROW) + MAP_OP(9, DELETE_SUBSCR) + MAP_OP(10, END_ASYNC_FOR) + MAP_OP(11, END_FOR) + MAP_OP(12, END_SEND) + MAP_OP(13, EXIT_INIT_CHECK) + MAP_OP(14, FORMAT_SIMPLE) + MAP_OP(15, FORMAT_WITH_SPEC) + MAP_OP(16, GET_AITER) + MAP_OP(17, RESERVED) + MAP_OP(18, GET_ANEXT) + MAP_OP(19, GET_ITER) + MAP_OP(20, GET_LEN) + MAP_OP(21, GET_YIELD_FROM_ITER) + MAP_OP(22, INTERPRETER_EXIT) + MAP_OP(23, LOAD_ASSERTION_ERROR) + MAP_OP(24, LOAD_BUILD_CLASS) + MAP_OP(25, LOAD_LOCALS) + MAP_OP(26, MAKE_FUNCTION) + MAP_OP(27, MATCH_KEYS) + MAP_OP(28, MATCH_MAPPING) + MAP_OP(29, MATCH_SEQUENCE) + MAP_OP(30, NOP) + MAP_OP(31, POP_EXCEPT) + MAP_OP(32, POP_TOP) + MAP_OP(33, PUSH_EXC_INFO) + MAP_OP(34, PUSH_NULL) + MAP_OP(35, RETURN_GENERATOR) + MAP_OP(36, RETURN_VALUE) + MAP_OP(37, SETUP_ANNOTATIONS) + MAP_OP(38, STORE_SLICE) + MAP_OP(39, STORE_SUBSCR) + MAP_OP(40, TO_BOOL) + MAP_OP(41, UNARY_INVERT) + MAP_OP(42, UNARY_NEGATIVE) + MAP_OP(43, UNARY_NOT) + MAP_OP(44, WITH_EXCEPT_START) + MAP_OP(45, BINARY_OP_A) + MAP_OP(46, BUILD_CONST_KEY_MAP_A) + MAP_OP(47, BUILD_LIST_A) + MAP_OP(48, BUILD_MAP_A) + MAP_OP(49, BUILD_SET_A) + MAP_OP(50, BUILD_SLICE_A) + MAP_OP(51, BUILD_STRING_A) + MAP_OP(52, BUILD_TUPLE_A) + MAP_OP(53, CALL_A) + MAP_OP(54, CALL_FUNCTION_EX_A) + MAP_OP(55, CALL_INTRINSIC_1_A) + MAP_OP(56, CALL_INTRINSIC_2_A) + MAP_OP(57, CALL_KW_A) + MAP_OP(58, COMPARE_OP_A) + MAP_OP(59, CONTAINS_OP_A) + MAP_OP(60, CONVERT_VALUE_A) + MAP_OP(61, COPY_A) + MAP_OP(62, COPY_FREE_VARS_A) + MAP_OP(63, DELETE_ATTR_A) + MAP_OP(64, DELETE_DEREF_A) + MAP_OP(65, DELETE_FAST_A) + MAP_OP(66, DELETE_GLOBAL_A) + MAP_OP(67, DELETE_NAME_A) + MAP_OP(68, DICT_MERGE_A) + MAP_OP(69, DICT_UPDATE_A) + MAP_OP(70, ENTER_EXECUTOR_A) + MAP_OP(71, EXTENDED_ARG_A) + MAP_OP(72, FOR_ITER_A) + MAP_OP(73, GET_AWAITABLE_A) + MAP_OP(74, IMPORT_FROM_A) + MAP_OP(75, IMPORT_NAME_A) + MAP_OP(76, IS_OP_A) + MAP_OP(77, JUMP_BACKWARD_A) + MAP_OP(78, JUMP_BACKWARD_NO_INTERRUPT_A) + MAP_OP(79, JUMP_FORWARD_A) + MAP_OP(80, LIST_APPEND_A) + MAP_OP(81, LIST_EXTEND_A) + MAP_OP(82, LOAD_ATTR_A) + MAP_OP(83, LOAD_CONST_A) + MAP_OP(84, LOAD_DEREF_A) + MAP_OP(85, LOAD_FAST_A) + MAP_OP(86, LOAD_FAST_AND_CLEAR_A) + MAP_OP(87, LOAD_FAST_CHECK_A) + MAP_OP(88, LOAD_FAST_LOAD_FAST_A) + MAP_OP(89, LOAD_FROM_DICT_OR_DEREF_A) + MAP_OP(90, LOAD_FROM_DICT_OR_GLOBALS_A) + MAP_OP(91, LOAD_GLOBAL_A) + MAP_OP(92, LOAD_NAME_A) + MAP_OP(93, LOAD_SUPER_ATTR_A) + MAP_OP(94, MAKE_CELL_A) + MAP_OP(95, MAP_ADD_A) + MAP_OP(96, MATCH_CLASS_A) + MAP_OP(97, POP_JUMP_IF_FALSE_A) + MAP_OP(98, POP_JUMP_IF_NONE_A) + MAP_OP(99, POP_JUMP_IF_NOT_NONE_A) + MAP_OP(100, POP_JUMP_IF_TRUE_A) + MAP_OP(101, RAISE_VARARGS_A) + MAP_OP(102, RERAISE_A) + MAP_OP(103, RETURN_CONST_A) + MAP_OP(104, SEND_A) + MAP_OP(105, SET_ADD_A) + MAP_OP(106, SET_FUNCTION_ATTRIBUTE_A) + MAP_OP(107, SET_UPDATE_A) + MAP_OP(108, STORE_ATTR_A) + MAP_OP(109, STORE_DEREF_A) + MAP_OP(110, STORE_FAST_A) + MAP_OP(111, STORE_FAST_LOAD_FAST_A) + MAP_OP(112, STORE_FAST_STORE_FAST_A) + MAP_OP(113, STORE_GLOBAL_A) + MAP_OP(114, STORE_NAME_A) + MAP_OP(115, SWAP_A) + MAP_OP(116, UNPACK_EX_A) + MAP_OP(117, UNPACK_SEQUENCE_A) + MAP_OP(118, YIELD_VALUE_A) + MAP_OP(149, RESUME_A) + MAP_OP(150, BINARY_OP_ADD_FLOAT_A) + MAP_OP(151, BINARY_OP_ADD_INT_A) + MAP_OP(152, BINARY_OP_ADD_UNICODE_A) + MAP_OP(153, BINARY_OP_MULTIPLY_FLOAT_A) + MAP_OP(154, BINARY_OP_MULTIPLY_INT_A) + MAP_OP(155, BINARY_OP_SUBTRACT_FLOAT_A) + MAP_OP(156, BINARY_OP_SUBTRACT_INT_A) + MAP_OP(157, BINARY_SUBSCR_DICT_A) + MAP_OP(158, BINARY_SUBSCR_GETITEM_A) + MAP_OP(159, BINARY_SUBSCR_LIST_INT_A) + MAP_OP(160, BINARY_SUBSCR_STR_INT_A) + MAP_OP(161, BINARY_SUBSCR_TUPLE_INT_A) + MAP_OP(162, CALL_ALLOC_AND_ENTER_INIT_A) + MAP_OP(163, CALL_BOUND_METHOD_EXACT_ARGS_A) + MAP_OP(164, CALL_BOUND_METHOD_GENERAL_A) + MAP_OP(165, CALL_BUILTIN_CLASS_A) + MAP_OP(166, CALL_BUILTIN_FAST_A) + MAP_OP(167, CALL_BUILTIN_FAST_WITH_KEYWORDS_A) + MAP_OP(168, CALL_BUILTIN_O_A) + MAP_OP(169, CALL_ISINSTANCE_A) + MAP_OP(170, CALL_LEN_A) + MAP_OP(171, CALL_LIST_APPEND_A) + MAP_OP(172, CALL_METHOD_DESCRIPTOR_FAST_A) + MAP_OP(173, CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS_A) + MAP_OP(174, CALL_METHOD_DESCRIPTOR_NOARGS_A) + MAP_OP(175, CALL_METHOD_DESCRIPTOR_O_A) + MAP_OP(176, CALL_NON_PY_GENERAL_A) + MAP_OP(177, CALL_PY_EXACT_ARGS_A) + MAP_OP(178, CALL_PY_GENERAL_A) + MAP_OP(179, CALL_STR_1_A) + MAP_OP(180, CALL_TUPLE_1_A) + MAP_OP(181, CALL_TYPE_1_A) + MAP_OP(182, COMPARE_OP_FLOAT_A) + MAP_OP(183, COMPARE_OP_INT_A) + MAP_OP(184, COMPARE_OP_STR_A) + MAP_OP(185, CONTAINS_OP_DICT_A) + MAP_OP(186, CONTAINS_OP_SET_A) + MAP_OP(187, FOR_ITER_GEN_A) + MAP_OP(188, FOR_ITER_LIST_A) + MAP_OP(189, FOR_ITER_RANGE_A) + MAP_OP(190, FOR_ITER_TUPLE_A) + MAP_OP(191, LOAD_ATTR_CLASS_A) + MAP_OP(192, LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN_A) + MAP_OP(193, LOAD_ATTR_INSTANCE_VALUE_A) + MAP_OP(194, LOAD_ATTR_METHOD_LAZY_DICT_A) + MAP_OP(195, LOAD_ATTR_METHOD_NO_DICT_A) + MAP_OP(196, LOAD_ATTR_METHOD_WITH_VALUES_A) + MAP_OP(197, LOAD_ATTR_MODULE_A) + MAP_OP(198, LOAD_ATTR_NONDESCRIPTOR_NO_DICT_A) + MAP_OP(199, LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES_A) + MAP_OP(200, LOAD_ATTR_PROPERTY_A) + MAP_OP(201, LOAD_ATTR_SLOT_A) + MAP_OP(202, LOAD_ATTR_WITH_HINT_A) + MAP_OP(203, LOAD_GLOBAL_BUILTIN_A) + MAP_OP(204, LOAD_GLOBAL_MODULE_A) + MAP_OP(205, LOAD_SUPER_ATTR_ATTR_A) + MAP_OP(206, LOAD_SUPER_ATTR_METHOD_A) + MAP_OP(207, RESUME_CHECK_A) + MAP_OP(208, SEND_GEN_A) + MAP_OP(209, STORE_ATTR_INSTANCE_VALUE_A) + MAP_OP(210, STORE_ATTR_SLOT_A) + MAP_OP(211, STORE_ATTR_WITH_HINT_A) + MAP_OP(212, STORE_SUBSCR_DICT_A) + MAP_OP(213, STORE_SUBSCR_LIST_INT_A) + MAP_OP(214, TO_BOOL_ALWAYS_TRUE_A) + MAP_OP(215, TO_BOOL_BOOL_A) + MAP_OP(216, TO_BOOL_INT_A) + MAP_OP(217, TO_BOOL_LIST_A) + MAP_OP(218, TO_BOOL_NONE_A) + MAP_OP(219, TO_BOOL_STR_A) + MAP_OP(220, UNPACK_SEQUENCE_LIST_A) + MAP_OP(221, UNPACK_SEQUENCE_TUPLE_A) + MAP_OP(222, UNPACK_SEQUENCE_TWO_TUPLE_A) + MAP_OP(236, INSTRUMENTED_RESUME_A) + MAP_OP(237, INSTRUMENTED_END_FOR_A) + MAP_OP(238, INSTRUMENTED_END_SEND_A) + MAP_OP(239, INSTRUMENTED_RETURN_VALUE_A) + MAP_OP(240, INSTRUMENTED_RETURN_CONST_A) + MAP_OP(241, INSTRUMENTED_YIELD_VALUE_A) + MAP_OP(242, INSTRUMENTED_LOAD_SUPER_ATTR_A) + MAP_OP(243, INSTRUMENTED_FOR_ITER_A) + MAP_OP(244, INSTRUMENTED_CALL_A) + MAP_OP(245, INSTRUMENTED_CALL_KW_A) + MAP_OP(246, INSTRUMENTED_CALL_FUNCTION_EX_A) + MAP_OP(247, INSTRUMENTED_INSTRUCTION_A) + MAP_OP(248, INSTRUMENTED_JUMP_FORWARD_A) + MAP_OP(249, INSTRUMENTED_JUMP_BACKWARD_A) + MAP_OP(250, INSTRUMENTED_POP_JUMP_IF_TRUE_A) + MAP_OP(251, INSTRUMENTED_POP_JUMP_IF_FALSE_A) + MAP_OP(252, INSTRUMENTED_POP_JUMP_IF_NONE_A) + MAP_OP(253, INSTRUMENTED_POP_JUMP_IF_NOT_NONE_A) + MAP_OP(254, INSTRUMENTED_LINE_A) +END_MAP() diff --git a/pyc_module.cpp b/pyc_module.cpp index 6f430fab4..98eecffe9 100644 --- a/pyc_module.cpp +++ b/pyc_module.cpp @@ -176,6 +176,12 @@ void PycModule::setVersion(unsigned int magic) m_unicode = true; break; + case MAGIC_3_13: + m_maj = 3; + m_min = 13; + m_unicode = true; + break; + /* Bad Magic detected */ default: m_maj = -1; diff --git a/pyc_module.h b/pyc_module.h index 888099832..695f4ba48 100644 --- a/pyc_module.h +++ b/pyc_module.h @@ -35,6 +35,7 @@ enum PycMagic { MAGIC_3_10 = 0x0A0D0D6F, MAGIC_3_11 = 0x0A0D0DA7, MAGIC_3_12 = 0x0A0D0DCB, + MAGIC_3_13 = 0x0A0D0DF3, INVALID = 0, };