简体   繁体   English

除了已加载的C库中的Python错误外

[英]Except an error in Python from a loaded C library

I have this piece of code which loads a library, but then it dies. 我有一段代码,它加载了一个库,但是死了。 I've surrounded it with the try/except pass, but it still dies. 我已经用try / except传递将它包围了,但是它仍然死了。 Does anyone know how to get past the error and continue with the print statement below: 有谁知道如何克服错误并继续执行以下打印语句:

      try: 
         library.load("test_data/polo.lib")
      except:
         pass

     defines = library.get_defines()
     print defines
     assert ( len(defines) == 3), "Make sure we have 3 defines"

The error I get right after the library.load line of code is: 我在library.load代码行之后立即得到的错误是:

scci18910> python test_001_library.py
Loading library test_data/polo.lib   
terminate called after throwing an instance of 'std::string'
Abort

You cant catch exception, because no python exception is thrown. 您无法捕获异常,因为没有python异常被抛出。 But you can use subprocess to bypass Abort : 但是您可以使用子流程绕过Abort

create file list_defines.py : 创建文件list_defines.py

library.load("test_data/polo.lib")
print(library.get_defines())

then: 然后:

import subprocess

try:
    defines = parse_string_to_list_somehow(subprocess.check_output('python', '-c', 'list_defines.py'))
except CalledProcessError as e:
    print('Library defines list failed', e)
    defines = []

PS Example is for python3 PS示例适用于python3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM