mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Add helper to build in-tree extensions. (#2831)
For single-file extensions, a convenient pattern offered by cython
is to place the source files directly in the python source tree
(`foo/__init__.py`, `foo/ext.pyx`), deriving the package names from
their filesystem location. Adapt this pattern for pybind11, using an
`intree_extensions` helper, which should be thought of as the moral
equivalent to `cythonize`.
Differences with cythonize: I chose not to include globbing support
(`intree_extensions(glob.glob("**/*.cpp"))` seems sufficient), nor to
provide extension-customization kwargs (directly setting the attributes
on the resulting Pybind11Extension objects seems sufficient).
We could choose to have `intree_extension` (singular instead) and make
users write `[*map(intree_extension, glob.glob("**/*.cpp"))]`; no strong
opinion here.
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
This commit is contained in:
@@ -99,3 +99,45 @@ def test_simple_setup_py(monkeypatch, tmpdir, parallel, std):
|
||||
subprocess.check_call(
|
||||
[sys.executable, "test.py"], stdout=sys.stdout, stderr=sys.stderr
|
||||
)
|
||||
|
||||
|
||||
def test_intree_extensions(monkeypatch, tmpdir):
|
||||
monkeypatch.syspath_prepend(MAIN_DIR)
|
||||
|
||||
from pybind11.setup_helpers import intree_extensions
|
||||
|
||||
monkeypatch.chdir(tmpdir)
|
||||
root = tmpdir
|
||||
root.ensure_dir()
|
||||
subdir = root / "dir"
|
||||
subdir.ensure_dir()
|
||||
src = subdir / "ext.cpp"
|
||||
src.ensure()
|
||||
(ext,) = intree_extensions([src.relto(tmpdir)])
|
||||
assert ext.name == "ext"
|
||||
subdir.ensure("__init__.py")
|
||||
(ext,) = intree_extensions([src.relto(tmpdir)])
|
||||
assert ext.name == "dir.ext"
|
||||
|
||||
|
||||
def test_intree_extensions_package_dir(monkeypatch, tmpdir):
|
||||
monkeypatch.syspath_prepend(MAIN_DIR)
|
||||
|
||||
from pybind11.setup_helpers import intree_extensions
|
||||
|
||||
monkeypatch.chdir(tmpdir)
|
||||
root = tmpdir / "src"
|
||||
root.ensure_dir()
|
||||
subdir = root / "dir"
|
||||
subdir.ensure_dir()
|
||||
src = subdir / "ext.cpp"
|
||||
src.ensure()
|
||||
(ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"})
|
||||
assert ext.name == "dir.ext"
|
||||
(ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"})
|
||||
assert ext.name == "foo.dir.ext"
|
||||
subdir.ensure("__init__.py")
|
||||
(ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"})
|
||||
assert ext.name == "dir.ext"
|
||||
(ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"})
|
||||
assert ext.name == "foo.dir.ext"
|
||||
|
||||
Reference in New Issue
Block a user