Skip to content

Commit a095a9b

Browse files
committed
Change BUILD_INTERPOLATION opcode to simplify
1 parent 450d771 commit a095a9b

File tree

13 files changed

+119
-135
lines changed

13 files changed

+119
-135
lines changed

Include/internal/pycore_interpolation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef struct {
1919

2020
extern PyTypeObject PyInterpolation_Type;
2121

22-
PyAPI_FUNC(PyObject *) _PyInterpolation_FromStackRefSteal(_PyStackRef *values, Py_ssize_t n);
22+
PyAPI_FUNC(PyObject *) _PyInterpolation_FromStackRefSteal(_PyStackRef *values);
2323

2424
extern PyStatus _PyInterpolation_InitTypes(PyInterpreterState *);
2525
extern void _PyInterpolation_FiniTypes(PyInterpreterState *);

Include/internal/pycore_opcode_metadata.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/opcode_ids.h

Lines changed: 40 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/_opcode_metadata.py

Lines changed: 40 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/interpolationobject.c

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,21 @@ _PyInterpolation_FiniTypes(PyInterpreterState *interp)
9595
}
9696

9797
PyObject *
98-
_PyInterpolation_FromStackRefSteal(_PyStackRef *values, Py_ssize_t oparg)
98+
_PyInterpolation_FromStackRefSteal(_PyStackRef *values)
9999
{
100-
Py_ssize_t index = 2;
101-
102-
PyObject *args = PyTuple_New(oparg + ((oparg >> 1) & 1) + (oparg & 1));
100+
PyObject *args = PyTuple_New(4);
103101
if (!args) {
104102
goto error;
105103
}
106104

107105
PyTuple_SET_ITEM(args, 0, PyStackRef_AsPyObjectSteal(values[0]));
108106
PyTuple_SET_ITEM(args, 1, PyStackRef_AsPyObjectSteal(values[1]));
109107

110-
if ((oparg >> 1) & 1) {
111-
PyTuple_SET_ITEM(args, 2, PyStackRef_AsPyObjectSteal(values[index]));
112-
index++;
113-
}
114-
else {
115-
PyTuple_SET_ITEM(args, 2, Py_NewRef(Py_None));
116-
}
108+
PyObject *conv = PyStackRef_AsPyObjectSteal(values[2]);
109+
PyTuple_SET_ITEM(args, 2, conv ? conv : Py_NewRef(Py_None));
117110

118-
if (oparg & 1) {
119-
PyTuple_SET_ITEM(args, 3, PyStackRef_AsPyObjectSteal(values[index]));
120-
}
121-
else {
122-
PyTuple_SET_ITEM(args, 3, &_Py_STR(empty));
123-
}
111+
PyObject *format_spec = PyStackRef_AsPyObjectSteal(values[3]);
112+
PyTuple_SET_ITEM(args, 3, format_spec ? format_spec : &_Py_STR(empty));
124113

125114
PyObject *interpolation = PyObject_CallObject((PyObject *) &PyInterpolation_Type, args);
126115
if (!interpolation) {
@@ -132,13 +121,7 @@ _PyInterpolation_FromStackRefSteal(_PyStackRef *values, Py_ssize_t oparg)
132121
error:
133122
PyStackRef_CLOSE(values[0]);
134123
PyStackRef_CLOSE(values[1]);
135-
index = 2;
136-
if ((oparg >> 1) & 1) {
137-
PyStackRef_XCLOSE(values[index]);
138-
index++;
139-
}
140-
if (oparg & 1) {
141-
PyStackRef_XCLOSE(values[index]);
142-
}
124+
PyStackRef_XCLOSE(values[2]);
125+
PyStackRef_XCLOSE(values[3]);
143126
return NULL;
144127
}

Parser/action_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ expr_ty _PyPegen_interpolation(Parser *p, expr_ty expression, Token *debug, Resu
14911491
int conversion_val = _get_interpolation_conversion(p, debug, conversion, format);
14921492
if (conversion_val >= 0) {
14931493
expr_ty conv = (expr_ty) conversion->result;
1494-
convstr = PyUnicode_FromObject(conv->v.Name.id);
1494+
convstr = _PyUnicode_Copy(conv->v.Name.id);
14951495
if (convstr == NULL) {
14961496
return NULL;
14971497
}

Python/bytecodes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,8 +1726,8 @@ dummy_func(
17261726
str = PyStackRef_FromPyObjectSteal(str_o);
17271727
}
17281728

1729-
inst(BUILD_INTERPOLATION, (values[2 + ((oparg >> 1) & 1) + (oparg & 1)] -- interpolation)) {
1730-
PyObject *interpolation_o = _PyInterpolation_FromStackRefSteal(values, oparg);
1729+
inst(BUILD_INTERPOLATION, (values[4] -- interpolation)) {
1730+
PyObject *interpolation_o = _PyInterpolation_FromStackRefSteal(values);
17311731
ERROR_IF(interpolation_o == NULL, error);
17321732
interpolation = PyStackRef_FromPyObjectSteal(interpolation_o);
17331733
}

Python/codegen.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3912,20 +3912,26 @@ codegen_interpolation(compiler *c, expr_ty e)
39123912
- (oparg >> 1) & 1 == 1, if conversion is not NULL
39133913
- oparg & 1 == 1, if format_spec is not NULL
39143914
*/
3915-
int oparg = 0b100;
39163915
location loc = LOC(e);
39173916

39183917
VISIT(c, expr, e->v.Interpolation.value);
39193918
ADDOP_LOAD_CONST(c, loc, e->v.Interpolation.str);
3919+
39203920
if (e->v.Interpolation.conversion) {
39213921
ADDOP_LOAD_CONST(c, loc, e->v.Interpolation.conversion);
3922-
oparg |= 0b10;
39233922
}
3923+
else {
3924+
ADDOP(c, loc, PUSH_NULL);
3925+
}
3926+
39243927
if (e->v.Interpolation.format_spec) {
39253928
VISIT(c, expr, e->v.Interpolation.format_spec);
3926-
oparg |= 1;
39273929
}
3928-
ADDOP_I(c, loc, BUILD_INTERPOLATION, oparg);
3930+
else {
3931+
ADDOP(c, loc, PUSH_NULL);
3932+
}
3933+
3934+
ADDOP(c, loc, BUILD_INTERPOLATION);
39293935
return SUCCESS;
39303936
}
39313937

Python/executor_cases.c.h

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)