diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-03-10 18:52:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-10 18:52:34 +0200 |
commit | 13d52c268699f199a8e917a0f1dc4c51e5346c42 (patch) | |
tree | d602c97d77e3222d38c6300ed822021e51bd9dce /Python/ast_unparse.c | |
parent | bpo-39869: Fix typo in 'Instance objects' section. (GH-18889) (diff) | |
download | cpython-13d52c268699f199a8e917a0f1dc4c51e5346c42.tar.gz cpython-13d52c268699f199a8e917a0f1dc4c51e5346c42.tar.bz2 cpython-13d52c268699f199a8e917a0f1dc4c51e5346c42.zip |
bpo-34822: Simplify AST for subscription. (GH-9605)
* Remove the slice type.
* Make Slice a kind of the expr type instead of the slice type.
* Replace ExtSlice(slices) with Tuple(slices, Load()).
* Replace Index(value) with a value itself.
All non-terminal nodes in AST for expressions are now of the expr type.
Diffstat (limited to 'Python/ast_unparse.c')
-rw-r--r-- | Python/ast_unparse.c | 51 |
1 files changed, 11 insertions, 40 deletions
diff --git a/Python/ast_unparse.c b/Python/ast_unparse.c index bd9c1396c07..5ecd1b0fef5 100644 --- a/Python/ast_unparse.c +++ b/Python/ast_unparse.c @@ -17,7 +17,7 @@ append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec); static int append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e); static int -append_ast_slice(_PyUnicodeWriter *writer, slice_ty slice); +append_ast_slice(_PyUnicodeWriter *writer, expr_ty e); static int append_charp(_PyUnicodeWriter *writer, const char *charp) @@ -718,62 +718,31 @@ append_ast_attribute(_PyUnicodeWriter *writer, expr_ty e) } static int -append_ast_simple_slice(_PyUnicodeWriter *writer, slice_ty slice) +append_ast_slice(_PyUnicodeWriter *writer, expr_ty e) { - if (slice->v.Slice.lower) { - APPEND_EXPR(slice->v.Slice.lower, PR_TEST); + if (e->v.Slice.lower) { + APPEND_EXPR(e->v.Slice.lower, PR_TEST); } APPEND_STR(":"); - if (slice->v.Slice.upper) { - APPEND_EXPR(slice->v.Slice.upper, PR_TEST); + if (e->v.Slice.upper) { + APPEND_EXPR(e->v.Slice.upper, PR_TEST); } - if (slice->v.Slice.step) { + if (e->v.Slice.step) { APPEND_STR(":"); - APPEND_EXPR(slice->v.Slice.step, PR_TEST); + APPEND_EXPR(e->v.Slice.step, PR_TEST); } return 0; } static int -append_ast_ext_slice(_PyUnicodeWriter *writer, slice_ty slice) -{ - Py_ssize_t i, dims_count; - dims_count = asdl_seq_LEN(slice->v.ExtSlice.dims); - for (i = 0; i < dims_count; i++) { - APPEND_STR_IF(i > 0, ", "); - APPEND(slice, (slice_ty)asdl_seq_GET(slice->v.ExtSlice.dims, i)); - } - APPEND_STR_IF(dims_count == 1, ","); - return 0; -} - -static int -append_ast_slice(_PyUnicodeWriter *writer, slice_ty slice) -{ - switch (slice->kind) { - case Slice_kind: - return append_ast_simple_slice(writer, slice); - case ExtSlice_kind: - return append_ast_ext_slice(writer, slice); - case Index_kind: - APPEND_EXPR(slice->v.Index.value, PR_TUPLE); - return 0; - default: - PyErr_SetString(PyExc_SystemError, - "unexpected slice kind"); - return -1; - } -} - -static int append_ast_subscript(_PyUnicodeWriter *writer, expr_ty e) { APPEND_EXPR(e->v.Subscript.value, PR_ATOM); APPEND_STR("["); - APPEND(slice, e->v.Subscript.slice); + APPEND_EXPR(e->v.Subscript.slice, PR_TUPLE); APPEND_STR_FINISH("]"); } @@ -878,6 +847,8 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level) return append_ast_subscript(writer, e); case Starred_kind: return append_ast_starred(writer, e); + case Slice_kind: + return append_ast_slice(writer, e); case Name_kind: return _PyUnicodeWriter_WriteStr(writer, e->v.Name.id); case List_kind: |