https://bugs.documentfoundation.org/show_bug.cgi?id=170443

--- Comment #2 from [email protected] ---
Created attachment 207478
  --> https://bugs.documentfoundation.org/attachment.cgi?id=207478&action=edit
pyuno: build pyuno against Python Stable ABI

Subject: pyuno: build pyuno against Python Stable ABI

This patch is a concrete first pass at removing pyuno's CPython-minor-version
lock by moving the pyuno C extension surface toward Python's Stable ABI.

Problem:
pyuno currently depends on CPython-version-specific ABI details. On Windows
this
shows up directly as a pyuno.pyd built for a specific pythonXY.dll, so Python
automation clients must match LibreOffice's bundled CPython minor version.

Approach:
- Add a pyuno-specific `python_abi3` external that defines
  `Py_LIMITED_API=0x030A0000`.
- Link pyuno through a pyuno-specific ABI3 external while leaving other
  LibreOffice Python consumers on the existing Python external. On POSIX
builds,
  prefer the non-embed `python3` pkg-config module and allow pyuno's Python API
  symbols to resolve from the loading interpreter instead of adding a
  minor-versioned `libpython3.x` dependency.
- Convert pyuno's static `PyTypeObject` definitions to heap types created with
  `PyType_FromSpec`.
- Replace direct CPython struct access such as `PyThreadState_Get()->interp`,
  `Py_TYPE(obj)->tp_dict`, and `tp_free` with public API usage.
- Replace APIs/macros not available at the Python 3.10 Limited API floor, such
  as `PyUnicode_AsUTF8` and `PyUnicode_FromKindAndData`.

Validation done here:
- Confirmed MSVC Build Tools and Windows SDK are installed and usable.
- Compiled, linked, and ran a small Windows C++ smoke test with `cl.exe`.
- Applied this patch to a full LibreOffice core checkout at commit `ea59b2284`.
- Configured a WSL Ubuntu 24.04 build with system Python 3.12 and a minimized
  headless build profile.
- Ran `make fetch`, `PARALLELISM=2 make Library_pyuno`, and
  `PARALLELISM=2 make Pyuno` successfully.
- Verified `instdir/program/libpyuno.so` does not have a `libpython3.12`
  `DT_NEEDED` entry.
- Smoke-loaded `instdir/program/libpyuno.so` from Python 3.12 with
  `ctypes.CDLL(...)` and `LD_LIBRARY_PATH` pointed at `instdir/program`.
- Scanned the patched pyuno module for remaining static type objects,
  CPython-version conditionals, direct type-field access, and removed Unicode
  APIs.
- Checked the used `PyType_Slot` constants and main APIs against CPython 3.10
  headers, matching the `Py_LIMITED_API=0x030A0000` floor.

Still needed upstream:
- Review whether POSIX pyuno should explicitly opt out of `-z defs` as done
here
  or use a gbuild-level Python-extension abstraction.
- Run the full pyuno Python tests, including Writer/Calc UNO import and bridge
  smoke tests from more than one Python 3 minor version.
- Run Windows and macOS builds, especially the Windows
`python3.lib`/`python3.dll`
  Stable ABI path that motivated this change.


# Validation Notes

LibreOffice core checkout:

- Source: `https://github.com/LibreOffice/core.git`
- Commit tested: `ea59b2284`
- Build host: Ubuntu 24.04 in WSL
- Python: system Python 3.12

Configure/build commands:

```sh
./autogen.sh --enable-python=system --disable-gui --without-x --without-java \
  --disable-gtk3 --disable-gtk4 --disable-dbus --disable-cups \
  --disable-gstreamer-1-0 --disable-sdremote --disable-sdremote-bluetooth \
  --disable-pdfimport --disable-skia --disable-lpsolve --disable-coinmp \
  --disable-firebird-sdbc --disable-postgresql-sdbc --disable-report-builder \
  --disable-extension-integration --without-help --without-myspell-dicts \
  --disable-ccache --disable-dependency-tracking --disable-werror

make fetch
PARALLELISM=2 make Library_pyuno
PARALLELISM=2 make Pyuno
```

Results:

- `Library_pyuno` built successfully.
- `Pyuno` built successfully.
- `config_host.mk` generated `PYTHON_ABI3_LIBS=$(gb_SPACE) -ldl`.
- `config_host.mk` still generated legacy `PYTHON_LIBS=$(gb_SPACE) -lpython3.12
-ldl`.
- `instdir/program/libpyuno.so` was produced.
- `readelf -d instdir/program/libpyuno.so` did not show any `libpython3.12`
  `DT_NEEDED` entry.
- This smoke load succeeded:

```sh
LD_LIBRARY_PATH=$PWD/instdir/program python3 - <<'PY'
import ctypes
ctypes.CDLL('instdir/program/libpyuno.so')
print('loaded libpyuno.so')
PY
```

Remaining upstream work:

- Run full pyuno Python tests and UNO bridge smoke tests.
- Build on Windows to validate the intended `python3.lib` / `python3.dll`
  Stable ABI path.
- Build on macOS and review platform-specific Python Stable ABI policy.

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to