Enable Python Limited API for PL/Python on MSVC
authorPeter Eisentraut <peter@eisentraut.org>
Wed, 14 Jan 2026 09:17:36 +0000 (10:17 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Wed, 14 Jan 2026 09:43:51 +0000 (10:43 +0100)
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.

Author: Bryan Green <dbryan.green@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/ee410de1-1e0b-4770-b125-eeefd4726a24%40eisentraut.org

meson.build
src/pl/plpython/plpython.h

index 555c94796c67ed186a83ba7200d0addb2c65d8af..d8a701440dab5268dc910c31acddcacf809dc93e 100644 (file)
@@ -1336,7 +1336,21 @@ 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)
+    # On MSVC, link against python3.lib instead of python3XX.lib for
+    # Python Limited API.  Right now, this is the only platform that
+    # needs this workaround.  In the long run, Meson should handle
+    # this internally:
+    # <https://github.com/mesonbuild/meson/issues/13824>.
+    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
index d0745bb68af1ab36ea1eef6007fb4cb2b020eb92..595e4af749126c3d154ac1fa5b6f7cf63aee5e94 100644 (file)
 
 /*
  * 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