Skip to content

Commit 6139cb5

Browse files
committed
1 parent 4813207 commit 6139cb5

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

‎Parser/pegen.c‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,7 +2661,9 @@ expr_ty _PyPegen_collect_call_seqs(Parser *p, asdl_expr_seq *a, asdl_seq *b,
26612661

26622662
static expr_ty
26632663
_PyPegen_fstring_bytes_constant_to_unicode(Parser* p, int is_raw, expr_ty constant) {
2664-
const char* bstr = PyBytes_AsString(constant->v.Constant.value);
2664+
assert(PyUnicode_CheckExact(constant->v.Constant.value));
2665+
2666+
const char* bstr = PyUnicode_AsUTF8(constant->v.Constant.value);
26652667
if (bstr == NULL) {
26662668
return NULL;
26672669
}
@@ -2670,11 +2672,12 @@ _PyPegen_fstring_bytes_constant_to_unicode(Parser* p, int is_raw, expr_ty consta
26702672
if (strcmp(bstr, "{{") == 0 || strcmp(bstr, "}}") == 0) {
26712673
len = 1;
26722674
} else {
2673-
len = PyBytes_GET_SIZE(constant->v.Constant.value);
2675+
len = PyUnicode_GET_LENGTH(constant->v.Constant.value);
26742676
}
26752677

26762678
PyObject *str = _PyPegen_DecodeFstring(p, is_raw, bstr, len, NULL);
26772679
if (str == NULL) {
2680+
raise_decode_error(p);
26782681
return NULL;
26792682
}
26802683
if (_PyArena_AddPyObject(p->arena, str) < 0) {
@@ -2736,7 +2739,7 @@ deal_with_gstring2(Parser *p, Token* a, asdl_expr_seq* raw_expressions, Token*b)
27362739
return NULL;
27372740
}
27382741

2739-
int is_raw = strchr(quote_str, 'r') == NULL;
2742+
int is_raw = strchr(quote_str, 'r') != NULL;
27402743
const char* _str = PyBytes_AsString(b->bytes);
27412744
if (_str == NULL) {
27422745
return NULL;
@@ -2750,7 +2753,7 @@ deal_with_gstring2(Parser *p, Token* a, asdl_expr_seq* raw_expressions, Token*b)
27502753
Py_ssize_t i = 0;
27512754
for (i= 0; i < asdl_seq_LEN(expr); i++) {
27522755
expr_ty item = asdl_seq_GET(expr, i);
2753-
if (item->kind == Constant_kind && PyBytes_CheckExact(item->v.Constant.value)) {
2756+
if (item->kind == Constant_kind) {
27542757
item = _PyPegen_fstring_bytes_constant_to_unicode(p, is_raw, item);
27552758
if (item == NULL) {
27562759
return NULL;
@@ -2775,7 +2778,15 @@ deal_with_gstring2(Parser *p, Token* a, asdl_expr_seq* raw_expressions, Token*b)
27752778
if (the_str == NULL) {
27762779
return NULL;
27772780
}
2778-
asdl_seq_SET(seq, n_items, the_str);
2781+
2782+
expr_ty decoded_str = _PyPegen_fstring_bytes_constant_to_unicode(
2783+
p, is_raw, the_str
2784+
);
2785+
if (decoded_str == NULL) {
2786+
return NULL;
2787+
}
2788+
2789+
asdl_seq_SET(seq, n_items, decoded_str);
27792790
}
27802791
return _PyAST_JoinedStr(seq, a->lineno, a->col_offset,
27812792
b->end_lineno, b->end_col_offset,
@@ -2813,6 +2824,7 @@ expr_ty _PyPegen_constant_from_token(Parser* p, Token* tok) {
28132824
const char *fstr;
28142825
Py_ssize_t fstrlen = -1;
28152826
if (_PyPegen_parsestr(p, &this_bytesmode, &this_rawmode, &s, &fstr, &fstrlen, tok) != 0) {
2827+
raise_decode_error(p);
28162828
return NULL;
28172829
}
28182830
PyObject *kind = NULL;

0 commit comments

Comments
 (0)