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
Simplify pywin32_bootstrap, avoid importing site #1651
Changes from 1 commit
979b205
d9fb26f
cb51ca4
df3e334
7a6de27
74c96dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,22 +7,27 @@ | |
# 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__))) | ||
try: | ||
Exc = ModuleNotFoundError # Introduced in Python 3.6 | ||
except NameError: | ||
Exc = ImportError | ||
|
||
site_packages_dirs = getattr(site, "getsitepackages", lambda: [])() | ||
if level3_up_dir not in site_packages_dirs: | ||
site_packages_dirs.insert(0, level3_up_dir) | ||
try: | ||
import pywin32_system32 | ||
except Exc: | ||
path_iterator = iter([]) | ||
else: | ||
# We're guaranteed only that __path__: Iterable[str] | ||
# https://docs.python.org/3/reference/import.html#__path__ | ||
path_iterator = iter(pywin32_system32.__path__) | ||
|
||
for site_packages_dir in site_packages_dirs: | ||
pywin32_system32 = os.path.join(site_packages_dir, "pywin32_system32") | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably simplify this by putting the rest of the body under this else - ie, just have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think df3e334 is responsive to this. |
||
pywin32_system32 = next(path_iterator) | ||
except StopIteration: | ||
pass | ||
else: | ||
if os.path.isdir(pywin32_system32): | ||
if hasattr(os, "add_dll_directory"): | ||
os.add_dll_directory(pywin32_system32) | ||
|
@@ -31,4 +36,3 @@ | |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this strictly necessary? IIUC,
ModuleNotFoundError
derives fromImportError
and I don't see a good reason to differentiate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My instinct is generally to pick the most specific possible exception for
except
to match. This avoids hiding errors unrelated to the absence ofpywin32_system32
. There would be some bizarre circumstances that would lead to such hiding, but the cost of being specific here seems low to me. Anyway, I won't die on this hill. If you insist, I'll just useImportError
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel too strongly about it, and in general I'd agree, but in this case I feel the added complexity from changing behaviors based on the version, when in practice it's exactly the same behavior regardless, swings the argument to just catching ImportError.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 7a6de27