Thread: Re: Use Python "Limited API" in PL/Python

Re: Use Python "Limited API" in PL/Python

From
Peter Eisentraut
Date:
On 02.12.24 09:51, Peter Eisentraut wrote:
> This patch changes PL/Python to use the Python "limited API". This API 
> has stronger ABI stability guarantees.[0] This means, you can build PL/ 
> Python against any Python 3.x version and use any other Python 3.x 
> version at run time.
> 
> This is especially useful for binary packages where the operating system 
> does not come with a fixed suitable version of Python. For example, 
> Postgres.app (for macOS) would prefer to link against the Python version 
> supplied by python.org (Python.app). But that has a 3.x version that 
> changes over time. So instead they bundle a Python version inside 
> Postgres.app. The Windows installer used to also bundle Python but as of 
> PG17 you have to get it yourself, but you have to get a very specific 
> version [1], which is unsatisfactory. This patch fixes that: You can use 
> any Python version independent of what PL/Python was built against. 
> (There is a mechanism to say "at least 3.N", but for this patch, we 
> don't need that, we can stick with the current minimum of 3.2.)
> 
> (I have only tested the macOS side of this, not the Windows side. In 
> fact, the patch currently doesn't build on Windows on CI. I haven't 
> figured out why.)

A bit more exploration of that Windows build failure:

This patch changes it so that, on Windows, plpython is linked against a 
library called "python3.lib" instead of previously "python311.lib" or 
similar.  The build failure is [0]

     LINK : fatal error LNK1104: cannot open file 'python3.lib'

[0]: https://cirrus-ci.com/task/5738515637469184

which suggests that the file isn't there.  But we have tested this 
internally and the build succeeded locally.  I also don't see anything 
in the Python installer code or documentation that suggests that there 
is an option not to install that file or something like that.  It should 
be installed right next to the python3XX.lib file.

How could one debug this?  Is there a way to log into the CI image or 
get a directory dump or something like that?




Re: Use Python "Limited API" in PL/Python

From
Peter Eisentraut
Date:
On 15.01.25 23:20, Peter Eisentraut wrote:
> On 15.01.25 12:28, Jakob Egger wrote:
>> On 14.01.2025, at 16:51, Jakob Egger <jakob@eggerapps.at> 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.
> 
> Thanks for checking.  I've also been poking at this, without success.
> 
> What we could do is just not activate the limited API on Windows 
> (actually MSVC, since it works on MinGW).  If someone later finds a way 
> to make it work on Windows/MSVC, then it's easy to change.
> 
> So here is an updated patch set with that change.  It should pass cfbot 
> like this.

Update for the hackers list: This patch set was briefly committed but 
had to be reverted because it crashed on some older Python versions; see 
[0].

I have locally reproduced the problem with Python 3.6 and 3.7; Python 
3.8 is ok.  This matches the results from the buildfarm.

I have poked at this a bit more but haven't gotten a good idea where or 
how to fix it so far.  Help welcome.


[0]: 
https://www.postgresql.org/message-id/flat/E1tnJNu-000CZW-0X%40gemulon.postgresql.org