From eafe0a3cc146d2b2e6b9b8e5ddf0b14f2b25b40f Mon Sep 17 00:00:00 2001 From: Andy <158001626+azhu-tower@users.noreply.github.com> Date: Tue, 30 Jan 2024 06:13:40 +1300 Subject: [PATCH] Move debug_stream initialization to helper method (#471) By default argcomplete tries to use fd 9 as its debug stream, but this could clash with something else trying to use the same file descriptor, for example a pytest plugin. Split into a _init_debug_stream() helper method which can now be overridden in subclasses. --- argcomplete/finders.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/argcomplete/finders.py b/argcomplete/finders.py index 95af973b..fb0f31ce 100644 --- a/argcomplete/finders.py +++ b/argcomplete/finders.py @@ -117,11 +117,7 @@ def __call__( # not an argument completion invocation return - try: - _io.debug_stream = os.fdopen(9, "w") - except Exception: - _io.debug_stream = sys.stderr - debug() + self._init_debug_stream() if output_stream is None: filename = os.environ.get("_ARGCOMPLETE_STDOUT_FILENAME") @@ -190,6 +186,19 @@ def __call__( _io.debug_stream.flush() exit_method(0) + def _init_debug_stream(self): + """Initialize debug output stream + + By default, writes to file descriptor 9, or stderr if that fails. + This can be overridden by derived classes, for example to avoid + clashes with file descriptors being used elsewhere (such as in pytest). + """ + try: + _io.debug_stream = os.fdopen(9, "w") + except Exception: + _io.debug_stream = sys.stderr + debug() + def _get_completions(self, comp_words, cword_prefix, cword_prequote, last_wordbreak_pos): active_parsers = self._patch_argument_parser()