You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

977 lines
26 KiB

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