Mercurial > public > mercurial-scm > hg
comparison mercurial/utils/procutil.py @ 46175:a04c03b0678e
procutil: assign pseudo file object if sys.stdout/stderr is missing
This basically simulates the Python 2 behavior. If libc stdio were used,
these file objects would be available and raise EBADF. There is subtle
difference between py2 and py3, but I think py3 behavior (i.e. exit 255)
is more correct.
"if" conditions are adjust so that they look similar to
dispatch.initstdio().
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Fri, 18 Dec 2020 20:09:11 +0900 |
parents | a1601ff3877c |
children | 128a17d8436f |
comparison
equal
deleted
inserted
replaced
46174:a1601ff3877c | 46175:a04c03b0678e |
---|---|
129 # the data. | 129 # the data. |
130 return WriteAllWrapper(stream) | 130 return WriteAllWrapper(stream) |
131 | 131 |
132 | 132 |
133 if pycompat.ispy3: | 133 if pycompat.ispy3: |
134 # Python 3 implements its own I/O streams. | 134 # Python 3 implements its own I/O streams. Unlike stdio of C library, |
135 # sys.stdin/stdout/stderr may be None if underlying fd is closed. | |
136 | |
135 # TODO: .buffer might not exist if std streams were replaced; we'll need | 137 # TODO: .buffer might not exist if std streams were replaced; we'll need |
136 # a silly wrapper to make a bytes stream backed by a unicode one. | 138 # a silly wrapper to make a bytes stream backed by a unicode one. |
137 | 139 |
138 # sys.stdin can be None | 140 if sys.stdin is None: |
139 if sys.stdin: | 141 stdin = BadFile() |
142 else: | |
140 stdin = sys.stdin.buffer | 143 stdin = sys.stdin.buffer |
144 if sys.stdout is None: | |
145 stdout = BadFile() | |
141 else: | 146 else: |
142 stdin = BadFile() | 147 stdout = _make_write_all(sys.stdout.buffer) |
143 stdout = _make_write_all(sys.stdout.buffer) | 148 if sys.stderr is None: |
144 stderr = _make_write_all(sys.stderr.buffer) | 149 stderr = BadFile() |
150 else: | |
151 stderr = _make_write_all(sys.stderr.buffer) | |
152 | |
145 if pycompat.iswindows: | 153 if pycompat.iswindows: |
146 # Work around Windows bugs. | 154 # Work around Windows bugs. |
147 stdout = platform.winstdout(stdout) | 155 stdout = platform.winstdout(stdout) |
148 stderr = platform.winstdout(stderr) | 156 stderr = platform.winstdout(stderr) |
149 if isatty(stdout): | 157 if isatty(stdout): |