office-gobmx/pyuno
Stephan Bergmann 1638b4f78a rhbz#2097411 Avoid obsolete PyThreadState_Delete crashing Python 3.11
1fb53a6375 "pyuno_loader::CreateInstance: delete
the initial PyThreadState" had added the PyThreadState_Delete for claimed
benefits but whose details appear lost to history (cf. the comment thread
starting at
<https://gerrit.libreoffice.org/c/core/+/3452/2#message-602ff52abdd1c95fd5c13cfe405b5fadd0048c5f>
"pyuno_loader::CreateInstance: delete the initial PyThreadState").  And at least
a recent master Linux --enable-python=fully-internal build with the bundled
Python 3.8.12 appears to succeed `make check` just fine with the
PyThreadState_Delete temporarily removed.

But on the other hand, building against upcoming Python 3.11 now started to make
CppunitTest_services fail with

> Fatal Python error: init_threadstate: thread state already initialized
> Python runtime state: initialized
> Thread 0x0000ffff81c8b020 (most recent call first):
>   <no Python frame>
> Fatal exception: Signal 6
> Stack:
> /builddir/build/BUILD/libreoffice-7.3.4.2/instdir/program/libuno_sal.so.3(+0x37c28)[0xffff81be7c28]
> /builddir/build/BUILD/libreoffice-7.3.4.2/instdir/program/libuno_sal.so.3(+0x37e40)[0xffff81be7e40]
> linux-vdso.so.1(__kernel_rt_sigreturn+0x0)[0xffff81ccb7ec]
> /lib64/libc.so.6(+0x82878)[0xffff81742878]
> /lib64/libc.so.6(raise+0x20)[0xffff816fae00]
> /lib64/libc.so.6(abort+0xe8)[0xffff816e72b8]
> /lib64/libpython3.11.so.1.0(+0x104e28)[0xfffee4de4e28]
> /lib64/libpython3.11.so.1.0(+0x105200)[0xfffee4de5200]
> /lib64/libpython3.11.so.1.0(PyThread_get_thread_native_id+0x0)[0xfffee4ed6764]
> /lib64/libpython3.11.so.1.0(PyThreadState_New+0x14)[0xfffee4ed6628]
> /builddir/build/BUILD/libreoffice-7.3.4.2/instdir/program/libpyuno.so(_ZN5pyuno14PyThreadAttachC2EP3_is+0x78)[0xfffee4c8c52c]
> /builddir/build/BUILD/libreoffice-7.3.4.2/instdir/program/libpythonloaderlo.so(pyuno_Loader_get_implementation+0x5c)[0xfffee5243060]
> /builddir/build/BUILD/libreoffice-7.3.4.2/instdir/program/libuno_cppuhelpergcc3.so.3(+0x544b4)[0xffff815544b4]

because of the PyThreadState_Delete.  (The deleted PyThreadState, while not
reused again directly, is still recorded in the state obtained from
PyInterpreterState_Head() later.)

So conservatively keep the PyThreadState_Delete of unclear benefit for older
Python versions and only drop it for 3.11 where it is known to have negative
effects now.

Change-Id: I9b99f1e947f0b165ddc95c2bfbd764858dda39db
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136006
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2022-06-16 22:40:28 +02:00
..
demo
doc
inc
qa/pytests
source
zipcore
CustomTarget_python_shell.mk
CustomTarget_pyuno_pythonloader_ini.mk
Executable_python.mk
IwyuFilter_pyuno.yaml
Library_pythonloader.mk
Library_pyuno.mk
Library_pyuno_wrapper.mk
Makefile
Module_pyuno.mk
Package_python_scripts.mk
Package_python_shell.mk
Package_pyuno_pythonloader_ini.mk
PythonTest_pyuno_pytests_insertremovecells.mk
PythonTest_pyuno_pytests_testcollections.mk
PythonTest_pyuno_pytests_testssl.mk
Rdb_pyuno.mk
README.md

Python UNO Bindings

UNO bindings for the Python programming language.

To have much joy debugging Python extensions you need to:

  • a) edit pythonloader.py in your install setting DEBUG=1 at the top
  • b) touch pyuno/source/module/pyuno_runtime.cxx and make debug=true in pyuno

Then you'll start to see your exceptions on the console instead of them getting lost at the UNO interface.

Python also comes with a gdb script libpython$(PYTHON_VERSION_MAJOR).$(PYTHON_VERSION_MINOR)m.so.1.0-gdb.py that is copied to instdir and will be auto-loaded by gdb; it provides commands like py-bt to get a Python-level backtrace, and py-print to print Python variables.

Another way to debug Python code is to use pdb: edit some initialization function to insert import pdb; pdb.set_trace() (somewhere so that it is executed early), then run soffice from a terminal and a command-line Python debugger will appear where you can set Python-level breakpoints.