Skip to content

Commit

Permalink
VS2010 Support: Backport "Fix python#13210. Port the Windows build fr…
Browse files Browse the repository at this point in the history
…om VS2008 to VS2010."

This commit is a partial backport of python/cpython@401f9f3. It was
originally designed to work with python-cmake-buildsystem.

The following modules have NOT been backported:

* Tools/msi
* Tools/buildbot
* PCBuild
  • Loading branch information
jcfr committed Jan 22, 2020
1 parent ca079a3 commit 97fa07c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Lib/distutils/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def finalize_options(self):
# Append the source distribution include and library directories,
# this allows distutils on windows to work in the source tree
self.include_dirs.append(os.path.join(sys.exec_prefix, 'PC'))
if MSVC_VERSION == 9:
if MSVC_VERSION >= 9:
# Use the .lib files for the correct architecture
if self.plat_name == 'win32':
suffix = ''
Expand Down
11 changes: 6 additions & 5 deletions Lib/distutils/msvc9compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,11 +662,12 @@ def link(self,
if mfinfo is not None:
mffilename, mfid = mfinfo
out_arg = '-outputresource:%s;%s' % (output_filename, mfid)
try:
self.spawn(['mt.exe', '-nologo', '-manifest',
mffilename, out_arg])
except DistutilsExecError, msg:
raise LinkError(msg)
if self.__version < 10:
try:
self.spawn(['mt.exe', '-nologo', '-manifest',
temp_manifest, out_arg])
except PackagingExecError as msg:
raise LinkError(msg)
else:
log.debug("skipping %s (up-to-date)", output_filename)

Expand Down
11 changes: 10 additions & 1 deletion PC/dl_nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ char dllVersionBuffer[16] = ""; // a private buffer
HMODULE PyWin_DLLhModule = NULL;
const char *PyWin_DLLVersionString = dllVersionBuffer;

// Windows "Activation Context" work:
#if HAVE_SXS
// Windows "Activation Context" work.
// Our .pyd extension modules are generally built without a manifest (ie,
// those included with Python and those built with a default distutils.
// This requires we perform some "activation context" magic when loading our
Expand All @@ -29,6 +30,8 @@ const char *PyWin_DLLVersionString = dllVersionBuffer;
// As an added complication, this magic only works on XP or later - we simply
// use the existence (or not) of the relevant function pointers from kernel32.
// See bug 4566 (http://python.org/sf/4566) for more details.
// In Visual Studio 2010, side by side assemblies are no longer used by
// default.

typedef BOOL (WINAPI * PFN_GETCURRENTACTCTX)(HANDLE *);
typedef BOOL (WINAPI * PFN_ACTIVATEACTCTX)(HANDLE, ULONG_PTR *);
Expand Down Expand Up @@ -76,6 +79,8 @@ void _Py_DeactivateActCtx(ULONG_PTR cookie)
OutputDebugString("Python failed to de-activate the activation context\n");
}

#endif /* HAVE_SXS */

BOOL WINAPI DllMain (HANDLE hInst,
ULONG ul_reason_for_call,
LPVOID lpReserved)
Expand All @@ -87,17 +92,21 @@ BOOL WINAPI DllMain (HANDLE hInst,
// 1000 is a magic number I picked out of the air. Could do with a #define, I spose...
LoadString(hInst, 1000, dllVersionBuffer, sizeof(dllVersionBuffer));

#if HAVE_SXS
// and capture our activation context for use when loading extensions.
_LoadActCtxPointers();
if (pfnGetCurrentActCtx && pfnAddRefActCtx)
if ((*pfnGetCurrentActCtx)(&PyWin_DLLhActivationContext))
if (!(*pfnAddRefActCtx)(PyWin_DLLhActivationContext))
OutputDebugString("Python failed to load the default activation context\n");
#endif
break;

case DLL_PROCESS_DETACH:
#if HAVE_SXS
if (pfnReleaseActCtx)
(*pfnReleaseActCtx)(PyWin_DLLhActivationContext);
#endif
break;
}
return TRUE;
Expand Down
15 changes: 14 additions & 1 deletion PC/msvcrtmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifdef _MSC_VER
#if _MSC_VER >= 1500 && _MSC_VER < 1600
#include <crtassem.h>
#elif _MSC_VER >= 1600
#include <crtversion.h>
#endif
#endif

Expand Down Expand Up @@ -398,7 +400,7 @@ PyMODINIT_FUNC
initmsvcrt(void)
{
int st;
PyObject *d;
PyObject *d, *version;
PyObject *m = Py_InitModule("msvcrt", msvcrt_functions);
if (m == NULL)
return;
Expand All @@ -412,6 +414,7 @@ initmsvcrt(void)
insertint(d, "LK_UNLCK", _LK_UNLCK);

/* constants for the crt versions */
(void)st;
#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
_VC_ASSEMBLY_PUBLICKEYTOKEN);
Expand All @@ -427,4 +430,14 @@ initmsvcrt(void)
__LIBRARIES_ASSEMBLY_NAME_PREFIX);
if (st < 0)return;
#endif

/* constants for the 2010 crt versions */
#if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
version = PyUnicode_FromFormat("%d.%d.%d.%d", _VC_CRT_MAJOR_VERSION,
_VC_CRT_MINOR_VERSION,
_VC_CRT_BUILD_VERSION,
_VC_CRT_RBUILD_VERSION);
st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version);
if (st < 0) return NULL;
#endif
}
9 changes: 7 additions & 2 deletions PC/pyconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,19 @@ typedef int pid_t;
#define hypot _hypot
#endif

#endif /* _MSC_VER */
/* Side by Side assemblies supported in VS 2005 and VS 2008 but not 2010*/
#if _MSC_VER >= 1400 && _MSC_VER < 1600
#define HAVE_SXS 1
#endif

/* define some ANSI types that are not defined in earlier Win headers */
#if defined(_MSC_VER) && _MSC_VER >= 1200
#if _MSC_VER >= 1200
/* This file only exists in VC 6.0 or higher */
#include <basetsd.h>
#endif

#endif /* _MSC_VER */

/* ------------------------------------------------------------------------*/
/* The Borland compiler defines __BORLANDC__ */
/* XXX These defines are likely incomplete, but should be easy to fix. */
Expand Down
8 changes: 8 additions & 0 deletions Python/dynload_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
#include <windows.h>

// "activation context" magic - see dl_nt.c...
#if HAVE_SXS
extern ULONG_PTR _Py_ActivateActCtx();
void _Py_DeactivateActCtx(ULONG_PTR cookie);
#endif

const struct filedescr _PyImport_DynLoadFiletab[] = {
#ifdef _DEBUG
Expand Down Expand Up @@ -176,7 +178,9 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
char pathbuf[260];
LPTSTR dummy;
unsigned int old_mode;
#if HAVE_SXS
ULONG_PTR cookie = 0;
#endif
/* We use LoadLibraryEx so Windows looks for dependent DLLs
in directory of pathname first. However, Windows95
can sometimes not work correctly unless the absolute
Expand All @@ -190,11 +194,15 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
sizeof(pathbuf),
pathbuf,
&dummy)) {
#if HAVE_SXS
ULONG_PTR cookie = _Py_ActivateActCtx();
#endif
/* XXX This call doesn't exist in Windows CE */
hDLL = LoadLibraryEx(pathname, NULL,
LOAD_WITH_ALTERED_SEARCH_PATH);
#if HAVE_SXS
_Py_DeactivateActCtx(cookie);
#endif
}

/* restore old error mode settings */
Expand Down

0 comments on commit 97fa07c

Please sign in to comment.