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

SourceForge.net noreply@sourceforge.net
Sun, 29 Jun 2003 08:45:19 -0700


Bugs item #762455, was opened at 2003-06-28 13:24
Message generated for change (Comment added) made by rhettinger
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: Closed
Resolution: Fixed
Priority: 5
Submitted By: Hye-Shik Chang (perky)
Assigned to: Raymond Hettinger (rhettinger)
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: Raymond Hettinger (rhettinger)
Date: 2003-06-29 10:45

Message:
Logged In: YES 
user_id=80475

Done.
See test_descr.py 1.196

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

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-06-29 09:57

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

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

Comment By: Tim Peters (tim_one)
Date: 2003-06-29 00:56

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

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-06-29 00:27

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



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

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-06-28 23:58

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

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-06-28 22:38

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

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

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-06-28 21: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 13: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