Skip to content

Commit

Permalink
Move debug_stream initialization to helper method (#471)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
azhu-tower authored Jan 29, 2024
1 parent 2a6c23b commit eafe0a3
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions argcomplete/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit eafe0a3

Please sign in to comment.