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.

972 lines
26 KiB

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