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: Python segfaults when sys.stdout is changed in getattr
Type: Stage:
Components: Interpreter Core Versions: Python 2.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: hyeshik.chang, nnorwitz, rhettinger, tim.peters
Priority: normal Keywords:

Created on 2003-06-28 18:24 by hyeshik.chang, last changed 2022-04-10 16:09 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
filefault.py hyeshik.chang, 2003-06-28 18:28 a script that duplicates the fault
stdout.diff nnorwitz, 2003-06-29 02:48 ceval patch 1
Messages (9)
msg16650 - (view) Author: Hyeshik Chang (hyeshik.chang) * (Python committer) Date: 2003-06-28 18:24
Reproducible source digest:

import sys 
class StdoutGuard: 
    def __getattr__(self, attr): 
        sys.stdout = sys.__stdout__ 
        raise RuntimeError("Premature access to 
sys.stdout.%s" % attr) 
sys.stdout = StdoutGuard() 
print "Oops!"


and Backtrace:

(gdb) bt
#0  0x00000002 in ?? ()
#1  0x08055ba6 in PyObject_GetAttrString 
(v=0x814b9e0,
    name=0x2 <Error reading address 0x2: Bad address>) 
at Objects/object.c:1066
#2  0x080a57d2 in PyFile_WriteObject (v=0x817d180, 
f=0x8178a8c, flags=135760524)
    at Objects/fileobject.c:1691
#3  0x08071493 in eval_frame (f=0x8121c0c) at 
Python/ceval.c:1371
#4  0x080730be in PyEval_EvalCodeEx (co=0x8126500, 
globals=0x2, locals=0x2, args=0x0, argcount=0,
    kws=0x0, kwcount=0, defs=0x0, defcount=0, 
closure=0x0) at Python/ceval.c:2595
#5  0x080702da in PyEval_EvalCode (co=0x8126500, 
globals=0x812fd0c, locals=0x812fd0c)
    at Python/ceval.c:481
#6  0x08089891 in run_node (n=0x8178a80, 
filename=0xbfbff648 "test.py", globals=0x812fd0c,
    locals=0x812fd0c, flags=0xbfbff4d8) at 
Python/pythonrun.c:1067
#7  0x08088af7 in PyRun_SimpleFileExFlags 
(fp=0x282c3780, filename=0xbfbff648 "test.py", 
closeit=1,
    flags=0xbfbff4d8) at Python/pythonrun.c:673
#8  0x08052a77 in Py_Main (argc=1, argv=0xbfbff538) 
at Modules/main.c:367
#9  0x080525cf in main (argc=2, argv=0xbfbff538) at 
Modules/python.c:10
#10 0x08052505 in _start ()
(gdb) f 2
#2  0x080a57d2 in PyFile_WriteObject (v=0x817d180, 
f=0x8178a8c, flags=135760524)
    at Objects/fileobject.c:1691
1691            writer = PyObject_GetAttrString(f, "write");
(gdb) p *f
$22 = {ob_refcnt = 1000843696, ob_type = 0x814b9e0}
msg16651 - (view) Author: Hyeshik Chang (hyeshik.chang) * (Python committer) Date: 2003-06-28 18:26
Logged In: YES 
user_id=55188

import sys 
class StdoutGuard: 
&nbsp;&nbsp;&nbsp;&nbsp;def __getattr__(self, attr): 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.std
out = sys.__stdout__ 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise 
RuntimeError("Premature access to sys.stdout.%s" % attr) 
sys.stdout = StdoutGuard() 
print "Oops!"
msg16652 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-06-29 02:48
Logged In: YES 
user_id=33168

Attached is a patch which fixes the problem.  I'm not
entirely sure the comment is accurate.  2.2.3 doesn't crash,
but goes into an infinite loop.  Tim should this go into 2.3b2?
msg16653 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-06-29 03:38
Logged In: YES 
user_id=80475

This looks like a clean fix to me and ought to go into 2.3b2 
so it can be shaken out further.

Please use filefault.py to create a unittest.
msg16654 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-06-29 04:58
Logged In: YES 
user_id=33168

I couldn't get the test working.  It always passed, before
and after the fix.  I would like to have a test for this,
but no time now.
msg16655 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-06-29 05:27
Logged In: YES 
user_id=80475

filefault.py fails for me before the patch and succeeds after 
the patch.

If you go ahead and apply the patch, I'll write the test.
Do you have an idea which test_*.py is the best place for it?

msg16656 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-06-29 05:56
Logged In: YES 
user_id=31435

I can't make time to analyze the problem, but the comment 
sounds plausible <wink>.

The fix isn't quite right, though:  because w can be NULL, you 
have to use Py_XINCREF and Py_XDECREF instead of 
Py_INCREF and Py_DECREF.  The latter will probably segfault 
when w is NULL.
msg16657 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-06-29 14:57
Logged In: YES 
user_id=33168

Checked in as:
 * Python/ceval.c 2.367 & 2.301.4.9

Assigned to Raymond to make a test.

As for the test, in test_descr.specials() there is some
stdout handling at the end of the function (around line
1881).  I don't know of a better place than that.
msg16658 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-06-29 15:45
Logged In: YES 
user_id=80475

Done.
See test_descr.py 1.196
History
Date User Action Args
2022-04-10 16:09:29adminsetgithub: 38730
2003-06-28 18:24:49hyeshik.changcreate