| Store | Cart

[Python-Dev] Using environment variables problematic for embedded python.

From: Campbell Barton <idea...@gmail.com>
Thu, 4 Oct 2012 13:35:25 +1000

We've run into an issue recently with blender3d on ms-windows where we
want to enforce the encoding is UTF-8 with the embedded python
(the encoding defaults to cp437).

I naively thought setting the environment variable before calling
Py_Initialize() would work, but the way python DLL loads, it gets its
own environment variables that cant be modified directly [1].
eg, _putenv("PYTHONIOENCODING=utf-8:surrogateescape");

We had bug reports by windows users not able to export files because
the stdout errors on printing paths with unsupported encoding. [2],[3]


Of course we could distribute blender with a bat file launcher that
sets env variables, or ask the user to set their env variable - but I
dont think this is really a good option.

I tried overriding the stderr & stdout, but this caused another bug in
a part of out code that catches exception messages from the stderr.
import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8',
errors='surrogateescape', line_buffering=True)
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8',
errors='surrogateescape', line_buffering=True)

IMHO either of these solutions would be fine.

* have a PyOS_PutEnv() function, gettext has gettext_putenv() to
workaround this problem.

* manage this the same as Py_GetPythonHome(), which can be defined by
the embedding application to override the default.

Id like to know if there is some known solution to workaround this
issue, if not - would either of these would be acceptable in python
(can write up a patch if it helps)



[1] http://stackoverflow.com/questions/5153547/environment-variables-are-different-for-dll-than-exe
[2] http://projects.blender.org/tracker/index.php?func=detail&aid=32750
[3] http://projects.blender.org/tracker/index.php?func=detail&aid=31555
[4] http://projects.blender.org/tracker/?func=detail&aid=32720
Python-Dev mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-dev-ml%40activestate.com

Recent Messages in this Thread
Campbell Barton Oct 04, 2012 03:35 am
Campbell Barton Oct 04, 2012 04:35 am
Guido van Rossum Oct 04, 2012 04:51 am
Campbell Barton Oct 04, 2012 01:01 pm
Messages in this thread