Skip to content

Commit

Permalink
Merge pull request #12 from diegoferigo/fix/win
Browse files Browse the repository at this point in the history
Fix importing build extension for Python >= 3.8 in Windows
  • Loading branch information
diegoferigo authored May 27, 2021
2 parents d35b520 + f20afff commit cefa43e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ jobs:
- ubuntu-20.04
- macos-latest
- windows-latest
include:
# https://github.com/diegoferigo/cmake-build-extension/issues/8
- os: windows-latest
python-version: 3.8
experimental: true
- os: windows-latest
python-version: 3.9
experimental: true

steps:

Expand Down
4 changes: 3 additions & 1 deletion examples/swig/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ packages = find:
package_dir =
=src
python_requires = >=3.6
install_requires = numpy
install_requires =
numpy
cmake-build-extension

[options.packages.find]
where = src
Expand Down
11 changes: 11 additions & 0 deletions examples/swig/setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import sys
from pathlib import Path

Expand All @@ -7,11 +8,21 @@
with open(Path(__file__).parent.absolute() / "README.md", encoding="utf-8") as f:
long_description = f.read()

init_py = inspect.cleandoc(
"""
from cmake_build_extension import build_extension_env
with build_extension_env():
from . import bindings
"""
)

setup(
ext_modules=[
CMakeExtension(
name="mymath",
install_prefix="mymath",
write_top_level_init=init_py,
source_dir=str(Path(__file__).parent.absolute()),
cmake_configure_options=[
f"-DPython3_ROOT_DIR={Path(sys.prefix)}",
Expand Down
45 changes: 45 additions & 0 deletions src/cmake_build_extension/__init__.py
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()

0 comments on commit cefa43e

Please sign in to comment.