Skip to content

Commit 06c15de

Browse files
committed
Make Interpolation.conversion an int and change bytecode def
1 parent 550fd07 commit 06c15de

17 files changed

+285
-192
lines changed

Include/internal/pycore_ast.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_interpolation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ extern PyTypeObject _PyInterpolation_Type;
1515

1616
#define _PyInterpolation_CheckExact(op) Py_IS_TYPE((op), &_PyInterpolation_Type)
1717

18-
PyAPI_FUNC(PyObject *) _PyInterpolation_FromStackRefStealOnSuccess(_PyStackRef *values);
18+
PyAPI_FUNC(PyObject *) _PyInterpolation_Build(PyObject *value, PyObject *str,
19+
int conversion, PyObject *format_spec);
1920

2021
extern PyStatus _PyInterpolation_InitTypes(PyInterpreterState *interp);
2122
extern PyObject *_PyInterpolation_GetValueRef(PyObject *interpolation);

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: 43 additions & 43 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: 43 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/interpolationobject.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,28 +158,38 @@ _PyInterpolation_InitTypes(PyInterpreterState *interp)
158158
}
159159

160160
PyObject *
161-
_PyInterpolation_FromStackRefStealOnSuccess(_PyStackRef *values)
161+
_PyInterpolation_Build(PyObject *value, PyObject *str, int conversion, PyObject *format_spec)
162162
{
163-
interpolationobject *interpolation = (interpolationobject *) _PyInterpolation_Type.tp_alloc(&_PyInterpolation_Type, 0);
163+
interpolationobject *interpolation =
164+
(interpolationobject *) _PyInterpolation_Type.tp_alloc(&_PyInterpolation_Type, 0);
164165
if (!interpolation) {
165166
return NULL;
166167
}
167168

168-
interpolation->value = PyStackRef_AsPyObjectSteal(values[0]);
169-
interpolation->expression = PyStackRef_AsPyObjectSteal(values[1]);
169+
interpolation->value = Py_NewRef(value);
170+
interpolation->expression = Py_NewRef(str);
171+
interpolation->format_spec = Py_NewRef(format_spec);
170172

171-
if (PyStackRef_IsNull(values[2])) {
172-
interpolation->conversion = Py_NewRef(Py_None);
173+
if (conversion == 0) {
174+
interpolation->conversion = Py_None;
173175
}
174176
else {
175-
interpolation->conversion = PyStackRef_AsPyObjectSteal(values[2]);
176-
}
177-
178-
if (PyStackRef_IsNull(values[3])) {
179-
interpolation->format_spec = Py_NewRef(&_Py_STR(empty));
180-
}
181-
else {
182-
interpolation->format_spec = PyStackRef_AsPyObjectSteal(values[3]);
177+
switch (conversion) {
178+
case FVC_ASCII:
179+
interpolation->conversion = _Py_LATIN1_CHR('a');
180+
break;
181+
case FVC_REPR:
182+
interpolation->conversion = _Py_LATIN1_CHR('r');
183+
break;
184+
case FVC_STR:
185+
interpolation->conversion = _Py_LATIN1_CHR('s');
186+
break;
187+
default:
188+
PyErr_SetString(PyExc_SystemError,
189+
"Interpolation() argument 'conversion' must be one of 's', 'a' or 'r'");
190+
Py_DECREF(interpolation);
191+
return NULL;
192+
}
183193
}
184194

185195
return (PyObject *) interpolation;

Parser/Python.asdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module Python
7878
| Compare(expr left, cmpop* ops, expr* comparators)
7979
| Call(expr func, expr* args, keyword* keywords)
8080
| FormattedValue(expr value, int conversion, expr? format_spec)
81-
| Interpolation(expr value, constant str, constant? conversion, expr? format_spec)
81+
| Interpolation(expr value, constant str, int conversion, expr? format_spec)
8282
| JoinedStr(expr* values)
8383
| TemplateStr(expr* values)
8484
| Constant(constant value, string? kind)

Parser/action_helpers.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,17 +1498,7 @@ expr_ty _PyPegen_interpolation(Parser *p, expr_ty expression, Token *debug, Resu
14981498
ResultTokenWithMetadata *format, Token *closing_brace, int lineno, int col_offset,
14991499
int end_lineno, int end_col_offset, PyArena *arena) {
15001500

1501-
constant convstr = NULL;
15021501
int conversion_val = _get_interpolation_conversion(p, debug, conversion, format);
1503-
if (conversion_val >= 0) {
1504-
char buf[1];
1505-
buf[0] = conversion_val;
1506-
convstr = PyUnicode_FromStringAndSize(buf, 1);
1507-
if (convstr == NULL || _PyArena_AddPyObject(arena, convstr) < 0) {
1508-
Py_XDECREF(convstr);
1509-
return NULL;
1510-
}
1511-
}
15121502

15131503
/* Find the non whitespace token after the "=" */
15141504
int debug_end_line, debug_end_offset;
@@ -1539,7 +1529,7 @@ expr_ty _PyPegen_interpolation(Parser *p, expr_ty expression, Token *debug, Resu
15391529
}
15401530

15411531
expr_ty interpolation = _PyAST_Interpolation(
1542-
expression, final_exprstr, convstr, format ? (expr_ty) format->result : NULL,
1532+
expression, final_exprstr, conversion_val, format ? (expr_ty) format->result : NULL,
15431533
lineno, col_offset, end_lineno,
15441534
end_col_offset, arena
15451535
);

0 commit comments

Comments
 (0)