Skip to content

Commit

Permalink
Move debug_stream initialization to helper method
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.

Change-Id: Ic37a324e137809ac9712c73ffc9f946ca3466571
  • Loading branch information
azhu-tower authored and azjps committed Jan 29, 2024
1 parent 2a6c23b commit 6eea11f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
7 changes: 6 additions & 1 deletion Changes.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Changes for v3.2.3a
===============================

- Move debug stream opening to helper method `CompletionFinder._init_debug_stream`

Changes for v3.2.2 (2024-01-23)
===============================

Expand tilde in zsh
- Expand tilde in zsh

Changes for v3.2.1 (2023-12-10)
===============================
Expand Down
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 6eea11f

Please sign in to comment.