This seems more fit for the tracker; can you file there? (Then post the issue link here.) I do think you have a legitimate use case to set the default encoding to utf-8. (Though there may be a way already.) Does Python 3.3 have te same bug?<span></span><br>
<br>On Wednesday, October 3, 2012, Campbell Barton  wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Thu, Oct 4, 2012 at 1:35 PM, Campbell Barton <<a href="javascript:;" onclick="_e(event, 'cvml', 'ideasman42@gmail.com')">ideasman42@gmail.com</a>> wrote:<br>

> Hi,<br>
><br>
> We've run into an issue recently with blender3d on ms-windows where we<br>
> want to enforce the encoding is UTF-8 with the embedded python<br>
> interpreter.<br>
> (the encoding defaults to cp437).<br>
><br>
> I naively thought setting the environment variable before calling<br>
> Py_Initialize() would work, but the way python DLL loads, it gets its<br>
> own environment variables that cant be modified directly [1].<br>
> eg, _putenv("PYTHONIOENCODING=utf-8:surrogateescape");<br>
><br>
> We had bug reports by windows users not able to export files because<br>
> the stdout errors on printing paths with unsupported encoding. [2],[3]<br>
><br>
> ---<br>
><br>
> Of course we could distribute blender with a bat file launcher that<br>
> sets env variables, or ask the user to set their env variable - but I<br>
> dont think this is really a good option.<br>
><br>
> I tried overriding the stderr & stdout, but this caused another bug in<br>
> a part of out code that catches exception messages from the stderr.<br>
> [4]<br>
> import sys, io<br>
> sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8',<br>
> errors='surrogateescape', line_buffering=True)<br>
> sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8',<br>
> errors='surrogateescape', line_buffering=True)<br>
><br>
><br>
><br>
> IMHO either of these solutions would be fine.<br>
><br>
> * have a PyOS_PutEnv() function, gettext has gettext_putenv() to<br>
> workaround this problem.<br>
><br>
> * manage this the same as Py_GetPythonHome(), which can be defined by<br>
> the embedding application to override the default.<br>
><br>
><br>
> Id like to know if there is some known solution to workaround this<br>
> issue, if not - would either of these would be acceptable in python<br>
> (can write up a patch if it helps)<br>
><br>
> Regards,<br>
> Campbell<br>
><br>
> ---<br>
><br>
> [1] <a href="http://stackoverflow.com/questions/5153547/environment-variables-are-different-for-dll-than-exe" target="_blank">http://stackoverflow.com/questions/5153547/environment-variables-are-different-for-dll-than-exe</a><br>

> [2] <a href="http://projects.blender.org/tracker/index.php?func=detail&aid=32750" target="_blank">http://projects.blender.org/tracker/index.php?func=detail&aid=32750</a><br>
> [3] <a href="http://projects.blender.org/tracker/index.php?func=detail&aid=31555" target="_blank">http://projects.blender.org/tracker/index.php?func=detail&aid=31555</a><br>
> [4] <a href="http://projects.blender.org/tracker/?func=detail&aid=32720" target="_blank">http://projects.blender.org/tracker/?func=detail&aid=32720</a><br>
<br>
To follow up and give a correction to overwriting sys.stdout/stderr,<br>
The issue seemed to be that __stderr__/__stdout__ was later<br>
overwritten, loosing the original reference to the buffer (maybe<br>
refcount issue here?), either way it would silence the output.<br>
<br>
<br>
import sys, io<br>
sys.__stdout__ = sys.stdout =<br>
io.TextIOWrapper(io.open(sys.stdout.fileno(), "wb", -1),<br>
encoding='utf-8', errors='surrogateescape', newline="\n",<br>
line_buffering=True)<br>
sys.__stderr__ = sys.stderr =<br>
io.TextIOWrapper(io.open(sys.stderr.fileno(), "wb", -1),<br>
encoding='utf-8', errors='surrogateescape', newline="\n",<br>
line_buffering=True)<br>
<br>
This all works as expected without bug [4] (above), however on exit I<br>
get an assert in MSVCR90.DLL's write.c (called from python32_d.dll):<br>
_VALIDATE_CLEAR_OSSERR_RETURN((_osfile(fh) & FOPEN), EBADF, -1);<br>
<br>
I'd rather not loose more time debugging why this assert happens,<br>
IMHO this is too low-level a way to change the encoing of stdio/stderr<br>
and error-prone too, so some way to reliably set PYTHONIOENCODING from<br>
a program embedding python is still needed.<br>
<br>
--<br>
- Campbell<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="javascript:;" onclick="_e(event, 'cvml', 'Python-Dev@python.org')">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/guido%40python.org" target="_blank">http://mail.python.org/mailman/options/python-dev/guido%40python.org</a><br>
</blockquote><br><br>-- <br>--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)<br>