Note that the OSX port already does this for framework builds. I don't know why we don't use the same code for shared library builds.
Issue #15498 contains a patch that switches this code from a deprecated nextstep-era API to dladdr.
Two comments on the patch attached to this issue:
1) The name "_PyImport_GetModulePath" is confusing, I'd use _PyImport_GetSharedLibPath to make clear that this is locating the shared library.
2) The code calls dladdr on a static variable that's introduced just for that, it is also possible to call dladdr on an already existing symbol (for example the address of a function in the public API). |