New GitHub issue #110234 from FFY00:<br>

<hr>

<pre>
# Bug report

### Bug description:

This is a followup from https://github.com/python/cpython/pull/110049#discussion_r1340595301, 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 there are 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

https://github.com/python/cpython/blob/8d92b6eff3bac45e7d4871c46c4511218b9b685a/Lib/ctypes/__init__.py#L469-L474

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

>From [dlopen's manpage](https://man.archlinux.org/man/dlopen.3)

> 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? :rofl:

#### 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.

https://github.com/python/cpython/blob/8d92b6eff3bac45e7d4871c46c4511218b9b685a/Lib/test/test_site.py#L569

https://github.com/python/cpython/blob/8d92b6eff3bac45e7d4871c46c4511218b9b685a/Lib/test/test_import/__init__.py#L742

https://github.com/python/cpython/blob/8d92b6eff3bac45e7d4871c46c4511218b9b685a/Lib/test/support/__init__.py#L1552

---

### Relevant information

#### Window static library build documentation

https://github.com/python/cpython/blob/8d92b6eff3bac45e7d4871c46c4511218b9b685a/PCbuild/readme.txt#L266-L274

---

### CPython versions tested on:

CPython main branch

### Operating systems tested on:

Windows
</pre>

<hr>

<a href="https://github.com/python/cpython/issues/110234">View on GitHub</a>
<p>Labels: type-bug, OS-windows</p>
<p>Assignee: </p>