This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Fix crash when replacing sys.stdout in sitecustomize
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: twouters Nosy List: nnorwitz, twouters
Priority: critical Keywords: patch

Created on 2007-01-08 22:55 by twouters, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
SetEncoding.diff twouters, 2007-01-08 22:55
Messages (4)
msg51707 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2007-01-08 22:55
When replacing sys.stdout, stderr and/or stdin with non-file, file-like objects in sitecustomize, and also having an environment that makes Python set the encoding of those streams, Python will crash. PyFile_SetEncoding() will be called after sys.stdout/stderr/stdin are replaced, passing the non-file objects.

Fix by not calling PyFile_SetEncoding() in these cases. I'm not entirely sure if we should warn or not; not setting encoding only for replaced streams may cause a disconnect between stdout and stderr that's hard to explain, when someone only replaces one of them (in sitecustomize.) Then again, not many people must be doing it, as it currently just crashes.

No idea how to test for this, from a unittest :P
msg51708 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2007-01-17 06:55
I can think of a nasty way to test this, but it's not really worth it.  You'd need to 'install' your own sitecustomize.py by setting PYTHONPATH and spawning a python.  Ok, so it's not a real unit test, but it is a test. :-)

This looks like it will also crash (before and after the patch) if sys.std{in,out,err} are just deleted rather than replaced (pythonrun.c).  sysmodule.c looks fine.

I think this is fine for 2.5.1.
msg51709 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2007-01-17 06:56
Forgot to mention that I agree about the warning.  If no one noticed so far, this is such an obscure case, it's not that important to warn.  Either way is fine with me.
msg51710 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2007-01-23 14:04
Oh, for the record: I was unable to produce a crash by *deleting* sys.stdin/stdout/stderr (although it produced funny results. In particular when I adding a 'print' statement after the deletes, in my sitecustomize.py, to make sure it was getting run. Of course, the print never arrived ;-P)
History
Date User Action Args
2022-04-11 14:56:22adminsetgithub: 44432
2007-01-08 22:55:16twouterscreate