Skip to content

Commit

Permalink
Port GetPythonImport() to mingw
Browse files Browse the repository at this point in the history
This looks for DLL names in the import table but while with MSVC the DLL
is named python311.dll in our case it is named libpython3.11.dll.
Adjust the strings and lengths accordingly.
  • Loading branch information
lazka authored and naveen521kk committed Nov 2, 2024
1 parent 721a85f commit e74ab76
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions Python/dynload_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ const char *_PyImport_DynLoadFiletab[] = {
#define DWORD_AT(mem) (*(DWORD *)(mem))
#define WORD_AT(mem) (*(WORD *)(mem))

#ifdef __MINGW32__
#define DLL_PREFIX "libpython"
#define DLL_PREFIX_LEN 9
#define DLL_NAME_FORMAT "libpython%d.%d"
#else
#define DLL_PREFIX "python"
#define DLL_PREFIX_LEN 6
#define DLL_NAME_FORMAT "python%d%d"
#endif

static char *GetPythonImport (HINSTANCE hModule)
{
unsigned char *dllbase, *import_data, *import_name;
Expand Down Expand Up @@ -122,23 +132,23 @@ static char *GetPythonImport (HINSTANCE hModule)
import_off);
while (DWORD_AT(import_data)) {
import_name = dllbase + DWORD_AT(import_data+12);
if (strlen(import_name) >= 6 &&
!strncmp(import_name,"python",6)) {
if (strlen(import_name) >= DLL_PREFIX_LEN &&
!strncmp(import_name, DLL_PREFIX, DLL_PREFIX_LEN)) {
char *pch;

/* Don't claim that python3.dll is a Python DLL. */
#ifdef _DEBUG
if (strcmp(import_name, "python3_d.dll") == 0) {
if (strcmp(import_name, DLL_PREFIX "3_d.dll") == 0) {
#else
if (strcmp(import_name, "python3.dll") == 0) {
if (strcmp(import_name, DLL_PREFIX "3.dll") == 0) {
#endif
import_data += 20;
continue;
}

/* Ensure python prefix is followed only
by numbers to the end of the basename */
pch = import_name + 6;
pch = import_name + DLL_PREFIX_LEN;
#ifdef _DEBUG
while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') {
#else
Expand Down Expand Up @@ -329,9 +339,9 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,

PyOS_snprintf(buffer, sizeof(buffer),
#ifdef _DEBUG
"python%d%d_d.dll",
DLL_NAME_FORMAT "_d.dll",
#else
"python%d%d.dll",
DLL_NAME_FORMAT ".dll",
#endif
PY_MAJOR_VERSION,PY_MINOR_VERSION);
import_python = GetPythonImport(hDLL);
Expand Down

0 comments on commit e74ab76

Please sign in to comment.