[Python-bugs-list] [ python-Bugs-762455 ] Python segfaults when sys.stdout is changed in getattr

SourceForge.net noreply@sourceforge.net
Sat, 28 Jun 2003 19:48:18 -0700


Bugs item #762455, was opened at 2003-06-28 14:24
Message generated for change (Comment added) made by nnorwitz
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=762455&group_id=5470

Category: Python Interpreter Core
>Group: Python 2.2.3
Status: Open
Resolution: None
>Priority: 6
Submitted By: Hye-Shik Chang (perky)
>Assigned to: Tim Peters (tim_one)
Summary: Python segfaults when sys.stdout is changed in getattr

Initial Comment:
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}


----------------------------------------------------------------------

>Comment By: Neal Norwitz (nnorwitz)
Date: 2003-06-28 22:48

Message:
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?

----------------------------------------------------------------------

Comment By: Hye-Shik Chang (perky)
Date: 2003-06-28 14:26

Message:
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!"


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=762455&group_id=5470