Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 271f977

Browse files
committed
Seem to be some changes related to DLL version from string resource,
again (Mark Hammond is the cause of all this).
1 parent ec68092 commit 271f977

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

PC/getpathp.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ search_for_prefix(argv0_path, landmark)
155155
}
156156

157157
#ifdef MS_WIN32
158+
#include "malloc.h" // for alloca - see comments below!
159+
extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup.
160+
158161

159162
/* Load a PYTHONPATH value from the registry.
160163
Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
@@ -172,9 +175,23 @@ getpythonregpath(HKEY keyBase, BOOL bWin32s)
172175
LONG rc;
173176
char *retval = NULL;
174177
char *dataBuf;
178+
const char keyPrefix[] = "Software\\Python\\PythonCore\\";
179+
const char keySuffix[] = "\\PythonPath";
180+
int versionLen;
181+
char *keyBuf;
182+
183+
// Tried to use sysget("winver") but here is too early :-(
184+
versionLen = strlen(PyWin_DLLVersionString);
185+
// alloca == no free required, but memory only local to fn.
186+
// also no heap fragmentation! Am I being silly?
187+
keyBuf = alloca(sizeof(keyPrefix)-1 + versionLen + sizeof(keySuffix)); // chars only, plus 1 NULL.
188+
// lots of constants here for the compiler to optimize away :-)
189+
memcpy(keyBuf, keyPrefix, sizeof(keyPrefix)-1);
190+
memcpy(keyBuf+sizeof(keyPrefix)-1, PyWin_DLLVersionString, versionLen);
191+
memcpy(keyBuf+sizeof(keyPrefix)-1+versionLen, keySuffix, sizeof(keySuffix)); // NULL comes with this one!
192+
175193
rc=RegOpenKey(keyBase,
176-
"Software\\Python\\PythonCore\\"
177-
MS_DLL_ID "\\PythonPath",
194+
keyBuf,
178195
&newKey);
179196
if (rc==ERROR_SUCCESS) {
180197
RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,

PC/import_nt.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "osdefs.h"
1111
#include <windows.h>
1212
#include "importdl.h"
13+
#include "malloc.h" // for alloca
14+
15+
extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup.
1316

1417
/* Return whether this is Win32s, i.e., Win32 API on Win 3.1(1).
1518
This function is exported! */
@@ -29,13 +32,32 @@ BOOL PyWin_IsWin32s()
2932

3033
FILE *PyWin_FindRegisteredModule( const char *moduleName, struct filedescr **ppFileDesc, char *pathBuf, int pathLen)
3134
{
32-
char moduleKey[128];
35+
char *moduleKey;
36+
char *pos;
37+
const char keyPrefix[] = "Software\\Python\\PythonCore\\";
38+
const char keySuffix[] = "\\Modules\\";
3339
struct filedescr *fdp = NULL;
3440
FILE *fp;
3541
int modNameSize = pathLen;
42+
int versionLen, moduleLen;
3643
HKEY keyBase = PyWin_IsWin32s() ? HKEY_CLASSES_ROOT : HKEY_LOCAL_MACHINE;
37-
strcpy(moduleKey, "Software\\Python\\PythonCore\\" MS_DLL_ID "\\Modules\\");
38-
strcat(moduleKey, moduleName);
44+
45+
// conceptually, this code is setting up:
46+
// sprintf(buf, "Software\\Python\\PythonCore\\%s\\Modules\\%s", PyWin_DLLVersionString, moduleName);
47+
// the sprintf would be clearer, but slower and less "length-safe"
48+
versionLen = strlen(PyWin_DLLVersionString);
49+
moduleLen = strlen(moduleName);
50+
// alloca == no free required, but memory only local to fn, also no heap fragmentation!
51+
moduleKey = alloca(sizeof(keyPrefix)-1 + versionLen + sizeof(keySuffix) + moduleLen); // chars only, plus 1 NULL.
52+
pos = moduleKey;
53+
memcpy(pos, keyPrefix, sizeof(keyPrefix)-1);
54+
pos += sizeof(keyPrefix)-1;
55+
memcpy(pos, PyWin_DLLVersionString, versionLen);
56+
pos +=versionLen;
57+
memcpy(pos, keySuffix, sizeof(keySuffix));
58+
pos += sizeof(keySuffix)-1;
59+
memcpy(pos, moduleName, moduleLen+1); // NULL comes with this one!
60+
3961
if (RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize)!=ERROR_SUCCESS)
4062
return NULL;
4163
// use the file extension to locate the type entry.

0 commit comments

Comments
 (0)