|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | from ast import (Call, Name, Subscript, Index, Compare, In, |
9 | | - Tuple, List, Str, Constant, BinOp, LShift, Lambda) |
| 9 | + Tuple, List, Constant, BinOp, LShift, Lambda) |
10 | 10 | import sys |
11 | 11 |
|
| 12 | +from .astcompat import getconstant, Str |
12 | 13 | from .nameutil import isx, make_isxpred |
13 | 14 |
|
14 | 15 | def where(*bindings): |
@@ -105,11 +106,8 @@ def islet(tree, expanded=True): |
105 | 106 | elif not isx(tree.func, _isletf): |
106 | 107 | return False |
107 | 108 | mode = [kw.value for kw in tree.keywords if kw.arg == "mode"] |
108 | | - assert len(mode) == 1 and type(mode[0]) in (Str, Constant) |
109 | | - if type(mode[0]) is Constant: # Python 3.8+: ast.Constant |
110 | | - mode = mode[0].value |
111 | | - else: |
112 | | - mode = mode[0].s |
| 109 | + assert len(mode) == 1 and type(mode[0]) in (Constant, Str) |
| 110 | + mode = getconstant(mode[0]) |
113 | 111 | kwnames = [kw.arg for kw in tree.keywords] |
114 | 112 | if "_envname" in kwnames: |
115 | 113 | return ("{}_decorator".format(kind), mode) # this call was generated by _dletimpl |
@@ -406,7 +404,10 @@ def _setbody(self, newbody): |
406 | 404 | if t == "decorator": |
407 | 405 | raise TypeError("the body of a decorator let form is the body of decorated function, not a subform of the let.") |
408 | 406 | elif t == "lispy_expr": |
409 | | - self._tree.slice.value = newbody |
| 407 | + if sys.version_info >= (3, 9, 0): # Python 3.9+: the Index wrapper is gone. |
| 408 | + self._tree.slice = newbody |
| 409 | + else: |
| 410 | + self._tree.slice.value = newbody |
410 | 411 | else: |
411 | 412 | theexpr = self._theexpr_ref() |
412 | 413 | if t == "in_expr": |
@@ -593,28 +594,22 @@ def _setbindings(self, newbindings): |
593 | 594 | for oldb, newb in zip(thebindings.elts, newbindings.elts): |
594 | 595 | oldk, thev = oldb.elts |
595 | 596 | newk, newv = newb.elts |
596 | | - newk_string = newk.value if type(newk) is Constant else newk.s # Python 3.8+: ast.Constant |
| 597 | + newk_string = getconstant(newk) # Python 3.8+: ast.Constant |
597 | 598 | if type(newv) is not Lambda: |
598 | 599 | raise TypeError("ExpandedLetView: letrec: each value must be of the form `lambda e: ...`") # pragma: no cover |
599 | 600 | if curried: |
600 | 601 | # ((k, currycall(currycall(namelambda, "letrec_binding_YYY"), curryf(lambda e: ...))), ...) |
601 | 602 | # ~~~~~~~~~~~~~~~~~~~~ ^^^^^^^^^^^^^ |
602 | 603 | newv.args.args[0].arg = envname # v0.14.3+: convenience: auto-inject correct envname |
603 | 604 | thev.args[1].args[0] = newv |
604 | | - if type(thev.args[0].args[1]) is Constant: # Python 3.8+: ast.Constant |
605 | | - thev.args[0].args[1].value = "letrec_binding_{}".format(newk_string) |
606 | | - else: # ast.Str |
607 | | - thev.args[0].args[1].s = "letrec_binding_{}".format(newk_string) |
| 605 | + thev.args[0].args[1] = Constant(value="letrec_binding_{}".format(newk_string)) # Python 3.8+: ast.Constant |
608 | 606 | else: |
609 | 607 | # ((k, (namelambda("letrec_binding_YYY"))(lambda e: ...)), ...) |
610 | 608 | # ~~~~~~~~~~~~~~~~~~~~ ^^^^^^^^^^^^^ |
611 | 609 | newv.args.args[0].arg = envname # v0.14.3+: convenience: auto-inject correct envname |
612 | 610 | thev.args[0] = newv |
613 | 611 | # update name in the namelambda(...) |
614 | | - if type(thev.func.args[0]) is Constant: # Python 3.8+: ast.Constant |
615 | | - thev.func.args[0].value = "letrec_binding_{}".format(newk_string) |
616 | | - else: |
617 | | - thev.func.args[0].s = "letrec_binding_{}".format(newk_string) |
| 612 | + thev.func.args[0] = Constant(value="letrec_binding_{}".format(newk_string)) # Python 3.8+: ast.Constant |
618 | 613 | # Macro-generated nodes may be missing source location information, |
619 | 614 | # in which case we let MacroPy fix it later. |
620 | 615 | # This is mainly an issue for the unit tests of this module, which macro-generate the "old" data. |
|
0 commit comments