代码之家  ›  专栏  ›  技术社区  ›  Rohit

在嵌入式ironpython中导入库时引发未处理的generatorexit异常

  •  1
  • Rohit  · 技术社区  · 15 年前

    我在C应用程序中嵌入了IronPython。我让用户编写IrimPython脚本,在这些脚本中,他们可以导入一组带有IrPython的标准库。在这些脚本中,当用户导入“random”库或“filecmp”库时,将引发未处理的generatorexit异常。

    其他库,如math、re、string和os,用户可以毫无问题地导入。

    这是我得到的堆栈跟踪:

    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   
    

    有人遇到过类似的问题吗?解决办法是什么?

    编辑 只有附加了visual studio调试器时才会发生这种情况。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Dino Viehland    15 年前

    这真的是一个未处理的异常吗?或者你只是在调试器中看到它?

    在IronPython2.0和2.6中,生成器的finalizer(这里运行的是finalize方法)有一个try/catch(exception),它可以吞下所有异常。因此,虽然可能会在终结器线程上引发异常,但它不会对应用程序产生任何影响。

    引发异常的原因是有人在生成器完成之前没有对其进行迭代。cpython文档说,当收集生成器时,它将向生成器发送一个异常,以便让任何finally块运行。

        2
  •  0
  •   babbageclunk    15 年前

    不是一个真正的答案(这里我没有访问ironpython的权限),但是如果您尝试运行这个脚本:

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

    ……它将显示一个python级的回溯,而不是c级的回溯——这可能会让它更清楚发生了什么。

    (如果sys.stdout没有连接到您捕获的任何内容,您可以使用 traceback.format_exc() 取而代之的是)