Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_win32sysloader now uses the same LoadLibrary flags as Python itself … #1794

Merged
merged 1 commit into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Note that build 228 was the last version supporting Python 2.

Since build 302:
----------------
* Tweaks to how DLLs are loaded and our installation found, which should
improve virtualenv support and version mismatch issues (#1787, #1794)
Comment on lines +11 to +12
Copy link

@jiasli jiasli Dec 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great PR as it prevents loading DLLs from the current directory which is considered insecure.

Besides "improve virtualenv support and version mismatch issues", using LOAD_LIBRARY_SEARCH_DEFAULT_DIRS and LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR also causes other changes regarding how DLLs are loaded, including "DLLs in the current directory are no longer loaded". It will be better if the change log can explain how "tweaks" are made. 😉


* Binary installers for 32-bit 3.10+ are no longer released (#1805)

Since build 301:
Expand Down
24 changes: 18 additions & 6 deletions win32/Lib/pywintypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,32 @@ def __import_pywin32_system_module__(modname, globs):
# easy_install...
if os.path.isfile(os.path.join(os.path.dirname(__file__), filename)):
found = os.path.join(os.path.dirname(__file__), filename)

# There are 2 site-packages directories - one "global" and one "user".
# We could be in either, or both (but with different versions!). Factors include
# virtualenvs, post-install script being run or not, `setup.py install` flags, etc.

# In a worst-case, it means, say 'python -c "import win32api"'
# will not work but 'python -c "import pywintypes, win32api"' will,
# but it's better than nothing.
# We prefer the "user" site-packages if it exists...
if found is None:
import site

maybe = os.path.join(site.USER_SITE, "pywin32_system32", filename)
if os.path.isfile(maybe):
found = maybe

# Or the "global" site-packages.
if found is None:
# We might have been installed via PIP and without the post-install
# script having been run, so they might be in the
# lib/site-packages/pywin32_system32 directory.
# This isn't ideal as it means, say 'python -c "import win32api"'
# will not work but 'python -c "import pywintypes, win32api"' will,
# but it's better than nothing...
import sysconfig

maybe = os.path.join(
sysconfig.get_paths()["platlib"], "pywin32_system32", filename
)
if os.path.isfile(maybe):
found = maybe

if found is None:
# give up in disgust.
raise ImportError("No system module '%s' (%s)" % (modname, filename))
Expand Down
11 changes: 10 additions & 1 deletion win32/src/_win32sysloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,17 @@ static PyObject *PyLoadModule(PyObject *self, PyObject *args)
TCHAR *modName = NULL;
if (!PyArg_ParseTuple(args, fmt, &modName))
return NULL;
HINSTANCE hinst = LoadLibrary(modName);

// Python 3.7 vs 3.8 use different flags for LoadLibraryEx and we match them.
// See github issue 1787.
#if (PY_VERSION_HEX < 0x03080000)
HINSTANCE hinst = LoadLibraryEx(modName, NULL,
LOAD_WITH_ALTERED_SEARCH_PATH);
#else
HINSTANCE hinst = LoadLibraryEx(modName, NULL,
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
#endif
if (hinst == NULL) {
Py_INCREF(Py_None);
return Py_None;
Expand Down