Author martin.panter
Recipients Arfrever, elixir, ishimoto, jwilk, loewis, martin.panter, methane, mrabarnett, ncoghlan, nikratio, pitrou, rurpy2, serhiy.storchaka, vstinner
Date 2014-09-15.04:36:48
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Some more use cases for temporarily switching error handler in the middle of writing to a stream:
* Possibly simplify the implementation of sys.displayhook()
* I have done a similar hack at <>, to output a comment field with a permissive error handler, while other data is output with strict error handling.

A use case for changing a reader’s newline translation mode is to use standard input with the built-in “csv” module. My current idea is to do something like this:

encoding = sys.stdin.encoding
errors = sys.stdin.errors
line_buffering = sys.stdin.line_buffering
# No way to retain write_through mode, but shouldn’t matter for reading
sys.stdin = TextIOWrapper(sys.stdin.detach(), encoding, errors,
    newline="", line_buffering=line_buffering)

for row in csv.reader(sys.stdin):

On the other hand, I wonder about rewinding an input file after already having read and buffered text in the wrong encoding. From a distance, the Python native version of the code seems rather complex and full of dark magic. Is there a use case, or maybe it would be simpler to have it only work when nothing has been read or buffered?
Date User Action Args
2014-09-15 04:36:49martin.pantersetrecipients: + martin.panter, loewis, ishimoto, ncoghlan, pitrou, vstinner, jwilk, mrabarnett, Arfrever, methane, nikratio, rurpy2, serhiy.storchaka, elixir
2014-09-15 04:36:49martin.pantersetmessageid: <>
2014-09-15 04:36:49martin.panterlinkissue15216 messages
2014-09-15 04:36:48martin.pantercreate