Skip to content

Commit 0273c4b

Browse files
committed
porting: remove make_isxpred, now obsolete
`mcpyrate` does not need to rename hygienically captured values, so the name will always match exactly. If the use site needs something more complex, `isx` already supports that: just pass a predicate instead of a string as the name to be detected.
1 parent 5d070b5 commit 0273c4b

8 files changed

Lines changed: 25 additions & 46 deletions

File tree

unpythonic/syntax/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
# However, 0.15.0 is the initial version that runs on `mcpyrate`, and the focus is to just get this running.
9090
# Cleanups can be done in a future release.
9191

92-
# TODO: `make_isxpred` is now obsolete because `mcpyrate` does not rename hygienic captures of run-time values. Make it explicit at the use sites what they want, and remove `make_isxpred`. (E.g. `curry` wants to match both `curryf` and `currycall`, exactly. Some use sites want to match only a single thing.)
9392

9493
# TODO: `let` constructs: document difference to Python 3.8 walrus operator (`let` creates a scope, `:=` doesn't)
9594

unpythonic/syntax/lambdatools.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616

1717
from ..dynassign import dyn
1818
from ..misc import namelambda
19-
from ..fun import orf
2019
from ..env import env
2120

2221
from .astcompat import getconstant, Str, NamedExpr
2322
from .letdo import do
2423
from .letdoutil import islet, isenvassign, UnexpandedLetView, UnexpandedEnvAssignView, ExpandedDoView
25-
from .util import (is_decorated_lambda, isx, make_isxpred, has_deco,
24+
from .util import (is_decorated_lambda, isx, has_deco,
2625
destructure_decorated_lambda, detect_lambda)
2726

2827
def multilambda(block_body):
@@ -53,18 +52,17 @@ def issingleassign(tree):
5352
return type(tree) is Assign and len(tree.targets) == 1 and type(tree.targets[0]) is Name
5453

5554
# detect a manual curry
56-
iscurry = make_isxpred("curry")
5755
def iscurrywithfinallambda(tree):
58-
if not (type(tree) is Call and isx(tree.func, iscurry) and tree.args):
56+
if not (type(tree) is Call and isx(tree.func, "curry") and tree.args):
5957
return False
6058
return type(tree.args[-1]) is Lambda
6159

6260
# Detect an autocurry from an already expanded "with autocurry".
6361
# CAUTION: These must match what unpythonic.syntax.curry.autocurry uses in its output.
64-
iscurrycall = make_isxpred("currycall")
65-
iscurryf = orf(make_isxpred("curryf"), make_isxpred("curry")) # auto or manual curry in a "with autocurry"
62+
currycall_name = "currycall"
63+
iscurryf = lambda name: name in ("curryf", "curry") # auto or manual curry in a "with autocurry"
6664
def isautocurrywithfinallambda(tree):
67-
if not (type(tree) is Call and isx(tree.func, iscurrycall) and tree.args and
65+
if not (type(tree) is Call and isx(tree.func, currycall_name) and tree.args and
6866
type(tree.args[-1]) is Call and isx(tree.args[-1].func, iscurryf)):
6967
return False
7068
return type(tree.args[-1].args[-1]) is Lambda
@@ -235,7 +233,6 @@ def isfunctionoruserlambda(tree):
235233

236234
# Create a renamed reference to the env() constructor to be sure the Call
237235
# nodes added by us have a unique .func (not used by other macros or user code)
238-
_ismakeenv = make_isxpred("_envify")
239236
_envify = env
240237

241238
class EnvifyTransformer(ASTTransformer):
@@ -284,7 +281,7 @@ def isourupdate(thecall):
284281
self.generic_withstate(tree, enames=(enames + [ename]), bindings=newbindings)
285282
else:
286283
# leave alone the _envify() added by us
287-
if type(tree) is Call and (isx(tree.func, _ismakeenv) or isourupdate(tree)):
284+
if type(tree) is Call and (isx(tree.func, "_envify") or isourupdate(tree)):
288285
# don't recurse
289286
return tree
290287
# transform env-assignments into our envs

unpythonic/syntax/lazify.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from mcpyrate.walkers import ASTTransformer
1111

1212
from .util import (suggest_decorator_index, sort_lambda_decorators, detect_lambda,
13-
isx, make_isxpred, getname, is_decorator, wrapwith)
13+
isx, getname, is_decorator, wrapwith)
1414
from .letdoutil import islet, isdo, ExpandedLetView
1515
from ..lazyutil import Lazy, passthrough_lazy_args, force, force1, maybe_force_args
1616
from ..dynassign import dyn
@@ -87,8 +87,8 @@ def lazy(tree):
8787
# variant `frozendict(mapping1, mapping2, ...)`.
8888
_ctorcalls_that_take_exactly_one_positional_arg = {"tuple", "list", "set", "dict", "frozenset", "llist"}
8989

90-
islazy = make_isxpred("lazy") # unexpanded
91-
isLazy = make_isxpred("Lazy") # expanded
90+
unexpanded_lazy_name = "lazy"
91+
expanded_lazy_name = "Lazy"
9292
def lazyrec(tree):
9393
# This helper doesn't need to recurse, so we don't need `ASTTransformer` here.
9494
def transform(tree):
@@ -99,9 +99,9 @@ def transform(tree):
9999
elif type(tree) is Call and any(isx(tree.func, ctor) for ctor in _ctorcalls_all):
100100
p, k = _ctor_handling_modes[getname(tree.func)]
101101
lazify_ctorcall(tree, p, k)
102-
elif type(tree) is Subscript and isx(tree.value, islazy): # unexpanded
102+
elif type(tree) is Subscript and isx(tree.value, unexpanded_lazy_name):
103103
pass
104-
elif type(tree) is Call and isx(tree.func, isLazy): # expanded
104+
elif type(tree) is Call and isx(tree.func, expanded_lazy_name):
105105
pass
106106
else:
107107
# mcpyrate supports hygienic macro capture, so we can just splice unexpanded
@@ -293,7 +293,7 @@ def transform_starred(tree, dstarred=False):
293293
# Lazy() is a strict function, takes a lambda, constructs a Lazy object
294294
# _autoref_resolve doesn't need any special handling
295295
elif (isdo(tree) or is_decorator(tree.func, "namelambda") or
296-
any(isx(tree.func, s) for s in _ctorcalls_all) or isx(tree.func, isLazy) or
296+
any(isx(tree.func, s) for s in _ctorcalls_all) or isx(tree.func, expanded_lazy_name) or
297297
any(isx(tree.func, s) for s in ("_autoref_resolve", "AutorefMarker"))):
298298
# here we know the operator (.func) to be one of specific names;
299299
# don't transform it to avoid confusing lazyrec[] (important if this

unpythonic/syntax/letdoutil.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
import sys
1111

1212
from .astcompat import getconstant, Str
13-
from .nameutil import isx, make_isxpred
13+
from .nameutil import isx
1414

1515
def where(*bindings):
1616
"""[syntax] Only meaningful in a let[body, where((k0, v0), ...)]."""
1717
raise RuntimeError("where() is only meaningful in a let[body, where((k0, v0), ...)]") # pragma: no cover
1818

19-
_isletf = make_isxpred("letter") # name must match what ``unpythonic.syntax.letdo._letimpl`` uses in its output.
20-
_isdof = make_isxpred("dof") # name must match what ``unpythonic.syntax.letdo.do`` uses in its output.
21-
_iscurrycall = make_isxpred("currycall") # output of ``unpythonic.syntax.curry``
19+
letf_name = "letter" # must match what ``unpythonic.syntax.letdo._letimpl`` uses in its output.
20+
dof_name = "dof" # name must match what ``unpythonic.syntax.letdo.do`` uses in its output.
21+
currycall_name = "currycall" # output of ``unpythonic.syntax.curry``
2222

2323
# TODO: switch from call to subscript in name position for let_syntax templates.
2424
def canonize_bindings(elts, allow_call_in_name_position=False): # public as of v0.14.3+
@@ -98,9 +98,9 @@ def islet(tree, expanded=True):
9898
if type(tree) is not Call:
9999
return False
100100
kind = "expanded"
101-
if isx(tree.func, _iscurrycall) and isx(tree.args[0], _isletf):
101+
if isx(tree.func, currycall_name) and isx(tree.args[0], letf_name):
102102
kind = "curried"
103-
elif not isx(tree.func, _isletf):
103+
elif not isx(tree.func, letf_name):
104104
return False
105105
mode = [kw.value for kw in tree.keywords if kw.arg == "mode"]
106106
assert len(mode) == 1 and type(mode[0]) in (Constant, Str)
@@ -203,9 +203,9 @@ def isdo(tree, expanded=True):
203203
if type(tree) is not Call:
204204
return False
205205
kind = "expanded"
206-
if isx(tree.func, _iscurrycall) and isx(tree.args[0], _isdof):
206+
if isx(tree.func, currycall_name) and isx(tree.args[0], dof_name):
207207
kind = "curried"
208-
elif not isx(tree.func, _isdof):
208+
elif not isx(tree.func, dof_name):
209209
return False
210210
return kind
211211

unpythonic/syntax/nameutil.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,6 @@ def isx(tree, x, accept_attr=True):
6666
(key and ismatch(name)) or
6767
(accept_attr and type(tree) is Attribute and ismatch(tree.attr)))
6868

69-
# TODO: obsolete function, remove
70-
def make_isxpred(x):
71-
"""Make a predicate for isx.
72-
73-
Here ``x`` is an ``str``; the resulting function will match also
74-
hygienically captured identifiers.
75-
"""
76-
# `mcpyrate` only renames captured macros; the names of captured
77-
# run-time values live in the keys in the `lookup_value` calls
78-
# (where the original name is preserved, with no renaming needed).
79-
return lambda name: name == x
80-
8169
def getname(tree, accept_attr=True):
8270
"""The cousin of ``isx``.
8371

unpythonic/syntax/tailtools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from mcpyrate.walkers import ASTTransformer, ASTVisitor
2424

2525
from .astcompat import getconstant, NameConstant
26-
from .util import (isx, make_isxpred, isec,
26+
from .util import (isx, isec,
2727
detect_callec, detect_lambda,
2828
has_tco, sort_lambda_decorators,
2929
suggest_decorator_index, ContinuationsMarker, wrapwith, isexpandedmacromarker)
@@ -601,7 +601,7 @@ def transform(self, tree):
601601
# Tail-position analysis for a return-value expression (also the body of a lambda).
602602
# Here we need to be very, very selective about where to recurse so this would not
603603
# benefit much from being made into an ASTTransformer. Just a function is fine.
604-
_isjump = orf(make_isxpred("jump"), make_isxpred("loop"))
604+
_isjump = lambda name: name in ("jump", "loop")
605605
def _transform_retexpr(tree, known_ecs, call_cb=None, data_cb=None):
606606
"""Analyze and TCO a return-value expression or a lambda body.
607607

unpythonic/syntax/tests/test_nameutil.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from mcpyrate.quotes import macros, q, h # noqa: F401, F811
88

9-
from ...syntax.nameutil import isx, make_isxpred, getname
9+
from ...syntax.nameutil import isx, getname
1010

1111
from ast import Call
1212

@@ -26,11 +26,6 @@ def runtests():
2626
test[isx(attribute, "ok")]
2727
test[not isx(attribute, "ok", accept_attr=False)]
2828

29-
with testset("make_isxpred"):
30-
isfab = make_isxpred("fab")
31-
test[isx(q[fab], isfab)] # noqa: F821
32-
test[isx(q[someobj.fab], isfab)] # noqa: F821
33-
3429
with testset("getname"):
3530
test[getname(barename) == "ok"]
3631
test[getname(captured.func) == "capture_this"]

unpythonic/syntax/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from .astcompat import getconstant
1313
from .letdoutil import isdo, ExpandedDoView
14-
from .nameutil import isx, make_isxpred, getname
14+
from .nameutil import isx, getname
1515

1616
from ..regutil import all_decorators, tco_decorators, decorator_registry
1717

@@ -52,7 +52,7 @@ def g(ec): # <-- should grab from here
5252
and `throw` covers the use of `unpythonic.ec.throw`.)
5353
"""
5454
fallbacks = ["ec", "brk", "throw"]
55-
iscallec = partial(isx, x=make_isxpred("call_ec"))
55+
iscallec = partial(isx, x="call_ec")
5656
def detect(tree):
5757
class Detector(ASTVisitor):
5858
def examine(self, tree):

0 commit comments

Comments
 (0)