Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit 199ad58

Browse files
author
Guido van Rossum
committed
Restore Str.kind attribute (PR #49)
1 parent 69c8474 commit 199ad58

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

ast3/Include/Python-ast.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ struct _expr {
318318

319319
struct {
320320
string s;
321+
string kind;
321322
} Str;
322323

323324
struct {
@@ -601,8 +602,8 @@ expr_ty _Ta3_Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int
601602
lineno, int col_offset, PyArena *arena);
602603
#define Num(a0, a1, a2, a3) _Ta3_Num(a0, a1, a2, a3)
603604
expr_ty _Ta3_Num(object n, int lineno, int col_offset, PyArena *arena);
604-
#define Str(a0, a1, a2, a3) _Ta3_Str(a0, a1, a2, a3)
605-
expr_ty _Ta3_Str(string s, int lineno, int col_offset, PyArena *arena);
605+
#define Str(a0, a1, a2, a3, a4) _Ta3_Str(a0, a1, a2, a3, a4)
606+
expr_ty _Ta3_Str(string s, string kind, int lineno, int col_offset, PyArena *arena);
606607
#define FormattedValue(a0, a1, a2, a3, a4, a5) _Ta3_FormattedValue(a0, a1, a2, a3, a4, a5)
607608
expr_ty _Ta3_FormattedValue(expr_ty value, int conversion, expr_ty format_spec,
608609
int lineno, int col_offset, PyArena *arena);

ast3/Parser/Python.asdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ module Python
7777
| Compare(expr left, cmpop* ops, expr* comparators)
7878
| Call(expr func, expr* args, keyword* keywords)
7979
| Num(object n) -- a number as a PyObject.
80-
| Str(string s) -- need to specify raw, unicode, etc?
80+
| Str(string s, string kind) -- need to specify raw, unicode, etc?
8181
| FormattedValue(expr value, int? conversion, expr? format_spec)
8282
| JoinedStr(expr* values)
8383
| Bytes(bytes s)

ast3/Python/Python-ast.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,10 @@ static char *Num_fields[]={
307307
};
308308
static PyTypeObject *Str_type;
309309
_Py_IDENTIFIER(s);
310+
_Py_IDENTIFIER(kind);
310311
static char *Str_fields[]={
311312
"s",
313+
"kind",
312314
};
313315
static PyTypeObject *FormattedValue_type;
314316
_Py_IDENTIFIER(conversion);
@@ -988,7 +990,7 @@ static int init_types(void)
988990
if (!Call_type) return 0;
989991
Num_type = make_type("Num", expr_type, Num_fields, 1);
990992
if (!Num_type) return 0;
991-
Str_type = make_type("Str", expr_type, Str_fields, 1);
993+
Str_type = make_type("Str", expr_type, Str_fields, 2);
992994
if (!Str_type) return 0;
993995
FormattedValue_type = make_type("FormattedValue", expr_type,
994996
FormattedValue_fields, 3);
@@ -2186,19 +2188,25 @@ Num(object n, int lineno, int col_offset, PyArena *arena)
21862188
}
21872189

21882190
expr_ty
2189-
Str(string s, int lineno, int col_offset, PyArena *arena)
2191+
Str(string s, string kind, int lineno, int col_offset, PyArena *arena)
21902192
{
21912193
expr_ty p;
21922194
if (!s) {
21932195
PyErr_SetString(PyExc_ValueError,
21942196
"field s is required for Str");
21952197
return NULL;
21962198
}
2199+
if (!kind) {
2200+
PyErr_SetString(PyExc_ValueError,
2201+
"field kind is required for Str");
2202+
return NULL;
2203+
}
21972204
p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
21982205
if (!p)
21992206
return NULL;
22002207
p->kind = Str_kind;
22012208
p->v.Str.s = s;
2209+
p->v.Str.kind = kind;
22022210
p->lineno = lineno;
22032211
p->col_offset = col_offset;
22042212
return p;
@@ -3450,6 +3458,11 @@ ast2obj_expr(void* _o)
34503458
if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
34513459
goto failed;
34523460
Py_DECREF(value);
3461+
value = ast2obj_string(o->v.Str.kind);
3462+
if (!value) goto failed;
3463+
if (_PyObject_SetAttrId(result, &PyId_kind, value) == -1)
3464+
goto failed;
3465+
Py_DECREF(value);
34533466
break;
34543467
case FormattedValue_kind:
34553468
result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
@@ -6998,6 +7011,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
69987011
}
69997012
if (isinstance) {
70007013
string s;
7014+
string kind;
70017015

70027016
if (lookup_attr_id(obj, &PyId_s, &tmp) < 0) {
70037017
return 1;
@@ -7012,7 +7026,18 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
70127026
if (res != 0) goto failed;
70137027
Py_CLEAR(tmp);
70147028
}
7015-
*out = Str(s, lineno, col_offset, arena);
7029+
if (_PyObject_HasAttrId(obj, &PyId_kind)) {
7030+
int res;
7031+
tmp = _PyObject_GetAttrId(obj, &PyId_kind);
7032+
if (tmp == NULL) goto failed;
7033+
res = obj2ast_string(tmp, &kind, arena);
7034+
if (res != 0) goto failed;
7035+
Py_CLEAR(tmp);
7036+
} else {
7037+
PyErr_SetString(PyExc_TypeError, "required field \"kind\" missing from Str");
7038+
return 1;
7039+
}
7040+
*out = Str(s, kind, lineno, col_offset, arena);
70167041
if (*out == NULL) goto failed;
70177042
return 0;
70187043
}

ast3/Python/ast.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5163,14 +5163,25 @@ FstringParser_Dealloc(FstringParser *state)
51635163
static expr_ty
51645164
make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
51655165
{
5166-
PyObject *s = *str;
5166+
PyObject *kind, *s = *str;
5167+
const char *raw = STR(CHILD(n, 0));
5168+
/* currently Python allows up to 2 string modifiers */
5169+
char *ch, s_kind[3] = {0, 0, 0};
5170+
ch = s_kind;
5171+
while (*raw && *raw != '\'' && *raw != '"') {
5172+
*ch++ = *raw++;
5173+
}
5174+
kind = PyUnicode_FromString(s_kind);
5175+
if (!kind) {
5176+
return NULL;
5177+
}
51675178
*str = NULL;
51685179
assert(PyUnicode_CheckExact(s));
51695180
if (PyArena_AddPyObject(c->c_arena, s) < 0) {
51705181
Py_DECREF(s);
51715182
return NULL;
51725183
}
5173-
return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
5184+
return Str(s, kind, LINENO(n), n->n_col_offset, c->c_arena);
51745185
}
51755186

51765187
/* Add a non-f-string (that is, a regular literal string). str is

0 commit comments

Comments
 (0)