NT specific files supplied by Mark Hammond

This commit is contained in:
Guido van Rossum 1996-08-21 15:03:37 +00:00
parent 019f424a0a
commit 6dbd190f5e
4 changed files with 293 additions and 0 deletions

34
PC/dl_nt.c Normal file
View file

@ -0,0 +1,34 @@
/*
Entry point for the Windows NT DLL.
About the only reason for having this, is so initall() can automatically
be called, removing that burden (and possible source of frustration if
forgotten) from the programmer.
*/
#include "windows.h"
/* NT and Python share these */
#undef INCREF
#undef DECREF
#include "config.h"
#include "allobjects.h"
HMODULE PyWin_DLLhModule = NULL;
BOOL WINAPI DllMain (HANDLE hInst,
ULONG ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
PyWin_DLLhModule = hInst;
initall();
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

178
PC/getpath_nt.c Normal file
View file

@ -0,0 +1,178 @@
#include "Python.h"
#include "osdefs.h"
#include <windows.h>
#ifndef WIN32_PATCH_LEVEL
#define WIN32_PATCH_LEVEL "000"
#endif
/* PREFIX and EXEC_PREFIX are meaningless on Windows */
#ifndef PREFIX
#define PREFIX ""
#endif
#ifndef EXEC_PREFIX
#define EXEC_PREFIX ""
#endif
/*
This is a special Win32 version of getpath.
* There is no default path. There is nothing even remotely resembling
a standard location. Maybe later "Program Files/Python", but not yet.
* The Registry is used as the primary store for the Python path.
* The environment variable PYTHONPATH _overrides_ the registry. This should
allow a "standard" Python environment, but allow you to manually setup
another (eg, a beta version).
*/
BOOL PyWin_IsWin32s()
{
static BOOL bIsWin32s = -1; // flag as "not yet looked"
if (bIsWin32s==-1) {
OSVERSIONINFO ver;
ver.dwOSVersionInfoSize = sizeof(ver);
GetVersionEx(&ver);
bIsWin32s = ver.dwPlatformId == VER_PLATFORM_WIN32s;
}
return bIsWin32s;
}
/* Load a PYTHONPATH value from the registry
Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER
Returns NULL, or a pointer that should be free'd.
*/
static char *
getpythonregpath(HKEY keyBase, BOOL bWin32s)
{
HKEY newKey = 0;
DWORD nameSize = 0;
DWORD dataSize = 0;
DWORD numEntries = 0;
LONG rc;
char *retval = NULL;
char *dataBuf;
if ((rc=RegOpenKey(keyBase, "Software\\Python\\PythonCore\\" WIN32_PATCH_LEVEL "\\PythonPath",
&newKey))==ERROR_SUCCESS) {
RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
&numEntries, &nameSize, &dataSize, NULL, NULL );
}
if (numEntries==0) {
if (newKey)
CloseHandle(newKey);
if ((rc=RegOpenKey(keyBase, "Software\\Python\\PythonPath",
&newKey))==ERROR_SUCCESS) {
RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
&numEntries, &nameSize, &dataSize, NULL, NULL );
}
}
if (bWin32s && numEntries==0 && dataSize==0) { /* must hardcode for Win32s */
numEntries = 1;
dataSize = 511;
}
if (numEntries) {
dataBuf = malloc(dataSize);
// on NT, datasize is unicode - ie, 2xstrlen,
// even when ascii string returned.
// presumably will be 1xstrlen on 95/win3.1
// Additionally, win32s doesnt work as expected, so
// the specific strlen() is required for 3.1.
rc = RegQueryValue(newKey, "", dataBuf, &dataSize);
if (rc==ERROR_SUCCESS) {
if (strlen(dataBuf)==0)
free(dataBuf);
else
retval = dataBuf; // caller will free
}
else
free(dataBuf);
}
if (newKey)
CloseHandle(newKey);
return retval;
}
/* Return the initial python search path. This is called once from
initsys() to initialize sys.path. The environment variable
PYTHONPATH is fetched and the default path appended. The default
path may be passed to the preprocessor; if not, a system-dependent
default is used. */
char *
Py_GetPath()
{
char *path = getenv("PYTHONPATH");
char *defpath = PYTHONPATH;
static char *buf = NULL;
char *p;
int n;
if (buf != NULL) {
free(buf);
buf = NULL;
}
if (path == NULL) {
char *machinePath, *userPath;
int machineLen, userLen;
/* lookup the registry */
BOOL bWin32s = PyWin_IsWin32s();
if (bWin32s) { /* are we running under Windows 3.1 Win32s */
/* only CLASSES_ROOT is supported */
machinePath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
userPath = NULL;
} else {
machinePath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
userPath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
}
if (machinePath==NULL && userPath==NULL) return defpath;
machineLen = machinePath ? strlen(machinePath) : 0;
userLen = userPath ? strlen(userPath) : 0;
n = machineLen + userLen + 1;
// this is a memory leak, as Python never frees it. Only ever called once, so big deal!
buf = malloc(n);
if (buf == NULL)
Py_FatalError("not enough memory to copy module search path");
p = buf;
*p = '\0';
if (machineLen) {
strcpy(p, machinePath);
p += machineLen;
}
if (userLen) {
if (machineLen)
*p++ = DELIM;
strcpy(p, userPath);
}
if (userPath) free(userPath);
if (machinePath) free(machinePath);
} else {
buf = malloc(strlen(path)+1);
if (buf == NULL)
Py_FatalError("not enough memory to copy module search path");
strcpy(buf, path);
}
return buf;
}
/* Similar for Makefile variables $prefix and $exec_prefix */
char *
Py_GetPrefix()
{
return PREFIX;
}
char *
Py_GetExecPrefix()
{
return EXEC_PREFIX;
}

44
PC/import_nt.c Normal file
View file

@ -0,0 +1,44 @@
/********************************************************************
importnt.c
Win32 specific import code.
*/
#include "allobjects.h"
#include "osdefs.h"
#include <windows.h>
#include "import.h"
#include "importdl.h"
#ifndef WIN32_PATCH_LEVEL
#define WIN32_PATCH_LEVEL "000"
#endif
extern BOOL PyWin_IsWin32s();
FILE *PyWin_FindRegisteredModule( const char *moduleName, struct filedescr **ppFileDesc, char *pathBuf, int pathLen)
{
char moduleKey[128];
struct filedescr *fdp = NULL;
FILE *fp;
int modNameSize = pathLen;
HKEY keyBase = PyWin_IsWin32s() ? HKEY_CLASSES_ROOT : HKEY_LOCAL_MACHINE;
strcpy(moduleKey, "Software\\Python\\PythonCore\\" WIN32_PATCH_LEVEL "\\Modules\\");
strcat(moduleKey, moduleName);
if (RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize)!=ERROR_SUCCESS)
return NULL;
// use the file extension to locate the type entry.
for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
int extLen=strlen(fdp->suffix);
if (modNameSize>extLen && strnicmp(pathBuf+(modNameSize-extLen-1),fdp->suffix,extLen)==0)
break;
}
if (fdp->suffix==NULL)
return NULL;
fp = fopen(pathBuf, fdp->mode);
if (fp != NULL)
*ppFileDesc = fdp;
return fp;
}

37
PC/main_nt.c Normal file
View file

@ -0,0 +1,37 @@
/* -*- C -*- ***********************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
/* Python interpreter main program */
#define HAVE_CONFIG_H
#include "Python.h"
#undef main
int
main(argc, argv)
int argc;
char **argv;
{
return Py_Main(argc, argv);
}