views:

105

answers:

2

I have IronPython embedded inside a C# application. I let users write IronPython scripts, in which they can import a set of the standard libraries shipped with IronPython. In these scripts when a user imports the "random" library or the "filecmp" library, an unhandled GeneratorExitException is thrown.

Other libraries like math, re, string and os are importable by users without any issues.

This is the stack trace which I get:

IronPython.dll!IronPython.Runtime.PythonGenerator.ThrowThrowable() + 0x85 bytes 
IronPython.dll!IronPython.Runtime.PythonGenerator.CheckThrowable() + 0x27 bytes 
IronPython.dll!IronPython.Runtime.PythonGenerator.CheckThrowableAndReturnSendValue() + 0x3c bytes 
IronPython.dll!IronPython.Runtime.Operations.PythonOps.GeneratorCheckThrowableAndReturnSendValue(object self = {IronPython.Runtime.PythonGenerator}) + 0x49 bytes 
Snippets.debug.scripting!S$12.lambda_method$344(ref int state = -1, ref object current = null) + 0x124 bytes Unknown
Microsoft.Scripting.dll!Microsoft.Scripting.Runtime.GeneratorEnumerator<object>.System.Collections.IEnumerator.MoveNext() + 0x3c bytes 
IronPython.dll!IronPython.Runtime.PythonGenerator.MoveNextWorker() + 0xa3 bytes 
IronPython.dll!IronPython.Runtime.PythonGenerator.System.Collections.IEnumerator.MoveNext() + 0x42 bytes 
IronPython.dll!IronPython.Runtime.PythonGenerator.throw(object type = {"Exception of type 'IronPython.Runtime.Exceptions.GeneratorExitException' was thrown."}, object value = null, object traceback = null) + 0xb5 bytes 
IronPython.dll!IronPython.Runtime.PythonGenerator.throw(object type = {"Exception of type 'IronPython.Runtime.Exceptions.GeneratorExitException' was thrown."}) + 0x2a bytes 
IronPython.dll!IronPython.Runtime.PythonGenerator.close() + 0x56 bytes 
IronPython.dll!IronPython.Runtime.PythonGenerator.Finalize() + 0x42 bytes

Has anyone faced a similar problem? And what's the solution?

EDIT This only happens when the Visual Studio debugger is attached.

A: 

Not really an answer (I don't have access to IronPython here), but if you try running this script:

import traceback
try:
    import random
except:
    traceback.print_exc()

...it will display a Python-level traceback rather than a C# one - that might make it clearer what's happening.

(If sys.stdout isn't hooked up to anything you're capturing, you could use traceback.format_exc() instead.)

wilberforce
+1  A: 

Is this really an unhandled exception or are you just seeing it in the debugger?

In both IronPython 2.0 and 2.6 the finalizer for generators (which is what's running here - see the Finalize method) has a try / catch(Exception) which swallows all exceptions. So while an exception might be getting thrown on the finalizer thread it should have not have any impact to your application.

The reason the exception is being thrown is that someone did not iterate over a generator until it's complete. The CPython documentation says that when the generator is collected it will send an exception into the generator in order to let any finally blocks running.

Dino Viehland
Yes, it only happens when the Visual Studio debugger is attached.
Rohit
Ok, in that case it should be something you can safely ignore and hopefully it'll go away in 2.6. To make your debugging experience better you can configure VS to not break on GeneratorExitExceptions in the exception dialog.
Dino Viehland