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

Fix importing build extension for Python >= 3.8 in Windows #12

Merged
merged 4 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/[email protected]/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()