将散景加载到嵌入式 python dll 崩溃

loading bokeh into embedded python dll crashes

python (3.7.3) 通过 C-API 嵌入到可执行文件中。当 Python 脚本加载函数 PyImport_Import() 并且脚本包含散景导入,即 from bokeh.plotting import figure 加载过程因浮点异常而提前结束。

可执行文件是用Delphi写的,但是它的pythonAPI和原来的C-API几乎一模一样。我加载了许多其他模块没有任何问题。 bokeh是第一个加载不出来的

调用 Delphi 程序,python dll 已经加载到 exe 进程中

  // module name as PPyObject
  pPyName := delpy.PyString_FromString(PAnsiChar(AnsiString(module)));
  // import module
  pPyModule := delpy.PyImport_Import(pPyName);

和python脚本

import ptvsd
import pandas as pd
from bokeh.plotting import figure

def main_fnc():
    ptvsd.enable_attach(address=('localhost', 5678), redirect_output=True)
    ptvsd.wait_for_attach()
    breakpoint()
    print('Hello World!')

以下事件日志列出了加载到进程中的 dll,只显示了最后的条目——根据它们的命名判断——与图形功能相关。最后是抛出的错误。

Loading Module: spline.cp37-win32.pyd. Without Debug-Infos. Base address: BD70000. Process XYZ.exe (8512)
Loading Module: _nd_image.cp37-win32.pyd. Without Debug-Infos. Base address: BDD0000. Process XYZ.exe (8512)
Loading Module: _ni_label.cp37-win32.pyd. Without Debug-Infos. Base address: BE00000. Process XYZ.exe (8512)
Loading Module: _spectral.cp37-win32.pyd. Without Debug-Infos. Base address: BE90000. Process XYZ.exe (8512)
Loading Module: _peak_finding_utils.cp37-win32.pyd. Without Debug-Infos. Base address: BEB0000. Process XYZ.exe (8512)
Loading Module: _hamilton_filter.cp37-win32.pyd. Without Debug-Infos. Base address: BFB0000. Process XYZ.exe (8512)
Loading Module: _kim_smoother.cp37-win32.pyd. Without Debug-Infos. Base address: BFF0000. Process XYZ.exe (8512)
Loading Module: linbin.cp37-win32.pyd. Without Debug-Infos. Base address: C030000. Process XYZ.exe (8512)
Loading Module: _smoothers_lowess.cp37-win32.pyd. Without Debug-Infos. Base address: C050000. Process XYZ.exe (8512)
Loading Module: _contour.cp37-win32.pyd. Without Debug-Infos. Base address: C280000. Process XYZ.exe (8512)
Loading Module: ft2font.cp37-win32.pyd. Without Debug-Infos. Base address: C2B0000. Process XYZ.exe (8512)
Loading Module: _png.cp37-win32.pyd. Without Debug-Infos. Base address: C450000. Process XYZ.exe (8512)
Loading Module: kiwisolver.cp37-win32.pyd. Without Debug-Infos. Base address: C4D0000. Process XYZ.exe (8512)
Loading Module: _image.cp37-win32.pyd. Without Debug-Infos. Base address: C580000. Process XYZ.exe (8512)
Loading Module: _tri.cp37-win32.pyd. Without Debug-Infos. Base address: C680000. Process XYZ.exe (8512)
Loading Module: _qhull.cp37-win32.pyd. Without Debug-Infos. Base address: C6B0000. Process XYZ.exe (8512)
First Chance Exception at 762F54. Exception-Class $C0000090 with Message 'floating point invalid operation at 0x20762f54'. Process XYZ.exe (8512)

这通常发生在使用外部库时。在这种情况下,调用 SetExceptionMask 来屏蔽浮点异常可能会有所帮助。