From a3ff671eb9b998b17f5474b29bb8ad437e93c0c9 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Thu, 23 Feb 2023 17:07:41 +0100 Subject: [PATCH] Python debugger compatible with PyQt Problem was that PyQt generates spontaneous Python events that do not originate from KLayout itself. This made the IDE detect a stop request and the application crashed. --- src/lay/lay/layMacroEditorDialog.cc | 35 +++++++++++++---------------- src/lay/lay/layMacroEditorDialog.h | 1 + 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/lay/lay/layMacroEditorDialog.cc b/src/lay/lay/layMacroEditorDialog.cc index ba4159cce..4924798da 100644 --- a/src/lay/lay/layMacroEditorDialog.cc +++ b/src/lay/lay/layMacroEditorDialog.cc @@ -3152,11 +3152,6 @@ MacroEditorDialog::translate_pseudo_id (size_t &file_id, int &line) } } -static void exit_from_macro () -{ - throw tl::ExitException (); -} - void MacroEditorDialog::exception_thrown (gsi::Interpreter *interpreter, size_t file_id, int line, const std::string &eclass, const std::string &emsg, const gsi::StackTraceProvider *stack_trace_provider) { @@ -3165,9 +3160,7 @@ MacroEditorDialog::exception_thrown (gsi::Interpreter *interpreter, size_t file_ return; } - if (!m_in_exec) { - exit_from_macro (); - } + exit_if_needed (); // avoid recursive breakpoints and exception catches from the console while in a breakpoint or exception stop if (lay::BusySection::is_busy ()) { @@ -3251,17 +3244,25 @@ MacroEditorDialog::exception_thrown (gsi::Interpreter *interpreter, size_t file_ throw; } - if (! m_in_exec) { - exit_from_macro (); + exit_if_needed (); +} + +void +MacroEditorDialog::exit_if_needed () +{ + // Exit if a stop is requested. + // NOTE: we must not raise ExitException from outside events (e.g. PyQt5 events) + // as ExitException would otherwise terminate the application. + // "mp_exec_controller" is 0 in that case. + if (! m_in_exec && mp_exec_controller != 0) { + throw tl::ExitException (); } } void MacroEditorDialog::trace (gsi::Interpreter *interpreter, size_t file_id, int line, const gsi::StackTraceProvider *stack_trace_provider) { - if (!m_in_exec) { - exit_from_macro (); - } + exit_if_needed (); // avoid recursive breakpoints and exception catches from the console while in a breakpoint or exception stop if (lay::BusySection::is_busy ()) { @@ -3310,9 +3311,7 @@ MacroEditorDialog::trace (gsi::Interpreter *interpreter, size_t file_id, int lin throw; } - if (! m_in_exec) { - exit_from_macro (); - } + exit_if_needed (); } else if (++m_trace_count == 20) { @@ -3328,9 +3327,7 @@ MacroEditorDialog::trace (gsi::Interpreter *interpreter, size_t file_id, int lin m_last_process_events = tl::Clock::current (); m_process_events_interval = std::max (0.05, std::min (2.0, (m_last_process_events - start).seconds () * 5.0)); - if (!m_in_exec) { - exit_from_macro (); - } + exit_if_needed (); } diff --git a/src/lay/lay/layMacroEditorDialog.h b/src/lay/lay/layMacroEditorDialog.h index e5031c72f..7461f2a2c 100644 --- a/src/lay/lay/layMacroEditorDialog.h +++ b/src/lay/lay/layMacroEditorDialog.h @@ -302,6 +302,7 @@ private: bool configure (const std::string &name, const std::string &value); void config_finalize (); void translate_pseudo_id (size_t &file_id, int &line); + void exit_if_needed (); lay::Dispatcher *mp_plugin_root; lym::MacroCollection *mp_root;