mirror of
https://github.com/python/cpython.git
synced 2025-09-03 15:31:08 +00:00
bpo-32892: Use ast.Constant instead of specific constant AST types. (GH-9445)
This commit is contained in:
parent
a94ee12c26
commit
3f22811fef
20 changed files with 337 additions and 678 deletions
82
Python/ast.c
82
Python/ast.c
|
@ -295,23 +295,6 @@ validate_expr(expr_ty exp, expr_context_ty ctx)
|
|||
return 0;
|
||||
}
|
||||
return 1;
|
||||
case Num_kind: {
|
||||
PyObject *n = exp->v.Num.n;
|
||||
if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
|
||||
!PyComplex_CheckExact(n)) {
|
||||
PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
case Str_kind: {
|
||||
PyObject *s = exp->v.Str.s;
|
||||
if (!PyUnicode_CheckExact(s)) {
|
||||
PyErr_SetString(PyExc_TypeError, "non-string type in Str");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
case JoinedStr_kind:
|
||||
return validate_exprs(exp->v.JoinedStr.values, Load, 0);
|
||||
case FormattedValue_kind:
|
||||
|
@ -320,14 +303,6 @@ validate_expr(expr_ty exp, expr_context_ty ctx)
|
|||
if (exp->v.FormattedValue.format_spec)
|
||||
return validate_expr(exp->v.FormattedValue.format_spec, Load);
|
||||
return 1;
|
||||
case Bytes_kind: {
|
||||
PyObject *b = exp->v.Bytes.s;
|
||||
if (!PyBytes_CheckExact(b)) {
|
||||
PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
case Attribute_kind:
|
||||
return validate_expr(exp->v.Attribute.value, Load);
|
||||
case Subscript_kind:
|
||||
|
@ -339,10 +314,8 @@ validate_expr(expr_ty exp, expr_context_ty ctx)
|
|||
return validate_exprs(exp->v.List.elts, ctx, 0);
|
||||
case Tuple_kind:
|
||||
return validate_exprs(exp->v.Tuple.elts, ctx, 0);
|
||||
/* These last cases don't have any checking. */
|
||||
/* This last case doesn't have any checking. */
|
||||
case Name_kind:
|
||||
case NameConstant_kind:
|
||||
case Ellipsis_kind:
|
||||
return 1;
|
||||
default:
|
||||
PyErr_SetString(PyExc_SystemError, "unexpected expression");
|
||||
|
@ -1040,19 +1013,23 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
|
|||
break;
|
||||
case Dict_kind:
|
||||
case Set_kind:
|
||||
case Num_kind:
|
||||
case Str_kind:
|
||||
case Bytes_kind:
|
||||
case JoinedStr_kind:
|
||||
case FormattedValue_kind:
|
||||
expr_name = "literal";
|
||||
break;
|
||||
case NameConstant_kind:
|
||||
expr_name = "keyword";
|
||||
break;
|
||||
case Ellipsis_kind:
|
||||
expr_name = "Ellipsis";
|
||||
case Constant_kind: {
|
||||
PyObject *value = e->v.Constant.value;
|
||||
if (value == Py_None || value == Py_False || value == Py_True) {
|
||||
expr_name = "keyword";
|
||||
}
|
||||
else if (value == Py_Ellipsis) {
|
||||
expr_name = "Ellipsis";
|
||||
}
|
||||
else {
|
||||
expr_name = "literal";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Compare_kind:
|
||||
expr_name = "comparison";
|
||||
break;
|
||||
|
@ -2091,11 +2068,11 @@ ast_for_atom(struct compiling *c, const node *n)
|
|||
size_t len = strlen(s);
|
||||
if (len >= 4 && len <= 5) {
|
||||
if (!strcmp(s, "None"))
|
||||
return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
return Constant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
if (!strcmp(s, "True"))
|
||||
return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
return Constant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
if (!strcmp(s, "False"))
|
||||
return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
return Constant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
}
|
||||
name = new_identifier(s, c);
|
||||
if (!name)
|
||||
|
@ -2144,10 +2121,10 @@ ast_for_atom(struct compiling *c, const node *n)
|
|||
Py_DECREF(pynum);
|
||||
return NULL;
|
||||
}
|
||||
return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
return Constant(pynum, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
}
|
||||
case ELLIPSIS: /* Ellipsis */
|
||||
return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
|
||||
return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
case LPAR: /* some parenthesized expressions */
|
||||
ch = CHILD(n, 1);
|
||||
|
||||
|
@ -4751,7 +4728,7 @@ typedef struct {
|
|||
expr_ty's, and then after that start dynamically allocating,
|
||||
doubling the number allocated each time. Note that the f-string
|
||||
f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
|
||||
Str for the literal 'a'. So you add expr_ty's about twice as
|
||||
Constant for the literal 'a'. So you add expr_ty's about twice as
|
||||
fast as you add exressions in an f-string. */
|
||||
|
||||
Py_ssize_t allocated; /* Number we've allocated. */
|
||||
|
@ -4903,7 +4880,7 @@ FstringParser_Dealloc(FstringParser *state)
|
|||
ExprList_Dealloc(&state->expr_list);
|
||||
}
|
||||
|
||||
/* Make a Str node, but decref the PyUnicode object being added. */
|
||||
/* Make a Constant node, but decref the PyUnicode object being added. */
|
||||
static expr_ty
|
||||
make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
|
||||
{
|
||||
|
@ -4914,7 +4891,7 @@ make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
|
|||
Py_DECREF(s);
|
||||
return NULL;
|
||||
}
|
||||
return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
return Constant(s, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
}
|
||||
|
||||
/* Add a non-f-string (that is, a regular literal string). str is
|
||||
|
@ -5002,11 +4979,11 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
|
|||
break;
|
||||
|
||||
/* We know we have an expression. Convert any existing string
|
||||
to a Str node. */
|
||||
to a Constant node. */
|
||||
if (!state->last_str) {
|
||||
/* Do nothing. No previous literal. */
|
||||
} else {
|
||||
/* Convert the existing last_str literal to a Str node. */
|
||||
/* Convert the existing last_str literal to a Constant node. */
|
||||
expr_ty str = make_str_node_and_del(&state->last_str, c, n);
|
||||
if (!str || ExprList_Append(&state->expr_list, str) < 0)
|
||||
return -1;
|
||||
|
@ -5033,7 +5010,7 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
|
|||
}
|
||||
|
||||
/* Convert the partial state reflected in last_str and expr_list to an
|
||||
expr_ty. The expr_ty can be a Str, or a JoinedStr. */
|
||||
expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
|
||||
static expr_ty
|
||||
FstringParser_Finish(FstringParser *state, struct compiling *c,
|
||||
const node *n)
|
||||
|
@ -5055,7 +5032,7 @@ FstringParser_Finish(FstringParser *state, struct compiling *c,
|
|||
return make_str_node_and_del(&state->last_str, c, n);
|
||||
}
|
||||
|
||||
/* Create a Str node out of last_str, if needed. It will be the
|
||||
/* Create a Constant node out of last_str, if needed. It will be the
|
||||
last node in our expression list. */
|
||||
if (state->last_str) {
|
||||
expr_ty str = make_str_node_and_del(&state->last_str, c, n);
|
||||
|
@ -5206,9 +5183,9 @@ parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
|
|||
|
||||
/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
|
||||
each STRING atom, and process it as needed. For bytes, just
|
||||
concatenate them together, and the result will be a Bytes node. For
|
||||
concatenate them together, and the result will be a Constant node. For
|
||||
normal strings and f-strings, concatenate them together. The result
|
||||
will be a Str node if there were no f-strings; a FormattedValue
|
||||
will be a Constant node if there were no f-strings; a FormattedValue
|
||||
node if there's just an f-string (with no leading or trailing
|
||||
literals), or a JoinedStr node if there are multiple f-strings or
|
||||
any literals involved. */
|
||||
|
@ -5279,7 +5256,7 @@ parsestrplus(struct compiling *c, const node *n)
|
|||
/* Just return the bytes object and we're done. */
|
||||
if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
|
||||
goto error;
|
||||
return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
return Constant(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
|
||||
}
|
||||
|
||||
/* We're not a bytes string, bytes_str should never have been set. */
|
||||
|
@ -5304,9 +5281,6 @@ _PyAST_GetDocString(asdl_seq *body)
|
|||
return NULL;
|
||||
}
|
||||
expr_ty e = st->v.Expr.value;
|
||||
if (e->kind == Str_kind) {
|
||||
return e->v.Str.s;
|
||||
}
|
||||
if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
|
||||
return e->v.Constant.value;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue