With LC_ALL=C, glib-mkenums crashes when writing non-ASCII chars to stdout
It is common to call glib-mkenums
from a Makefile like this:
glib-mkenums ... > $@.tmp && mv $@.tmp $@
If the template (and hence output) contains non-ASCII characters, and LC_ALL=C
, then this will fail:
$ cat enums.h.template
/*** BEGIN file-header ***/
/* © */
/*** END file-header ***/
$ LC_ALL=C glib-mkenums --template enums.h.template c.h > /tmp/enums.h
Traceback (most recent call last):
File "/usr/bin/glib-mkenums", line 425, in <module>
write_output(prod)
File "/usr/bin/glib-mkenums", line 74, in write_output
print(output, file=output_stream)
UnicodeEncodeError: 'ascii' codec can't encode character '\xa9' in position 3: ordinal not in range(128)
This is apparently not specific to running this script with Python 3.
This is not purely theoretical because GNOME Builder sets LC_ALL
and friends to C
in order to be able to parse directory changes in the output.
The fix on a per-project basis is to use the --output
argument, which seems to have existed for a long time and is the documented pattern, but perhaps it's worth trying to cope better, perhaps by detecting that stdout's encoding is ASCII and silently “upgrading” it to UTF-8.