-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from diegoferigo/fix/win
Fix importing build extension for Python >= 3.8 in Windows
- Loading branch information
Showing
4 changed files
with
59 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,48 @@ | ||
import os | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
|
||
from . import build_ext_option | ||
from .build_extension import BuildExtension | ||
from .cmake_extension import CMakeExtension | ||
|
||
|
||
@contextmanager | ||
def build_extension_env(): | ||
""" | ||
Creates a context in which build extensions can be imported. | ||
It fixes a change of behaviour of Python >= 3.8 in Windows: | ||
https://docs.python.org/3/whatsnew/3.8.html#bpo-36085-whatsnew | ||
Other related resources: | ||
- https://stackoverflow.com/a/23805306 | ||
- https://www.mail-archive.com/dev@subversion.apache.org/msg40414.html | ||
Example: | ||
.. code-block:: python | ||
from cmake_build_extension import build_extension_env | ||
with build_extension_env(): | ||
from . import bindings | ||
""" | ||
|
||
cookies = [] | ||
|
||
# Windows and Python >= 3.8 | ||
if hasattr(os, "add_dll_directory"): | ||
|
||
for path in os.environ.get("PATH", "").split(os.pathsep): | ||
|
||
if path and Path(path).is_absolute() and Path(path).is_dir(): | ||
cookies.append(os.add_dll_directory(path)) | ||
|
||
try: | ||
yield | ||
|
||
finally: | ||
for cookie in cookies: | ||
cookie.close() |