Hi,

I've been evaluating a suite of python code originally targeting CPython 2.5 to 
see what needs to be fixed/changed to run under IronPython 2.6.  I ran into a 
problem in IronPython 2.6 where I redirect sys.stdout through a class object 
that forwards output to the original sys.stdout.  When control returns to the 
python command prompt, an exception is displayed in an infinite loop.

If I redirect sys.stderr first (to the same object) before redirecting 
sys.stdout, IronPython 2.6 shows two exception errors then abruptly exits.

Since this particular part of my python code runs fine under CPython 2.5, 2.6 
and IronPython 2.0.3, I'm thinking it may be a bug in IronPython 2.6.

What follows is a reproduction case showing IronPython 2.6 exiting after two 
exceptions.  Note that the redirection is actually working and that the 
exception occurs only when the python console prompt is displayed.

1. Create a file called testredirect.py with the following code:

import sys

class _StreamLog(object):
    def __init__(self, ostream):
        self.ostream = ostream

    def write(self, *args):
        self.ostream.write("{")
        self.ostream.write(*args)
        self.ostream.write("}")

    def flush(self):
        self.ostream.flush()

if not sys.stderr.__class__ == _StreamLog:
    sys.stderr = _StreamLog(sys.stderr)
if not sys.stdout.__class__ == _StreamLog:
    sys.stdout = _StreamLog(sys.stdout)

print >> sys.stderr, "Redirected stderr\n"
print >> sys.stdout, "Redirected stdout\n"


2. Open an IronPython 2.6 console and import the testredirect module.  This 
produces the following output:

C:\>ipy
IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.1433
Type "help", "copyright", "credits" or "license" for more information.
>>> import testredirect
{Redirected stderr
}{
}{Redirected stdout
}{
}{>>> }{Traceback (most recent call last):
SystemError: Object reference not set to an instance of an object.
}Unhandled exception:
Traceback (most recent call last):
SystemError: Object reference not set to an instance of an object.


4. Rerunning the above test with -X:ExceptionDetail enabled, I get the 
following:

C:\ >ipy -X:ExceptionDetail
IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.1433
Type "help", "copyright", "credits" or "license" for more information.
>>> import testredirect
{Redirected stderr
}{
}{Redirected stdout
}{
}{>>> }{Object reference not set to an instance of an object.
   at IronPython.Runtime.OutputWriter.Flush()
   at Microsoft.Scripting.Hosting.Shell.BasicConsole.Write(String text, Style 
style)
   at Microsoft.Scripting.Hosting.Shell.CommandLine.ReadStatement(Boolean& 
continueInteraction)
   at IronPython.Hosting.PythonCommandLine.RunOneInteraction()
   at IronPython.Hosting.PythonCommandLine.TryInteractiveActionWorker()
   at IronPython.Hosting.PythonCommandLine.TryInteractiveAction()
   at Microsoft.Scripting.Hosting.Shell.CommandLine.RunInteractiveLoop()
SystemError: Object reference not set to an instance of an object.
}Unhandled exception:
Object reference not set to an instance of an object.
   at IronPython.Runtime.OutputWriter.Flush()
   at Microsoft.Scripting.Hosting.Shell.BasicConsole.Write(String text, Style 
style)
   at Microsoft.Scripting.Hosting.Shell.BasicConsole.WriteLine(String text, 
Style style)
   at IronPython.Runtime.Operations.PythonOps.PrintException(CodeContext 
context, Exception exception, IConsole console)
   at IronPython.Hosting.PythonCommandLine.UnhandledException(Exception e)
   at Microsoft.Scripting.Hosting.Shell.CommandLine.RunInteractiveLoop()
   at IronPython.Hosting.PythonCommandLine.RunInteractive()
   at Microsoft.Scripting.Hosting.Shell.CommandLine.Run()
   at IronPython.Hosting.PythonCommandLine.Run()
   at Microsoft.Scripting.Hosting.Shell.CommandLine.Run(ScriptEngine engine, 
IConsole console, ConsoleOptions options)
   at Microsoft.Scripting.Hosting.Shell.ConsoleHost.RunCommandLine()
SystemError: Object reference not set to an instance of an object.


_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to