mirror of
https://github.com/python/cpython.git
synced 2025-10-21 14:12:27 +00:00
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.
This commit is contained in:
parent
65d98d0f53
commit
9a4135e939
11 changed files with 286 additions and 49 deletions
|
@ -3946,8 +3946,8 @@ compiler_formatted_value(struct compiler *c, expr_ty e)
|
|||
/* Our oparg encodes 2 pieces of information: the conversion
|
||||
character, and whether or not a format_spec was provided.
|
||||
|
||||
Convert the conversion char to 2 bits:
|
||||
None: 000 0x0 FVC_NONE
|
||||
Convert the conversion char to 3 bits:
|
||||
: 000 0x0 FVC_NONE The default if nothing specified.
|
||||
!s : 001 0x1 FVC_STR
|
||||
!r : 010 0x2 FVC_REPR
|
||||
!a : 011 0x3 FVC_ASCII
|
||||
|
@ -3957,19 +3957,26 @@ compiler_formatted_value(struct compiler *c, expr_ty e)
|
|||
no : 000 0x0
|
||||
*/
|
||||
|
||||
int conversion = e->v.FormattedValue.conversion;
|
||||
int oparg;
|
||||
|
||||
/* Evaluate the expression to be formatted. */
|
||||
if (e->v.FormattedValue.expr_text) {
|
||||
/* Push the text of the expression (which already has the '=' in
|
||||
it. */
|
||||
ADDOP_LOAD_CONST(c, e->v.FormattedValue.expr_text);
|
||||
}
|
||||
|
||||
/* The expression to be formatted. */
|
||||
VISIT(c, expr, e->v.FormattedValue.value);
|
||||
|
||||
switch (e->v.FormattedValue.conversion) {
|
||||
switch (conversion) {
|
||||
case 's': oparg = FVC_STR; break;
|
||||
case 'r': oparg = FVC_REPR; break;
|
||||
case 'a': oparg = FVC_ASCII; break;
|
||||
case -1: oparg = FVC_NONE; break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_SystemError,
|
||||
"Unrecognized conversion character");
|
||||
PyErr_Format(PyExc_SystemError,
|
||||
"Unrecognized conversion character %d", conversion);
|
||||
return 0;
|
||||
}
|
||||
if (e->v.FormattedValue.format_spec) {
|
||||
|
@ -3980,6 +3987,12 @@ compiler_formatted_value(struct compiler *c, expr_ty e)
|
|||
|
||||
/* And push our opcode and oparg */
|
||||
ADDOP_I(c, FORMAT_VALUE, oparg);
|
||||
|
||||
/* If we have expr_text, join the 2 strings on the stack. */
|
||||
if (e->v.FormattedValue.expr_text) {
|
||||
ADDOP_I(c, BUILD_STRING, 2);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue