From da086309c2dcb492b2888749d5d44f5ef6f7ff01 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Tue, 14 Mar 2023 16:41:42 -0400 Subject: [PATCH] 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 https://github.com/commontk/CTK/pull/520 --- .../Python/Core/Python/ctk/__init__.py.in | 12 +++++++++++- .../Scripting/Python/Core/Python/qt/__init__.py.in | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Libs/Scripting/Python/Core/Python/ctk/__init__.py.in b/Libs/Scripting/Python/Core/Python/ctk/__init__.py.in index 71ee73c251..c67d527677 100644 --- a/Libs/Scripting/Python/Core/Python/ctk/__init__.py.in +++ b/Libs/Scripting/Python/Core/Python/ctk/__init__.py.in @@ -5,12 +5,22 @@ its namespace.""" __kits_to_load = [ @CTK_PYTHON_WRAPPED_LIBRARIES@ ] +import os +import sys +_standalone_python = "python" in str.lower(os.path.split(sys.executable)[-1]) + # Set to True when debugging _CTK_VERBOSE_IMPORT = False kits = [] for kit in __kits_to_load: try: + # 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 https://github.com/commontk/CTK/pull/520 + if _standalone_python: + continue exec("from CTK%sPythonQt import *" % kit) kits.append(kit) except ImportError as detail: @@ -103,6 +113,6 @@ if _lib == 'Widgets': decorates_ctkWorkflowWidgetStep_initialize_method() # Removing things the user shouldn't have to see. -del __kits_to_load, _lib, _CTK_VERBOSE_IMPORT +del __kits_to_load, _lib, _standalone_python, _CTK_VERBOSE_IMPORT del add_methodclass_to_ctkWorkflowStep_or_ctkWorkflowWidgetStep del add_methodclass_to_ctkWorkflowWidgetStep, decorates_ctkWorkflowWidgetStep_initialize_method diff --git a/Libs/Scripting/Python/Core/Python/qt/__init__.py.in b/Libs/Scripting/Python/Core/Python/qt/__init__.py.in index 828e5ad842..153a7f8b54 100644 --- a/Libs/Scripting/Python/Core/Python/qt/__init__.py.in +++ b/Libs/Scripting/Python/Core/Python/qt/__init__.py.in @@ -5,18 +5,28 @@ its namespace.""" __kits_to_load = [ @QT_PYTHON_WRAPPED_LIBRARIES@ ] +import os +import sys +_standalone_python = "python" in str.lower(os.path.split(sys.executable)[-1]) + # Set to True when debugging _CTK_VERBOSE_IMPORT = False for kit in __kits_to_load: + # 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 https://github.com/commontk/CTK/pull/520 + if _standalone_python: + continue try: exec("from PythonQt.Qt%s import *" % kit) except ImportError as detail: if _CTK_VERBOSE_IMPORT: print(detail) -if "QObject" not in locals(): +if "QObject" not in locals() and not _standalone_python: from PythonQt.private import QObject # Removing things the user shouldn't have to see. -del __kits_to_load, _CTK_VERBOSE_IMPORT +del __kits_to_load, _standalone_python, _CTK_VERBOSE_IMPORT