Skip to content

Are static library builds on Windows still supported? #110234

@FFY00

Description

@FFY00

Bug report

Bug description:

This is a followup from #110049 (comment), where @colesbury pointed out that we are assuming in a couple places that Windows builds are using a shared library.

I think this might steam from the "availability" section of the sys.dllhandle documentation only stating "Windows".

If static library builds on Windows are still supported, we should document that sys.dllhandle will only be available on shared library Windows builds.

This likely hasn't come up until now because we don't test this in the CI. AFAICT this is a "best-effort" option, where we are not really paying much attention to it, but will try to fix bugs if reported. It'd probably make sense to document that somewhere.

But with this going unnoticed for a while, it also raises the question, do we even want to support this use-case? The maintenance is minimal, so IMO it should be fine, but we definitely need to fix the sys.dllhandle documentation.


Possibly problematic code

ctypes

if _os.name == "nt":
pythonapi = PyDLL("python dll", None, _sys.dllhandle)
elif _sys.platform == "cygwin":
pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
else:
pythonapi = PyDLL(None)

On POSIX, no object name or handle is provided, because we want to get the handle for the running executable.

From dlopen's manpage

void *dlopen(const char *filename, int flags);

If filename is NULL, then the returned handle is for the main program.

On Windows, we load the dynamic library instead. I am a bit out of my depth here, but AFAICT we do this because the dynamic library symbols are not present on the current process. Again, not sure about this, but AFAICT that should not be the case when using a static libpython, so, if I understand everything correctly, we would want to take the same approach as POSIX in this situation, and from what I can tell, that can be done with GetModuleHandle(NULL).

@zooba, could check if my analysis of the issue on Windows is correct? 🤣

Tests

Failures from this wouldn't show up on CI, but should when running the tests on static library Windows builds. This is something we should probably fix, but is low priority.

dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)

dllname = GetModuleFileName(sys.dllhandle)

dll = _winapi.GetModuleFileName(sys.dllhandle)


Relevant information

Window static library build documentation

cpython/PCbuild/readme.txt

Lines 266 to 274 in 8d92b6e

Static library
--------------
The solution has no configuration for static libraries. However it is
easy to build a static library instead of a DLL. You simply have to set
the "Configuration Type" to "Static Library (.lib)" and alter the
preprocessor macro "Py_ENABLE_SHARED" to "Py_NO_ENABLE_SHARED". You may
also have to change the "Runtime Library" from "Multi-threaded DLL
(/MD)" to "Multi-threaded (/MT)".


CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowstype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions