Commit acae9301 authored by Ralf W. Grosse-Kunstleve's avatar Ralf W. Grosse-Kunstleve
Browse files

Merge branch 'master' into stable

parents e315e1fe f7b49961
...@@ -9,6 +9,13 @@ on: ...@@ -9,6 +9,13 @@ on:
- stable - stable
- v* - v*
concurrency:
group: test-${{ github.ref }}
cancel-in-progress: true
env:
PIP_ONLY_BINARY: numpy
jobs: jobs:
# This is the "main" test suite, which tests a large number of different # This is the "main" test suite, which tests a large number of different
# versions of default compilers and Python versions in GitHub Actions. # versions of default compilers and Python versions in GitHub Actions.
...@@ -18,13 +25,14 @@ jobs: ...@@ -18,13 +25,14 @@ jobs:
matrix: matrix:
runs-on: [ubuntu-latest, windows-latest, macos-latest] runs-on: [ubuntu-latest, windows-latest, macos-latest]
python: python:
- 2.7 - '2.7'
- 3.5 - '3.5'
- 3.6 - '3.6'
- 3.9 - '3.9'
- 3.10-dev - '3.10'
- pypy2 # - '3.11-dev'
- pypy3 - 'pypy-3.7-v7.3.5'
# - 'pypy-3.8'
# Items in here will either be added to the build matrix (if not # Items in here will either be added to the build matrix (if not
# present), or add new keys to an existing matrix element if all the # present), or add new keys to an existing matrix element if all the
...@@ -42,18 +50,8 @@ jobs: ...@@ -42,18 +50,8 @@ jobs:
python: 3.6 python: 3.6
args: > args: >
-DPYBIND11_FINDPYTHON=ON -DPYBIND11_FINDPYTHON=ON
- runs-on: macos-latest
# These items will be removed from the build matrix, keys must match. python: pypy-2.7
exclude:
# Currently 32bit only, and we build 64bit
- runs-on: windows-latest
python: pypy2
- runs-on: windows-latest
python: pypy3
# TODO: PyPy2 7.3.3 segfaults, while 7.3.2 was fine.
- runs-on: ubuntu-latest
python: pypy2
name: "🐍 ${{ matrix.python }} ${{ matrix.runs-on }} x64 ${{ matrix.args }}" name: "🐍 ${{ matrix.python }} ${{ matrix.runs-on }} x64 ${{ matrix.args }}"
runs-on: ${{ matrix.runs-on }} runs-on: ${{ matrix.runs-on }}
...@@ -89,7 +87,8 @@ jobs: ...@@ -89,7 +87,8 @@ jobs:
key: ${{ runner.os }}-pip-${{ matrix.python }}-x64-${{ hashFiles('tests/requirements.txt') }} key: ${{ runner.os }}-pip-${{ matrix.python }}-x64-${{ hashFiles('tests/requirements.txt') }}
- name: Prepare env - name: Prepare env
run: python -m pip install -r tests/requirements.txt --prefer-binary run: |
python -m pip install -r tests/requirements.txt
- name: Setup annotations on Linux - name: Setup annotations on Linux
if: runner.os == 'Linux' if: runner.os == 'Linux'
...@@ -113,7 +112,7 @@ jobs: ...@@ -113,7 +112,7 @@ jobs:
- name: C++11 tests - name: C++11 tests
# TODO: Figure out how to load the DLL on Python 3.8+ # TODO: Figure out how to load the DLL on Python 3.8+
if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10' || matrix.python == '3.11-dev' || matrix.python == 'pypy-3.8'))"
run: cmake --build . --target cpptest -j 2 run: cmake --build . --target cpptest -j 2
- name: Interface test C++11 - name: Interface test C++11
...@@ -141,7 +140,7 @@ jobs: ...@@ -141,7 +140,7 @@ jobs:
- name: C++ tests - name: C++ tests
# TODO: Figure out how to load the DLL on Python 3.8+ # TODO: Figure out how to load the DLL on Python 3.8+
if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10' || matrix.python == '3.11-dev' || matrix.python == 'pypy-3.8'))"
run: cmake --build build2 --target cpptest run: cmake --build build2 --target cpptest
# Third build - C++17 mode with unstable ABI # Third build - C++17 mode with unstable ABI
...@@ -169,7 +168,7 @@ jobs: ...@@ -169,7 +168,7 @@ jobs:
# MSVC, but for now, this action works: # MSVC, but for now, this action works:
- name: Prepare compiler environment for Windows 🐍 2.7 - name: Prepare compiler environment for Windows 🐍 2.7
if: matrix.python == 2.7 && runner.os == 'Windows' if: matrix.python == 2.7 && runner.os == 'Windows'
uses: ilammy/msvc-dev-cmd@v1.9.0 uses: ilammy/msvc-dev-cmd@v1.10.0
with: with:
arch: x64 arch: x64
...@@ -192,11 +191,12 @@ jobs: ...@@ -192,11 +191,12 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
include: include:
- python-version: 3.9 # TODO: Fails on 3.10, investigate
- python-version: "3.9"
python-debug: true python-debug: true
valgrind: true valgrind: true
- python-version: 3.10-dev # - python-version: "3.11-dev"
python-debug: false # python-debug: false
name: "🐍 ${{ matrix.python-version }}${{ matrix.python-debug && '-dbg' || '' }} (deadsnakes)${{ matrix.valgrind && ' Valgrind' || '' }} x64" name: "🐍 ${{ matrix.python-version }}${{ matrix.python-debug && '-dbg' || '' }} (deadsnakes)${{ matrix.valgrind && ' Valgrind' || '' }} x64"
runs-on: ubuntu-latest runs-on: ubuntu-latest
...@@ -240,7 +240,8 @@ jobs: ...@@ -240,7 +240,8 @@ jobs:
sudo apt-get install libc6-dbg # Needed by Valgrind sudo apt-get install libc6-dbg # Needed by Valgrind
- name: Prepare env - name: Prepare env
run: python -m pip install -r tests/requirements.txt --prefer-binary run: |
python -m pip install -r tests/requirements.txt
- name: Configure - name: Configure
run: > run: >
...@@ -517,7 +518,7 @@ jobs: ...@@ -517,7 +518,7 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: | run: |
set +e; source /opt/intel/oneapi/setvars.sh; set -e set +e; source /opt/intel/oneapi/setvars.sh; set -e
python3 -m pip install -r tests/requirements.txt --prefer-binary python3 -m pip install -r tests/requirements.txt
- name: Configure C++11 - name: Configure C++11
run: | run: |
...@@ -608,7 +609,8 @@ jobs: ...@@ -608,7 +609,8 @@ jobs:
run: python3 -m pip install --upgrade pip run: python3 -m pip install --upgrade pip
- name: Install dependencies - name: Install dependencies
run: python3 -m pip install cmake -r tests/requirements.txt --prefer-binary run: |
python3 -m pip install cmake -r tests/requirements.txt
- name: VAR_BUILD_TYPE 7 - name: VAR_BUILD_TYPE 7
if: matrix.centos == 7 if: matrix.centos == 7
...@@ -735,8 +737,7 @@ jobs: ...@@ -735,8 +737,7 @@ jobs:
- 3.7 - 3.7
- 3.8 - 3.8
- 3.9 - 3.9
- pypy3 - pypy-3.6
# TODO: fix hang on pypy2
include: include:
- python: 3.9 - python: 3.9
...@@ -760,12 +761,13 @@ jobs: ...@@ -760,12 +761,13 @@ jobs:
uses: jwlawson/actions-setup-cmake@v1.11 uses: jwlawson/actions-setup-cmake@v1.11
- name: Prepare MSVC - name: Prepare MSVC
uses: ilammy/msvc-dev-cmd@v1.9.0 uses: ilammy/msvc-dev-cmd@v1.10.0
with: with:
arch: x86 arch: x86
- name: Prepare env - name: Prepare env
run: python -m pip install -r tests/requirements.txt --prefer-binary run: |
python -m pip install -r tests/requirements.txt
# First build - C++11 mode and inplace # First build - C++11 mode and inplace
- name: Configure ${{ matrix.args }} - name: Configure ${{ matrix.args }}
...@@ -806,12 +808,13 @@ jobs: ...@@ -806,12 +808,13 @@ jobs:
uses: jwlawson/actions-setup-cmake@v1.11 uses: jwlawson/actions-setup-cmake@v1.11
- name: Prepare MSVC - name: Prepare MSVC
uses: ilammy/msvc-dev-cmd@v1.9.0 uses: ilammy/msvc-dev-cmd@v1.10.0
with: with:
toolset: 14.0 toolset: 14.0
- name: Prepare env - name: Prepare env
run: python -m pip install -r tests/requirements.txt --prefer-binary run: |
python -m pip install -r tests/requirements.txt
# First build - C++11 mode and inplace # First build - C++11 mode and inplace
- name: Configure - name: Configure
...@@ -847,6 +850,10 @@ jobs: ...@@ -847,6 +850,10 @@ jobs:
std: 17 std: 17
args: > args: >
-DCMAKE_CXX_FLAGS="/permissive- /EHsc /GR" -DCMAKE_CXX_FLAGS="/permissive- /EHsc /GR"
- python: 3.7
std: 17
args: >
-DCMAKE_CXX_FLAGS="/permissive- /EHsc /GR"
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
...@@ -860,7 +867,8 @@ jobs: ...@@ -860,7 +867,8 @@ jobs:
uses: jwlawson/actions-setup-cmake@v1.11 uses: jwlawson/actions-setup-cmake@v1.11
- name: Prepare env - name: Prepare env
run: python -m pip install -r tests/requirements.txt --prefer-binary run: |
python -m pip install -r tests/requirements.txt
# First build - C++11 mode and inplace # First build - C++11 mode and inplace
- name: Configure - name: Configure
...@@ -892,7 +900,8 @@ jobs: ...@@ -892,7 +900,8 @@ jobs:
- { sys: mingw64, env: x86_64 } - { sys: mingw64, env: x86_64 }
- { sys: mingw32, env: i686 } - { sys: mingw32, env: i686 }
steps: steps:
- uses: msys2/setup-msys2@v2 # Force version because of https://github.com/msys2/setup-msys2/issues/167
- uses: msys2/setup-msys2@v2.4.2
with: with:
msystem: ${{matrix.sys}} msystem: ${{matrix.sys}}
install: >- install: >-
......
...@@ -12,6 +12,9 @@ on: ...@@ -12,6 +12,9 @@ on:
types: types:
- published - published
env:
PIP_ONLY_BINARY: numpy
jobs: jobs:
# This builds the sdists and wheels and makes sure the files are exactly as # This builds the sdists and wheels and makes sure the files are exactly as
# expected. Using Windows and Python 2.7, since that is often the most # expected. Using Windows and Python 2.7, since that is often the most
...@@ -29,7 +32,8 @@ jobs: ...@@ -29,7 +32,8 @@ jobs:
python-version: 2.7 python-version: 2.7
- name: Prepare env - name: Prepare env
run: python -m pip install -r tests/requirements.txt --prefer-binary run: |
python -m pip install -r tests/requirements.txt
- name: Python Packaging tests - name: Python Packaging tests
run: pytest tests/extra_python_package/ run: pytest tests/extra_python_package/
...@@ -50,7 +54,8 @@ jobs: ...@@ -50,7 +54,8 @@ jobs:
python-version: 3.8 python-version: 3.8
- name: Prepare env - name: Prepare env
run: python -m pip install -r tests/requirements.txt build twine --prefer-binary run: |
python -m pip install -r tests/requirements.txt build twine
- name: Python Packaging tests - name: Python Packaging tests
run: pytest tests/extra_python_package/ run: pytest tests/extra_python_package/
......
...@@ -61,20 +61,32 @@ repos: ...@@ -61,20 +61,32 @@ repos:
hooks: hooks:
- id: remove-tabs - id: remove-tabs
# Autoremoves unused imports - repo: https://github.com/pre-commit/pygrep-hooks
- repo: https://github.com/hadialqattan/pycln rev: v1.9.0
rev: v1.0.3
hooks: hooks:
- id: pycln - id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
- id: python-no-log-warn
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal
# Flake8 also supports pre-commit natively (same author) # Flake8 also supports pre-commit natively (same author)
- repo: https://github.com/PyCQA/flake8 - repo: https://github.com/PyCQA/flake8
rev: 3.9.2 rev: 4.0.1
hooks: hooks:
- id: flake8 - id: flake8
additional_dependencies: [flake8-bugbear, pep8-naming] additional_dependencies: &flake8_dependencies
- flake8-bugbear
- pep8-naming
exclude: ^(docs/.*|tools/.*)$ exclude: ^(docs/.*|tools/.*)$
- repo: https://github.com/asottile/yesqa
rev: v1.3.0
hooks:
- id: yesqa
additional_dependencies: *flake8_dependencies
# CMake formatting # CMake formatting
- repo: https://github.com/cheshirekow/cmake-format-precommit - repo: https://github.com/cheshirekow/cmake-format-precommit
rev: v0.6.13 rev: v0.6.13
...@@ -86,12 +98,9 @@ repos: ...@@ -86,12 +98,9 @@ repos:
# Check static types with mypy # Check static types with mypy
- repo: https://github.com/pre-commit/mirrors-mypy - repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910 rev: v0.910-1
hooks: hooks:
- id: mypy - id: mypy
# The default Python type ignores .pyi files, so let's rerun if detected
types: [text]
files: ^pybind11.*\.pyi?$
# Running per-file misbehaves a bit, so just run on all files, it's fast # Running per-file misbehaves a bit, so just run on all files, it's fast
pass_filenames: false pass_filenames: false
additional_dependencies: [typed_ast] additional_dependencies: [typed_ast]
......
...@@ -7,13 +7,13 @@ ...@@ -7,13 +7,13 @@
cmake_minimum_required(VERSION 3.4) cmake_minimum_required(VERSION 3.4)
# The `cmake_minimum_required(VERSION 3.4...3.21)` syntax does not work with # The `cmake_minimum_required(VERSION 3.4...3.22)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate # some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround: # the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.21) if(${CMAKE_VERSION} VERSION_LESS 3.22)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else() else()
cmake_policy(VERSION 3.21) cmake_policy(VERSION 3.22)
endif() endif()
# Extract project version from source # Extract project version from source
......
...@@ -1266,7 +1266,7 @@ Custom type setup ...@@ -1266,7 +1266,7 @@ Custom type setup
================= =================
For advanced use cases, such as enabling garbage collection support, you may For advanced use cases, such as enabling garbage collection support, you may
wish to directly manipulate the `PyHeapTypeObject` corresponding to a wish to directly manipulate the ``PyHeapTypeObject`` corresponding to a
``py::class_`` definition. ``py::class_`` definition.
You can do that using ``py::custom_type_setup``: You can do that using ``py::custom_type_setup``:
......
...@@ -40,15 +40,15 @@ The essential structure of the ``main.cpp`` file looks like this: ...@@ -40,15 +40,15 @@ The essential structure of the ``main.cpp`` file looks like this:
} }
The interpreter must be initialized before using any Python API, which includes The interpreter must be initialized before using any Python API, which includes
all the functions and classes in pybind11. The RAII guard class `scoped_interpreter` all the functions and classes in pybind11. The RAII guard class ``scoped_interpreter``
takes care of the interpreter lifetime. After the guard is destroyed, the interpreter takes care of the interpreter lifetime. After the guard is destroyed, the interpreter
shuts down and clears its memory. No Python functions can be called after this. shuts down and clears its memory. No Python functions can be called after this.
Executing Python code Executing Python code
===================== =====================
There are a few different ways to run Python code. One option is to use `eval`, There are a few different ways to run Python code. One option is to use ``eval``,
`exec` or `eval_file`, as explained in :ref:`eval`. Here is a quick example in ``exec`` or ``eval_file``, as explained in :ref:`eval`. Here is a quick example in
the context of an executable with an embedded interpreter: the context of an executable with an embedded interpreter:
.. code-block:: cpp .. code-block:: cpp
...@@ -108,7 +108,7 @@ The two approaches can also be combined: ...@@ -108,7 +108,7 @@ The two approaches can also be combined:
Importing modules Importing modules
================= =================
Python modules can be imported using `module_::import()`: Python modules can be imported using ``module_::import()``:
.. code-block:: cpp .. code-block:: cpp
...@@ -134,7 +134,7 @@ embedding the interpreter. This makes it easy to import local Python files: ...@@ -134,7 +134,7 @@ embedding the interpreter. This makes it easy to import local Python files:
int n = result.cast<int>(); int n = result.cast<int>();
assert(n == 3); assert(n == 3);
Modules can be reloaded using `module_::reload()` if the source is modified e.g. Modules can be reloaded using ``module_::reload()`` if the source is modified e.g.
by an external process. This can be useful in scenarios where the application by an external process. This can be useful in scenarios where the application
imports a user defined data processing script which needs to be updated after imports a user defined data processing script which needs to be updated after
changes by the user. Note that this function does not reload modules recursively. changes by the user. Note that this function does not reload modules recursively.
...@@ -144,7 +144,7 @@ changes by the user. Note that this function does not reload modules recursively ...@@ -144,7 +144,7 @@ changes by the user. Note that this function does not reload modules recursively
Adding embedded modules Adding embedded modules
======================= =======================
Embedded binary modules can be added using the `PYBIND11_EMBEDDED_MODULE` macro. Embedded binary modules can be added using the ``PYBIND11_EMBEDDED_MODULE`` macro.
Note that the definition must be placed at global scope. They can be imported Note that the definition must be placed at global scope. They can be imported
like any other module. like any other module.
...@@ -170,7 +170,7 @@ like any other module. ...@@ -170,7 +170,7 @@ like any other module.
Unlike extension modules where only a single binary module can be created, on Unlike extension modules where only a single binary module can be created, on
the embedded side an unlimited number of modules can be added using multiple the embedded side an unlimited number of modules can be added using multiple
`PYBIND11_EMBEDDED_MODULE` definitions (as long as they have unique names). ``PYBIND11_EMBEDDED_MODULE`` definitions (as long as they have unique names).
These modules are added to Python's list of builtins, so they can also be These modules are added to Python's list of builtins, so they can also be
imported in pure Python files loaded by the interpreter. Everything interacts imported in pure Python files loaded by the interpreter. Everything interacts
...@@ -216,9 +216,9 @@ naturally: ...@@ -216,9 +216,9 @@ naturally:
Interpreter lifetime Interpreter lifetime
==================== ====================
The Python interpreter shuts down when `scoped_interpreter` is destroyed. After The Python interpreter shuts down when ``scoped_interpreter`` is destroyed. After
this, creating a new instance will restart the interpreter. Alternatively, the this, creating a new instance will restart the interpreter. Alternatively, the
`initialize_interpreter` / `finalize_interpreter` pair of functions can be used ``initialize_interpreter`` / ``finalize_interpreter`` pair of functions can be used
to directly set the state at any time. to directly set the state at any time.
Modules created with pybind11 can be safely re-initialized after the interpreter Modules created with pybind11 can be safely re-initialized after the interpreter
...@@ -230,8 +230,8 @@ global data. All the details can be found in the CPython documentation. ...@@ -230,8 +230,8 @@ global data. All the details can be found in the CPython documentation.
.. warning:: .. warning::
Creating two concurrent `scoped_interpreter` guards is a fatal error. So is Creating two concurrent ``scoped_interpreter`` guards is a fatal error. So is
calling `initialize_interpreter` for a second time after the interpreter calling ``initialize_interpreter`` for a second time after the interpreter
has already been initialized. has already been initialized.
Do not use the raw CPython API functions ``Py_Initialize`` and Do not use the raw CPython API functions ``Py_Initialize`` and
...@@ -242,7 +242,7 @@ global data. All the details can be found in the CPython documentation. ...@@ -242,7 +242,7 @@ global data. All the details can be found in the CPython documentation.
Sub-interpreter support Sub-interpreter support
======================= =======================
Creating multiple copies of `scoped_interpreter` is not possible because it Creating multiple copies of ``scoped_interpreter`` is not possible because it
represents the main Python interpreter. Sub-interpreters are something different represents the main Python interpreter. Sub-interpreters are something different
and they do permit the existence of multiple interpreters. This is an advanced and they do permit the existence of multiple interpreters. This is an advanced
feature of the CPython API and should be handled with care. pybind11 does not feature of the CPython API and should be handled with care. pybind11 does not
...@@ -258,5 +258,5 @@ We'll just mention a couple of caveats the sub-interpreters support in pybind11: ...@@ -258,5 +258,5 @@ We'll just mention a couple of caveats the sub-interpreters support in pybind11:
2. Managing multiple threads, multiple interpreters and the GIL can be 2. Managing multiple threads, multiple interpreters and the GIL can be
challenging and there are several caveats here, even within the pure challenging and there are several caveats here, even within the pure
CPython API (please refer to the Python docs for details). As for CPython API (please refer to the Python docs for details). As for
pybind11, keep in mind that `gil_scoped_release` and `gil_scoped_acquire` pybind11, keep in mind that ``gil_scoped_release`` and ``gil_scoped_acquire``
do not take sub-interpreters into account. do not take sub-interpreters into account.
...@@ -56,7 +56,9 @@ at its exception handler. ...@@ -56,7 +56,9 @@ at its exception handler.
+--------------------------------------+--------------------------------------+ +--------------------------------------+--------------------------------------+
| :class:`pybind11::buffer_error` | ``BufferError`` | | :class:`pybind11::buffer_error` | ``BufferError`` |
+--------------------------------------+--------------------------------------+ +--------------------------------------+--------------------------------------+
| :class:`pybind11::import_error` | ``import_error`` | | :class:`pybind11::import_error` | ``ImportError`` |
+--------------------------------------+--------------------------------------+
| :class:`pybind11::attribute_error` | ``AttributeError`` |
+--------------------------------------+--------------------------------------+ +--------------------------------------+--------------------------------------+
| Any other exception | ``RuntimeError`` | | Any other exception | ``RuntimeError`` |
+--------------------------------------+--------------------------------------+ +--------------------------------------+--------------------------------------+
...@@ -96,18 +98,18 @@ A matching function is available for registering a local exception translator: ...@@ -96,18 +98,18 @@ A matching function is available for registering a local exception translator:
It is possible to specify base class for the exception using the third It is possible to specify base class for the exception using the third
parameter, a `handle`: parameter, a ``handle``:
.. code-block:: cpp .. code-block:: cpp
py::register_exception<CppExp>(module, "PyExp", PyExc_RuntimeError); py::register_exception<CppExp>(module, "PyExp", PyExc_RuntimeError);
py::register_local_exception<CppExp>(module, "PyExp", PyExc_RuntimeError); py::register_local_exception<CppExp>(module, "PyExp", PyExc_RuntimeError);
Then `PyExp` can be caught both as `PyExp` and `RuntimeError`. Then ``PyExp`` can be caught both as ``PyExp`` and ``RuntimeError``.
The class objects of the built-in Python exceptions are listed in the Python The class objects of the built-in Python exceptions are listed in the Python
documentation on `Standard Exceptions <https://docs.python.org/3/c-api/exceptions.html#standard-exceptions>`_. documentation on `Standard Exceptions <https://docs.python.org/3/c-api/exceptions.html#standard-exceptions>`_.
The default base class is `PyExc_Exception`. The default base class is ``PyExc_Exception``.
When more advanced exception translation is needed, the functions When more advanced exception translation is needed, the functions
``py::register_exception_translator(translator)`` and ``py::register_exception_translator(translator)`` and
......
...@@ -232,7 +232,7 @@ is equivalent to the following pseudocode: ...@@ -232,7 +232,7 @@ is equivalent to the following pseudocode:
}); });
The only requirement is that ``T`` is default-constructible, but otherwise any The only requirement is that ``T`` is default-constructible, but otherwise any
scope guard will work. This is very useful in combination with `gil_scoped_release`. scope guard will work. This is very useful in combination with ``gil_scoped_release``.
See :ref:`gil`. See :ref:`gil`.
Multiple guards can also be specified as ``py::call_guard<T1, T2, T3...>``. The Multiple guards can also be specified as ``py::call_guard<T1, T2, T3...>``. The
......
...@@ -84,7 +84,7 @@ could be realized as follows (important changes highlighted): ...@@ -84,7 +84,7 @@ could be realized as follows (important changes highlighted):
}); });
} }
The ``call_go`` wrapper can also be simplified using the `call_guard` policy The ``call_go`` wrapper can also be simplified using the ``call_guard`` policy
(see :ref:`call_policies`) which yields the same result: (see :ref:`call_policies`) which yields the same result:
.. code-block:: cpp .. code-block:: cpp
......
...@@ -41,24 +41,17 @@ A tuple of python objects can be instantiated using :func:`py::make_tuple`: ...@@ -41,24 +41,17 @@ A tuple of python objects can be instantiated using :func:`py::make_tuple`:
Each element is converted to a supported Python type. Each element is converted to a supported Python type.
A `simple namespace`_ can be instantiated using A `simple namespace`_ can be instantiated using
:func:`py::make_simple_namespace`:
.. code-block:: cpp .. code-block:: cpp
using namespace pybind11::literals; // to bring in the `_a` literal using namespace pybind11::literals; // to bring in the `_a` literal
py::object ns = py::make_simple_namespace("spam"_a=py::none(), "eggs"_a=42); py::object SimpleNamespace = py::module_::import("types").attr("SimpleNamespace");
py::object ns = SimpleNamespace("spam"_a=py::none(), "eggs"_a=42);
Attributes on a namespace can be modified with the :func:`py::delattr`, Attributes on a namespace can be modified with the :func:`py::delattr`,
:func:`py::getattr`, and :func:`py::setattr` functions. Simple namespaces can :func:`py::getattr`, and :func:`py::setattr` functions. Simple namespaces can
be useful as lightweight stand-ins for class instances. be useful as lightweight stand-ins for class instances.
.. note::
``make_simple_namespace`` is not available in Python 2.
.. versionchanged:: 2.8
``make_simple_namespace`` added.
.. _simple namespace: https://docs.python.org/3/library/types.html#types.SimpleNamespace .. _simple namespace: https://docs.python.org/3/library/types.html#types.SimpleNamespace
.. _casting_back_and_forth: .. _casting_back_and_forth:
......
...@@ -28,7 +28,7 @@ Capturing standard output from ostream ...@@ -28,7 +28,7 @@ Capturing standard output from ostream
Often, a library will use the streams ``std::cout`` and ``std::cerr`` to print, Often, a library will use the streams ``std::cout`` and ``std::cerr`` to print,
but this does not play well with Python's standard ``sys.stdout`` and ``sys.stderr`` but this does not play well with Python's standard ``sys.stdout`` and ``sys.stderr``
redirection. Replacing a library's printing with `py::print <print>` may not redirection. Replacing a library's printing with ``py::print <print>`` may not
be feasible. This can be fixed using a guard around the library function that be feasible. This can be fixed using a guard around the library function that
redirects output to the corresponding Python streams: redirects output to the corresponding Python streams:
...@@ -62,9 +62,9 @@ This method respects flushes on the output streams and will flush if needed ...@@ -62,9 +62,9 @@ This method respects flushes on the output streams and will flush if needed
when the scoped guard is destroyed. This allows the output to be redirected in when the scoped guard is destroyed. This allows the output to be redirected in
real time, such as to a Jupyter notebook. The two arguments, the C++ stream and real time, such as to a Jupyter notebook. The two arguments, the C++ stream and
the Python output, are optional, and default to standard output if not given. An the Python output, are optional, and default to standard output if not given. An
extra type, `py::scoped_estream_redirect <scoped_estream_redirect>`, is identical extra type, ``py::scoped_estream_redirect <scoped_estream_redirect>``, is identical
except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful with except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful with
`py::call_guard`, which allows multiple items, but uses the default constructor: ``py::call_guard``, which allows multiple items, but uses the default constructor:
.. code-block:: cpp .. code-block:: cpp
...@@ -74,7 +74,7 @@ except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful wi ...@@ -74,7 +74,7 @@ except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful wi
py::scoped_estream_redirect>()); py::scoped_estream_redirect>());
The redirection can also be done in Python with the addition of a context The redirection can also be done in Python with the addition of a context
manager, using the `py::add_ostream_redirect() <add_ostream_redirect>` function: manager, using the ``py::add_ostream_redirect() <add_ostream_redirect>`` function:
.. code-block:: cpp .. code-block:: cpp
...@@ -103,7 +103,7 @@ arguments to disable one of the streams if needed. ...@@ -103,7 +103,7 @@ arguments to disable one of the streams if needed.
Evaluating Python expressions from strings and files Evaluating Python expressions from strings and files
==================================================== ====================================================
pybind11 provides the `eval`, `exec` and `eval_file` functions to evaluate pybind11 provides the ``eval``, ``exec`` and ``eval_file`` functions to evaluate
Python expressions and statements. The following example illustrates how they Python expressions and statements. The following example illustrates how they
can be used. can be used.
......
...@@ -6,6 +6,54 @@ Changelog ...@@ -6,6 +6,54 @@ Changelog
Starting with version 1.8.0, pybind11 releases use a `semantic versioning Starting with version 1.8.0, pybind11 releases use a `semantic versioning
<http://semver.org>`_ policy. <http://semver.org>`_ policy.
IN DEVELOPMENT
--------------
v2.8.1 (Oct 27, 2021)
---------------------
Changes and additions:
* The simple namespace creation shortcut added in 2.8.0 was deprecated due to
usage of CPython internal API, and will be removed soon. Use
``py::module_::import("types").attr("SimpleNamespace")``.
`#3374 <https://github.com/pybinyyd/pybind11/pull/3374>`_
* Add C++ Exception type to throw and catch ``AttributeError``. Useful for
defining custom ``__setattr__`` and ``__getattr__`` methods.
`#3387 <https://github.com/pybind/pybind11/pull/3387>`_
Fixes:
* Fixed the potential for dangling references when using properties with
``std::optional`` types.
`#3376 <https://github.com/pybind/pybind11/pull/3376>`_
* Modernize usage of ``PyCodeObject`` on Python 3.9+ (moving toward support for
Python 3.11a1)
`#3368 <https://github.com/pybind/pybind11/pull/3368>`_
* A long-standing bug in ``eigen.h`` was fixed (originally PR #3343). The bug
was unmasked by newly added ``static_assert``'s in the Eigen 3.4.0 release.
`#3352 <https://github.com/pybind/pybind11/pull/3352>`_
* Support multiple raw inclusion of CMake helper files (Conan.io does this for
multi-config generators).
`#3420 <https://github.com/pybind/pybind11/pull/3420>`_
* Fix harmless warning on upcoming CMake 3.22.
`#3368 <https://github.com/pybind/pybind11/pull/3368>`_
* Fix 2.8.0 regression with MSVC 2017 + C++17 mode + Python 3.
`#3407 <https://github.com/pybind/pybind11/pull/3407>`_
* Fix 2.8.0 regression that caused undefined behavior (typically
segfaults) in ``make_key_iterator``/``make_value_iterator`` if dereferencing
the iterator returned a temporary value instead of a reference.
`#3348 <https://github.com/pybind/pybind11/pull/3348>`_
v2.8.0 (Oct 4, 2021) v2.8.0 (Oct 4, 2021)
-------------------- --------------------
...@@ -19,10 +67,10 @@ New features: ...@@ -19,10 +67,10 @@ New features:
``register_local_exception_translator(ExceptionTranslator&& translator)`` ``register_local_exception_translator(ExceptionTranslator&& translator)``
instead of ``register_exception_translator(ExceptionTranslator&& instead of ``register_exception_translator(ExceptionTranslator&&
translator)`` to keep your exception remapping code local to the module. translator)`` to keep your exception remapping code local to the module.
`#2650 <https://github.com/pybind/pybind11/pull/2650>`_ `#2650 <https://github.com/pybinyyd/pybind11/pull/2650>`_
* Add ``make_simple_namespace`` function for instantiating Python * Add ``make_simple_namespace`` function for instantiating Python
``SimpleNamespace`` objects. ``SimpleNamespace`` objects. **Deprecated in 2.8.1.**
`#2840 <https://github.com/pybind/pybind11/pull/2840>`_ `#2840 <https://github.com/pybind/pybind11/pull/2840>`_
* ``pybind11::scoped_interpreter`` and ``initialize_interpreter`` have new * ``pybind11::scoped_interpreter`` and ``initialize_interpreter`` have new
...@@ -47,9 +95,10 @@ New features: ...@@ -47,9 +95,10 @@ New features:
`#3293 <https://github.com/pybind/pybind11/pull/3293>`_ `#3293 <https://github.com/pybind/pybind11/pull/3293>`_
* Improve the classes generated by ``bind_map``: `#3310 <https://github.com/pybind/pybind11/pull/3310>`_ * Improve the classes generated by ``bind_map``: `#3310 <https://github.com/pybind/pybind11/pull/3310>`_
* Change ``.items`` from an iterator to a dictionary view.
* Add ``.keys`` and ``.values`` (both dictionary views). * Change ``.items`` from an iterator to a dictionary view.
* Allow ``__contains__`` to take any object. * Add ``.keys`` and ``.values`` (both dictionary views).
* Allow ``__contains__`` to take any object.
* ``pybind11::custom_type_setup`` was added, for customizing the * ``pybind11::custom_type_setup`` was added, for customizing the
``PyHeapTypeObject`` corresponding to a class, which may be useful for ``PyHeapTypeObject`` corresponding to a class, which may be useful for
...@@ -244,8 +293,8 @@ New features: ...@@ -244,8 +293,8 @@ New features:
Changes: Changes:
* ``py::str`` changed to exclusively hold `PyUnicodeObject`. Previously * ``py::str`` changed to exclusively hold ``PyUnicodeObject``. Previously
``py::str`` could also hold `bytes`, which is probably surprising, was ``py::str`` could also hold ``bytes``, which is probably surprising, was
never documented, and can mask bugs (e.g. accidental use of ``py::str`` never documented, and can mask bugs (e.g. accidental use of ``py::str``
instead of ``py::bytes``). instead of ``py::bytes``).
`#2409 <https://github.com/pybind/pybind11/pull/2409>`_ `#2409 <https://github.com/pybind/pybind11/pull/2409>`_
...@@ -1398,7 +1447,7 @@ v2.2.0 (August 31, 2017) ...@@ -1398,7 +1447,7 @@ v2.2.0 (August 31, 2017)
* Intel C++ compiler compatibility fixes. * Intel C++ compiler compatibility fixes.
`#937 <https://github.com/pybind/pybind11/pull/937>`_. `#937 <https://github.com/pybind/pybind11/pull/937>`_.
* Fixed implicit conversion of `py::enum_` to integer types on Python 2.7. * Fixed implicit conversion of ``py::enum_`` to integer types on Python 2.7.
`#821 <https://github.com/pybind/pybind11/pull/821>`_. `#821 <https://github.com/pybind/pybind11/pull/821>`_.
* Added ``py::hash`` to fetch the hash value of Python objects, and * Added ``py::hash`` to fetch the hash value of Python objects, and
......
...@@ -27,6 +27,7 @@ To release a new version of pybind11: ...@@ -27,6 +27,7 @@ To release a new version of pybind11:
``include/pybind11/detail/common.h``. PATCH should be a simple integer. ``include/pybind11/detail/common.h``. PATCH should be a simple integer.
- Update the version HEX just below, as well. - Update the version HEX just below, as well.
- Update ``pybind11/_version.py`` (match above) - Update ``pybind11/_version.py`` (match above)
- Run ``nox -s tests_packaging`` to ensure this was done correctly.
- Ensure that all the information in ``setup.cfg`` is up-to-date, like - Ensure that all the information in ``setup.cfg`` is up-to-date, like
supported Python versions. supported Python versions.
- Add release date in ``docs/changelog.rst``. - Add release date in ``docs/changelog.rst``.
...@@ -49,13 +50,15 @@ To release a new version of pybind11: ...@@ -49,13 +50,15 @@ To release a new version of pybind11:
- Make a GitHub release (this shows up in the UI, sends new release - Make a GitHub release (this shows up in the UI, sends new release
notifications to users watching releases, and also uploads PyPI packages). notifications to users watching releases, and also uploads PyPI packages).
(Note: if you do not use an existing tag, this creates a new lightweight tag (Note: if you do not use an existing tag, this creates a new lightweight tag
for you, so you could skip the above step). for you, so you could skip the above step.)
- GUI method: click "Create a new release" on the far right, fill in the tag
name (if you didn't tag above, it will be made here), fill in a release - GUI method: Under `releases <https://github.com/pybind/pybind11/releases>`_
name like "Version X.Y.Z", and optionally copy-and-paste the changelog into click "Draft a new release" on the far right, fill in the tag name
the description (processed as markdown by Pandoc). Check "pre-release" if (if you didn't tag above, it will be made here), fill in a release name
this is a beta/RC. You can get partway there with like "Version X.Y.Z", and copy-and-paste the markdown-formatted (!) changelog
``cat docs/changelog.rst | pandoc -f rst -t gfm``. into the description (usually ``cat docs/changelog.rst | pandoc -f rst -t gfm``).
Check "pre-release" if this is a beta/RC.
- CLI method: with ``gh`` installed, run ``gh release create vX.Y.Z -t "Version X.Y.Z"`` - CLI method: with ``gh`` installed, run ``gh release create vX.Y.Z -t "Version X.Y.Z"``
If this is a pre-release, add ``-p``. If this is a pre-release, add ``-p``.
...@@ -64,6 +67,7 @@ To release a new version of pybind11: ...@@ -64,6 +67,7 @@ To release a new version of pybind11:
- Update version macros in ``include/pybind11/detail/common.h`` (set PATCH to - Update version macros in ``include/pybind11/detail/common.h`` (set PATCH to
``0.dev1`` and increment MINOR). ``0.dev1`` and increment MINOR).
- Update ``_version.py`` to match - Update ``_version.py`` to match
- Run ``nox -s tests_packaging`` to ensure this was done correctly.
- Add a spot for in-development updates in ``docs/changelog.rst``. - Add a spot for in-development updates in ``docs/changelog.rst``.
- ``git add``, ``git commit``, ``git push`` - ``git add``, ``git commit``, ``git push``
...@@ -71,7 +75,7 @@ If a version branch is updated, remember to set PATCH to ``1.dev1``. ...@@ -71,7 +75,7 @@ If a version branch is updated, remember to set PATCH to ``1.dev1``.
If you'd like to bump homebrew, run: If you'd like to bump homebrew, run:
.. code-block:: .. code-block:: console
brew bump-formula-pr --url https://github.com/pybind/pybind11/archive/vX.Y.Z.tar.gz brew bump-formula-pr --url https://github.com/pybind/pybind11/archive/vX.Y.Z.tar.gz
......
...@@ -8,7 +8,17 @@ to a new version. But it goes into more detail. This includes things like ...@@ -8,7 +8,17 @@ to a new version. But it goes into more detail. This includes things like
deprecated APIs and their replacements, build system changes, general code deprecated APIs and their replacements, build system changes, general code
modernization and other useful information. modernization and other useful information.
.. _upgrade-guide-2.6: .. _upgrade-guide-2.9:
v2.9
====
* Any usage of the recently added ``py::make_simple_namespace`` should be
converted to using ``py::module_::import("types").attr("SimpleNamespace")``
instead.
.. _upgrade-guide-2.7:
v2.7 v2.7
==== ====
...@@ -34,6 +44,7 @@ to be common: ...@@ -34,6 +44,7 @@ to be common:
careful review and custom fixes. careful review and custom fixes.
.. _upgrade-guide-2.6:
v2.6 v2.6
==== ====
......
...@@ -1036,16 +1036,6 @@ template <return_value_policy policy = return_value_policy::automatic_reference, ...@@ -1036,16 +1036,6 @@ template <return_value_policy policy = return_value_policy::automatic_reference,
return result; return result;
} }
#if PY_VERSION_HEX >= 0x03030000
template <typename... Args,
typename = detail::enable_if_t<args_are_all_keyword_or_ds<Args...>()>>
object make_simple_namespace(Args&&... args_) {
PyObject *ns = _PyNamespace_New(dict(std::forward<Args>(args_)...).ptr());
if (!ns) throw error_already_set();
return reinterpret_steal<object>(ns);
}
#endif
/// \ingroup annotations /// \ingroup annotations
/// Annotation for arguments /// Annotation for arguments
struct arg { struct arg {
......
...@@ -11,11 +11,11 @@ ...@@ -11,11 +11,11 @@
#define PYBIND11_VERSION_MAJOR 2 #define PYBIND11_VERSION_MAJOR 2
#define PYBIND11_VERSION_MINOR 8 #define PYBIND11_VERSION_MINOR 8
#define PYBIND11_VERSION_PATCH 0 #define PYBIND11_VERSION_PATCH 1
// Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html
// Additional convention: 0xD = dev // Additional convention: 0xD = dev
#define PYBIND11_VERSION_HEX 0x02080000 #define PYBIND11_VERSION_HEX 0x02080100
#define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_BEGIN(name) namespace name {
#define PYBIND11_NAMESPACE_END(name) } #define PYBIND11_NAMESPACE_END(name) }
...@@ -831,6 +831,7 @@ PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError) ...@@ -831,6 +831,7 @@ PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError)
PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError) PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError)
PYBIND11_RUNTIME_EXCEPTION(buffer_error, PyExc_BufferError) PYBIND11_RUNTIME_EXCEPTION(buffer_error, PyExc_BufferError)
PYBIND11_RUNTIME_EXCEPTION(import_error, PyExc_ImportError) PYBIND11_RUNTIME_EXCEPTION(import_error, PyExc_ImportError)
PYBIND11_RUNTIME_EXCEPTION(attribute_error, PyExc_AttributeError)
PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybind11::cast or handle::call fail due to a type casting error PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybind11::cast or handle::call fail due to a type casting error
PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally
......
...@@ -468,12 +468,19 @@ PYBIND11_NOINLINE std::string error_string() { ...@@ -468,12 +468,19 @@ PYBIND11_NOINLINE std::string error_string() {
PyFrameObject *frame = trace->tb_frame; PyFrameObject *frame = trace->tb_frame;
errorString += "\n\nAt:\n"; errorString += "\n\nAt:\n";
while (frame) { while (frame) {
#if PY_VERSION_HEX >= 0x03090000
PyCodeObject *f_code = PyFrame_GetCode(frame);
#else
PyCodeObject *f_code = frame->f_code;
Py_INCREF(f_code);
#endif
int lineno = PyFrame_GetLineNumber(frame); int lineno = PyFrame_GetLineNumber(frame);
errorString += errorString +=
" " + handle(frame->f_code->co_filename).cast<std::string>() + " " + handle(f_code->co_filename).cast<std::string>() +
"(" + std::to_string(lineno) + "): " + "(" + std::to_string(lineno) + "): " +
handle(frame->f_code->co_name).cast<std::string>() + "\n"; handle(f_code->co_name).cast<std::string>() + "\n";
frame = frame->f_back; frame = frame->f_back;
Py_DECREF(f_code);
} }
} }
#endif #endif
......
...@@ -17,9 +17,23 @@ ...@@ -17,9 +17,23 @@
#include "numpy.h" #include "numpy.h"
// The C4127 suppression was introduced for Eigen 3.4.0. In theory we could
// make it version specific, or even remove it later, but considering that
// 1. C4127 is generally far more distracting than useful for modern template code, and
// 2. we definitely want to ignore any MSVC warnings originating from Eigen code,
// it is probably best to keep this around indefinitely.
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable: 4127) // C4127: conditional expression is constant
#endif
#include <Eigen/Core> #include <Eigen/Core>
#include <Eigen/SparseCore> #include <Eigen/SparseCore>
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
// Eigen prior to 3.2.7 doesn't have proper move constructors--but worse, some classes get implicit // Eigen prior to 3.2.7 doesn't have proper move constructors--but worse, some classes get implicit
// move constructors that break things. We could detect this an explicitly copy, but an extra copy // move constructors that break things. We could detect this an explicitly copy, but an extra copy
// of matrices seems highly undesirable. // of matrices seems highly undesirable.
...@@ -559,7 +573,9 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> { ...@@ -559,7 +573,9 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
if (!values || !innerIndices || !outerIndices) if (!values || !innerIndices || !outerIndices)
return false; return false;
value = Eigen::MappedSparseMatrix<Scalar, Type::Flags, StorageIndex>( value = Eigen::MappedSparseMatrix<Scalar,
Type::Flags & (Eigen::RowMajor | Eigen::ColMajor),
StorageIndex>(
shape[0].cast<Index>(), shape[1].cast<Index>(), nnz, shape[0].cast<Index>(), shape[1].cast<Index>(), nnz,
outerIndices.mutable_data(), innerIndices.mutable_data(), values.mutable_data()); outerIndices.mutable_data(), innerIndices.mutable_data(), values.mutable_data());
......
/* /*
pybind11/exec.h: Support for evaluating Python expressions and statements pybind11/eval.h: Support for evaluating Python expressions and statements
from strings and files from strings and files
Copyright (c) 2016 Klemens Morgenstern <klemens.morgenstern@ed-chemnitz.de> and Copyright (c) 2016 Klemens Morgenstern <klemens.morgenstern@ed-chemnitz.de> and
......
...@@ -69,7 +69,7 @@ public: ...@@ -69,7 +69,7 @@ public:
// ensure GIL is held during functor destruction // ensure GIL is held during functor destruction
struct func_handle { struct func_handle {
function f; function f;
#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17) && PY_MAJOR_VERSION < 3) #if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17))
// This triggers a syntax error under very special conditions (very weird indeed). // This triggers a syntax error under very special conditions (very weird indeed).
explicit explicit
#endif #endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment