Skip to content

Commit cbc2c9b

Browse files
committed
BUG: Avoid segfault if importing qt & ctk python package in standalone python
Since importing a PythonQt-based module outside of a Qt application leads to a segfault, skip the import if it happens in a standalone python interpreter. See commontk#520
1 parent eaa9200 commit cbc2c9b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

Libs/Scripting/Python/Core/Python/ctk/__init__.py.in

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ its namespace."""
55

66
__kits_to_load = [ @CTK_PYTHON_WRAPPED_LIBRARIES@ ]
77

8+
import os
9+
import sys
10+
_standalone_python = "python" in str.lower(os.path.split(sys.executable)[-1])
11+
812
# Set to True when debugging
913
_CTK_VERBOSE_IMPORT = False
1014

1115
kits = []
1216
for kit in __kits_to_load:
17+
# Since importing a PythonQt-based module outside of a Qt application
18+
# leads to a segfault, skip the import if it happens in a standalone
19+
# python interpreter.
20+
# See https://github.com/commontk/CTK/pull/520
21+
if _standalone_python:
22+
continue
1323
try:
1424
exec("from CTK%sPythonQt import *" % kit)
1525
kits.append(kit)
@@ -103,6 +113,6 @@ if _lib == 'Widgets':
103113
decorates_ctkWorkflowWidgetStep_initialize_method()
104114

105115
# Removing things the user shouldn't have to see.
106-
del __kits_to_load, _lib, _CTK_VERBOSE_IMPORT
116+
del __kits_to_load, _lib, _standalone_python, _CTK_VERBOSE_IMPORT
107117
del add_methodclass_to_ctkWorkflowStep_or_ctkWorkflowWidgetStep
108118
del add_methodclass_to_ctkWorkflowWidgetStep, decorates_ctkWorkflowWidgetStep_initialize_method

Libs/Scripting/Python/Core/Python/qt/__init__.py.in

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@ its namespace."""
55

66
__kits_to_load = [ @QT_PYTHON_WRAPPED_LIBRARIES@ ]
77

8+
import os
9+
import sys
10+
_standalone_python = "python" in str.lower(os.path.split(sys.executable)[-1])
11+
812
# Set to True when debugging
913
_CTK_VERBOSE_IMPORT = False
1014

1115
for kit in __kits_to_load:
16+
# Since importing a PythonQt-based module outside of a Qt application
17+
# leads to a segfault, skip the import if it happens in a standalone
18+
# python interpreter.
19+
# See https://github.com/commontk/CTK/pull/520
20+
if _standalone_python:
21+
continue
1222
try:
1323
exec("from PythonQt.Qt%s import *" % kit)
1424
except ImportError as detail:
1525
if _CTK_VERBOSE_IMPORT:
1626
print(detail)
1727

18-
if "QObject" not in locals():
28+
if "QObject" not in locals() and not _standalone_python:
1929
from PythonQt.private import QObject
2030

2131
# Removing things the user shouldn't have to see.
22-
del __kits_to_load, _CTK_VERBOSE_IMPORT
32+
del __kits_to_load, _standalone_python, _CTK_VERBOSE_IMPORT

0 commit comments

Comments
 (0)