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(dbl_open_br, "{{");
  12. _Py_DECLARE_STR(dbl_close_br, "}}");
  13. /* We would statically initialize this if doing so were simple enough. */
  14. #define _str_replace_inf(interp) \
  15. _Py_INTERP_CACHED_OBJECT(interp, str_replace_inf)
  16. /* Forward declarations for recursion via helper functions. */
  17. static PyObject *
  18. expr_as_unicode(expr_ty e, int level);
  19. static int
  20. append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level);
  21. static int
  22. append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec);
  23. static int
  24. append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e);
  25. static int
  26. append_ast_slice(_PyUnicodeWriter *writer, expr_ty e);
  27. static int
  28. append_charp(_PyUnicodeWriter *writer, const char *charp)
  29. {
  30. return _PyUnicodeWriter_WriteASCIIString(writer, charp, -1);
  31. }
  32. #define APPEND_STR_FINISH(str) do { \
  33. return append_charp(writer, (str)); \
  34. } while (0)
  35. #define APPEND_STR(str) do { \
  36. if (-1 == append_charp(writer, (str))) { \
  37. return -1; \
  38. } \
  39. } while (0)
  40. #define APPEND_STR_IF(cond, str) do { \
  41. if ((cond) && -1 == append_charp(writer, (str))) { \
  42. return -1; \
  43. } \
  44. } while (0)
  45. #define APPEND_STR_IF_NOT_FIRST(str) do { \
  46. APPEND_STR_IF(!first, (str)); \
  47. first = false; \
  48. } while (0)
  49. #define APPEND_EXPR(expr, pr) do { \
  50. if (-1 == append_ast_expr(writer, (expr), (pr))) { \
  51. return -1; \
  52. } \
  53. } while (0)
  54. #define APPEND(type, value) do { \
  55. if (-1 == append_ast_ ## type(writer, (value))) { \
  56. return -1; \
  57. } \
  58. } while (0)
  59. static int
  60. append_repr(_PyUnicodeWriter *writer, PyObject *obj)
  61. {
  62. PyObject *repr = PyObject_Repr(obj);
  63. if (!repr) {
  64. return -1;
  65. }
  66. if ((PyFloat_CheckExact(obj) && Py_IS_INFINITY(PyFloat_AS_DOUBLE(obj))) ||
  67. PyComplex_CheckExact(obj))
  68. {
  69. PyInterpreterState *interp = _PyInterpreterState_GET();
  70. PyObject *new_repr = PyUnicode_Replace(
  71. repr,
  72. &_Py_ID(inf),
  73. _str_replace_inf(interp),
  74. -1
  75. );
  76. Py_DECREF(repr);
  77. if (!new_repr) {
  78. return -1;
  79. }
  80. repr = new_repr;
  81. }
  82. int ret = _PyUnicodeWriter_WriteStr(writer, repr);
  83. Py_DECREF(repr);
  84. return ret;
  85. }
  86. /* Priority levels */
  87. enum {
  88. PR_TUPLE,
  89. PR_TEST, /* 'if'-'else', 'lambda' */
  90. PR_OR, /* 'or' */
  91. PR_AND, /* 'and' */
  92. PR_NOT, /* 'not' */
  93. PR_CMP, /* '<', '>', '==', '>=', '<=', '!=',
  94. 'in', 'not in', 'is', 'is not' */
  95. PR_EXPR,
  96. PR_BOR = PR_EXPR, /* '|' */
  97. PR_BXOR, /* '^' */
  98. PR_BAND, /* '&' */
  99. PR_SHIFT, /* '<<', '>>' */
  100. PR_ARITH, /* '+', '-' */
  101. PR_TERM, /* '*', '@', '/', '%', '//' */
  102. PR_FACTOR, /* unary '+', '-', '~' */
  103. PR_POWER, /* '**' */
  104. PR_AWAIT, /* 'await' */
  105. PR_ATOM,
  106. };
  107. static int
  108. append_ast_boolop(_PyUnicodeWriter *writer, expr_ty e, int level)
  109. {
  110. Py_ssize_t i, value_count;
  111. asdl_expr_seq *values;
  112. const char *op = (e->v.BoolOp.op == And) ? " and " : " or ";
  113. int pr = (e->v.BoolOp.op == And) ? PR_AND : PR_OR;
  114. APPEND_STR_IF(level > pr, "(");
  115. values = e->v.BoolOp.values;
  116. value_count = asdl_seq_LEN(values);
  117. for (i = 0; i < value_count; ++i) {
  118. APPEND_STR_IF(i > 0, op);
  119. APPEND_EXPR((expr_ty)asdl_seq_GET(values, i), pr + 1);
  120. }
  121. APPEND_STR_IF(level > pr, ")");
  122. return 0;
  123. }
  124. static int
  125. append_ast_binop(_PyUnicodeWriter *writer, expr_ty e, int level)
  126. {
  127. const char *op;
  128. int pr;
  129. bool rassoc = false; /* is right-associative? */
  130. switch (e->v.BinOp.op) {
  131. case Add: op = " + "; pr = PR_ARITH; break;
  132. case Sub: op = " - "; pr = PR_ARITH; break;
  133. case Mult: op = " * "; pr = PR_TERM; break;
  134. case MatMult: op = " @ "; pr = PR_TERM; break;
  135. case Div: op = " / "; pr = PR_TERM; break;
  136. case Mod: op = " % "; pr = PR_TERM; break;
  137. case LShift: op = " << "; pr = PR_SHIFT; break;
  138. case RShift: op = " >> "; pr = PR_SHIFT; break;
  139. case BitOr: op = " | "; pr = PR_BOR; break;
  140. case BitXor: op = " ^ "; pr = PR_BXOR; break;
  141. case BitAnd: op = " & "; pr = PR_BAND; break;
  142. case FloorDiv: op = " // "; pr = PR_TERM; break;
  143. case Pow: op = " ** "; pr = PR_POWER; rassoc = true; break;
  144. default:
  145. PyErr_SetString(PyExc_SystemError,
  146. "unknown binary operator");
  147. return -1;
  148. }
  149. APPEND_STR_IF(level > pr, "(");
  150. APPEND_EXPR(e->v.BinOp.left, pr + rassoc);
  151. APPEND_STR(op);
  152. APPEND_EXPR(e->v.BinOp.right, pr + !rassoc);
  153. APPEND_STR_IF(level > pr, ")");
  154. return 0;
  155. }
  156. static int
  157. append_ast_unaryop(_PyUnicodeWriter *writer, expr_ty e, int level)
  158. {
  159. const char *op;
  160. int pr;
  161. switch (e->v.UnaryOp.op) {
  162. case Invert: op = "~"; pr = PR_FACTOR; break;
  163. case Not: op = "not "; pr = PR_NOT; break;
  164. case UAdd: op = "+"; pr = PR_FACTOR; break;
  165. case USub: op = "-"; pr = PR_FACTOR; break;
  166. default:
  167. PyErr_SetString(PyExc_SystemError,
  168. "unknown unary operator");
  169. return -1;
  170. }
  171. APPEND_STR_IF(level > pr, "(");
  172. APPEND_STR(op);
  173. APPEND_EXPR(e->v.UnaryOp.operand, pr);
  174. APPEND_STR_IF(level > pr, ")");
  175. return 0;
  176. }
  177. static int
  178. append_ast_arg(_PyUnicodeWriter *writer, arg_ty arg)
  179. {
  180. if (-1 == _PyUnicodeWriter_WriteStr(writer, arg->arg)) {
  181. return -1;
  182. }
  183. if (arg->annotation) {
  184. APPEND_STR(": ");
  185. APPEND_EXPR(arg->annotation, PR_TEST);
  186. }
  187. return 0;
  188. }
  189. static int
  190. append_ast_args(_PyUnicodeWriter *writer, arguments_ty args)
  191. {
  192. bool first;
  193. Py_ssize_t i, di, arg_count, posonlyarg_count, default_count;
  194. first = true;
  195. /* positional-only and positional arguments with defaults */
  196. posonlyarg_count = asdl_seq_LEN(args->posonlyargs);
  197. arg_count = asdl_seq_LEN(args->args);
  198. default_count = asdl_seq_LEN(args->defaults);
  199. for (i = 0; i < posonlyarg_count + arg_count; i++) {
  200. APPEND_STR_IF_NOT_FIRST(", ");
  201. if (i < posonlyarg_count){
  202. APPEND(arg, (arg_ty)asdl_seq_GET(args->posonlyargs, i));
  203. } else {
  204. APPEND(arg, (arg_ty)asdl_seq_GET(args->args, i-posonlyarg_count));
  205. }
  206. di = i - posonlyarg_count - arg_count + default_count;
  207. if (di >= 0) {
  208. APPEND_STR("=");
  209. APPEND_EXPR((expr_ty)asdl_seq_GET(args->defaults, di), PR_TEST);
  210. }
  211. if (posonlyarg_count && i + 1 == posonlyarg_count) {
  212. APPEND_STR(", /");
  213. }
  214. }
  215. /* vararg, or bare '*' if no varargs but keyword-only arguments present */
  216. if (args->vararg || asdl_seq_LEN(args->kwonlyargs)) {
  217. APPEND_STR_IF_NOT_FIRST(", ");
  218. APPEND_STR("*");
  219. if (args->vararg) {
  220. APPEND(arg, args->vararg);
  221. }
  222. }
  223. /* keyword-only arguments */
  224. arg_count = asdl_seq_LEN(args->kwonlyargs);
  225. default_count = asdl_seq_LEN(args->kw_defaults);
  226. for (i = 0; i < arg_count; i++) {
  227. APPEND_STR_IF_NOT_FIRST(", ");
  228. APPEND(arg, (arg_ty)asdl_seq_GET(args->kwonlyargs, i));
  229. di = i - arg_count + default_count;
  230. if (di >= 0) {
  231. expr_ty default_ = (expr_ty)asdl_seq_GET(args->kw_defaults, di);
  232. if (default_) {
  233. APPEND_STR("=");
  234. APPEND_EXPR(default_, PR_TEST);
  235. }
  236. }
  237. }
  238. /* **kwargs */
  239. if (args->kwarg) {
  240. APPEND_STR_IF_NOT_FIRST(", ");
  241. APPEND_STR("**");
  242. APPEND(arg, args->kwarg);
  243. }
  244. return 0;
  245. }
  246. static int
  247. append_ast_lambda(_PyUnicodeWriter *writer, expr_ty e, int level)
  248. {
  249. APPEND_STR_IF(level > PR_TEST, "(");
  250. Py_ssize_t n_positional = (asdl_seq_LEN(e->v.Lambda.args->args) +
  251. asdl_seq_LEN(e->v.Lambda.args->posonlyargs));
  252. APPEND_STR(n_positional ? "lambda " : "lambda");
  253. APPEND(args, e->v.Lambda.args);
  254. APPEND_STR(": ");
  255. APPEND_EXPR(e->v.Lambda.body, PR_TEST);
  256. APPEND_STR_IF(level > PR_TEST, ")");
  257. return 0;
  258. }
  259. static int
  260. append_ast_ifexp(_PyUnicodeWriter *writer, expr_ty e, int level)
  261. {
  262. APPEND_STR_IF(level > PR_TEST, "(");
  263. APPEND_EXPR(e->v.IfExp.body, PR_TEST + 1);
  264. APPEND_STR(" if ");
  265. APPEND_EXPR(e->v.IfExp.test, PR_TEST + 1);
  266. APPEND_STR(" else ");
  267. APPEND_EXPR(e->v.IfExp.orelse, PR_TEST);
  268. APPEND_STR_IF(level > PR_TEST, ")");
  269. return 0;
  270. }
  271. static int
  272. append_ast_dict(_PyUnicodeWriter *writer, expr_ty e)
  273. {
  274. Py_ssize_t i, value_count;
  275. expr_ty key_node;
  276. APPEND_STR("{");
  277. value_count = asdl_seq_LEN(e->v.Dict.values);
  278. for (i = 0; i < value_count; i++) {
  279. APPEND_STR_IF(i > 0, ", ");
  280. key_node = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i);
  281. if (key_node != NULL) {
  282. APPEND_EXPR(key_node, PR_TEST);
  283. APPEND_STR(": ");
  284. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Dict.values, i), PR_TEST);
  285. }
  286. else {
  287. APPEND_STR("**");
  288. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Dict.values, i), PR_EXPR);
  289. }
  290. }
  291. APPEND_STR_FINISH("}");
  292. }
  293. static int
  294. append_ast_set(_PyUnicodeWriter *writer, expr_ty e)
  295. {
  296. Py_ssize_t i, elem_count;
  297. APPEND_STR("{");
  298. elem_count = asdl_seq_LEN(e->v.Set.elts);
  299. for (i = 0; i < elem_count; i++) {
  300. APPEND_STR_IF(i > 0, ", ");
  301. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Set.elts, i), PR_TEST);
  302. }
  303. APPEND_STR_FINISH("}");
  304. }
  305. static int
  306. append_ast_list(_PyUnicodeWriter *writer, expr_ty e)
  307. {
  308. Py_ssize_t i, elem_count;
  309. APPEND_STR("[");
  310. elem_count = asdl_seq_LEN(e->v.List.elts);
  311. for (i = 0; i < elem_count; i++) {
  312. APPEND_STR_IF(i > 0, ", ");
  313. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.List.elts, i), PR_TEST);
  314. }
  315. APPEND_STR_FINISH("]");
  316. }
  317. static int
  318. append_ast_tuple(_PyUnicodeWriter *writer, expr_ty e, int level)
  319. {
  320. Py_ssize_t i, elem_count;
  321. elem_count = asdl_seq_LEN(e->v.Tuple.elts);
  322. if (elem_count == 0) {
  323. APPEND_STR_FINISH("()");
  324. }
  325. APPEND_STR_IF(level > PR_TUPLE, "(");
  326. for (i = 0; i < elem_count; i++) {
  327. APPEND_STR_IF(i > 0, ", ");
  328. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Tuple.elts, i), PR_TEST);
  329. }
  330. APPEND_STR_IF(elem_count == 1, ",");
  331. APPEND_STR_IF(level > PR_TUPLE, ")");
  332. return 0;
  333. }
  334. static int
  335. append_ast_comprehension(_PyUnicodeWriter *writer, comprehension_ty gen)
  336. {
  337. Py_ssize_t i, if_count;
  338. APPEND_STR(gen->is_async ? " async for " : " for ");
  339. APPEND_EXPR(gen->target, PR_TUPLE);
  340. APPEND_STR(" in ");
  341. APPEND_EXPR(gen->iter, PR_TEST + 1);
  342. if_count = asdl_seq_LEN(gen->ifs);
  343. for (i = 0; i < if_count; i++) {
  344. APPEND_STR(" if ");
  345. APPEND_EXPR((expr_ty)asdl_seq_GET(gen->ifs, i), PR_TEST + 1);
  346. }
  347. return 0;
  348. }
  349. static int
  350. append_ast_comprehensions(_PyUnicodeWriter *writer, asdl_comprehension_seq *comprehensions)
  351. {
  352. Py_ssize_t i, gen_count;
  353. gen_count = asdl_seq_LEN(comprehensions);
  354. for (i = 0; i < gen_count; i++) {
  355. APPEND(comprehension, (comprehension_ty)asdl_seq_GET(comprehensions, i));
  356. }
  357. return 0;
  358. }
  359. static int
  360. append_ast_genexp(_PyUnicodeWriter *writer, expr_ty e)
  361. {
  362. APPEND_STR("(");
  363. APPEND_EXPR(e->v.GeneratorExp.elt, PR_TEST);
  364. APPEND(comprehensions, e->v.GeneratorExp.generators);
  365. APPEND_STR_FINISH(")");
  366. }
  367. static int
  368. append_ast_listcomp(_PyUnicodeWriter *writer, expr_ty e)
  369. {
  370. APPEND_STR("[");
  371. APPEND_EXPR(e->v.ListComp.elt, PR_TEST);
  372. APPEND(comprehensions, e->v.ListComp.generators);
  373. APPEND_STR_FINISH("]");
  374. }
  375. static int
  376. append_ast_setcomp(_PyUnicodeWriter *writer, expr_ty e)
  377. {
  378. APPEND_STR("{");
  379. APPEND_EXPR(e->v.SetComp.elt, PR_TEST);
  380. APPEND(comprehensions, e->v.SetComp.generators);
  381. APPEND_STR_FINISH("}");
  382. }
  383. static int
  384. append_ast_dictcomp(_PyUnicodeWriter *writer, expr_ty e)
  385. {
  386. APPEND_STR("{");
  387. APPEND_EXPR(e->v.DictComp.key, PR_TEST);
  388. APPEND_STR(": ");
  389. APPEND_EXPR(e->v.DictComp.value, PR_TEST);
  390. APPEND(comprehensions, e->v.DictComp.generators);
  391. APPEND_STR_FINISH("}");
  392. }
  393. static int
  394. append_ast_compare(_PyUnicodeWriter *writer, expr_ty e, int level)
  395. {
  396. const char *op;
  397. Py_ssize_t i, comparator_count;
  398. asdl_expr_seq *comparators;
  399. asdl_int_seq *ops;
  400. APPEND_STR_IF(level > PR_CMP, "(");
  401. comparators = e->v.Compare.comparators;
  402. ops = e->v.Compare.ops;
  403. comparator_count = asdl_seq_LEN(comparators);
  404. assert(comparator_count > 0);
  405. assert(comparator_count == asdl_seq_LEN(ops));
  406. APPEND_EXPR(e->v.Compare.left, PR_CMP + 1);
  407. for (i = 0; i < comparator_count; i++) {
  408. switch ((cmpop_ty)asdl_seq_GET(ops, i)) {
  409. case Eq:
  410. op = " == ";
  411. break;
  412. case NotEq:
  413. op = " != ";
  414. break;
  415. case Lt:
  416. op = " < ";
  417. break;
  418. case LtE:
  419. op = " <= ";
  420. break;
  421. case Gt:
  422. op = " > ";
  423. break;
  424. case GtE:
  425. op = " >= ";
  426. break;
  427. case Is:
  428. op = " is ";
  429. break;
  430. case IsNot:
  431. op = " is not ";
  432. break;
  433. case In:
  434. op = " in ";
  435. break;
  436. case NotIn:
  437. op = " not in ";
  438. break;
  439. default:
  440. PyErr_SetString(PyExc_SystemError,
  441. "unexpected comparison kind");
  442. return -1;
  443. }
  444. APPEND_STR(op);
  445. APPEND_EXPR((expr_ty)asdl_seq_GET(comparators, i), PR_CMP + 1);
  446. }
  447. APPEND_STR_IF(level > PR_CMP, ")");
  448. return 0;
  449. }
  450. static int
  451. append_ast_keyword(_PyUnicodeWriter *writer, keyword_ty kw)
  452. {
  453. if (kw->arg == NULL) {
  454. APPEND_STR("**");
  455. }
  456. else {
  457. if (-1 == _PyUnicodeWriter_WriteStr(writer, kw->arg)) {
  458. return -1;
  459. }
  460. APPEND_STR("=");
  461. }
  462. APPEND_EXPR(kw->value, PR_TEST);
  463. return 0;
  464. }
  465. static int
  466. append_ast_call(_PyUnicodeWriter *writer, expr_ty e)
  467. {
  468. bool first;
  469. Py_ssize_t i, arg_count, kw_count;
  470. expr_ty expr;
  471. APPEND_EXPR(e->v.Call.func, PR_ATOM);
  472. arg_count = asdl_seq_LEN(e->v.Call.args);
  473. kw_count = asdl_seq_LEN(e->v.Call.keywords);
  474. if (arg_count == 1 && kw_count == 0) {
  475. expr = (expr_ty)asdl_seq_GET(e->v.Call.args, 0);
  476. if (expr->kind == GeneratorExp_kind) {
  477. /* Special case: a single generator expression. */
  478. return append_ast_genexp(writer, expr);
  479. }
  480. }
  481. APPEND_STR("(");
  482. first = true;
  483. for (i = 0; i < arg_count; i++) {
  484. APPEND_STR_IF_NOT_FIRST(", ");
  485. APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Call.args, i), PR_TEST);
  486. }
  487. for (i = 0; i < kw_count; i++) {
  488. APPEND_STR_IF_NOT_FIRST(", ");
  489. APPEND(keyword, (keyword_ty)asdl_seq_GET(e->v.Call.keywords, i));
  490. }
  491. APPEND_STR_FINISH(")");
  492. }
  493. static PyObject *
  494. escape_braces(PyObject *orig)
  495. {
  496. PyObject *temp;
  497. PyObject *result;
  498. temp = PyUnicode_Replace(orig, _Py_LATIN1_CHR('{'),
  499. &_Py_STR(dbl_open_br), -1);
  500. if (!temp) {
  501. return NULL;
  502. }
  503. result = PyUnicode_Replace(temp, _Py_LATIN1_CHR('}'),
  504. &_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_LATIN1_CHR('{'), 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. }