164 # and sys.stderr. They will inherit the line ending normalization settings, |
164 # and sys.stderr. They will inherit the line ending normalization settings, |
165 # potentially causing e.g. \r\n to be emitted. Since emitting \n should |
165 # potentially causing e.g. \r\n to be emitted. Since emitting \n should |
166 # "just work," here we change the sys.* streams to disable line ending |
166 # "just work," here we change the sys.* streams to disable line ending |
167 # normalization, ensuring compatibility with our ui type. |
167 # normalization, ensuring compatibility with our ui type. |
168 |
168 |
169 # write_through is new in Python 3.7. |
169 if sys.stdout is not None: |
170 kwargs = { |
170 # write_through is new in Python 3.7. |
171 "newline": "\n", |
171 kwargs = { |
172 "line_buffering": sys.stdout.line_buffering, |
172 "newline": "\n", |
173 } |
173 "line_buffering": sys.stdout.line_buffering, |
174 if util.safehasattr(sys.stdout, "write_through"): |
174 } |
175 kwargs["write_through"] = sys.stdout.write_through |
175 if util.safehasattr(sys.stdout, "write_through"): |
176 sys.stdout = io.TextIOWrapper( |
176 kwargs["write_through"] = sys.stdout.write_through |
177 sys.stdout.buffer, sys.stdout.encoding, sys.stdout.errors, **kwargs |
177 sys.stdout = io.TextIOWrapper( |
178 ) |
178 sys.stdout.buffer, |
179 |
179 sys.stdout.encoding, |
180 kwargs = { |
180 sys.stdout.errors, |
181 "newline": "\n", |
181 **kwargs |
182 "line_buffering": sys.stderr.line_buffering, |
182 ) |
183 } |
183 |
184 if util.safehasattr(sys.stderr, "write_through"): |
184 if sys.stderr is not None: |
185 kwargs["write_through"] = sys.stderr.write_through |
185 kwargs = { |
186 sys.stderr = io.TextIOWrapper( |
186 "newline": "\n", |
187 sys.stderr.buffer, sys.stderr.encoding, sys.stderr.errors, **kwargs |
187 "line_buffering": sys.stderr.line_buffering, |
188 ) |
188 } |
|
189 if util.safehasattr(sys.stderr, "write_through"): |
|
190 kwargs["write_through"] = sys.stderr.write_through |
|
191 sys.stderr = io.TextIOWrapper( |
|
192 sys.stderr.buffer, |
|
193 sys.stderr.encoding, |
|
194 sys.stderr.errors, |
|
195 **kwargs |
|
196 ) |
189 |
197 |
190 if sys.stdin is not None: |
198 if sys.stdin is not None: |
191 # No write_through on read-only stream. |
199 # No write_through on read-only stream. |
192 sys.stdin = io.TextIOWrapper( |
200 sys.stdin = io.TextIOWrapper( |
193 sys.stdin.buffer, |
201 sys.stdin.buffer, |