Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/prompt_toolkit/input/defaults.py: 26%
31 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1from __future__ import annotations
3import io
4import sys
5from typing import ContextManager, TextIO
7from .base import DummyInput, Input, PipeInput
9__all__ = [
10 "create_input",
11 "create_pipe_input",
12]
15def create_input(stdin: TextIO | None = None, always_prefer_tty: bool = False) -> Input:
16 """
17 Create the appropriate `Input` object for the current os/environment.
19 :param always_prefer_tty: When set, if `sys.stdin` is connected to a Unix
20 `pipe`, check whether `sys.stdout` or `sys.stderr` are connected to a
21 pseudo terminal. If so, open the tty for reading instead of reading for
22 `sys.stdin`. (We can open `stdout` or `stderr` for reading, this is how
23 a `$PAGER` works.)
24 """
25 if sys.platform == "win32":
26 from .win32 import Win32Input
28 # If `stdin` was assigned `None` (which happens with pythonw.exe), use
29 # a `DummyInput`. This triggers `EOFError` in the application code.
30 if stdin is None and sys.stdin is None:
31 return DummyInput()
33 return Win32Input(stdin or sys.stdin)
34 else:
35 from .vt100 import Vt100Input
37 # If no input TextIO is given, use stdin/stdout.
38 if stdin is None:
39 stdin = sys.stdin
41 if always_prefer_tty:
42 for obj in [sys.stdin, sys.stdout, sys.stderr]:
43 if obj.isatty():
44 stdin = obj
45 break
47 # If we can't access the file descriptor for the selected stdin, return
48 # a `DummyInput` instead. This can happen for instance in unit tests,
49 # when `sys.stdin` is patched by something that's not an actual file.
50 # (Instantiating `Vt100Input` would fail in this case.)
51 try:
52 stdin.fileno()
53 except io.UnsupportedOperation:
54 return DummyInput()
56 return Vt100Input(stdin)
59def create_pipe_input() -> ContextManager[PipeInput]:
60 """
61 Create an input pipe.
62 This is mostly useful for unit testing.
64 Usage::
66 with create_pipe_input() as input:
67 input.send_text('inputdata')
69 Breaking change: In prompt_toolkit 3.0.28 and earlier, this was returning
70 the `PipeInput` directly, rather than through a context manager.
71 """
72 if sys.platform == "win32":
73 from .win32_pipe import Win32PipeInput
75 return Win32PipeInput.create()
76 else:
77 from .posix_pipe import PosixPipeInput
79 return PosixPipeInput.create()