On 1/15/2025 5:28 AM, Jakob Egger wrote:
> On 14.01.2025, at 16:51, Jakob Egger <[email protected]> wrote:
>> I've tried to create a patch with this change. I'm attaching it to this 
>> message so that cfbot picks it up. (I was unable to reproduce the issue 
>> locally)
> 
> Apologies, please disregard my last patch. It does not work.
> 
> It looks like meson just can't build libraries that link with Python that use 
> the limited API.
> 
> My understanding of Meson is limited, so I am unsure how to best work around 
> this limitation.
> 
> I've spent two days trying to figure it out; I'm at the end of my wits.
> 
> Best regards,
> Jakob
Hi Jakob and Peter,

I've been working on enabling the Python Limited API on MSVC, which was
disabled in the recent commit due to build failures.

The issue is that Meson doesn't automatically link against python3.lib
when using the Limited API. Meson's Python dependency always returns the
version-specific library (python3XX.lib) regardless of whether
limited api is defined. This appears to be a known Meson limitation:
https://github.com/mesonbuild/meson/issues/13824

Initially, I thought that maybe the library wasn't being installed, but
that is not the case and the patch to our CI Windows MSVC image is not
needed.

I have a patch that works around this by manually constructing the
Python dependency on MSVC to use python3.lib instead of python3XX.lib,
using cc.find_library('python3') and building the dependency with the
appropriate include directory.  It passes CI when I push.  I'll see what
cfbot thinks.

Still testing, but wanted to share progress and get feedback on the
approach.

-- 
Bryan Green
EDB: https://www.enterprisedb.com
From 52efe8af1377e8adbac3533537f15ad20f7af1cf Mon Sep 17 00:00:00 2001
From: Bryan Green <[email protected]>
Date: Fri, 2 Jan 2026 10:48:54 -0600
Subject: [PATCH v1] Enable Python Limited API for PL/Python on MSVC

Previously, the Python Limited API was disabled on MSVC due to build
failures caused by Meson not knowing to link against python3.lib
instead of python3XX.lib when using the Limited API.

This commit works around the Meson limitation by explicitly finding
and linking against python3.lib on MSVC, and removes the preprocessor
guard that was disabling the Limited API on MSVC in plpython.h.

This requires python3.lib to be present in the Python installation,
which is included when Python is installed.

Discussion: 
https://www.postgresql.org/message-id/flat/ee410de1-1e0b-4770-b125-eeefd4726a24%40eisentraut.org
---
 meson.build                | 12 +++++++++++-
 src/pl/plpython/plpython.h |  4 ----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/meson.build b/meson.build
index d7c5193d4c..e4ecd78a7a 100644
--- a/meson.build
+++ b/meson.build
@@ -1329,7 +1329,17 @@ if not pyopt.disabled()
   pm = import('python')
   python3_inst = pm.find_installation(python.full_path(), required: pyopt)
   if python3_inst.found()
-    python3_dep = python3_inst.dependency(embed: true, required: pyopt)
+    # For Limited API on MSVC, link against python3.lib instead of 
python3XX.lib
+    if host_system == 'windows' and cc.get_id() == 'msvc'
+      python3_libdir = python3_inst.get_variable('prefix') / 'libs'
+      python3_lib = cc.find_library('python3', dirs: python3_libdir, required: 
pyopt)
+      python3_dep = declare_dependency(
+        include_directories: 
include_directories(python3_inst.get_variable('prefix') / 'include'),
+        dependencies: python3_lib,
+      )
+    else
+      python3_dep = python3_inst.dependency(embed: true, required: pyopt)
+    endif
     # Remove this check after we depend on Meson >= 1.1.0
     if not cc.check_header('Python.h', dependencies: python3_dep, required: 
pyopt, include_directories: postgres_inc)
       python3_dep = not_found_dep
diff --git a/src/pl/plpython/plpython.h b/src/pl/plpython/plpython.h
index 118b310084..445f7bc31a 100644
--- a/src/pl/plpython/plpython.h
+++ b/src/pl/plpython/plpython.h
@@ -25,12 +25,8 @@
 
 /*
  * Enable Python Limited API
- *
- * XXX currently not enabled on MSVC because of build failures
  */
-#if !defined(_MSC_VER)
 #define Py_LIMITED_API 0x03020000
-#endif
 
 /*
  * Pull in Python headers via a wrapper header, to control the scope of
-- 
2.52.0.windows.1

Reply via email to