Embedded python: How to debug code in an isolated way

Barry Scott barry at barrys-emacs.org
Wed Aug 26 03:21:10 EDT 2020



> On 24 Aug 2020, at 12:52, Eko palypse <ekopalypse at gmail.com> wrote:
> 
> Thank you very much for your interest in my little problem.
> 
>> When the app calls into python does the event loop of the gui block?
> 
> Yes, the cpp app calls a callback function from the embedded python
> interpreter synchronously.

> 
>> When you print does that trigger the event loop to run in a nested call
> back into the cpp?
> 
> I can't say for sure but I don't think so. At least my script or the
> embedded interpreter doesn't do that.
> 
> 
>> Maybe you need to run python in its own thread and Marshall in and out
> of the gui main thread with the event loop?
> 
> I've checked by using GetCurrentThreadId
> <https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentthreadid>
> that the registered callback function and debugger run in the same thread
> but it is different to the UI thread.

> Do you think I have to run the debugger in its own thread? I'll give it a
> try anyways. :-)

No I doubt that can will work. What I was thinking is that your life was made more
complex by multi threads and GUI event loops.

I think that you should not attempt to use a debugger at all.

Add code to put the information you need to find the bug into
a log file.

You might want to build a feature into the GUI that will show the log file to you
if it not easy to get at a log file on disk.

As an aside I have never used a python debugger to fix python code.
I have always relied on logging key information to allow me to fix bugs.
I have used gdb to debug C/C++ and python.

All the non-trivial projects I work on have multi stream logging builtin.
I tend to design the logging with the aim of debugging and maintaining
code from the outset these days. When a new bug turns up I just switch on
the appropriate stream of logs to help me fix it, add new logs as necessary.
 
Barry



More information about the Python-list mailing list