|
|
@ -271,16 +271,32 @@ else:
|
|
|
|
stderr_write = stderr_write_flush
|
|
|
|
stderr_write = stderr_write_flush
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def replace_std_streams(errors="replace"):
|
|
|
|
def configure_standard_streams():
|
|
|
|
"""Replace standard streams and set their error handlers to 'errors'"""
|
|
|
|
for name in ("stdout", "stderr", "stdin"):
|
|
|
|
for name in ("stdout", "stdin", "stderr"):
|
|
|
|
options = config.get(("output",), name)
|
|
|
|
stream = getattr(sys, name)
|
|
|
|
if not options:
|
|
|
|
if stream:
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stream = getattr(sys, name, None)
|
|
|
|
|
|
|
|
if not stream:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(options, str):
|
|
|
|
|
|
|
|
options = {"encoding": options, "errors": "replace"}
|
|
|
|
|
|
|
|
elif not options.get("errors"):
|
|
|
|
|
|
|
|
options["errors"] = "replace"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
stream.reconfigure(**options)
|
|
|
|
|
|
|
|
except AttributeError:
|
|
|
|
|
|
|
|
# no 'reconfigure' support
|
|
|
|
|
|
|
|
oget = options.get
|
|
|
|
setattr(sys, name, stream.__class__(
|
|
|
|
setattr(sys, name, stream.__class__(
|
|
|
|
stream.buffer,
|
|
|
|
stream.buffer,
|
|
|
|
errors=errors,
|
|
|
|
encoding=oget("encoding", stream.encoding),
|
|
|
|
newline=stream.newlines,
|
|
|
|
errors=oget("errors", "replace"),
|
|
|
|
line_buffering=stream.line_buffering,
|
|
|
|
newline=oget("newline", stream.newlines),
|
|
|
|
|
|
|
|
line_buffering=oget("line_buffering", stream.line_buffering),
|
|
|
|
))
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|