Skip to content

Commit

Permalink
Modernize FORMAT_VALUE
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum committed Feb 7, 2023
1 parent 43beb67 commit 575b810
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
16 changes: 2 additions & 14 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3052,18 +3052,10 @@ dummy_func(
ERROR_IF(slice == NULL, error);
}

// error: FORMAT_VALUE has irregular stack effect
inst(FORMAT_VALUE) {
inst(FORMAT_VALUE, (value, fmt_spec if ((oparg & FVS_MASK) == FVS_HAVE_SPEC) -- result)) {
/* Handles f-string value formatting. */
PyObject *result;
PyObject *fmt_spec;
PyObject *value;
PyObject *(*conv_fn)(PyObject *);
int which_conversion = oparg & FVC_MASK;
int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;

fmt_spec = have_fmt_spec ? POP() : NULL;
value = POP();

/* See if any conversion is specified. */
switch (which_conversion) {
Expand Down Expand Up @@ -3104,12 +3096,8 @@ dummy_func(
result = PyObject_Format(value, fmt_spec);
Py_DECREF(value);
Py_XDECREF(fmt_spec);
if (result == NULL) {
goto error;
}
ERROR_IF(result == NULL, error);
}

PUSH(result);
}

inst(COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {
Expand Down
18 changes: 6 additions & 12 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
case BUILD_SLICE:
return ((oparg == 3) ? 1 : 0) + 2;
case FORMAT_VALUE:
return -1;
return (((oparg & FVS_MASK) == FVS_HAVE_SPEC) ? 1 : 0) + 1;
case COPY:
return (oparg-1) + 1;
case BINARY_OP:
Expand Down Expand Up @@ -677,7 +677,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
case BUILD_SLICE:
return 1;
case FORMAT_VALUE:
return -1;
return 1;
case COPY:
return (oparg-1) + 2;
case BINARY_OP:
Expand Down

0 comments on commit 575b810

Please sign in to comment.