diff options
author | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2019-05-08 20:28:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-08 20:28:48 (GMT) |
commit | 9a4135e939bc223f592045a38e0f927ba170da32 (patch) | |
tree | de347c6f2df801dc98b36ab5084dada335741517 /Python/Python-ast.c | |
parent | 65d98d0f53f558d7c799098da0abf376068c15fd (diff) | |
download | cpython-9a4135e939bc223f592045a38e0f927ba170da32.zip cpython-9a4135e939bc223f592045a38e0f927ba170da32.tar.gz cpython-9a4135e939bc223f592045a38e0f927ba170da32.tar.bz2 |
bpo-36817: Add f-string debugging using '='. (GH-13123)
If a "=" is specified a the end of an f-string expression, the f-string will evaluate to the text of the expression, followed by '=', followed by the repr of the value of the expression.
Diffstat (limited to 'Python/Python-ast.c')
-rw-r--r-- | Python/Python-ast.c | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 6c8488f..cb53a41 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -314,10 +314,12 @@ static char *Call_fields[]={ static PyTypeObject *FormattedValue_type; _Py_IDENTIFIER(conversion); _Py_IDENTIFIER(format_spec); +_Py_IDENTIFIER(expr_text); static char *FormattedValue_fields[]={ "value", "conversion", "format_spec", + "expr_text", }; static PyTypeObject *JoinedStr_type; static char *JoinedStr_fields[]={ @@ -950,7 +952,7 @@ static int init_types(void) Call_type = make_type("Call", expr_type, Call_fields, 3); if (!Call_type) return 0; FormattedValue_type = make_type("FormattedValue", expr_type, - FormattedValue_fields, 3); + FormattedValue_fields, 4); if (!FormattedValue_type) return 0; JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1); if (!JoinedStr_type) return 0; @@ -2249,9 +2251,9 @@ Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int } expr_ty -FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno, - int col_offset, int end_lineno, int end_col_offset, PyArena - *arena) +FormattedValue(expr_ty value, int conversion, expr_ty format_spec, string + expr_text, int lineno, int col_offset, int end_lineno, int + end_col_offset, PyArena *arena) { expr_ty p; if (!value) { @@ -2266,6 +2268,7 @@ FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno, p->v.FormattedValue.value = value; p->v.FormattedValue.conversion = conversion; p->v.FormattedValue.format_spec = format_spec; + p->v.FormattedValue.expr_text = expr_text; p->lineno = lineno; p->col_offset = col_offset; p->end_lineno = end_lineno; @@ -3496,6 +3499,11 @@ ast2obj_expr(void* _o) if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1) goto failed; Py_DECREF(value); + value = ast2obj_string(o->v.FormattedValue.expr_text); + if (!value) goto failed; + if (_PyObject_SetAttrId(result, &PyId_expr_text, value) == -1) + goto failed; + Py_DECREF(value); break; case JoinedStr_kind: result = PyType_GenericNew(JoinedStr_type, NULL, NULL); @@ -7148,6 +7156,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena) expr_ty value; int conversion; expr_ty format_spec; + string expr_text; if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) { return 1; @@ -7188,8 +7197,22 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena) if (res != 0) goto failed; Py_CLEAR(tmp); } - *out = FormattedValue(value, conversion, format_spec, lineno, - col_offset, end_lineno, end_col_offset, arena); + if (_PyObject_LookupAttrId(obj, &PyId_expr_text, &tmp) < 0) { + return 1; + } + if (tmp == NULL || tmp == Py_None) { + Py_CLEAR(tmp); + expr_text = NULL; + } + else { + int res; + res = obj2ast_string(tmp, &expr_text, arena); + if (res != 0) goto failed; + Py_CLEAR(tmp); + } + *out = FormattedValue(value, conversion, format_spec, expr_text, + lineno, col_offset, end_lineno, end_col_offset, + arena); if (*out == NULL) goto failed; return 0; } |