ast_unparse.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. #include "Python.h"
  2. #include "pycore_ast.h" // expr_ty
  3. #include "pycore_pystate.h" // _PyInterpreterState_GET()
  4. #include "pycore_runtime.h" // _Py_ID()
  5. #include <float.h> // DBL_MAX_10_EXP
  6. #include <stdbool.h>
  7. /* This limited unparser is used to convert annotations back to strings
  8. * during compilation rather than being a full AST unparser.
  9. * See ast.unparse for a full unparser (written in Python)
  10. */
  11. _Py_DECLARE_STR(open_br, "{");
  12. _Py_DECLARE_STR(dbl_open_br, "{{");
  13. _Py_DECLARE_STR(close_br, "}");
  14. _Py_DECLARE_STR(dbl_close_br, "}}");
  15. /* We would statically initialize this if doing so were simple enough. */
  16. #define _str_replace_inf(interp) \
  17. _Py_INTERP_CACHED_OBJECT(interp, str_replace_inf)
  18. /* Forward declarations for recursion via helper functions. */
  19. static PyObject *
  20. expr_as_unicode(expr_ty e, int level);
  21. static int
  22. append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level);
  23. static int
  24. append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec);
  25. static int
  26. append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e);
  27. static int
  28. append_ast_slice(_PyUnicodeWriter *writer, expr_ty e);
  29. static int
  30. append_charp(_PyUnicodeWriter *writer, const char *charp)
  31. {
  32. return _PyUnicodeWriter_WriteASCIIString(writer, charp, -1);
  33. }
  34. #define APPEND_STR_FINISH(str) do { \
  35. return append_charp(writer, (str)); \
  36. } while (0)
  37. #define APPEND_STR(str) do { \
  38. if (-1 == append_charp(writer, (str))) { \
  39. return -1; \
  40. } \
  41. } while (0)
  42. #define APPEND_STR_IF(cond, str) do { \
  43. if ((cond) && -1 == append_charp(writer, (str))) { \
  44. return -1; \
  45. } \
  46. } while (0)
  47. #define APPEND_STR_IF_NOT_FIRST(str) do { \
  48. APPEND_STR_IF(!first, (str)); \
  49. first = false; \
  50. } while (0)
  51. #define APPEND_EXPR(expr, pr) do { \
  52. if (-1 == append_ast_expr(writer, (expr), (pr))) { \
  53. return -1; \
  54. } \
  55. } while (0)
  56. #define APPEND(type, value) do { \
  57. if (-1 == append_ast_ ## type(writer, (value))) { \
  58. return -1; \
  59. } \
  60. } while (0)
  61. static int
  62. append_repr(_PyUnicodeWriter *writer, PyObject *obj)
  63. {
  64. PyObject *repr = PyObject_Repr(obj);
  65. if (!repr) {
  66. return -1;
  67. }
  68. if ((PyFloat_CheckExact(obj) && Py_IS_INFINITY(PyFloat_AS_DOUBLE(obj))) ||
  69. PyComplex_CheckExact(obj))
  70. {
  71. PyInterpreterState *interp = _PyInterpreterState_GET();
  72. PyObject *new_repr = PyUnicode_Replace(
  73. repr,
  74. &_Py_ID(inf),
  75. _str_replace_inf(interp),
  76. -1
  77. );
  78. Py_DECREF(repr);
  79. if (!new_repr) {
  80. return -1;
  81. }
  82. repr = new_repr;
  83. }
  84. int ret = _PyUnicodeWriter_WriteStr(writer, repr);
  85. Py_DECREF(repr);
  86. return ret;
  87. }
  88. /* Priority levels */
  89. enum {
  90. PR_TUPLE,
  91. PR_TEST, /* 'if'-'else', 'lambda' */
  92. PR_OR, /* 'or' */
  93. PR_AND, /* 'and' */
  94. PR_NOT, /* 'not' */
  95. PR_CMP, /* '<', '>', '==', '>=', '<=', '!=',
  96. 'in', 'not in', 'is', 'is not' */
  97. PR_EXPR,
  98. PR_BOR = PR_EXPR, /* '|' */
  99. PR_BXOR, /* '^' */
  100. PR_BAND, /* '&' */
  101. PR_SHIFT, /* '<<', '>>' */
  102. PR_ARITH, /* '+', '-' */
  103. PR_TERM, /* '*', '@', '/', '%', '//' */
  104. PR_FACTOR, /* unary '+', '-', '~' */
  105. PR_POWER, /* '**' */
  106. PR_AWAIT, /* 'await' */
  107. PR_ATOM,
  108. };
  109. static int
  110. append_ast_boolop(_PyUnicodeWriter *writer, expr_ty e, int level)
  111. {
  112. Py_ssize_t i, value_count;
  113. asdl_expr_seq *values;
  114. const char *op = (e->v.BoolOp.op == And) ? " and " : " or ";
  115. int pr = (e->v.BoolOp.op == And) ? PR_AND : PR_OR;
  116. APPEND_STR_IF(level > pr, "(");
  117. values = e->v.BoolOp.values;
  118. value_count = asdl_seq_LEN(values);
  119. for (i = 0; i < value_count; ++i) {
  120. APPEND_STR_IF(i > 0, op);
  121. APPEND_EXPR((expr_ty)asdl_seq_GET(values, i), pr + 1);
  122. }
  123. APPEND_STR_IF(level > pr, ")");
  124. return 0;
  125. }
  126. static int
  127. append_ast_binop(_PyUnicodeWriter *writer, expr_ty e, int level)
  128. {
  129. const char *op;
  130. int pr;
  131. bool rassoc = false; /* is right-associative? */
  132. switch (e->v.BinOp.op) {
  133. case Add: op = " + "; pr = PR_ARITH; break;
  134. case Sub: op = " - "; pr = PR_ARITH; break;
  135. case Mult: op = " * "; pr = PR_TERM; break;
  136. case MatMult: op = " @ "; pr = PR_TERM; break;
  137. case Div: op = " / "; pr = PR_TERM; break;
  138. case Mod: op = " % "; pr = PR_TERM; break;
  139. case LShift: op = " << "; pr = PR_SHIFT; break;
  140. case RShift: op = " >> "; pr = PR_SHIFT; break;
  141. case BitOr: op = " | "; pr = PR_BOR; break;
  142. case BitXor: op = " ^ "; pr = PR_BXOR; break;
  143. case BitAnd: op = " & "; pr = PR_BAND; break;
  144. case FloorDiv: op = " // "; pr = PR_TERM; break;
  145. case Pow: op = " ** "; pr = PR_POWER; rassoc = true; break;
  146. default:
  147. PyErr_SetString(PyExc_SystemError,
  148. "unknown binary operator");
  149. return -1;
  150. }
  151. APPEND_STR_IF(level > pr, "(");
  152. APPEND_EXPR(e->v.BinOp.left, pr + rassoc);
  153. APPEND_STR(op);
  154. APPEND_EXPR(e->v.BinOp.right, pr + !rassoc);
  155. APPEND_STR_IF(level > pr, ")");
  156. return 0;
  157. }
  158. static int
  159. append_ast_unaryop(_PyUnicodeWriter *writer, expr_ty e, int level)
  160. {
  161. const char *op;
  162. int pr;
  163. switch (e->v.UnaryOp.op) {
  164. case Invert: op = "~"; pr = PR_FACTOR; break;
  165. case Not: op = "not "; pr = PR_NOT; break;
  166. case UAdd: op = "+"; pr = PR_FACTOR; break;
  167. case USub: op = "-"; pr = PR_FACTOR; break;
  168. default:
  169. PyErr_SetString(PyExc_SystemError,
  170. "unknown unary operator");
  171. return -1;
  172. }
  173. APPEND_STR_IF(level > pr, "(");
  174. APPEND_STR(op);
  175. APPEND_EXPR(e->v.UnaryOp.operand, pr);
  176. APPEND_STR_IF(level > pr, ")");
  177. return 0;
  178. }
  179. static int
  180. append_ast_arg(_PyUnicodeWriter *writer, arg_ty arg)
  181. {
  182. if (-1 == _PyUnicodeWriter_WriteStr(writer, arg->arg)) {
  183. return -1;
  184. }
  185. if (arg->annotation) {
  186. APPEND_STR(": ");
  187. APPEND_EXPR(arg->annotation, PR_TEST);
  188. }
  189. return 0;
  190. }
  191. static int
  192. append_ast_args(_PyUnicodeWriter *writer, arguments_ty args)
  193. {
  194. bool first;
  195. Py_ssize_t i, di, arg_count, posonlyarg_count, default_count;
  196. first = true;
  197. /* positional-only and positional arguments with defaults */
  198. posonlyarg_count = asdl_seq_LEN(args->posonlyargs);
  199. arg_count = asdl_seq_LEN(args->args);
  200. default_count = asdl_seq_LEN(args->defaults);
  201. for (i = 0; i < posonlyarg_count + arg_count; i++) {
  202. APPEND_STR_IF_NOT_FIRST(", ");
  203. if (i < posonlyarg_count){
  204. APPEND(arg, (arg_ty)asdl_seq_GET(args->posonlyargs, i));
  205. } else {
  206. APPEND(arg, (arg_ty)asdl_seq_GET(args->args, i-posonlyarg_count));
  207. }
  208. di = i - posonlyarg_count - arg_count + default_count;
  209. if (di >= 0) {
  210. APPEND_STR("=");
  211. APPEND_EXPR((expr_ty)asdl_seq_GET(args->defaults, di), PR_TEST);
  212. }
  213. if (posonlyarg_count && i + 1 == posonlyarg_count) {
  214. APPEND_STR(", /");
  215. }
  216. }
  217. /* vararg, or bare '*' if no varargs but keyword-only arguments present */
  218. if (args->vararg || asdl_seq_LEN(args->kwonlyargs)) {
  219. APPEND_STR_IF_NOT_FIRST(", ");
  220. APPEND_STR("*");
  221. if (args->vararg) {
  222. APPEND(arg, args->vararg);
  223. }
  224. }
  225. /* keyword-only arguments */
  226. arg_count = asdl_seq_LEN(args->kwonlyargs);
  227. default_count = asdl_seq_LEN(args->kw_defaults);
  228. for (i = 0; i < arg_count; i++) {
  229. APPEND_STR_IF_NOT_FIRST(", ");
  230. APPEND(arg, (arg_ty)asdl_seq_GET(args->kwonlyargs, i));
  231. di = i - arg_count + default_count;
  232. if (di >= 0) {
  233. expr_ty default_ = (expr_ty)asdl_seq_GET(args->kw_defaults, di);
  234. if (default_) {
  235. APPEND_STR("=");
  236. APPEND_EXPR(default_, PR_TEST);
  237. }
  238. }
  239. }
  240. /* **kwargs */
  241. if (args->kwarg) {
  242. APPEND_STR_IF_NOT_FIRST(", ");
  243. APPEND_STR("**");
  244. APPEND(arg, args->kwarg);
  245. }
  246. return 0;
  247. }
  248. static int
  249. append_ast_lambda(_PyUnicodeWriter *writer, expr_ty e, int level)
  250. {
  251. APPEND_STR_IF(level > PR_TEST, "(");
  252. Py_ssize_t n_positional = (asdl_seq_LEN(e->v.Lambda.args->args) +
  253. asdl_seq_LEN(e->v.Lambda.args->posonlyargs));
  254. APPEND_STR(n_positional ? "lambda " : "lambda");
  255. APPEND(args, e->v.Lambda.args);
  256. APPEND_STR(": ");
  257. APPEND_EXPR(e->v.Lambda.body, PR_TEST);
  258. APPEND_STR_IF(level > PR_TEST, ")");
  259. return 0;
  260. }
  261. static int
  262. append_ast_ifexp(_PyUnicodeWriter *writer, expr_ty e, int level)
  263. {
  264. APPEND_STR_IF(level > PR_TEST, "(");
  265. APPEND_EXPR(e->v.IfExp.body, PR_TEST + 1);
  266. APPEND_STR(" if ");
  267. APPEND_EXPR(e->v.IfExp.test, PR_TEST + 1);
  268. APPEND_STR(" else ");
  269. APPEND_EXPR(e->v.IfExp.orelse, PR_TEST);
  270. APPEND_STR_IF(level > PR_TEST, ")");
  271. return 0;
  272. }
  273. static int
  274. append_ast_dict(_PyUnicodeWriter *writer, expr_ty e)
  275. {
  276. Py_ssize_t i, value_count;
  277. expr_ty key_node;
  278. APPEND_STR("{");
  279. value_count = asdl_seq_LEN(e->v.Dict.values);
  280. for (i = 0; i < value_count; i++) {
  281. APPEND_STR_IF(i > 0, ", ");
  282. key_node = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i);
  283. if (key_node != NULL) {
  284. APPEND_EXPR(key_node, PR_TEST);
  285. APPEND_STR(": ");
  286. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Dict.values, i), PR_TEST);
  287. }
  288. else {
  289. APPEND_STR("**");
  290. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Dict.values, i), PR_EXPR);
  291. }
  292. }
  293. APPEND_STR_FINISH("}");
  294. }
  295. static int
  296. append_ast_set(_PyUnicodeWriter *writer, expr_ty e)
  297. {
  298. Py_ssize_t i, elem_count;
  299. APPEND_STR("{");
  300. elem_count = asdl_seq_LEN(e->v.Set.elts);
  301. for (i = 0; i < elem_count; i++) {
  302. APPEND_STR_IF(i > 0, ", ");
  303. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Set.elts, i), PR_TEST);
  304. }
  305. APPEND_STR_FINISH("}");
  306. }
  307. static int
  308. append_ast_list(_PyUnicodeWriter *writer, expr_ty e)
  309. {
  310. Py_ssize_t i, elem_count;
  311. APPEND_STR("[");
  312. elem_count = asdl_seq_LEN(e->v.List.elts);
  313. for (i = 0; i < elem_count; i++) {
  314. APPEND_STR_IF(i > 0, ", ");
  315. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.List.elts, i), PR_TEST);
  316. }
  317. APPEND_STR_FINISH("]");
  318. }
  319. static int
  320. append_ast_tuple(_PyUnicodeWriter *writer, expr_ty e, int level)
  321. {
  322. Py_ssize_t i, elem_count;
  323. elem_count = asdl_seq_LEN(e->v.Tuple.elts);
  324. if (elem_count == 0) {
  325. APPEND_STR_FINISH("()");
  326. }
  327. APPEND_STR_IF(level > PR_TUPLE, "(");
  328. for (i = 0; i < elem_count; i++) {
  329. APPEND_STR_IF(i > 0, ", ");
  330. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Tuple.elts, i), PR_TEST);
  331. }
  332. APPEND_STR_IF(elem_count == 1, ",");
  333. APPEND_STR_IF(level > PR_TUPLE, ")");
  334. return 0;
  335. }
  336. static int
  337. append_ast_comprehension(_PyUnicodeWriter *writer, comprehension_ty gen)
  338. {
  339. Py_ssize_t i, if_count;
  340. APPEND_STR(gen->is_async ? " async for " : " for ");
  341. APPEND_EXPR(gen->target, PR_TUPLE);
  342. APPEND_STR(" in ");
  343. APPEND_EXPR(gen->iter, PR_TEST + 1);
  344. if_count = asdl_seq_LEN(gen->ifs);
  345. for (i = 0; i < if_count; i++) {
  346. APPEND_STR(" if ");
  347. APPEND_EXPR((expr_ty)asdl_seq_GET(gen->ifs, i), PR_TEST + 1);
  348. }
  349. return 0;
  350. }
  351. static int
  352. append_ast_comprehensions(_PyUnicodeWriter *writer, asdl_comprehension_seq *comprehensions)
  353. {
  354. Py_ssize_t i, gen_count;
  355. gen_count = asdl_seq_LEN(comprehensions);
  356. for (i = 0; i < gen_count; i++) {
  357. APPEND(comprehension, (comprehension_ty)asdl_seq_GET(comprehensions, i));
  358. }
  359. return 0;
  360. }
  361. static int
  362. append_ast_genexp(_PyUnicodeWriter *writer, expr_ty e)
  363. {
  364. APPEND_STR("(");
  365. APPEND_EXPR(e->v.GeneratorExp.elt, PR_TEST);
  366. APPEND(comprehensions, e->v.GeneratorExp.generators);
  367. APPEND_STR_FINISH(")");
  368. }
  369. static int
  370. append_ast_listcomp(_PyUnicodeWriter *writer, expr_ty e)
  371. {
  372. APPEND_STR("[");
  373. APPEND_EXPR(e->v.ListComp.elt, PR_TEST);
  374. APPEND(comprehensions, e->v.ListComp.generators);
  375. APPEND_STR_FINISH("]");
  376. }
  377. static int
  378. append_ast_setcomp(_PyUnicodeWriter *writer, expr_ty e)
  379. {
  380. APPEND_STR("{");
  381. APPEND_EXPR(e->v.SetComp.elt, PR_TEST);
  382. APPEND(comprehensions, e->v.SetComp.generators);
  383. APPEND_STR_FINISH("}");
  384. }
  385. static int
  386. append_ast_dictcomp(_PyUnicodeWriter *writer, expr_ty e)
  387. {
  388. APPEND_STR("{");
  389. APPEND_EXPR(e->v.DictComp.key, PR_TEST);
  390. APPEND_STR(": ");
  391. APPEND_EXPR(e->v.DictComp.value, PR_TEST);
  392. APPEND(comprehensions, e->v.DictComp.generators);
  393. APPEND_STR_FINISH("}");
  394. }
  395. static int
  396. append_ast_compare(_PyUnicodeWriter *writer, expr_ty e, int level)
  397. {
  398. const char *op;
  399. Py_ssize_t i, comparator_count;
  400. asdl_expr_seq *comparators;
  401. asdl_int_seq *ops;
  402. APPEND_STR_IF(level > PR_CMP, "(");
  403. comparators = e->v.Compare.comparators;
  404. ops = e->v.Compare.ops;
  405. comparator_count = asdl_seq_LEN(comparators);
  406. assert(comparator_count > 0);
  407. assert(comparator_count == asdl_seq_LEN(ops));
  408. APPEND_EXPR(e->v.Compare.left, PR_CMP + 1);
  409. for (i = 0; i < comparator_count; i++) {
  410. switch ((cmpop_ty)asdl_seq_GET(ops, i)) {
  411. case Eq:
  412. op = " == ";
  413. break;
  414. case NotEq:
  415. op = " != ";
  416. break;
  417. case Lt:
  418. op = " < ";
  419. break;
  420. case LtE:
  421. op = " <= ";
  422. break;
  423. case Gt:
  424. op = " > ";
  425. break;
  426. case GtE:
  427. op = " >= ";
  428. break;
  429. case Is:
  430. op = " is ";
  431. break;
  432. case IsNot:
  433. op = " is not ";
  434. break;
  435. case In:
  436. op = " in ";
  437. break;
  438. case NotIn:
  439. op = " not in ";
  440. break;
  441. default:
  442. PyErr_SetString(PyExc_SystemError,
  443. "unexpected comparison kind");
  444. return -1;
  445. }
  446. APPEND_STR(op);
  447. APPEND_EXPR((expr_ty)asdl_seq_GET(comparators, i), PR_CMP + 1);
  448. }
  449. APPEND_STR_IF(level > PR_CMP, ")");
  450. return 0;
  451. }
  452. static int
  453. append_ast_keyword(_PyUnicodeWriter *writer, keyword_ty kw)
  454. {
  455. if (kw->arg == NULL) {
  456. APPEND_STR("**");
  457. }
  458. else {
  459. if (-1 == _PyUnicodeWriter_WriteStr(writer, kw->arg)) {
  460. return -1;
  461. }
  462. APPEND_STR("=");
  463. }
  464. APPEND_EXPR(kw->value, PR_TEST);
  465. return 0;
  466. }
  467. static int
  468. append_ast_call(_PyUnicodeWriter *writer, expr_ty e)
  469. {
  470. bool first;
  471. Py_ssize_t i, arg_count, kw_count;
  472. expr_ty expr;
  473. APPEND_EXPR(e->v.Call.func, PR_ATOM);
  474. arg_count = asdl_seq_LEN(e->v.Call.args);
  475. kw_count = asdl_seq_LEN(e->v.Call.keywords);
  476. if (arg_count == 1 && kw_count == 0) {
  477. expr = (expr_ty)asdl_seq_GET(e->v.Call.args, 0);
  478. if (expr->kind == GeneratorExp_kind) {
  479. /* Special case: a single generator expression. */
  480. return append_ast_genexp(writer, expr);
  481. }
  482. }
  483. APPEND_STR("(");
  484. first = true;
  485. for (i = 0; i < arg_count; i++) {
  486. APPEND_STR_IF_NOT_FIRST(", ");
  487. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Call.args, i), PR_TEST);
  488. }
  489. for (i = 0; i < kw_count; i++) {
  490. APPEND_STR_IF_NOT_FIRST(", ");
  491. APPEND(keyword, (keyword_ty)asdl_seq_GET(e->v.Call.keywords, i));
  492. }
  493. APPEND_STR_FINISH(")");
  494. }
  495. static PyObject *
  496. escape_braces(PyObject *orig)
  497. {
  498. PyObject *temp;
  499. PyObject *result;
  500. temp = PyUnicode_Replace(orig, &_Py_STR(open_br), &_Py_STR(dbl_open_br), -1);
  501. if (!temp) {
  502. return NULL;
  503. }
  504. result = PyUnicode_Replace(temp, &_Py_STR(close_br), &_Py_STR(dbl_close_br), -1);
  505. Py_DECREF(temp);
  506. return result;
  507. }
  508. static int
  509. append_fstring_unicode(_PyUnicodeWriter *writer, PyObject *unicode)
  510. {
  511. PyObject *escaped;
  512. int result = -1;
  513. escaped = escape_braces(unicode);
  514. if (escaped) {
  515. result = _PyUnicodeWriter_WriteStr(writer, escaped);
  516. Py_DECREF(escaped);
  517. }
  518. return result;
  519. }
  520. static int
  521. append_fstring_element(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
  522. {
  523. switch (e->kind) {
  524. case Constant_kind:
  525. return append_fstring_unicode(writer, e->v.Constant.value);
  526. case JoinedStr_kind:
  527. return append_joinedstr(writer, e, is_format_spec);
  528. case FormattedValue_kind:
  529. return append_formattedvalue(writer, e);
  530. default:
  531. PyErr_SetString(PyExc_SystemError,
  532. "unknown expression kind inside f-string");
  533. return -1;
  534. }
  535. }
  536. /* Build body separately to enable wrapping the entire stream of Strs,
  537. Constants and FormattedValues in one opening and one closing quote. */
  538. static PyObject *
  539. build_fstring_body(asdl_expr_seq *values, bool is_format_spec)
  540. {
  541. Py_ssize_t i, value_count;
  542. _PyUnicodeWriter body_writer;
  543. _PyUnicodeWriter_Init(&body_writer);
  544. body_writer.min_length = 256;
  545. body_writer.overallocate = 1;
  546. value_count = asdl_seq_LEN(values);
  547. for (i = 0; i < value_count; ++i) {
  548. if (-1 == append_fstring_element(&body_writer,
  549. (expr_ty)asdl_seq_GET(values, i),
  550. is_format_spec
  551. )) {
  552. _PyUnicodeWriter_Dealloc(&body_writer);
  553. return NULL;
  554. }
  555. }
  556. return _PyUnicodeWriter_Finish(&body_writer);
  557. }
  558. static int
  559. append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
  560. {
  561. int result = -1;
  562. PyObject *body = build_fstring_body(e->v.JoinedStr.values, is_format_spec);
  563. if (!body) {
  564. return -1;
  565. }
  566. if (!is_format_spec) {
  567. if (-1 != append_charp(writer, "f") &&
  568. -1 != append_repr(writer, body))
  569. {
  570. result = 0;
  571. }
  572. }
  573. else {
  574. result = _PyUnicodeWriter_WriteStr(writer, body);
  575. }
  576. Py_DECREF(body);
  577. return result;
  578. }
  579. static int
  580. append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e)
  581. {
  582. const char *conversion;
  583. const char *outer_brace = "{";
  584. /* Grammar allows PR_TUPLE, but use >PR_TEST for adding parenthesis
  585. around a lambda with ':' */
  586. PyObject *temp_fv_str = expr_as_unicode(e->v.FormattedValue.value, PR_TEST + 1);
  587. if (!temp_fv_str) {
  588. return -1;
  589. }
  590. if (PyUnicode_Find(temp_fv_str, &_Py_STR(open_br), 0, 1, 1) == 0) {
  591. /* Expression starts with a brace, split it with a space from the outer
  592. one. */
  593. outer_brace = "{ ";
  594. }
  595. if (-1 == append_charp(writer, outer_brace)) {
  596. Py_DECREF(temp_fv_str);
  597. return -1;
  598. }
  599. if (-1 == _PyUnicodeWriter_WriteStr(writer, temp_fv_str)) {
  600. Py_DECREF(temp_fv_str);
  601. return -1;
  602. }
  603. Py_DECREF(temp_fv_str);
  604. if (e->v.FormattedValue.conversion > 0) {
  605. switch (e->v.FormattedValue.conversion) {
  606. case 'a':
  607. conversion = "!a";
  608. break;
  609. case 'r':
  610. conversion = "!r";
  611. break;
  612. case 's':
  613. conversion = "!s";
  614. break;
  615. default:
  616. PyErr_SetString(PyExc_SystemError,
  617. "unknown f-value conversion kind");
  618. return -1;
  619. }
  620. APPEND_STR(conversion);
  621. }
  622. if (e->v.FormattedValue.format_spec) {
  623. if (-1 == _PyUnicodeWriter_WriteASCIIString(writer, ":", 1) ||
  624. -1 == append_fstring_element(writer,
  625. e->v.FormattedValue.format_spec,
  626. true
  627. ))
  628. {
  629. return -1;
  630. }
  631. }
  632. APPEND_STR_FINISH("}");
  633. }
  634. static int
  635. append_ast_constant(_PyUnicodeWriter *writer, PyObject *constant)
  636. {
  637. if (PyTuple_CheckExact(constant)) {
  638. Py_ssize_t i, elem_count;
  639. elem_count = PyTuple_GET_SIZE(constant);
  640. APPEND_STR("(");
  641. for (i = 0; i < elem_count; i++) {
  642. APPEND_STR_IF(i > 0, ", ");
  643. if (append_ast_constant(writer, PyTuple_GET_ITEM(constant, i)) < 0) {
  644. return -1;
  645. }
  646. }
  647. APPEND_STR_IF(elem_count == 1, ",");
  648. APPEND_STR(")");
  649. return 0;
  650. }
  651. return append_repr(writer, constant);
  652. }
  653. static int
  654. append_ast_attribute(_PyUnicodeWriter *writer, expr_ty e)
  655. {
  656. const char *period;
  657. expr_ty v = e->v.Attribute.value;
  658. APPEND_EXPR(v, PR_ATOM);
  659. /* Special case: integers require a space for attribute access to be
  660. unambiguous. */
  661. if (v->kind == Constant_kind && PyLong_CheckExact(v->v.Constant.value)) {
  662. period = " .";
  663. }
  664. else {
  665. period = ".";
  666. }
  667. APPEND_STR(period);
  668. return _PyUnicodeWriter_WriteStr(writer, e->v.Attribute.attr);
  669. }
  670. static int
  671. append_ast_slice(_PyUnicodeWriter *writer, expr_ty e)
  672. {
  673. if (e->v.Slice.lower) {
  674. APPEND_EXPR(e->v.Slice.lower, PR_TEST);
  675. }
  676. APPEND_STR(":");
  677. if (e->v.Slice.upper) {
  678. APPEND_EXPR(e->v.Slice.upper, PR_TEST);
  679. }
  680. if (e->v.Slice.step) {
  681. APPEND_STR(":");
  682. APPEND_EXPR(e->v.Slice.step, PR_TEST);
  683. }
  684. return 0;
  685. }
  686. static int
  687. append_ast_subscript(_PyUnicodeWriter *writer, expr_ty e)
  688. {
  689. APPEND_EXPR(e->v.Subscript.value, PR_ATOM);
  690. APPEND_STR("[");
  691. APPEND_EXPR(e->v.Subscript.slice, PR_TUPLE);
  692. APPEND_STR_FINISH("]");
  693. }
  694. static int
  695. append_ast_starred(_PyUnicodeWriter *writer, expr_ty e)
  696. {
  697. APPEND_STR("*");
  698. APPEND_EXPR(e->v.Starred.value, PR_EXPR);
  699. return 0;
  700. }
  701. static int
  702. append_ast_yield(_PyUnicodeWriter *writer, expr_ty e)
  703. {
  704. if (!e->v.Yield.value) {
  705. APPEND_STR_FINISH("(yield)");
  706. }
  707. APPEND_STR("(yield ");
  708. APPEND_EXPR(e->v.Yield.value, PR_TEST);
  709. APPEND_STR_FINISH(")");
  710. }
  711. static int
  712. append_ast_yield_from(_PyUnicodeWriter *writer, expr_ty e)
  713. {
  714. APPEND_STR("(yield from ");
  715. APPEND_EXPR(e->v.YieldFrom.value, PR_TEST);
  716. APPEND_STR_FINISH(")");
  717. }
  718. static int
  719. append_ast_await(_PyUnicodeWriter *writer, expr_ty e, int level)
  720. {
  721. APPEND_STR_IF(level > PR_AWAIT, "(");
  722. APPEND_STR("await ");
  723. APPEND_EXPR(e->v.Await.value, PR_ATOM);
  724. APPEND_STR_IF(level > PR_AWAIT, ")");
  725. return 0;
  726. }
  727. static int
  728. append_named_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
  729. {
  730. APPEND_STR_IF(level > PR_TUPLE, "(");
  731. APPEND_EXPR(e->v.NamedExpr.target, PR_ATOM);
  732. APPEND_STR(" := ");
  733. APPEND_EXPR(e->v.NamedExpr.value, PR_ATOM);
  734. APPEND_STR_IF(level > PR_TUPLE, ")");
  735. return 0;
  736. }
  737. static int
  738. append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
  739. {
  740. switch (e->kind) {
  741. case BoolOp_kind:
  742. return append_ast_boolop(writer, e, level);
  743. case BinOp_kind:
  744. return append_ast_binop(writer, e, level);
  745. case UnaryOp_kind:
  746. return append_ast_unaryop(writer, e, level);
  747. case Lambda_kind:
  748. return append_ast_lambda(writer, e, level);
  749. case IfExp_kind:
  750. return append_ast_ifexp(writer, e, level);
  751. case Dict_kind:
  752. return append_ast_dict(writer, e);
  753. case Set_kind:
  754. return append_ast_set(writer, e);
  755. case GeneratorExp_kind:
  756. return append_ast_genexp(writer, e);
  757. case ListComp_kind:
  758. return append_ast_listcomp(writer, e);
  759. case SetComp_kind:
  760. return append_ast_setcomp(writer, e);
  761. case DictComp_kind:
  762. return append_ast_dictcomp(writer, e);
  763. case Yield_kind:
  764. return append_ast_yield(writer, e);
  765. case YieldFrom_kind:
  766. return append_ast_yield_from(writer, e);
  767. case Await_kind:
  768. return append_ast_await(writer, e, level);
  769. case Compare_kind:
  770. return append_ast_compare(writer, e, level);
  771. case Call_kind:
  772. return append_ast_call(writer, e);
  773. case Constant_kind:
  774. if (e->v.Constant.value == Py_Ellipsis) {
  775. APPEND_STR_FINISH("...");
  776. }
  777. if (e->v.Constant.kind != NULL
  778. && -1 == _PyUnicodeWriter_WriteStr(writer, e->v.Constant.kind)) {
  779. return -1;
  780. }
  781. return append_ast_constant(writer, e->v.Constant.value);
  782. case JoinedStr_kind:
  783. return append_joinedstr(writer, e, false);
  784. case FormattedValue_kind:
  785. return append_formattedvalue(writer, e);
  786. /* The following exprs can be assignment targets. */
  787. case Attribute_kind:
  788. return append_ast_attribute(writer, e);
  789. case Subscript_kind:
  790. return append_ast_subscript(writer, e);
  791. case Starred_kind:
  792. return append_ast_starred(writer, e);
  793. case Slice_kind:
  794. return append_ast_slice(writer, e);
  795. case Name_kind:
  796. return _PyUnicodeWriter_WriteStr(writer, e->v.Name.id);
  797. case List_kind:
  798. return append_ast_list(writer, e);
  799. case Tuple_kind:
  800. return append_ast_tuple(writer, e, level);
  801. case NamedExpr_kind:
  802. return append_named_expr(writer, e, level);
  803. // No default so compiler emits a warning for unhandled cases
  804. }
  805. PyErr_SetString(PyExc_SystemError,
  806. "unknown expression kind");
  807. return -1;
  808. }
  809. static int
  810. maybe_init_static_strings(void)
  811. {
  812. PyInterpreterState *interp = _PyInterpreterState_GET();
  813. if (_str_replace_inf(interp) == NULL) {
  814. PyObject *tmp = PyUnicode_FromFormat("1e%d", 1 + DBL_MAX_10_EXP);
  815. if (tmp == NULL) {
  816. return -1;
  817. }
  818. _str_replace_inf(interp) = tmp;
  819. }
  820. return 0;
  821. }
  822. static PyObject *
  823. expr_as_unicode(expr_ty e, int level)
  824. {
  825. _PyUnicodeWriter writer;
  826. _PyUnicodeWriter_Init(&writer);
  827. writer.min_length = 256;
  828. writer.overallocate = 1;
  829. if (-1 == maybe_init_static_strings() ||
  830. -1 == append_ast_expr(&writer, e, level))
  831. {
  832. _PyUnicodeWriter_Dealloc(&writer);
  833. return NULL;
  834. }
  835. return _PyUnicodeWriter_Finish(&writer);
  836. }
  837. PyObject *
  838. _PyAST_ExprAsUnicode(expr_ty e)
  839. {
  840. return expr_as_unicode(e, PR_TEST);
  841. }