GH-77273: Better bytecodes for f-strings (GH-6132)

This commit is contained in:
Mark Shannon 2023-06-14 16:15:08 +01:00 committed by GitHub
parent 307bceaa65
commit 1d857da7f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 525 additions and 485 deletions

View File

@ -1465,26 +1465,47 @@ iterations of the loop.
an argument from two-byte to four-byte.
.. opcode:: FORMAT_VALUE (flags)
.. opcode:: CONVERT_VALUE (oparg)
Used for implementing formatted literal strings (f-strings). Pops
an optional *fmt_spec* from the stack, then a required *value*.
*flags* is interpreted as follows:
Convert value to a string, depending on ``oparg``::
* ``(flags & 0x03) == 0x00``: *value* is formatted as-is.
* ``(flags & 0x03) == 0x01``: call :func:`str` on *value* before
formatting it.
* ``(flags & 0x03) == 0x02``: call :func:`repr` on *value* before
formatting it.
* ``(flags & 0x03) == 0x03``: call :func:`ascii` on *value* before
formatting it.
* ``(flags & 0x04) == 0x04``: pop *fmt_spec* from the stack and use
it, else use an empty *fmt_spec*.
value = STACK.pop()
result = func(value)
STACK.push(result)
Formatting is performed using :c:func:`PyObject_Format`. The
result is pushed on the stack.
* ``oparg == 1``: call :func:`str` on *value*
* ``oparg == 2``: call :func:`repr` on *value*
* ``oparg == 3``: call :func:`ascii` on *value*
.. versionadded:: 3.6
Used for implementing formatted literal strings (f-strings).
.. versionadded:: 3.13
.. opcode:: FORMAT_SIMPLE
Formats the value on top of stack::
value = STACK.pop()
result = value.__format__("")
STACK.push(result)
Used for implementing formatted literal strings (f-strings).
.. versionadded:: 3.13
.. opcode:: FORMAT_SPEC
Formats the given value with the given format spec::
spec = STACK.pop()
value = STACK.pop()
result = value.__format__(spec)
STACK.push(result)
Used for implementing formatted literal strings (f-strings).
.. versionadded:: 3.13
.. opcode:: MATCH_CLASS (count)

View File

@ -102,6 +102,7 @@ const uint8_t _PyOpcode_Deopt[256] = {
[COMPARE_OP_INT] = COMPARE_OP,
[COMPARE_OP_STR] = COMPARE_OP,
[CONTAINS_OP] = CONTAINS_OP,
[CONVERT_VALUE] = CONVERT_VALUE,
[COPY] = COPY,
[COPY_FREE_VARS] = COPY_FREE_VARS,
[DELETE_ATTR] = DELETE_ATTR,
@ -117,7 +118,8 @@ const uint8_t _PyOpcode_Deopt[256] = {
[END_SEND] = END_SEND,
[ENTER_EXECUTOR] = ENTER_EXECUTOR,
[EXTENDED_ARG] = EXTENDED_ARG,
[FORMAT_VALUE] = FORMAT_VALUE,
[FORMAT_SIMPLE] = FORMAT_SIMPLE,
[FORMAT_WITH_SPEC] = FORMAT_WITH_SPEC,
[FOR_ITER] = FOR_ITER,
[FOR_ITER_GEN] = FOR_ITER,
[FOR_ITER_LIST] = FOR_ITER,
@ -286,6 +288,8 @@ static const char *const _PyOpcode_OpName[267] = {
[CHECK_EG_MATCH] = "CHECK_EG_MATCH",
[CALL_BUILTIN_FAST_WITH_KEYWORDS] = "CALL_BUILTIN_FAST_WITH_KEYWORDS",
[CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS] = "CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS",
[FORMAT_SIMPLE] = "FORMAT_SIMPLE",
[FORMAT_WITH_SPEC] = "FORMAT_WITH_SPEC",
[CALL_NO_KW_BUILTIN_FAST] = "CALL_NO_KW_BUILTIN_FAST",
[CALL_NO_KW_BUILTIN_O] = "CALL_NO_KW_BUILTIN_O",
[CALL_NO_KW_ISINSTANCE] = "CALL_NO_KW_ISINSTANCE",
@ -293,8 +297,6 @@ static const char *const _PyOpcode_OpName[267] = {
[CALL_NO_KW_LIST_APPEND] = "CALL_NO_KW_LIST_APPEND",
[CALL_NO_KW_METHOD_DESCRIPTOR_FAST] = "CALL_NO_KW_METHOD_DESCRIPTOR_FAST",
[CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS] = "CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS",
[CALL_NO_KW_METHOD_DESCRIPTOR_O] = "CALL_NO_KW_METHOD_DESCRIPTOR_O",
[CALL_NO_KW_STR_1] = "CALL_NO_KW_STR_1",
[WITH_EXCEPT_START] = "WITH_EXCEPT_START",
[GET_AITER] = "GET_AITER",
[GET_ANEXT] = "GET_ANEXT",
@ -302,39 +304,39 @@ static const char *const _PyOpcode_OpName[267] = {
[BEFORE_WITH] = "BEFORE_WITH",
[END_ASYNC_FOR] = "END_ASYNC_FOR",
[CLEANUP_THROW] = "CLEANUP_THROW",
[CALL_NO_KW_METHOD_DESCRIPTOR_O] = "CALL_NO_KW_METHOD_DESCRIPTOR_O",
[CALL_NO_KW_STR_1] = "CALL_NO_KW_STR_1",
[CALL_NO_KW_TUPLE_1] = "CALL_NO_KW_TUPLE_1",
[CALL_NO_KW_TYPE_1] = "CALL_NO_KW_TYPE_1",
[COMPARE_OP_FLOAT] = "COMPARE_OP_FLOAT",
[COMPARE_OP_INT] = "COMPARE_OP_INT",
[STORE_SUBSCR] = "STORE_SUBSCR",
[DELETE_SUBSCR] = "DELETE_SUBSCR",
[COMPARE_OP_FLOAT] = "COMPARE_OP_FLOAT",
[COMPARE_OP_INT] = "COMPARE_OP_INT",
[COMPARE_OP_STR] = "COMPARE_OP_STR",
[FOR_ITER_LIST] = "FOR_ITER_LIST",
[FOR_ITER_TUPLE] = "FOR_ITER_TUPLE",
[FOR_ITER_RANGE] = "FOR_ITER_RANGE",
[FOR_ITER_GEN] = "FOR_ITER_GEN",
[LOAD_SUPER_ATTR_ATTR] = "LOAD_SUPER_ATTR_ATTR",
[GET_ITER] = "GET_ITER",
[GET_YIELD_FROM_ITER] = "GET_YIELD_FROM_ITER",
[LOAD_SUPER_ATTR_METHOD] = "LOAD_SUPER_ATTR_METHOD",
[FOR_ITER_GEN] = "FOR_ITER_GEN",
[LOAD_BUILD_CLASS] = "LOAD_BUILD_CLASS",
[LOAD_ATTR_CLASS] = "LOAD_ATTR_CLASS",
[LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN] = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN",
[LOAD_SUPER_ATTR_ATTR] = "LOAD_SUPER_ATTR_ATTR",
[LOAD_SUPER_ATTR_METHOD] = "LOAD_SUPER_ATTR_METHOD",
[LOAD_ASSERTION_ERROR] = "LOAD_ASSERTION_ERROR",
[RETURN_GENERATOR] = "RETURN_GENERATOR",
[LOAD_ATTR_CLASS] = "LOAD_ATTR_CLASS",
[LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN] = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN",
[LOAD_ATTR_INSTANCE_VALUE] = "LOAD_ATTR_INSTANCE_VALUE",
[LOAD_ATTR_MODULE] = "LOAD_ATTR_MODULE",
[LOAD_ATTR_PROPERTY] = "LOAD_ATTR_PROPERTY",
[LOAD_ATTR_SLOT] = "LOAD_ATTR_SLOT",
[LOAD_ATTR_WITH_HINT] = "LOAD_ATTR_WITH_HINT",
[LOAD_ATTR_METHOD_LAZY_DICT] = "LOAD_ATTR_METHOD_LAZY_DICT",
[LOAD_ATTR_METHOD_NO_DICT] = "LOAD_ATTR_METHOD_NO_DICT",
[RETURN_VALUE] = "RETURN_VALUE",
[LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES",
[LOAD_ATTR_METHOD_LAZY_DICT] = "LOAD_ATTR_METHOD_LAZY_DICT",
[SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS",
[LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN",
[LOAD_ATTR_METHOD_NO_DICT] = "LOAD_ATTR_METHOD_NO_DICT",
[LOAD_LOCALS] = "LOAD_LOCALS",
[LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE",
[LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES",
[POP_EXCEPT] = "POP_EXCEPT",
[STORE_NAME] = "STORE_NAME",
[DELETE_NAME] = "DELETE_NAME",
@ -357,9 +359,9 @@ static const char *const _PyOpcode_OpName[267] = {
[IMPORT_NAME] = "IMPORT_NAME",
[IMPORT_FROM] = "IMPORT_FROM",
[JUMP_FORWARD] = "JUMP_FORWARD",
[LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN",
[LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE",
[STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE",
[STORE_ATTR_SLOT] = "STORE_ATTR_SLOT",
[STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT",
[POP_JUMP_IF_FALSE] = "POP_JUMP_IF_FALSE",
[POP_JUMP_IF_TRUE] = "POP_JUMP_IF_TRUE",
[LOAD_GLOBAL] = "LOAD_GLOBAL",
@ -378,7 +380,7 @@ static const char *const _PyOpcode_OpName[267] = {
[POP_JUMP_IF_NONE] = "POP_JUMP_IF_NONE",
[RAISE_VARARGS] = "RAISE_VARARGS",
[GET_AWAITABLE] = "GET_AWAITABLE",
[STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT",
[STORE_ATTR_SLOT] = "STORE_ATTR_SLOT",
[BUILD_SLICE] = "BUILD_SLICE",
[JUMP_BACKWARD_NO_INTERRUPT] = "JUMP_BACKWARD_NO_INTERRUPT",
[MAKE_CELL] = "MAKE_CELL",
@ -394,20 +396,20 @@ static const char *const _PyOpcode_OpName[267] = {
[LIST_APPEND] = "LIST_APPEND",
[SET_ADD] = "SET_ADD",
[MAP_ADD] = "MAP_ADD",
[STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT",
[STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT",
[COPY_FREE_VARS] = "COPY_FREE_VARS",
[YIELD_VALUE] = "YIELD_VALUE",
[RESUME] = "RESUME",
[MATCH_CLASS] = "MATCH_CLASS",
[STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT",
[STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT",
[UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST",
[UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE",
[FORMAT_VALUE] = "FORMAT_VALUE",
[BUILD_CONST_KEY_MAP] = "BUILD_CONST_KEY_MAP",
[BUILD_STRING] = "BUILD_STRING",
[CONVERT_VALUE] = "CONVERT_VALUE",
[UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE",
[UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE",
[SEND_GEN] = "SEND_GEN",
[160] = "<160>",
[161] = "<161>",
[LIST_EXTEND] = "LIST_EXTEND",
[SET_UPDATE] = "SET_UPDATE",
[DICT_MERGE] = "DICT_MERGE",
@ -517,8 +519,6 @@ static const char *const _PyOpcode_OpName[267] = {
#endif
#define EXTRA_CASES \
case 160: \
case 161: \
case 166: \
case 167: \
case 178: \

86
Include/opcode.h generated
View File

@ -30,6 +30,8 @@ extern "C" {
#define PUSH_EXC_INFO 35
#define CHECK_EXC_MATCH 36
#define CHECK_EG_MATCH 37
#define FORMAT_SIMPLE 40
#define FORMAT_WITH_SPEC 41
#define WITH_EXCEPT_START 49
#define GET_AITER 50
#define GET_ANEXT 51
@ -107,9 +109,9 @@ extern "C" {
#define YIELD_VALUE 150
#define RESUME 151
#define MATCH_CLASS 152
#define FORMAT_VALUE 155
#define BUILD_CONST_KEY_MAP 156
#define BUILD_STRING 157
#define CONVERT_VALUE 158
#define LIST_EXTEND 162
#define SET_UPDATE 163
#define DICT_MERGE 164
@ -175,47 +177,47 @@ extern "C" {
#define CALL_BUILTIN_CLASS 34
#define CALL_BUILTIN_FAST_WITH_KEYWORDS 38
#define CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS 39
#define CALL_NO_KW_BUILTIN_FAST 40
#define CALL_NO_KW_BUILTIN_O 41
#define CALL_NO_KW_ISINSTANCE 42
#define CALL_NO_KW_LEN 43
#define CALL_NO_KW_LIST_APPEND 44
#define CALL_NO_KW_METHOD_DESCRIPTOR_FAST 45
#define CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS 46
#define CALL_NO_KW_METHOD_DESCRIPTOR_O 47
#define CALL_NO_KW_STR_1 48
#define CALL_NO_KW_TUPLE_1 56
#define CALL_NO_KW_TYPE_1 57
#define COMPARE_OP_FLOAT 58
#define COMPARE_OP_INT 59
#define COMPARE_OP_STR 62
#define FOR_ITER_LIST 63
#define FOR_ITER_TUPLE 64
#define FOR_ITER_RANGE 65
#define FOR_ITER_GEN 66
#define LOAD_SUPER_ATTR_ATTR 67
#define LOAD_SUPER_ATTR_METHOD 70
#define LOAD_ATTR_CLASS 72
#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 73
#define LOAD_ATTR_INSTANCE_VALUE 76
#define LOAD_ATTR_MODULE 77
#define LOAD_ATTR_PROPERTY 78
#define LOAD_ATTR_SLOT 79
#define LOAD_ATTR_WITH_HINT 80
#define LOAD_ATTR_METHOD_LAZY_DICT 81
#define LOAD_ATTR_METHOD_NO_DICT 82
#define LOAD_ATTR_METHOD_WITH_VALUES 84
#define LOAD_GLOBAL_BUILTIN 86
#define LOAD_GLOBAL_MODULE 88
#define STORE_ATTR_INSTANCE_VALUE 111
#define STORE_ATTR_SLOT 112
#define STORE_ATTR_WITH_HINT 113
#define STORE_SUBSCR_DICT 132
#define STORE_SUBSCR_LIST_INT 148
#define UNPACK_SEQUENCE_LIST 153
#define UNPACK_SEQUENCE_TUPLE 154
#define UNPACK_SEQUENCE_TWO_TUPLE 158
#define SEND_GEN 159
#define CALL_NO_KW_BUILTIN_FAST 42
#define CALL_NO_KW_BUILTIN_O 43
#define CALL_NO_KW_ISINSTANCE 44
#define CALL_NO_KW_LEN 45
#define CALL_NO_KW_LIST_APPEND 46
#define CALL_NO_KW_METHOD_DESCRIPTOR_FAST 47
#define CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS 48
#define CALL_NO_KW_METHOD_DESCRIPTOR_O 56
#define CALL_NO_KW_STR_1 57
#define CALL_NO_KW_TUPLE_1 58
#define CALL_NO_KW_TYPE_1 59
#define COMPARE_OP_FLOAT 62
#define COMPARE_OP_INT 63
#define COMPARE_OP_STR 64
#define FOR_ITER_LIST 65
#define FOR_ITER_TUPLE 66
#define FOR_ITER_RANGE 67
#define FOR_ITER_GEN 70
#define LOAD_SUPER_ATTR_ATTR 72
#define LOAD_SUPER_ATTR_METHOD 73
#define LOAD_ATTR_CLASS 76
#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 77
#define LOAD_ATTR_INSTANCE_VALUE 78
#define LOAD_ATTR_MODULE 79
#define LOAD_ATTR_PROPERTY 80
#define LOAD_ATTR_SLOT 81
#define LOAD_ATTR_WITH_HINT 82
#define LOAD_ATTR_METHOD_LAZY_DICT 84
#define LOAD_ATTR_METHOD_NO_DICT 86
#define LOAD_ATTR_METHOD_WITH_VALUES 88
#define LOAD_GLOBAL_BUILTIN 111
#define LOAD_GLOBAL_MODULE 112
#define STORE_ATTR_INSTANCE_VALUE 113
#define STORE_ATTR_SLOT 132
#define STORE_ATTR_WITH_HINT 148
#define STORE_SUBSCR_DICT 153
#define STORE_SUBSCR_LIST_INT 154
#define UNPACK_SEQUENCE_LIST 155
#define UNPACK_SEQUENCE_TUPLE 159
#define UNPACK_SEQUENCE_TWO_TUPLE 160
#define SEND_GEN 161
#define HAS_ARG(op) ((((op) >= HAVE_ARGUMENT) && (!IS_PSEUDO_OPCODE(op)))\
|| ((op) == JUMP) \

View File

@ -25,13 +25,8 @@ del _opcodes_all
_have_code = (types.MethodType, types.FunctionType, types.CodeType,
classmethod, staticmethod, type)
FORMAT_VALUE = opmap['FORMAT_VALUE']
FORMAT_VALUE_CONVERTERS = (
(None, ''),
(str, 'str'),
(repr, 'repr'),
(ascii, 'ascii'),
)
CONVERT_VALUE = opmap['CONVERT_VALUE']
SET_FUNCTION_ATTRIBUTE = opmap['SET_FUNCTION_ATTRIBUTE']
FUNCTION_ATTR_FLAGS = ('defaults', 'kwdefaults', 'annotations', 'closure')
@ -579,13 +574,9 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
elif deop in hascompare:
argval = cmp_op[arg>>4]
argrepr = argval
elif deop == FORMAT_VALUE:
argval, argrepr = FORMAT_VALUE_CONVERTERS[arg & 0x3]
argval = (argval, bool(arg & 0x4))
if argval[1]:
if argrepr:
argrepr += ', '
argrepr += 'with format'
elif deop == CONVERT_VALUE:
argval = (None, str, repr, ascii)[arg]
argrepr = ('', 'str', 'repr', 'ascii')[arg]
elif deop == SET_FUNCTION_ATTRIBUTE:
argrepr = ', '.join(s for i, s in enumerate(FUNCTION_ATTR_FLAGS)
if arg & (1<<i))

View File

@ -447,7 +447,9 @@ _code_type = type(_write_atomic.__code__)
# Python 3.12b1 3531 (Add PEP 695 changes)
# Python 3.13a1 3550 (Plugin optimizer support)
# Python 3.13a1 3551 (Compact superinstructions)
# Python 3.13a1 3552 (Add SET_FUNCTION_ATTRIBUTE)
# Python 3.13a1 3552 (Remove LOAD_FAST__LOAD_CONST and LOAD_CONST__LOAD_FAST)
# Python 3.13a1 3553 (Add SET_FUNCTION_ATTRIBUTE)
# Python 3.13a1 3554 (more efficient bytecodes for f-strings)
# Python 3.14 will start with 3600
@ -464,7 +466,7 @@ _code_type = type(_write_atomic.__code__)
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.
MAGIC_NUMBER = (3552).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3554).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

View File

@ -110,6 +110,9 @@ def_op('PUSH_EXC_INFO', 35)
def_op('CHECK_EXC_MATCH', 36)
def_op('CHECK_EG_MATCH', 37)
def_op('FORMAT_SIMPLE', 40)
def_op('FORMAT_WITH_SPEC', 41)
def_op('WITH_EXCEPT_START', 49)
def_op('GET_AITER', 50)
def_op('GET_ANEXT', 51)
@ -213,9 +216,9 @@ def_op('YIELD_VALUE', 150)
def_op('RESUME', 151) # This must be kept in sync with deepfreeze.py
def_op('MATCH_CLASS', 152)
def_op('FORMAT_VALUE', 155)
def_op('BUILD_CONST_KEY_MAP', 156)
def_op('BUILD_STRING', 157)
def_op('CONVERT_VALUE', 158)
def_op('LIST_EXTEND', 162)
def_op('SET_UPDATE', 163)

View File

@ -443,18 +443,20 @@ dis_fstring = """\
%3d RESUME 0
%3d LOAD_FAST 0 (a)
FORMAT_VALUE 0
FORMAT_SIMPLE
LOAD_CONST 1 (' ')
LOAD_FAST 1 (b)
LOAD_CONST 2 ('4')
FORMAT_VALUE 4 (with format)
FORMAT_WITH_SPEC
LOAD_CONST 1 (' ')
LOAD_FAST 2 (c)
FORMAT_VALUE 2 (repr)
CONVERT_VALUE 2 (repr)
FORMAT_SIMPLE
LOAD_CONST 1 (' ')
LOAD_FAST 3 (d)
CONVERT_VALUE 2 (repr)
LOAD_CONST 2 ('4')
FORMAT_VALUE 6 (repr, with format)
FORMAT_WITH_SPEC
BUILD_STRING 7
RETURN_VALUE
""" % (_fstring.__code__.co_firstlineno, _fstring.__code__.co_firstlineno + 1)

View File

@ -0,0 +1,3 @@
Simplify and speed up interpreter for f-strings. Removes ``FORMAT_VALUE``
opcode. Add ``CONVERT_VALUE``, ``FORMAT_SIMPLE`` and ``FORMAT_WITH_SPEC``
opcode. Compiler emits more efficient sequence for each format expression.

View File

@ -9,8 +9,8 @@ unsigned char M_test_frozenmain[] = {
1,0,2,0,101,1,106,8,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,171,0,0,0,0,0,
0,0,100,4,25,0,0,0,90,5,100,5,68,0,93,20,
0,0,90,6,2,0,101,2,100,6,101,6,155,0,100,7,
101,5,101,6,25,0,0,0,155,0,157,4,171,1,0,0,
0,0,90,6,2,0,101,2,100,6,101,6,40,0,100,7,
101,5,101,6,25,0,0,0,40,0,157,4,171,1,0,0,
0,0,0,0,1,0,140,22,0,0,4,0,121,1,41,8,
233,0,0,0,0,78,122,18,70,114,111,122,101,110,32,72,
101,108,108,111,32,87,111,114,108,100,122,8,115,121,115,46,

View File

@ -52,6 +52,8 @@
#define family(name, ...) static int family_##name
#define pseudo(name) static int pseudo_##name
typedef PyObject *(*convertion_func_ptr)(PyObject *);
// Dummy variables for stack effects.
static PyObject *value, *value1, *value2, *left, *right, *res, *sum, *prod, *sub;
static PyObject *container, *start, *stop, *v, *lhs, *rhs, *res2;
@ -3367,41 +3369,33 @@ dummy_func(
ERROR_IF(slice == NULL, error);
}
inst(FORMAT_VALUE, (value, fmt_spec if ((oparg & FVS_MASK) == FVS_HAVE_SPEC) -- result)) {
/* Handles f-string value formatting. */
PyObject *(*conv_fn)(PyObject *);
int which_conversion = oparg & FVC_MASK;
/* See if any conversion is specified. */
switch (which_conversion) {
case FVC_NONE: conv_fn = NULL; break;
case FVC_STR: conv_fn = PyObject_Str; break;
case FVC_REPR: conv_fn = PyObject_Repr; break;
case FVC_ASCII: conv_fn = PyObject_ASCII; break;
default:
_PyErr_Format(tstate, PyExc_SystemError,
"unexpected conversion flag %d",
which_conversion);
goto error;
}
/* If there's a conversion function, call it and replace
value with that result. Otherwise, just use value,
without conversion. */
if (conv_fn != NULL) {
inst(CONVERT_VALUE, (value -- result)) {
convertion_func_ptr conv_fn;
assert(oparg >= FVC_STR && oparg <= FVC_ASCII);
conv_fn = CONVERSION_FUNCTIONS[oparg];
result = conv_fn(value);
Py_DECREF(value);
if (result == NULL) {
Py_XDECREF(fmt_spec);
ERROR_IF(true, error);
}
value = result;
ERROR_IF(result == NULL, error);
}
result = PyObject_Format(value, fmt_spec);
inst(FORMAT_SIMPLE, (value -- res)) {
/* If value is a unicode object, then we know the result
* of format(value) is value itself. */
if (!PyUnicode_CheckExact(value)) {
res = PyObject_Format(value, NULL);
Py_DECREF(value);
Py_XDECREF(fmt_spec);
ERROR_IF(result == NULL, error);
ERROR_IF(res == NULL, error);
}
else {
res = value;
}
}
inst(FORMAT_WITH_SPEC, (value, fmt_spec -- res)) {
res = PyObject_Format(value, fmt_spec);
Py_DECREF(value);
Py_DECREF(fmt_spec);
ERROR_IF(res == NULL, error);
}
inst(COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {

View File

@ -222,6 +222,14 @@ _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, PyFunctionObject *func,
static void
_PyEvalFrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame);
typedef PyObject *(*convertion_func_ptr)(PyObject *);
static const convertion_func_ptr CONVERSION_FUNCTIONS[4] = {
[FVC_STR] = PyObject_Str,
[FVC_REPR] = PyObject_Repr,
[FVC_ASCII] = PyObject_ASCII
};
#define UNBOUNDLOCAL_ERROR_MSG \
"cannot access local variable '%s' where it is not associated with a value"
#define UNBOUNDFREE_ERROR_MSG \

View File

@ -4985,26 +4985,26 @@ compiler_formatted_value(struct compiler *c, expr_ty e)
/* The expression to be formatted. */
VISIT(c, expr, e->v.FormattedValue.value);
location loc = LOC(e);
if (conversion != -1) {
switch (conversion) {
case 's': oparg = FVC_STR; break;
case 'r': oparg = FVC_REPR; break;
case 'a': oparg = FVC_ASCII; break;
case -1: oparg = FVC_NONE; break;
default:
PyErr_Format(PyExc_SystemError,
"Unrecognized conversion character %d", conversion);
return ERROR;
}
ADDOP_I(c, loc, CONVERT_VALUE, oparg);
}
if (e->v.FormattedValue.format_spec) {
/* Evaluate the format spec, and update our opcode arg. */
VISIT(c, expr, e->v.FormattedValue.format_spec);
oparg |= FVS_HAVE_SPEC;
ADDOP(c, loc, FORMAT_WITH_SPEC);
} else {
ADDOP(c, loc, FORMAT_SIMPLE);
}
/* And push our opcode and oparg */
location loc = LOC(e);
ADDOP_I(c, loc, FORMAT_VALUE, oparg);
return SUCCESS;
}

File diff suppressed because it is too large Load Diff

View File

@ -402,8 +402,12 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
return 0;
case BUILD_SLICE:
return ((oparg == 3) ? 1 : 0) + 2;
case FORMAT_VALUE:
return (((oparg & FVS_MASK) == FVS_HAVE_SPEC) ? 1 : 0) + 1;
case CONVERT_VALUE:
return 1;
case FORMAT_SIMPLE:
return 1;
case FORMAT_WITH_SPEC:
return 2;
case COPY:
return (oparg-1) + 1;
case BINARY_OP:
@ -820,7 +824,11 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
return 0;
case BUILD_SLICE:
return 1;
case FORMAT_VALUE:
case CONVERT_VALUE:
return 1;
case FORMAT_SIMPLE:
return 1;
case FORMAT_WITH_SPEC:
return 1;
case COPY:
return (oparg-1) + 2;
@ -1064,7 +1072,9 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[512] = {
[SET_FUNCTION_ATTRIBUTE] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[RETURN_GENERATOR] = { true, INSTR_FMT_IX, 0 },
[BUILD_SLICE] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[FORMAT_VALUE] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[CONVERT_VALUE] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[FORMAT_SIMPLE] = { true, INSTR_FMT_IX, 0 },
[FORMAT_WITH_SPEC] = { true, INSTR_FMT_IX, 0 },
[COPY] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[BINARY_OP] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG },
[SWAP] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },

View File

@ -39,6 +39,8 @@ static void *opcode_targets[256] = {
&&TARGET_CHECK_EG_MATCH,
&&TARGET_CALL_BUILTIN_FAST_WITH_KEYWORDS,
&&TARGET_CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS,
&&TARGET_FORMAT_SIMPLE,
&&TARGET_FORMAT_WITH_SPEC,
&&TARGET_CALL_NO_KW_BUILTIN_FAST,
&&TARGET_CALL_NO_KW_BUILTIN_O,
&&TARGET_CALL_NO_KW_ISINSTANCE,
@ -46,8 +48,6 @@ static void *opcode_targets[256] = {
&&TARGET_CALL_NO_KW_LIST_APPEND,
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_FAST,
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS,
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_O,
&&TARGET_CALL_NO_KW_STR_1,
&&TARGET_WITH_EXCEPT_START,
&&TARGET_GET_AITER,
&&TARGET_GET_ANEXT,
@ -55,39 +55,39 @@ static void *opcode_targets[256] = {
&&TARGET_BEFORE_WITH,
&&TARGET_END_ASYNC_FOR,
&&TARGET_CLEANUP_THROW,
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_O,
&&TARGET_CALL_NO_KW_STR_1,
&&TARGET_CALL_NO_KW_TUPLE_1,
&&TARGET_CALL_NO_KW_TYPE_1,
&&TARGET_COMPARE_OP_FLOAT,
&&TARGET_COMPARE_OP_INT,
&&TARGET_STORE_SUBSCR,
&&TARGET_DELETE_SUBSCR,
&&TARGET_COMPARE_OP_FLOAT,
&&TARGET_COMPARE_OP_INT,
&&TARGET_COMPARE_OP_STR,
&&TARGET_FOR_ITER_LIST,
&&TARGET_FOR_ITER_TUPLE,
&&TARGET_FOR_ITER_RANGE,
&&TARGET_FOR_ITER_GEN,
&&TARGET_LOAD_SUPER_ATTR_ATTR,
&&TARGET_GET_ITER,
&&TARGET_GET_YIELD_FROM_ITER,
&&TARGET_LOAD_SUPER_ATTR_METHOD,
&&TARGET_FOR_ITER_GEN,
&&TARGET_LOAD_BUILD_CLASS,
&&TARGET_LOAD_ATTR_CLASS,
&&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN,
&&TARGET_LOAD_SUPER_ATTR_ATTR,
&&TARGET_LOAD_SUPER_ATTR_METHOD,
&&TARGET_LOAD_ASSERTION_ERROR,
&&TARGET_RETURN_GENERATOR,
&&TARGET_LOAD_ATTR_CLASS,
&&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN,
&&TARGET_LOAD_ATTR_INSTANCE_VALUE,
&&TARGET_LOAD_ATTR_MODULE,
&&TARGET_LOAD_ATTR_PROPERTY,
&&TARGET_LOAD_ATTR_SLOT,
&&TARGET_LOAD_ATTR_WITH_HINT,
&&TARGET_LOAD_ATTR_METHOD_LAZY_DICT,
&&TARGET_LOAD_ATTR_METHOD_NO_DICT,
&&TARGET_RETURN_VALUE,
&&TARGET_LOAD_ATTR_METHOD_WITH_VALUES,
&&TARGET_LOAD_ATTR_METHOD_LAZY_DICT,
&&TARGET_SETUP_ANNOTATIONS,
&&TARGET_LOAD_GLOBAL_BUILTIN,
&&TARGET_LOAD_ATTR_METHOD_NO_DICT,
&&TARGET_LOAD_LOCALS,
&&TARGET_LOAD_GLOBAL_MODULE,
&&TARGET_LOAD_ATTR_METHOD_WITH_VALUES,
&&TARGET_POP_EXCEPT,
&&TARGET_STORE_NAME,
&&TARGET_DELETE_NAME,
@ -110,9 +110,9 @@ static void *opcode_targets[256] = {
&&TARGET_IMPORT_NAME,
&&TARGET_IMPORT_FROM,
&&TARGET_JUMP_FORWARD,
&&TARGET_LOAD_GLOBAL_BUILTIN,
&&TARGET_LOAD_GLOBAL_MODULE,
&&TARGET_STORE_ATTR_INSTANCE_VALUE,
&&TARGET_STORE_ATTR_SLOT,
&&TARGET_STORE_ATTR_WITH_HINT,
&&TARGET_POP_JUMP_IF_FALSE,
&&TARGET_POP_JUMP_IF_TRUE,
&&TARGET_LOAD_GLOBAL,
@ -131,7 +131,7 @@ static void *opcode_targets[256] = {
&&TARGET_POP_JUMP_IF_NONE,
&&TARGET_RAISE_VARARGS,
&&TARGET_GET_AWAITABLE,
&&TARGET_STORE_SUBSCR_DICT,
&&TARGET_STORE_ATTR_SLOT,
&&TARGET_BUILD_SLICE,
&&TARGET_JUMP_BACKWARD_NO_INTERRUPT,
&&TARGET_MAKE_CELL,
@ -147,20 +147,20 @@ static void *opcode_targets[256] = {
&&TARGET_LIST_APPEND,
&&TARGET_SET_ADD,
&&TARGET_MAP_ADD,
&&TARGET_STORE_SUBSCR_LIST_INT,
&&TARGET_STORE_ATTR_WITH_HINT,
&&TARGET_COPY_FREE_VARS,
&&TARGET_YIELD_VALUE,
&&TARGET_RESUME,
&&TARGET_MATCH_CLASS,
&&TARGET_STORE_SUBSCR_DICT,
&&TARGET_STORE_SUBSCR_LIST_INT,
&&TARGET_UNPACK_SEQUENCE_LIST,
&&TARGET_UNPACK_SEQUENCE_TUPLE,
&&TARGET_FORMAT_VALUE,
&&TARGET_BUILD_CONST_KEY_MAP,
&&TARGET_BUILD_STRING,
&&TARGET_CONVERT_VALUE,
&&TARGET_UNPACK_SEQUENCE_TUPLE,
&&TARGET_UNPACK_SEQUENCE_TWO_TUPLE,
&&TARGET_SEND_GEN,
&&_unknown_opcode,
&&_unknown_opcode,
&&TARGET_LIST_EXTEND,
&&TARGET_SET_UPDATE,
&&TARGET_DICT_MERGE,