Sunday, March 15, 2015

Redirecting non-ascii stdout to a file

repairdata now supports positional arguments: if arguments are specified, then they are expected to be full names of the models to be tested.

I got UnicodeEncodeError when I wanted to redirect the output of repairdata to a file. For example, in lino.projects.min2 the following command gives quite some output, including Non-ASCII text:

$ python manage.py repairdata

And it works, at least if the LANG is correctly set. But redirecting that output to a file does not work:

$ python manage.py repairdata > tmp.txt

And after having read posts like this one, I added the following line to atelier:

import sys, codecs, locale
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)

That is: importing atelier now automatically adds a codecs writer to sys.stdout. I then discovered that as a consequence, atelier.doctest_utf8 is no longer needed.

Result is that I can now do the above redirection. And everything else seems to work.