New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fatal error on startup with invalid PYTHONIOENCODING #50750
Comments
When using Python 3.1 for Apache/mod_wsgi (3.0c4) on Windows, Apache will Fatal Python error: Py_Initialize: can't initialize sys standard streams I first mentioned this on bpo-6393, but have now created it as a separate In the Windows case there is actually an encoding, that of 'cp0' where as on The same mod_wsgi code works fine under Python 3.1 on MacOS X. |
As a workaround, I suggest to set the environment variable |
Yes, Apache remaps stdout and stderr to the Apache error log to still What would be an appropriate value to set PYTHONIOENCODING to on Windows |
On a Western Windows, I suggest But |
The workaround of using: #if defined(WIN32) && PY_MAJOR_VERSION >= 3
_wputenv(L"PYTHONIOENCODING=cp1252:backslashreplace");
#endif
Py_Initialize(); gets around the crash on startup. I haven't done sufficient testing to know if this may introduce any other |
Graham, is the workaround ok for you or do you think this is something |
Python should be as robust as possible and thus should be fixed, but I am |
Patch to prevent crash when PYTHONIOENCODING is invalid: ~ $ PYTHONIOENCODING= ./python |
Well, this might prevent the crash but how does the system behave |
The issue is not Windows specific, so I am changing the title to reflect that. On OSX, for example, I get $ PYTHONIOENCODING=xyz ./python.exe
Fatal Python error: Py_Initialize: can't initialize sys standard streams
LookupError: unknown encoding: xyz
Abort trap I agree that abort() is too drastic for a typo in the environment variable setting, but ignoring it silently is not a good option either. Someone setting PYTHONIOENCODING most likely does it for a reason and giving him or her some sort of default behavior for mistyped encoding is not helpful. (Note that this is how many C libraries treat TZ environment variable setting and this is often very frustrating.) I think errors in environment variables that can be detected on startup should be treated the same way as the command line typos: a descriptive message on C stderr and exit(1). Currently different environment variables are treated differently. For example, mistakes in PYTHONHOME and PYTHONIOENCODING cause fatal error while an error in PYTHONSTARTUP is reported but does not terminate python: $ PYTHONSTARTUP=xyz.py ./python.exe
Python 3.2rc2+ (py3k:88320, Feb 2 2011, 14:07:18)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Could not open PYTHONSTARTUP
IOError: [Errno 2] No such file or directory: 'xyz.py'
>>> |
I run into this problem when I start a Python app as a subprocess from Erlang (open_port() function). The PYTHONIOENCODING fix works when I launch my py app via pythonw.exe, but it does *not* work when I use the cx-freeze version of the app. I am using the Win32GUI base for cx-freeze which appears to be a thin WinMain() wrapper around Py_Initialize(). I am going to continue investigating the cx-freeze related problems. I am using Python 3.2 under Windows. The failure is basically silent under Windows (generic MSVC runtime error), so I wasted a lot time figuring out what the problem actually was. Python 2 doesn't seem to have this problem. |
It turns out that cx-freeze deliberately sets Py_IgnoreEnvironmentFlag to ensure that the final executable is really an isolated, standalone executable (ie, it can't be subverted by setting PYTHONPATH.) Therefore the PYTHONIOENCODING work-around does not work in this situation. I am currently using a cx-freeze work-around from the author to enable the PYTHONIOENCODING work-around. Altogether not that pleasant. Could Python 3 could just default to some reasonable encoding and keep on chugging? |
That's a bug in os.device_encoding(): os.device_encoding(sys.stdout.fileno()) should return None if the application has no console (if sys.stdout is not a Windows console stream). Attached device_encoding.patch should fix this issue. (I didn't test the patch yet.) |
Why using this very small charset whereas a web server can use UTF-8? I don't think that using backslashreplace on stdout is a good idea.
Starting at Python 3.2, you should use mbcs:replace to replace unencodable characters by '?'. The strict error handler is now strict: it raises a UnicodeEncodeError if a character is not encodable to mbcs. Note: mbcs is the ANSI code page. -- Using device_encoding.patch, I suppose that sys.std* streams will use the ANSI code page (mbcs, which is the code page 1252 on a Western Windows setup) in grahamd's usecase (Python program running in Apache). |
New changeset 5783a55a2418 by Victor Stinner in branch 'default': |
@Grahamd: Can you try the development version of Python 3.3, or try to patch your version using device_encoding.patch? You will not get cp0 encoding anymore. If the patch fixes your issue, I will backport it. I don't see anything interesting to do for this issue. |
If PYTHONSTARTUP is the only envvar with non-fatal errors, I think it’s okay. PYTHONHOME contains vital information, PYTHONIOENCODING is set by the programmer/admin and their code probably depends on it, but PYTHONSTARTUP is just niceties for the interactive interpreter, so non-vital IMO. |
Is there anything to backport as referred to in msg136670 or can this be closed? |
The fix is present in Python 3.3 since Python 3.3.0 according to the changelog. Python 3.2 doesn't accept bugfixes anymore. Python 2.7 doesn't have the function os.device_encoding(), I don't think that it is affected. There is nothing more to do, I'm closing the bug. Thanks for the report. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: