prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.11k stars 717 forks source link

Contrib SSH : can't import on Windows due to posix pipe use #1154

Open adamrosenstein opened 4 years ago

adamrosenstein commented 4 years ago

I have set up an async SSHD on my Windows 10 box and I had to patch contrib/ssh/server.py in order to do it. The problem is that we import prompt_toolkit.input.posix_pipe at the top, when we need to be versatile to platform and import prompt_toolkit.input.win32_pipe on Windows platforms instead.

Here's how I'm handling this locally:

--- a/prompt_toolkit/contrib/ssh/server.py      2020-05-29 13:15:17.950284900 -0700
+++ b/prompt_toolkit/contrib/ssh/server.py      2020-05-29 13:27:21.302283000 -0700
@@ -9,7 +9,14 @@

 from prompt_toolkit.application.current import AppSession, create_app_session
 from prompt_toolkit.data_structures import Size
-from prompt_toolkit.input.posix_pipe import PosixPipeInput
+from importlib import import_module
+from prompt_toolkit.utils import is_windows
+if (is_windows()):
+    pipe_module = import_module('prompt_toolkit.input.win32_pipe')
+    pipe_input  = pipe_module.Win32PipeInput
+else:
+    pipe_module = import_module('prompt_toolkit.input.posix_pipe')
+    pipe_input  = pipe_module.PosixPipeInput
 from prompt_toolkit.output.vt100 import Vt100_Output

 __all__ = [
@@ -27,7 +34,7 @@
         # PipInput object, for sending input in the CLI.
         # (This is something that we can use in the prompt_toolkit event loop,
         # but still write date in manually.)
-        self._input = PosixPipeInput()
+        self._input = pipe_input()

         # Output object. Don't render to the real stdout, but write everything
         # in the SSH channel.
jonathanslenders commented 4 years ago

This could be a fix: https://github.com/prompt-toolkit/python-prompt-toolkit/pull/1150