diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py --- a/mercurial/dispatch.py +++ b/mercurial/dispatch.py @@ -165,6 +165,7 @@ # normalization, ensuring compatibility with our ui type. # write_through is new in Python 3.7. + sys.stdout = sys.stdout or io.TextIOWrapper(io.BytesIO()) kwargs = { "newline": "\n", "line_buffering": sys.stdout.line_buffering, @@ -175,6 +176,7 @@ sys.stdout.buffer, sys.stdout.encoding, sys.stdout.errors, **kwargs ) + sys.stderr = sys.stderr or io.TextIOWrapper(io.BytesIO()) kwargs = { "newline": "\n", "line_buffering": sys.stderr.line_buffering, @@ -186,6 +188,7 @@ ) # No write_through on read-only stream. + sys.stdin = sys.stdin or io.TextIOWrapper(io.BytesIO()) sys.stdin = io.TextIOWrapper( sys.stdin.buffer, sys.stdin.encoding, diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py --- a/mercurial/utils/procutil.py +++ b/mercurial/utils/procutil.py @@ -114,12 +114,17 @@ if pycompat.ispy3: + # Stdio objects can be 'None' on Python 3. Most code paths (for example, + # dispatch.initstdio) do not expect that. + stdin = sys.stdin or io.TextIOWrapper(io.BytesIO()) + stdout = sys.stdout or io.TextIOWrapper(io.BytesIO()) + stderr = sys.stderr or io.TextIOWrapper(io.BytesIO()) # Python 3 implements its own I/O streams. # TODO: .buffer might not exist if std streams were replaced; we'll need # a silly wrapper to make a bytes stream backed by a unicode one. - stdin = sys.stdin.buffer - stdout = _make_write_all(sys.stdout.buffer) - stderr = _make_write_all(sys.stderr.buffer) + stdin = stdin.buffer + stdout = _make_write_all(stdout.buffer) + stderr = _make_write_all(stderr.buffer) if pycompat.iswindows: # Work around Windows bugs. stdout = platform.winstdout(stdout) diff --git a/tests/test-stdio-missing.t b/tests/test-stdio-missing.t new file mode 100644 --- /dev/null +++ b/tests/test-stdio-missing.t @@ -0,0 +1,13 @@ + $ cat > prompt.py << 'EOF' + > from mercurial import exthelper + > eh = exthelper.exthelper() + > cmdtable = eh.cmdtable + > @eh.command(b'prompt', [], norepo=True) + > def prompt(ui): + > chosen = ui.promptchoice(b"is stdin present? (y/N) $$ &Yes $$ &No", default=1) + > ui.write(b"chosen: %d\n" % chosen) + > EOF + + $ hg --config extensions.prompt=prompt.py prompt 0<&- + is stdin present? (y/N) n + chosen: 1