Skip to content

Commit

Permalink
Simplify pywin32_bootstrap, avoid importing site (#1651)
Browse files Browse the repository at this point in the history
Since pywin32 has dropped support for Python 2.7 since version 300 and support
for Python 3.0 through 3.4 since version 223, we can now rely on [PEP-420]
semantics when importing names of directories available on `sys.path` and not
containing an `__init__.py`. This applies to to `pywin32_system32`.
  • Loading branch information
wkschwartz committed Jan 21, 2021
1 parent fa8554d commit f3f55ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Expand Up @@ -7,6 +7,10 @@ However contributors are encouraged to add their own entries for their work.
Note that, baring some major issue building and cutting the release, build
228 will be the last version supporting Python 2.

Since build 300:
----------------
* Shifted work in win32.lib.pywin32_bootstrap to Python's import system from
manual path manipulations (@wkschwartz in #1651)

Since build 228:
----------------
Expand Down
42 changes: 18 additions & 24 deletions win32/Lib/pywin32_bootstrap.py
Expand Up @@ -6,29 +6,23 @@
# modules are imported.
# If Python has `os.add_dll_directory()`, we need to call it with this path.
# Otherwise, we add this path to PATH.
import os
import site

# The directory should be installed under site-packages.

dirname = os.path.dirname
# This is to get the "...\Lib\site-packages" directory
# out of this file name: "...\Lib\site-packages\win32\Lib\pywin32_bootstrap.py".
# It needs to be searched when installed in virtual environments.
level3_up_dir = dirname(dirname(dirname(__file__)))

site_packages_dirs = getattr(site, "getsitepackages", lambda: [])()
if level3_up_dir not in site_packages_dirs:
site_packages_dirs.insert(0, level3_up_dir)

for site_packages_dir in site_packages_dirs:
pywin32_system32 = os.path.join(site_packages_dir, "pywin32_system32")
if os.path.isdir(pywin32_system32):
if hasattr(os, "add_dll_directory"):
os.add_dll_directory(pywin32_system32)
# This is to ensure the pywin32 path is in the beginning to find the
# pywin32 DLLs first and prevent other PATH entries to shadow them
elif not os.environ["PATH"].startswith(pywin32_system32):
os.environ["PATH"] = os.environ["PATH"].replace(os.pathsep + pywin32_system32, "")
os.environ["PATH"] = pywin32_system32 + os.pathsep + os.environ["PATH"]
break
try:
import pywin32_system32
except ImportError: # Python ≥3.6: replace ImportError with ModuleNotFoundError
pass
else:
import os
# We're guaranteed only that __path__: Iterable[str]
# https://docs.python.org/3/reference/import.html#__path__
for path in pywin32_system32.__path__:
if os.path.isdir(path):
if hasattr(os, "add_dll_directory"):
os.add_dll_directory(path)
# This is to ensure the pywin32 path is in the beginning to find the
# pywin32 DLLs first and prevent other PATH entries to shadow them
elif not os.environ["PATH"].startswith(path):
os.environ["PATH"] = os.environ["PATH"].replace(os.pathsep + path, "")
os.environ["PATH"] = path + os.pathsep + os.environ["PATH"]
break

0 comments on commit f3f55ab

Please sign in to comment.