Segfault caused by how modules are imported in Python example
I built VTK against a python
provided by conda-forge, with the following configure command:
cmake -GNinja \
-DCMAKE_INSTALL_PREFIX:PATH=/data/scott/Documents/miniconda2/envs/vtk-cdat-py3-debug \
-DCMAKE_BUILD_TYPE=Debug \
-DVTK_BUILD_DOCUMENTATION:BOOL=OFF \
-DVTK_BUILD_TESTING:STRING=WANT \
-DVTK_BUILD_SHARED_LIBS:BOOL=ON \
-DVTK_WRAP_PYTHON=ON \
-DVTK_PYTHON_VERSION=3 \
-DPYTHON_EXECUTABLE=/data/scott/Documents/miniconda2/envs/vtk-cdat-py3-debug/bin/python \
-DPython3_INCLUDE_DIR=/data/scott/Documents/miniconda2/envs/vtk-cdat-py3-debug/include/python3.6m \
-DPython3_LIBRARY_RELEASE:FILEPATH=/data/scott/Documents/miniconda2/envs/vtk-cdat-py3-debug/lib/libpython3.6m.so \
-DVTK_MODULE_ENABLE_VTK_ViewsContext2D:STRING=YES \
-DVTK_MODULE_ENABLE_VTK_PythonContext2D:STRING=YES \
-DVTK_MODULE_ENABLE_VTK_RenderingContext2D:STRING=YES \
-DVTK_MODULE_ENABLE_VTK_RenderingContextOpenGL2:STRING=YES \
-DVTK_MODULE_ENABLE_VTK_RenderingCore:STRING=YES \
-DVTK_MODULE_ENABLE_VTK_RenderingOpenGL2:STRING=YES \
-DVTK_MODULE_ENABLE_VTK_RenderingFreeType:STRING=YES \
-DVTK_MODULE_ENABLE_VTK_RenderingMatplotlib:STRING=YES \
-DVTK_MODULE_ENABLE_VTK_InteractionImage:STRING=YES \
-DVTK_PYTHON_OPTIONAL_LINK:BOOL=ON \
-DVTK_DEBUG_LEAKS:BOOL=ON \
../vtkSrc/
Note that in order for VTK_PYTHON_OPTIONAL_LINK
to be used, I built against the version of VTK in !5275 (merged). But I also sent the following python code to another VTK developer to try in a recent build tree, and he reported the same behavior.
The python code comes from porting an existing C++ test (Rendering/Matplotlib/Testing/Cxx/TestRenderString.cxx
), and looks like this:
from vtkmodules.vtkRenderingMatplotlib import vtkMatplotlibMathTextUtilities
from vtkmodules.vtkCommonDataModel import vtkImageData
from vtkmodules.vtkRenderingCore import vtkTextProperty, vtkRenderWindowInteractor
from vtkmodules.vtkInteractionImage import vtkImageViewer2
# from vtkmodules import vtkRenderingOpenGL2
# import vtk
mathTextStr = "$\\hat{H}\\psi = \\left(-\\frac{\\hbar}{2m}\\nabla^2 + V(r)\\right) \\psi = \\psi\\cdot E $"
image = vtkImageData()
# image = vtk.vtkImageData()
utils = vtkMatplotlibMathTextUtilities()
# utils = vtk.vtkMatplotlibMathTextUtilities()
# utils = vtkMathTextUtilities()
utils.SetScaleToPowerOfTwo(False)
tprop = vtkTextProperty()
# tprop = vtk.vtkTextProperty()
tprop.SetColor(1, 1, 1)
tprop.SetFontSize(50)
viewer = vtkImageViewer2()
# viewer = vtk.vtkImageViewer2()
utils.RenderString(mathTextStr, image, tprop,
viewer.GetRenderWindow().GetDPI())
viewer.SetInputData(image)
iren = vtkRenderWindowInteractor()
# iren = vtk.vtkRenderWindowInteractor()
viewer.SetupInteractor(iren)
viewer.Render()
viewer.GetRenderer().ResetCamera()
viewer.GetRenderer().GetActiveCamera().Zoom(6.0)
viewer.Render()
viewer.GetRenderWindow().SetMultiSamples(0)
viewer.GetRenderWindow().GetInteractor().Initialize()
viewer.GetRenderWindow().GetInteractor().Start()
When I run that example using vtkpython
, I get the following output:
$ ./bin/vtkpython /blah/blah/VTK/Python/mathTextExample.py
( 0.146s) [main thread ]vtkImageSliceMapper.cxx:35 WARN| Error: no override found for 'vtkImageSliceMapper'.
Loguru caught a signal: SIGSEGV
Stack trace:
21 0x400ae9 ./bin/vtkpython() [0x400ae9]
20 0x7f7355a82830 __libc_start_main + 240
19 0x400c8a ./bin/vtkpython() [0x400c8a]
18 0x7f735585798c vtkPythonInterpreter::PyMain(int, char**) + 938
17 0x7f735564d8fb Py_Main + 3739
16 0x7f73555c4484 PyRun_SimpleFileExFlags + 244
15 0x7f73555c05f7 PyRun_FileExFlags + 183
14 0x7f73555c8cfc PyEval_EvalCode + 28
13 0x7f73555c8cce PyEval_EvalCodeEx + 62
12 0x7f73555c82c3 /data/scott/Documents/miniconda2/envs/vtk-cdat-py3-debug/lib/libpython3.6m.so.1.0(+0xc92c3) [0x7f73555c82c3]
11 0x7f73555cc531 _PyEval_EvalFrameDefault + 13313
10 0x7f73555c8717 /data/scott/Documents/miniconda2/envs/vtk-cdat-py3-debug/lib/libpython3.6m.so.1.0(+0xc9717) [0x7f73555c8717]
9 0x7f73556ed2ca _PyObject_FastCallDict + 138
8 0x7f7355605a25 /data/scott/Documents/miniconda2/envs/vtk-cdat-py3-debug/lib/libpython3.6m.so.1.0(+0x106a25) [0x7f7355605a25]
7 0x7f735284bc86 PyVTKObject_New + 268
6 0x7f735284c682 PyVTKObject_FromPointer + 771
5 0x7f734c22fe0c /data/scott/projects/VTK/build-config-test/lib/python3.6/site-packages/vtkmodules/vtkInteractionImagePython.so(+0xde0c) [0x7f734c22fe0c]
4 0x7f734c012f16 vtkImageViewer2::New() + 32
3 0x7f734c012f94 vtkImageViewer2::vtkImageViewer2() + 72
2 0x7f734eaac9a0 vtkImageActor::New() + 32
1 0x7f734eaacb15 vtkImageActor::vtkImageActor() + 319
0 0x7f7355a974b0 /lib/x86_64-linux-gnu/libc.so.6(+0x354b0) [0x7f7355a974b0]
( 0.146s) [main thread ] :0 FATL| Signal: SIGSEGV
Segmentation fault (core dumped)
If I make either one of the following changes to the python code above, it seems to fix the issue:
- Uncomment the line which imports the
vtkRenderingOpenGL2
module. - Replace all the
from vtkmodules.vtk... import ...
with the singleimport vtk
, and then update how all the instance are created to prefix withvtk.
Is this a bug? Or is it the expected behavior of the python wrappings under the new module system?