mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-05 00:41:00 +01:00
partial proxy partial friendly
This commit is contained in:
parent
d58820ceb1
commit
5e53bb01cc
2 changed files with 24 additions and 3 deletions
14
news/partial.rst
Normal file
14
news/partial.rst
Normal file
|
@ -0,0 +1,14 @@
|
|||
**Added:** None
|
||||
|
||||
**Changed:**
|
||||
|
||||
* Callable alias proxy functions are now more friendly to
|
||||
``functools.partial()``.
|
||||
|
||||
**Deprecated:** None
|
||||
|
||||
**Removed:** None
|
||||
|
||||
**Fixed:** None
|
||||
|
||||
**Security:** None
|
|
@ -1176,19 +1176,26 @@ def proxy_four(f, args, stdin, stdout, stderr, spec):
|
|||
|
||||
|
||||
PROXIES = (proxy_zero, proxy_one, proxy_two, proxy_three, proxy_four)
|
||||
PROXY_KWARG_NAMES = frozenset(['args', 'stdin', 'stdout', 'stderr', 'spec'])
|
||||
|
||||
|
||||
def partial_proxy(f):
|
||||
"""Dispatches the approriate proxy function based on the number of args."""
|
||||
numargs = len(inspect.signature(f).parameters)
|
||||
numargs = 0
|
||||
for name, param in inspect.signature(f).parameters.items():
|
||||
if param.kind == param.POSITIONAL_ONLY or \
|
||||
param.kind == param.POSITIONAL_OR_KEYWORD:
|
||||
numargs += 1
|
||||
elif name in PROXY_KWARG_NAMES and param.kind == param.KEYWORD_ONLY:
|
||||
numargs += 1
|
||||
if numargs < 5:
|
||||
return functools.partial(PROXIES[numargs], f)
|
||||
elif numargs == 5:
|
||||
# don't need to partial.
|
||||
return f
|
||||
else:
|
||||
e = 'Expected proxy with 5 or fewer arguments, not {}'
|
||||
raise XonshError(e.format(numargs))
|
||||
e = 'Expected proxy with 5 or fewer arguments for {}, not {}'
|
||||
raise XonshError(e.format(', '.join(PROXY_KWARG_NAMES), numargs))
|
||||
|
||||
|
||||
class ProcProxyThread(threading.Thread):
|
||||
|
|
Loading…
Add table
Reference in a new issue