diff options
author | Nikita Slyusarev <nslus@yandex-team.com> | 2022-02-10 16:46:52 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:52 +0300 |
commit | cd77cecfc03a3eaf87816af28a33067c4f0cdb59 (patch) | |
tree | 1308e0bae862d52e0020d881fe758080437fe389 /contrib/python/prompt-toolkit/py2/prompt_toolkit/utils.py | |
parent | cdae02d225fb5b3afbb28990e79a7ac6c9125327 (diff) | |
download | ydb-cd77cecfc03a3eaf87816af28a33067c4f0cdb59.tar.gz |
Restoring authorship annotation for Nikita Slyusarev <nslus@yandex-team.com>. Commit 1 of 2.
Diffstat (limited to 'contrib/python/prompt-toolkit/py2/prompt_toolkit/utils.py')
-rw-r--r-- | contrib/python/prompt-toolkit/py2/prompt_toolkit/utils.py | 184 |
1 files changed, 92 insertions, 92 deletions
diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/utils.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/utils.py index 3cd931883cc..233821e6ff4 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/utils.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/utils.py @@ -1,17 +1,17 @@ from __future__ import unicode_literals -import inspect +import inspect import os import signal import sys import threading -import weakref +import weakref from wcwidth import wcwidth -from six.moves import range +from six.moves import range __all__ = ( - 'Event', + 'Event', 'DummyContext', 'get_cwidth', 'suspend_to_background_supported', @@ -19,58 +19,58 @@ __all__ = ( 'is_windows', 'in_main_thread', 'take_using_weights', - 'test_callable_args', + 'test_callable_args', ) -class Event(object): +class Event(object): """ - Simple event to which event handlers can be attached. For instance:: + Simple event to which event handlers can be attached. For instance:: - class Cls: - def __init__(self): - # Define event. The first parameter is the sender. - self.event = Event(self) + class Cls: + def __init__(self): + # Define event. The first parameter is the sender. + self.event = Event(self) - obj = Cls() + obj = Cls() - def handler(sender): - pass + def handler(sender): + pass - # Add event handler by using the += operator. - obj.event += handler - - # Fire event. - obj.event() + # Add event handler by using the += operator. + obj.event += handler + + # Fire event. + obj.event() """ - def __init__(self, sender, handler=None): - self.sender = sender - self._handlers = [] - - if handler is not None: - self += handler + def __init__(self, sender, handler=None): + self.sender = sender + self._handlers = [] - def __call__(self): - " Fire event. " + if handler is not None: + self += handler + + def __call__(self): + " Fire event. " for handler in self._handlers: - handler(self.sender) - - def fire(self): - " Alias for just calling the event. " - self() + handler(self.sender) + def fire(self): + " Alias for just calling the event. " + self() + def __iadd__(self, handler): """ Add another handler to this callback. - (Handler should be a callable that takes exactly one parameter: the - sender object.) + (Handler should be a callable that takes exactly one parameter: the + sender object.) """ - # Test handler. - assert callable(handler) - if not test_callable_args(handler, [None]): - raise TypeError("%r doesn't take exactly one argument." % handler) - - # Add to list of event handlers. + # Test handler. + assert callable(handler) + if not test_callable_args(handler, [None]): + raise TypeError("%r doesn't take exactly one argument." % handler) + + # Add to list of event handlers. self._handlers.append(handler) return self @@ -82,53 +82,53 @@ class Event(object): return self -# Cache of signatures. Improves the performance of `test_callable_args`. -_signatures_cache = weakref.WeakKeyDictionary() - - -def test_callable_args(func, args): - """ - Return True when this function can be called with the given arguments. - """ - assert isinstance(args, (list, tuple)) - signature = getattr(inspect, 'signature', None) - - if signature is not None: - # For Python 3, use inspect.signature. - try: - sig = _signatures_cache[func] - except KeyError: - sig = signature(func) - _signatures_cache[func] = sig - - try: - sig.bind(*args) - except TypeError: - return False - else: - return True - else: - # For older Python versions, fall back to using getargspec. - spec = inspect.getargspec(func) - - # Drop the 'self' - def drop_self(spec): - args, varargs, varkw, defaults = spec - if args[0:1] == ['self']: - args = args[1:] - return inspect.ArgSpec(args, varargs, varkw, defaults) - - spec = drop_self(spec) - - # When taking *args, always return True. - if spec.varargs is not None: - return True - - # Test whether the given amount of args is between the min and max - # accepted argument counts. - return len(spec.args) - len(spec.defaults or []) <= len(args) <= len(spec.args) - - +# Cache of signatures. Improves the performance of `test_callable_args`. +_signatures_cache = weakref.WeakKeyDictionary() + + +def test_callable_args(func, args): + """ + Return True when this function can be called with the given arguments. + """ + assert isinstance(args, (list, tuple)) + signature = getattr(inspect, 'signature', None) + + if signature is not None: + # For Python 3, use inspect.signature. + try: + sig = _signatures_cache[func] + except KeyError: + sig = signature(func) + _signatures_cache[func] = sig + + try: + sig.bind(*args) + except TypeError: + return False + else: + return True + else: + # For older Python versions, fall back to using getargspec. + spec = inspect.getargspec(func) + + # Drop the 'self' + def drop_self(spec): + args, varargs, varkw, defaults = spec + if args[0:1] == ['self']: + args = args[1:] + return inspect.ArgSpec(args, varargs, varkw, defaults) + + spec = drop_self(spec) + + # When taking *args, always return True. + if spec.varargs is not None: + return True + + # Test whether the given amount of args is between the min and max + # accepted argument counts. + return len(spec.args) - len(spec.defaults or []) <= len(args) <= len(spec.args) + + class DummyContext(object): """ (contextlib.nested is not available on Py3) @@ -154,11 +154,11 @@ class _CharSizesCache(dict): else: result = sum(max(0, wcwidth(c)) for c in string) - # Cache for short strings. - # (It's hard to tell what we can consider short...) - if len(string) < 256: - self[string] = result - + # Cache for short strings. + # (It's hard to tell what we can consider short...) + if len(string) < 256: + self[string] = result + return result |