MCPcopy Create free account
hub / github.com/pyinvoke/invoke / handle_stdin

Method handle_stdin

invoke/runners.py:852–916  ·  view source on GitHub ↗

Read local stdin, copying into process' stdin as necessary. Intended for use as a thread target. .. note:: Because real terminal stdin streams have no well-defined "end", if such a stream is detected (based on existence of a callable ``.

(
        self,
        input_: IO,
        output: IO,
        echo: bool = False,
    )

Source from the content-addressed store, hash-verified

850 return bytes_
851
852 def handle_stdin(
853 self,
854 input_: IO,
855 output: IO,
856 echo: bool = False,
857 ) -> None:
858 """
859 Read local stdin, copying into process' stdin as necessary.
860
861 Intended for use as a thread target.
862
863 .. note::
864 Because real terminal stdin streams have no well-defined "end", if
865 such a stream is detected (based on existence of a callable
866 ``.fileno()``) this method will wait until `program_finished` is
867 set, before terminating.
868
869 When the stream doesn't appear to be from a terminal, the same
870 semantics as `handle_stdout` are used - the stream is simply
871 ``read()`` from until it returns an empty value.
872
873 :param input_: Stream (file-like object) from which to read.
874 :param output: Stream (file-like object) to which echoing may occur.
875 :param bool echo: User override option for stdin-stdout echoing.
876
877 :returns: ``None``.
878
879 .. versionadded:: 1.0
880 """
881 # TODO: reinstate lock/whatever thread logic from fab v1 which prevents
882 # reading from stdin while other parts of the code are prompting for
883 # runtime passwords? (search for 'input_enabled')
884 # TODO: fabric#1339 is strongly related to this, if it's not literally
885 # exposing some regression in Fabric 1.x itself.
886 closed_stdin = False
887 with character_buffered(input_):
888 while True:
889 data = self.read_our_stdin(input_)
890 if data:
891 # Mirror what we just read to process' stdin.
892 # We encode to ensure bytes, but skip the decode step since
893 # there's presumably no need (nobody's interacting with
894 # this data programmatically).
895 self.write_proc_stdin(data)
896 # Also echo it back to local stdout (or whatever
897 # out_stream is set to) when necessary.
898 if echo is None:
899 echo = self.should_echo_stdin(input_, output)
900 if echo:
901 self.write_our_output(stream=output, string=data)
902 # Empty string/char/byte != None. Can't just use 'else' here.
903 elif data is not None:
904 # When reading from file-like objects that aren't "real"
905 # terminal streams, an empty byte signals EOF.
906 if not self.using_pty and not closed_stdin:
907 self.close_proc_stdin()
908 closed_stdin = True
909 # Dual all-done signals: program being executed is done

Callers

nothing calls this directly

Calls 6

read_our_stdinMethod · 0.95
write_proc_stdinMethod · 0.95
should_echo_stdinMethod · 0.95
write_our_outputMethod · 0.95
close_proc_stdinMethod · 0.95
character_bufferedFunction · 0.85

Tested by

no test coverage detected