Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-98831: Modernize FORMAT_VALUE #101628

Merged
merged 4 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 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 All @@ -3086,7 +3078,7 @@ dummy_func(
Py_DECREF(value);
if (result == NULL) {
Py_XDECREF(fmt_spec);
goto error;
ERROR_IF(true, error);
}
value = result;
}
Expand All @@ -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
20 changes: 7 additions & 13 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
9 changes: 8 additions & 1 deletion Tools/cases_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,14 @@ def stack_effect(self) -> StackEffect | None:
@contextual
def expression(self) -> Expression | None:
tokens: list[lx.Token] = []
while (tkn := self.peek()) and tkn.kind not in (lx.RBRACKET, lx.RPAREN):
level = 1
while tkn := self.peek():
if tkn.kind in (lx.LBRACKET, lx.LPAREN):
level += 1
elif tkn.kind in (lx.RBRACKET, lx.RPAREN):
level -= 1
if level == 0:
break
tokens.append(tkn)
self.next()
if not tokens:
Expand Down
8 changes: 4 additions & 4 deletions Tools/cases_generator/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,20 +503,20 @@ def test_register():

def test_cond_effect():
input = """
inst(OP, (aa, input if (oparg & 1), cc -- xx, output if (oparg & 2), zz)) {
inst(OP, (aa, input if ((oparg & 1) == 1), cc -- xx, output if (oparg & 2), zz)) {
output = spam(oparg, input);
}
"""
output = """
TARGET(OP) {
PyObject *cc = PEEK(1);
PyObject *input = (oparg & 1) ? PEEK(1 + ((oparg & 1) ? 1 : 0)) : NULL;
PyObject *aa = PEEK(2 + ((oparg & 1) ? 1 : 0));
PyObject *input = ((oparg & 1) == 1) ? PEEK(1 + (((oparg & 1) == 1) ? 1 : 0)) : NULL;
PyObject *aa = PEEK(2 + (((oparg & 1) == 1) ? 1 : 0));
PyObject *xx;
PyObject *output = NULL;
PyObject *zz;
output = spam(oparg, input);
STACK_SHRINK(((oparg & 1) ? 1 : 0));
STACK_SHRINK((((oparg & 1) == 1) ? 1 : 0));
STACK_GROW(((oparg & 2) ? 1 : 0));
POKE(1, zz);
if (oparg & 2) { POKE(1 + ((oparg & 2) ? 1 : 0), output); }
Expand Down