diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:39 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:39 +0300 |
commit | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch) | |
tree | 64175d5cadab313b3e7039ebaa06c5bc3295e274 /contrib/tools/python3/src/Python/ast_unparse.c | |
parent | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff) | |
download | ydb-e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Python/ast_unparse.c')
-rw-r--r-- | contrib/tools/python3/src/Python/ast_unparse.c | 242 |
1 files changed, 121 insertions, 121 deletions
diff --git a/contrib/tools/python3/src/Python/ast_unparse.c b/contrib/tools/python3/src/Python/ast_unparse.c index 99610d2661..e699751a05 100644 --- a/contrib/tools/python3/src/Python/ast_unparse.c +++ b/contrib/tools/python3/src/Python/ast_unparse.c @@ -1,4 +1,4 @@ -#include <float.h> /* DBL_MAX_10_EXP */ +#include <float.h> /* DBL_MAX_10_EXP */ #include <stdbool.h> #include "Python.h" #include "Python-ast.h" @@ -7,8 +7,8 @@ static PyObject *_str_open_br; static PyObject *_str_dbl_open_br; static PyObject *_str_close_br; static PyObject *_str_dbl_close_br; -static PyObject *_str_inf; -static PyObject *_str_replace_inf; +static PyObject *_str_inf; +static PyObject *_str_replace_inf; /* Forward declarations for recursion via helper functions. */ static PyObject * @@ -18,9 +18,9 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level); static int append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec); static int -append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e); +append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e); static int -append_ast_slice(_PyUnicodeWriter *writer, expr_ty e); +append_ast_slice(_PyUnicodeWriter *writer, expr_ty e); static int append_charp(_PyUnicodeWriter *writer, const char *charp) @@ -64,28 +64,28 @@ append_charp(_PyUnicodeWriter *writer, const char *charp) static int append_repr(_PyUnicodeWriter *writer, PyObject *obj) { - PyObject *repr = PyObject_Repr(obj); - + PyObject *repr = PyObject_Repr(obj); + if (!repr) { return -1; } - - if ((PyFloat_CheckExact(obj) && Py_IS_INFINITY(PyFloat_AS_DOUBLE(obj))) || - PyComplex_CheckExact(obj)) - { - PyObject *new_repr = PyUnicode_Replace( - repr, - _str_inf, - _str_replace_inf, - -1 - ); - Py_DECREF(repr); - if (!new_repr) { - return -1; - } - repr = new_repr; - } - int ret = _PyUnicodeWriter_WriteStr(writer, repr); + + if ((PyFloat_CheckExact(obj) && Py_IS_INFINITY(PyFloat_AS_DOUBLE(obj))) || + PyComplex_CheckExact(obj)) + { + PyObject *new_repr = PyUnicode_Replace( + repr, + _str_inf, + _str_replace_inf, + -1 + ); + Py_DECREF(repr); + if (!new_repr) { + return -1; + } + repr = new_repr; + } + int ret = _PyUnicodeWriter_WriteStr(writer, repr); Py_DECREF(repr); return ret; } @@ -211,30 +211,30 @@ static int append_ast_args(_PyUnicodeWriter *writer, arguments_ty args) { bool first; - Py_ssize_t i, di, arg_count, posonlyarg_count, default_count; + Py_ssize_t i, di, arg_count, posonlyarg_count, default_count; first = true; - /* positional-only and positional arguments with defaults */ - posonlyarg_count = asdl_seq_LEN(args->posonlyargs); + /* positional-only and positional arguments with defaults */ + posonlyarg_count = asdl_seq_LEN(args->posonlyargs); arg_count = asdl_seq_LEN(args->args); default_count = asdl_seq_LEN(args->defaults); - for (i = 0; i < posonlyarg_count + arg_count; i++) { + for (i = 0; i < posonlyarg_count + arg_count; i++) { APPEND_STR_IF_NOT_FIRST(", "); - if (i < posonlyarg_count){ - APPEND(arg, (arg_ty)asdl_seq_GET(args->posonlyargs, i)); - } else { - APPEND(arg, (arg_ty)asdl_seq_GET(args->args, i-posonlyarg_count)); - } + if (i < posonlyarg_count){ + APPEND(arg, (arg_ty)asdl_seq_GET(args->posonlyargs, i)); + } else { + APPEND(arg, (arg_ty)asdl_seq_GET(args->args, i-posonlyarg_count)); + } - di = i - posonlyarg_count - arg_count + default_count; + di = i - posonlyarg_count - arg_count + default_count; if (di >= 0) { APPEND_STR("="); APPEND_EXPR((expr_ty)asdl_seq_GET(args->defaults, di), PR_TEST); } - if (posonlyarg_count && i + 1 == posonlyarg_count) { - APPEND_STR(", /"); - } + if (posonlyarg_count && i + 1 == posonlyarg_count) { + APPEND_STR(", /"); + } } /* vararg, or bare '*' if no varargs but keyword-only arguments present */ @@ -277,9 +277,9 @@ static int append_ast_lambda(_PyUnicodeWriter *writer, expr_ty e, int level) { APPEND_STR_IF(level > PR_TEST, "("); - Py_ssize_t n_positional = (asdl_seq_LEN(e->v.Lambda.args->args) + - asdl_seq_LEN(e->v.Lambda.args->posonlyargs)); - APPEND_STR(n_positional ? "lambda " : "lambda"); + Py_ssize_t n_positional = (asdl_seq_LEN(e->v.Lambda.args->args) + + asdl_seq_LEN(e->v.Lambda.args->posonlyargs)); + APPEND_STR(n_positional ? "lambda " : "lambda"); APPEND(args, e->v.Lambda.args); APPEND_STR(": "); APPEND_EXPR(e->v.Lambda.body, PR_TEST); @@ -601,7 +601,7 @@ append_fstring_element(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec) case JoinedStr_kind: return append_joinedstr(writer, e, is_format_spec); case FormattedValue_kind: - return append_formattedvalue(writer, e); + return append_formattedvalue(writer, e); default: PyErr_SetString(PyExc_SystemError, "unknown expression kind inside f-string"); @@ -658,7 +658,7 @@ append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec) } static int -append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e) +append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e) { const char *conversion; const char *outer_brace = "{"; @@ -716,37 +716,37 @@ append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e) } static int -append_ast_constant(_PyUnicodeWriter *writer, PyObject *constant) -{ - if (PyTuple_CheckExact(constant)) { - Py_ssize_t i, elem_count; - - elem_count = PyTuple_GET_SIZE(constant); - APPEND_STR("("); - for (i = 0; i < elem_count; i++) { - APPEND_STR_IF(i > 0, ", "); - if (append_ast_constant(writer, PyTuple_GET_ITEM(constant, i)) < 0) { - return -1; - } - } - - APPEND_STR_IF(elem_count == 1, ","); - APPEND_STR(")"); - return 0; - } - return append_repr(writer, constant); -} - -static int +append_ast_constant(_PyUnicodeWriter *writer, PyObject *constant) +{ + if (PyTuple_CheckExact(constant)) { + Py_ssize_t i, elem_count; + + elem_count = PyTuple_GET_SIZE(constant); + APPEND_STR("("); + for (i = 0; i < elem_count; i++) { + APPEND_STR_IF(i > 0, ", "); + if (append_ast_constant(writer, PyTuple_GET_ITEM(constant, i)) < 0) { + return -1; + } + } + + APPEND_STR_IF(elem_count == 1, ","); + APPEND_STR(")"); + return 0; + } + return append_repr(writer, constant); +} + +static int append_ast_attribute(_PyUnicodeWriter *writer, expr_ty e) { const char *period; - expr_ty v = e->v.Attribute.value; - APPEND_EXPR(v, PR_ATOM); + expr_ty v = e->v.Attribute.value; + APPEND_EXPR(v, PR_ATOM); /* Special case: integers require a space for attribute access to be - unambiguous. */ - if (v->kind == Constant_kind && PyLong_CheckExact(v->v.Constant.value)) { + unambiguous. */ + if (v->kind == Constant_kind && PyLong_CheckExact(v->v.Constant.value)) { period = " ."; } else { @@ -758,42 +758,42 @@ append_ast_attribute(_PyUnicodeWriter *writer, expr_ty e) } static int -append_ast_slice(_PyUnicodeWriter *writer, expr_ty e) +append_ast_slice(_PyUnicodeWriter *writer, expr_ty e) { - if (e->v.Slice.lower) { - APPEND_EXPR(e->v.Slice.lower, PR_TEST); + if (e->v.Slice.lower) { + APPEND_EXPR(e->v.Slice.lower, PR_TEST); } APPEND_STR(":"); - if (e->v.Slice.upper) { - APPEND_EXPR(e->v.Slice.upper, PR_TEST); + if (e->v.Slice.upper) { + APPEND_EXPR(e->v.Slice.upper, PR_TEST); } - if (e->v.Slice.step) { + if (e->v.Slice.step) { APPEND_STR(":"); - APPEND_EXPR(e->v.Slice.step, PR_TEST); + APPEND_EXPR(e->v.Slice.step, PR_TEST); } return 0; } static int -append_ast_subscript(_PyUnicodeWriter *writer, expr_ty e) +append_ast_subscript(_PyUnicodeWriter *writer, expr_ty e) { - APPEND_EXPR(e->v.Subscript.value, PR_ATOM); - int level = PR_TUPLE; - expr_ty slice = e->v.Subscript.slice; - if (slice->kind == Tuple_kind) { - for (Py_ssize_t i = 0; i < asdl_seq_LEN(slice->v.Tuple.elts); i++) { - expr_ty element = asdl_seq_GET(slice->v.Tuple.elts, i); - if (element->kind == Starred_kind) { - ++level; - break; - } - } - } + APPEND_EXPR(e->v.Subscript.value, PR_ATOM); + int level = PR_TUPLE; + expr_ty slice = e->v.Subscript.slice; + if (slice->kind == Tuple_kind) { + for (Py_ssize_t i = 0; i < asdl_seq_LEN(slice->v.Tuple.elts); i++) { + expr_ty element = asdl_seq_GET(slice->v.Tuple.elts, i); + if (element->kind == Starred_kind) { + ++level; + break; + } + } + } APPEND_STR("["); - APPEND_EXPR(e->v.Subscript.slice, level); + APPEND_EXPR(e->v.Subscript.slice, level); APPEND_STR_FINISH("]"); } @@ -836,17 +836,17 @@ append_ast_await(_PyUnicodeWriter *writer, expr_ty e, int level) } static int -append_named_expr(_PyUnicodeWriter *writer, expr_ty e, int level) -{ - APPEND_STR_IF(level > PR_TUPLE, "("); - APPEND_EXPR(e->v.NamedExpr.target, PR_ATOM); - APPEND_STR(" := "); - APPEND_EXPR(e->v.NamedExpr.value, PR_ATOM); - APPEND_STR_IF(level > PR_TUPLE, ")"); - return 0; -} - -static int +append_named_expr(_PyUnicodeWriter *writer, expr_ty e, int level) +{ + APPEND_STR_IF(level > PR_TUPLE, "("); + APPEND_EXPR(e->v.NamedExpr.target, PR_ATOM); + APPEND_STR(" := "); + APPEND_EXPR(e->v.NamedExpr.value, PR_ATOM); + APPEND_STR_IF(level > PR_TUPLE, ")"); + return 0; +} + +static int append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level) { switch (e->kind) { @@ -883,18 +883,18 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level) case Call_kind: return append_ast_call(writer, e); case Constant_kind: - if (e->v.Constant.value == Py_Ellipsis) { - APPEND_STR_FINISH("..."); - } - if (e->v.Constant.kind != NULL - && -1 == _PyUnicodeWriter_WriteStr(writer, e->v.Constant.kind)) { - return -1; - } - return append_ast_constant(writer, e->v.Constant.value); + if (e->v.Constant.value == Py_Ellipsis) { + APPEND_STR_FINISH("..."); + } + if (e->v.Constant.kind != NULL + && -1 == _PyUnicodeWriter_WriteStr(writer, e->v.Constant.kind)) { + return -1; + } + return append_ast_constant(writer, e->v.Constant.value); case JoinedStr_kind: return append_joinedstr(writer, e, false); case FormattedValue_kind: - return append_formattedvalue(writer, e); + return append_formattedvalue(writer, e); /* The following exprs can be assignment targets. */ case Attribute_kind: return append_ast_attribute(writer, e); @@ -902,16 +902,16 @@ 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 Slice_kind: + return append_ast_slice(writer, e); case Name_kind: return _PyUnicodeWriter_WriteStr(writer, e->v.Name.id); case List_kind: return append_ast_list(writer, e); case Tuple_kind: return append_ast_tuple(writer, e, level); - case NamedExpr_kind: - return append_named_expr(writer, e, level); + case NamedExpr_kind: + return append_named_expr(writer, e, level); default: PyErr_SetString(PyExc_SystemError, "unknown expression kind"); @@ -938,14 +938,14 @@ maybe_init_static_strings(void) !(_str_dbl_close_br = PyUnicode_InternFromString("}}"))) { return -1; } - if (!_str_inf && - !(_str_inf = PyUnicode_FromString("inf"))) { - return -1; - } - if (!_str_replace_inf && - !(_str_replace_inf = PyUnicode_FromFormat("1e%d", 1 + DBL_MAX_10_EXP))) { - return -1; - } + if (!_str_inf && + !(_str_inf = PyUnicode_FromString("inf"))) { + return -1; + } + if (!_str_replace_inf && + !(_str_replace_inf = PyUnicode_FromFormat("1e%d", 1 + DBL_MAX_10_EXP))) { + return -1; + } return 0; } |