Skip to content

Commit

Permalink
Bump to pybind11>=2.5 to simplify build.
Browse files Browse the repository at this point in the history
  • Loading branch information
anntzer committed Apr 3, 2020
1 parent 82ed1ef commit de98f71
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 52 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
next
====

- Dropped support for Py3.5.
- Bumped dependencies to Python≥3.6, pybind11≥2.5.
- ``pybind11`` is now a ``setup_requires`` and ``-march=native`` is no longer
passed as compilation option by default; drop support for
``MPLCAIRO_BUILD_TYPE``.
Expand Down
8 changes: 3 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mplcairo requires

Additionally, building mplcairo from source requires

- pybind11≥2.2.4 [#]_ (declared as ``setup_requires``),
- pybind11≥2.5.0 [#]_ (declared as ``setup_requires``),
- on Linux and macOS, pycairo≥1.16.0 (declared as ``setup_requires`` on macOS,
but not on Linux).

Expand All @@ -83,8 +83,6 @@ OpenType font features. Refer to the instructions on that project's
website for installation on Linux and macOS. You may want to look at
https://github.com/HOST-Oman/libraqm-cmake for Windows build scripts.

.. _pybind11-1362: https://github.com/pybind/pybind11/issues/1362

.. [#] pycairo 1.16.0 added ``get_include()``.
We do not actually rely on pycairo's Python bindings. Rather, specifying a
Expand Down Expand Up @@ -114,8 +112,8 @@ https://github.com/HOST-Oman/libraqm-cmake for Windows build scripts.
of cairo, it is suggested to use a commit ≥dfe3aa6, as the latter further
fixes another bug that can cause crashes in mplcairo.
.. [#] The version requirement comes from pybind11 issue `#1362
<pybind11-1362_>`_.
.. [#] pybind11 2.5.0 is the earliest version that supports being added as
``setup_requires`` (and read-only buffers).
On Fedora, the package is available as `python-mplcairo <fedora-package_>`_.

Expand Down
43 changes: 3 additions & 40 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""

from distutils.version import LooseVersion
from enum import Enum
import functools
import json
import os
Expand Down Expand Up @@ -78,10 +77,7 @@ def paths_from_link_libpaths():
class build_ext(build_ext):

def build_extensions(self):
try:
import importlib.metadata as importlib_metadata
except ImportError:
import importlib_metadata
import pybind11

ext, = self.distribution.ext_modules

Expand All @@ -95,40 +91,8 @@ def build_extensions(self):
else:
ext.sources += [*map(str, Path("src").glob("*.cpp"))]
ext.sources.remove("src/_unity_build.cpp")
ext.language = "c++"

# pybind11.get_include() is brittle (pybind #1425).
pybind11_include_path = next(
path for path in importlib_metadata.files("pybind11")
if path.name == "pybind11.h").locate().parents[1]
if not (pybind11_include_path / "pybind11/pybind11.h").exists():
# egg-install from setup_requires:
# importlib-metadata thinks the headers are at
# .eggs/pybind11-VER-TAG.egg/pybind11-VER.data/headers/pybind11.h
# but they're actually at
# .eggs/pybind11-VER-TAG.egg/pybind11.h
# pybind11_include_path is
# /<...>/.eggs/pybind11-VER-TAG.egg/pybind11-VER.data
# so just create the proper structure there.
try:
is_egg = (pybind11_include_path.relative_to(
Path(__file__).resolve().parent).parts[0] == ".eggs")
except ValueError:
# Arch Linux ships completely wrong metadata, but the headers
# are in the default include paths, so just leave things as is.
is_egg = False
if is_egg:
shutil.rmtree(pybind11_include_path / "pybind11",
ignore_errors=True)
for file in [*pybind11_include_path.parent.glob("**/*")]:
if file.is_dir():
continue
dest = (pybind11_include_path / "pybind11" /
file.relative_to(pybind11_include_path.parent))
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(file, dest)

ext.include_dirs += [pybind11_include_path]
ext.include_dirs += [pybind11.get_include()]

tmp_include_dir = Path(self.get_finalized_command("build").build_base,
"include")
Expand Down Expand Up @@ -272,9 +236,8 @@ def exec_module(module):
ext_modules=[Extension("mplcairo._mplcairo", [])],
python_requires=">=3.6",
setup_requires=[
"importlib_metadata>=0.8; python_version<'3.8'", # Added files().
"setuptools_scm",
"pybind11>=2.2.4",
"pybind11>=2.5.0",
# Actually also a setup_requires on Linux, but in the manylinux build
# we need to shim it.
"pycairo>=1.16.0; sys_platform == 'darwin'",
Expand Down
7 changes: 1 addition & 6 deletions src/_mplcairo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,8 @@ cairo_t* GraphicsContextRenderer::cr_from_fileformat_args(
-> cairo_status_t {
auto const& write =
py::reinterpret_borrow<py::object>(static_cast<PyObject*>(closure));
// FIXME[pybind11]: Work around lack of const buffers in pybind11.
auto const& buf_info = py::buffer_info{
const_cast<unsigned char*>(data),
sizeof(char), py::format_descriptor<char>::format(),
1, {length}, {sizeof(char)}};
return
write(py::memoryview{buf_info}).cast<unsigned int>() == length
write(py::memoryview{{data, length}}).cast<unsigned int>() == length
// NOTE: This does not appear to affect the context status.
? CAIRO_STATUS_SUCCESS : CAIRO_STATUS_WRITE_ERROR;
};
Expand Down

0 comments on commit de98f71

Please sign in to comment.