Skip to content

Commit 7d0e0c9

Browse files
committed
Closes #23253: Delay-load ShellExecute
1 parent b95b561 commit 7d0e0c9

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

Doc/library/os.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,6 +3024,10 @@ written in Python, such as a mail server's external command delivery program.
30243024
doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
30253025
the path is properly encoded for Win32.
30263026

3027+
To reduce interpreter startup overhead, the Win32 :c:func:`ShellExecute`
3028+
function is not resolved until this function is first called. If the function
3029+
cannot be resolved, :exc:`NotImplementedError` will be raised.
3030+
30273031
Availability: Windows.
30283032

30293033

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #23253: Delay-load ShellExecute[AW] in os.startfile for reduced
14+
startup overhead on Windows.
15+
1316
- Issue #22038: pyatomic.h now uses stdatomic.h or GCC built-in functions for
1417
atomic memory access if available. Patch written by Vitor de Lima and Gustavo
1518
Temple.

Modules/posixmodule.c

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15128,6 +15128,37 @@ The filepath is relative to the current directory. If you want to use\n\
1512815128
an absolute path, make sure the first character is not a slash (\"/\");\n\
1512915129
the underlying Win32 ShellExecute function doesn't work if it is.");
1513015130

15131+
/* Grab ShellExecute dynamically from shell32 */
15132+
static int has_ShellExecute = -1;
15133+
static HINSTANCE (CALLBACK *Py_ShellExecuteA)(HWND, LPCSTR, LPCSTR, LPCSTR,
15134+
LPCSTR, INT);
15135+
static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
15136+
LPCWSTR, INT);
15137+
static int
15138+
check_ShellExecute()
15139+
{
15140+
HINSTANCE hShell32;
15141+
15142+
/* only recheck */
15143+
if (-1 == has_ShellExecute) {
15144+
Py_BEGIN_ALLOW_THREADS
15145+
hShell32 = LoadLibraryW(L"SHELL32");
15146+
Py_END_ALLOW_THREADS
15147+
if (hShell32) {
15148+
*(FARPROC*)&Py_ShellExecuteA = GetProcAddress(hShell32,
15149+
"ShellExecuteA");
15150+
*(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
15151+
"ShellExecuteW");
15152+
has_ShellExecute = Py_ShellExecuteA &&
15153+
Py_ShellExecuteW;
15154+
} else {
15155+
has_ShellExecute = 0;
15156+
}
15157+
}
15158+
return has_ShellExecute;
15159+
}
15160+
15161+
1513115162
static PyObject *
1513215163
win32_startfile(PyObject *self, PyObject *args)
1513315164
{
@@ -15138,6 +15169,14 @@ win32_startfile(PyObject *self, PyObject *args)
1513815169
HINSTANCE rc;
1513915170

1514015171
PyObject *unipath, *uoperation = NULL;
15172+
15173+
if(!check_ShellExecute()) {
15174+
/* If the OS doesn't have ShellExecute, return a
15175+
NotImplementedError. */
15176+
return PyErr_Format(PyExc_NotImplementedError,
15177+
"startfile not available on this platform");
15178+
}
15179+
1514115180
if (!PyArg_ParseTuple(args, "U|s:startfile",
1514215181
&unipath, &operation)) {
1514315182
PyErr_Clear();
@@ -15166,8 +15205,8 @@ win32_startfile(PyObject *self, PyObject *args)
1516615205
woperation = NULL;
1516715206

1516815207
Py_BEGIN_ALLOW_THREADS
15169-
rc = ShellExecuteW((HWND)0, woperation, wpath,
15170-
NULL, NULL, SW_SHOWNORMAL);
15208+
rc = Py_ShellExecuteW((HWND)0, woperation, wpath,
15209+
NULL, NULL, SW_SHOWNORMAL);
1517115210
Py_END_ALLOW_THREADS
1517215211

1517315212
Py_XDECREF(uoperation);
@@ -15189,8 +15228,8 @@ win32_startfile(PyObject *self, PyObject *args)
1518915228
}
1519015229
filepath = PyBytes_AsString(ofilepath);
1519115230
Py_BEGIN_ALLOW_THREADS
15192-
rc = ShellExecute((HWND)0, operation, filepath,
15193-
NULL, NULL, SW_SHOWNORMAL);
15231+
rc = Py_ShellExecuteA((HWND)0, operation, filepath,
15232+
NULL, NULL, SW_SHOWNORMAL);
1519415233
Py_END_ALLOW_THREADS
1519515234
if (rc <= (HINSTANCE)32) {
1519615235
PyObject *errval = win32_error("startfile", filepath);

0 commit comments

Comments
 (0)