Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Some progress -- translate foo'bar' into foo('bar')
  • Loading branch information
gvanrossum committed May 3, 2022
commit a17f33606dad717e0940688e069b2787c2e236f4
4 changes: 4 additions & 0 deletions Python/ast_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,10 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
case JoinedStr_kind:
CALL_SEQ(astfold_expr, expr, node_->v.JoinedStr.values);
break;
case TagString_kind:
CALL(astfold_expr, expr_ty, node_->v.TagString.tag);
CALL(astfold_expr, expr_ty, node_->v.TagString.str);
break;
case Attribute_kind:
CALL(astfold_expr, expr_ty, node_->v.Attribute.value);
break;
Expand Down
2 changes: 2 additions & 0 deletions Python/ast_unparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,8 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
return append_ast_constant(writer, e->v.Constant.value);
case JoinedStr_kind:
return append_joinedstr(writer, e, false);
case TagString_kind:
assert(0); // TODO
case FormattedValue_kind:
return append_formattedvalue(writer, e);
/* The following exprs can be assignment targets. */
Expand Down
35 changes: 32 additions & 3 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4691,7 +4691,6 @@ check_caller(struct compiler *c, expr_ty e)
case SetComp_kind:
case GeneratorExp_kind:
case JoinedStr_kind:
case TagString_kind:
case FormattedValue_kind:
return compiler_warn(c, "'%.200s' object is not callable; "
"perhaps you missed a comma?",
Expand Down Expand Up @@ -4751,7 +4750,6 @@ check_index(struct compiler *c, expr_ty e, expr_ty s)
case List_kind:
case ListComp_kind:
case JoinedStr_kind:
case TagString_kind:
case FormattedValue_kind:
return compiler_warn(c, "%.200s indices must be integers or slices, "
"not %.200s; "
Expand Down Expand Up @@ -4910,6 +4908,37 @@ compiler_joined_str(struct compiler *c, expr_ty e)
return 1;
}

static int
compiler_tag_string(struct compiler *c, expr_ty e)
{
if (e->kind == TagString_kind) {
expr_ty tag = e->v.TagString.tag;
expr_ty str = e->v.TagString.str;
if (tag->kind == Name_kind) {
if (str->kind == Constant_kind) {
PyObject *value = str->v.Constant.value;
PyObject *kind = str->v.Constant.kind;
if (kind == NULL && PyUnicode_CheckExact(value)) {
// Generate code for tag(value)
asdl_expr_seq *args =
_Py_asdl_expr_seq_new(1, c->c_arena);
if (args == NULL)
return 0;
asdl_seq_SET(args, 0, str);
asdl_keyword_seq *keywords =
_Py_asdl_keyword_seq_new(0, c->c_arena);
if (keywords == NULL)
return 0;
ADDOP(c, PUSH_NULL);
VISIT(c, expr, tag);
return compiler_call_helper(c, 0, args, keywords);
}
}
}
}
return compiler_error(c, "More complicated tag-string not yet supported");
}

/* Used to implement f-strings. Format a single value. */
static int
compiler_formatted_value(struct compiler *c, expr_ty e)
Expand Down Expand Up @@ -5838,7 +5867,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
case JoinedStr_kind:
return compiler_joined_str(c, e);
case TagString_kind:
return compiler_error(c, "TagString not yet supported");
return compiler_tag_string(c, e);
break;
case FormattedValue_kind:
return compiler_formatted_value(c, e);
Expand Down
4 changes: 4 additions & 0 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,10 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
case JoinedStr_kind:
VISIT_SEQ(st, expr, e->v.JoinedStr.values);
break;
case TagString_kind:
VISIT(st, expr, e->v.TagString.tag);
VISIT(st, expr, e->v.TagString.str);
break;
case Constant_kind:
/* Nothing to do here. */
break;
Expand Down