4

Here is my problem:

I have two C++ modules, A and B, which are built as dynamically-linked libraries. A offers basic math functions, and custom exception types. B is a higher level module that uses A.

B::someFunction() calls a function from A, and tries to catch custom exception A:MyExceptionFromA in order to convert it into a custom type B:MyExceptionFromB (since users of module B do not need to know about the implementation details of A).

Everything works fine as long as I remain in the C++ domain. However, if I expose B::someFunction() in python via boost python, the exception is not caught anymore in the C++ module.

I can catch std::runtime_error, from which A:MyExceptionFromA derives, and call typeid(e).name() to get the retrieve the correct mangled name, so I know the correct exception is thrown. Therefore I suspect that the problem comes from resolving this mangled symbol into the correct exception type.

I have found this link, which explains that "python uses [the insular] model to open extension modules, so that extension module writers don't need to know what symbols other extension modules might be using.". I'm suspecting this is part of the problem/solution, but I do not know enough about symbol resolution to figure out how to solve my problem.

Any ideas?

4

1 回答 1

2

我找到了解决我的问题的方法。基于此链接文本,我发现添加

import sys, dl
sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)

在我的包含解决问题之前,通过强制 python 以立即全局模式打开库。但我仍然希望有一个替代解决方案,如果有的话。如第一个链接中所述,我怀疑这可能会产生无法预料的影响(我已经知道名称冲突可能是一个问题,并且我怀疑性能也会受到影响,但是还有其他副作用吗?)

于 2010-08-23T13:19:12.703 回答