@@ -72,7 +72,7 @@ def log(x):
7272f = open ('/tmp/bpython.log' ,'a' )
7373f .write ('%s\n ' % (x ,))
7474
75-
75+ orig_stdout = sys . __stdout__
7676stdscr = None
7777
7878class Struct (object ):
@@ -1061,7 +1061,8 @@ def write(self, s):
10611061t = s
10621062
10631063if isinstance (t ,unicode ):
1064- t = t .encode (getattr (sys .__stdout__ ,'encoding' )or sys .getdefaultencoding ())
1064+ t = (t .encode (getattr (orig_stdout ,'encoding' ,None )or
1065+ sys .getdefaultencoding ()))
10651066
10661067if not self .stdout_hist :
10671068self .stdout_hist = t
@@ -1096,7 +1097,8 @@ def echo(self, s, redraw=True):
10961097 srings. It won't update the screen if it's reevaluating the code (as it
10971098 does with undo)."""
10981099if isinstance (s ,unicode ):
1099- s = s .encode (getattr (sys .__stdout__ ,'encoding' )or sys .getdefaultencoding ())
1100+ s = (s .encode (getattr (orig_stdout ,'encoding' ,None )
1101+ or sys .getdefaultencoding ()))
11001102
11011103a = curses .color_pair (0 )
11021104if '\x01 ' in s :
@@ -1712,7 +1714,7 @@ def gethw():
17121714
17131715 """
17141716h ,w = struct .unpack (
1715- "hhhh" ,fcntl .ioctl (sys . __stdout__ ,termios .TIOCGWINSZ ,"\000 " * 8 ))[0 :2 ]
1717+ "hhhh" ,fcntl .ioctl (orig_stdout ,termios .TIOCGWINSZ ,"\000 " * 8 ))[0 :2 ]
17161718return h ,w
17171719
17181720
@@ -1932,7 +1934,7 @@ def main(args=None):
19321934curses .nocbreak ()
19331935curses .endwin ()
19341936
1935- sys .stdout = sys . __stdout__
1937+ sys .stdout = orig_stdout
19361938if tb :
19371939print tb
19381940sys .exit (1 )