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

Simplify pywin32_bootstrap, avoid importing site #1651

Merged
merged 6 commits into from Jan 21, 2021

Commits on Jan 15, 2021

  1. Simplify pywin32_bootstrap, avoid importing site

    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`. The previous
    version of `win32.lib.pywin32_boostrap` manually searched site-packages
    directories, stopping at the first one found. Now we pass this responsibility
    off to Python's import machinery. When we `import pywin32_system32`, if
    successful, `pywin32_system32` will likely be a
    `_frozen_importlib_external._NamespacePath` object. We obtain its first entry,
    being careful that it didn't support sequence indexing until Python 3.7 or so
    (and in any case, that's all undocumented; the [documentation promises] only
    that `__path__: Iterable[str]`). This first entry is the path to the directory
    containing the DLLs, just as before, so the remainder of the code is unchanged.
    
    The only semantic differences between this implementation and the previous one
    is that
    1. we no longer import `site` (always better to import less!), and
    2. `pywin32_system32` is no longer _required_ to be in a site-packages directory
    
    Just as with the previous implementation, this one does not raise an exception
    if `pywin32_system32` cannot be found.
    
    I did one annoying thing in this patch to minimize the diff, which is that I
    redefine the variable `pywin32_system32`: first it refers to a namespace package
    module object, and then later it refers to the `str` directory name. It might be
    reasonable after initial review of the patch to rename the latter instance of
    the variable to something like `path`.
    
    Finally, I added no tests and no change log entry, but can if necessary.
    
    [PEP-420]: https://www.python.org/dev/peps/pep-0420/#specification
    [documentation promises]: https://docs.python.org/3/reference/import.html#__path__
    wkschwartz committed Jan 15, 2021
    Configuration menu
    Copy the full SHA
    979b205 View commit details
    Browse the repository at this point in the history

Commits on Jan 20, 2021

  1. Configuration menu
    Copy the full SHA
    d9fb26f View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    cb51ca4 View commit details
    Browse the repository at this point in the history
  3. Simplify per review comment

    mhammond#1651 (comment)
    
    This very slightly changes semantics: if `pywin32_system32` is found to
    have multiple entries in `__path__` and the first one is not an existing
    directory, now the search will continue whereas before it would give up.
    wkschwartz committed Jan 20, 2021
    Configuration menu
    Copy the full SHA
    df3e334 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    7a6de27 View commit details
    Browse the repository at this point in the history
  5. Avoid importing os unless necessary

    Normally I wouldn't push the line on PEP 8 for importing `os`, but
    `win32.lib.pywin32_bootstrap` is imported by `site` at Python startup via
    pywin32.pth. Programs that care a lot about startup time might appreciate our
    caution here.
    wkschwartz committed Jan 20, 2021
    Configuration menu
    Copy the full SHA
    74c96dc View commit details
    Browse the repository at this point in the history