Libffi and ctypes.LIBFFI_VERSION

I had a reason to find out which underlying version of libffi was used to build the _ctypes C module. Google’s AI overview hallucinated and said that with Python 3.14+ and libffi 3.5+, one could run

import ctypes
ctypes.LIBFFI_VERSION

to get the underlying version of libffi that was used. But this code throws an exception. I know on Linux (where I build the libffi version I want to use), the resulting libffi.so.8 contains the symbols ffi_get_version and ffi_get_version_number. However, on Windows where libffi is an external dependency, these symbols are not exported from libffi-8.dll.

I happened to notice if I use vcpkg to build ffi-8.dll, these symbols are exported.

My rationale for wanting those symbols available is so that one can access them to get the version of libffi used to build _ctypes. I do not know if anyone is clamoring for this. I went to file a GitHub issue for a feature request, but I was directed to discuss.python.org to solicit feedback as to whether there was community support for it.

Circling back to libffi-8.dll , I see that this is built using a Cygwin environment which redirects to the Visual Studio tool chain. I have zero clue as to whether the Python development community would want to switch to using a vcpkg built version of libffi. That adds another layer of complexity for building Python on Windows.

So, with that said, any comments about this are welcomed, even if you tell me to go pound sand.

Google’s AI overview hallucinated and said that with Python 3.14+ and libffi 3.5+, one could run

Mistake here is the CPython version. It’s 3.16+, see gh-157487: Add structured version info for ctypes by serhiy-storchaka · Pull Request #157488 · python/cpython · GitHub.

OK, I looked over the PR and see that it uses FFI_VERSION_NUMBER, which is a compile time constant. However, in the case that one has a product with an embedded Python interpreter which uses ctypes built with the system libffi and this product is run on a different system which has a system libffi that matches in shared library names (libffi.so.8, for instance) they might have a different underlying version for libffi.so.8. So, I suppose what I am after is what is done with lxml

>>> from lxml import etree
>>> etree.LIBXML_VERSION
(2, 14, 6)
>>> etree.LIBXML_COMPILED_VERSION
(2, 14, 6)
>>> etree.LIBXSLT_VERSION
(1, 1, 43)
>>> etree.LIBXSLT_COMPILED_VERSION
(1, 1, 43)

So you can see how libxst and libxml2 were used when compiling lxml.etree versus what is being used at run time. This would depend on the run time libffi library exposing ffi_get_version and ffi_get_version_number and have those called from _ctypes. Linux has these by default. On Windows, the libffi-8.dll (at least in the one distributed with Python 3.15.2) doesn’t have these symbols in its export table, so there would be no joy in accessing them from _ctypes.c (or related files). Which is why I mentioned vcpkg for building libffi on Windows, as this makes sure those symbols are exported.