aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/pybind11/docs/advanced/pycpp
diff options
context:
space:
mode:
authorgatecat <gatecat@ds0.me>2022-09-14 09:28:47 +0200
committergatecat <gatecat@ds0.me>2022-09-14 09:28:47 +0200
commita72f898ff4c4237424c468044a6db9d6953b541e (patch)
tree1c4a543f661dd1b281aecf4660388491702fa8d8 /3rdparty/pybind11/docs/advanced/pycpp
parentf1349e114f3a16ccd002e8513339e18f5be4d31b (diff)
downloadnextpnr-a72f898ff4c4237424c468044a6db9d6953b541e.tar.gz
nextpnr-a72f898ff4c4237424c468044a6db9d6953b541e.tar.bz2
nextpnr-a72f898ff4c4237424c468044a6db9d6953b541e.zip
3rdparty: Bump vendored pybind11 version for py3.11 support
Signed-off-by: gatecat <gatecat@ds0.me>
Diffstat (limited to '3rdparty/pybind11/docs/advanced/pycpp')
-rw-r--r--3rdparty/pybind11/docs/advanced/pycpp/numpy.rst67
-rw-r--r--3rdparty/pybind11/docs/advanced/pycpp/object.rst37
-rw-r--r--3rdparty/pybind11/docs/advanced/pycpp/utilities.rst23
3 files changed, 96 insertions, 31 deletions
diff --git a/3rdparty/pybind11/docs/advanced/pycpp/numpy.rst b/3rdparty/pybind11/docs/advanced/pycpp/numpy.rst
index 19ed10b3..07c96930 100644
--- a/3rdparty/pybind11/docs/advanced/pycpp/numpy.rst
+++ b/3rdparty/pybind11/docs/advanced/pycpp/numpy.rst
@@ -87,7 +87,7 @@ buffer objects (e.g. a NumPy matrix).
/* Request a buffer descriptor from Python */
py::buffer_info info = b.request();
- /* Some sanity checks ... */
+ /* Some basic validation checks ... */
if (info.format != py::format_descriptor<Scalar>::format())
throw std::runtime_error("Incompatible format: expected a double array!");
@@ -150,8 +150,10 @@ NumPy array containing double precision values.
When it is invoked with a different type (e.g. an integer or a list of
integers), the binding code will attempt to cast the input into a NumPy array
-of the requested type. Note that this feature requires the
-:file:`pybind11/numpy.h` header to be included.
+of the requested type. This feature requires the :file:`pybind11/numpy.h`
+header to be included. Note that :file:`pybind11/numpy.h` does not depend on
+the NumPy headers, and thus can be used without declaring a build-time
+dependency on NumPy; NumPy>=1.7.0 is a runtime dependency.
Data in NumPy arrays is not guaranteed to packed in a dense manner;
furthermore, entries can be separated by arbitrary column and row strides.
@@ -169,6 +171,31 @@ template parameter, and it ensures that non-conforming arguments are converted
into an array satisfying the specified requirements instead of trying the next
function overload.
+There are several methods on arrays; the methods listed below under references
+work, as well as the following functions based on the NumPy API:
+
+- ``.dtype()`` returns the type of the contained values.
+
+- ``.strides()`` returns a pointer to the strides of the array (optionally pass
+ an integer axis to get a number).
+
+- ``.flags()`` returns the flag settings. ``.writable()`` and ``.owndata()``
+ are directly available.
+
+- ``.offset_at()`` returns the offset (optionally pass indices).
+
+- ``.squeeze()`` returns a view with length-1 axes removed.
+
+- ``.view(dtype)`` returns a view of the array with a different dtype.
+
+- ``.reshape({i, j, ...})`` returns a view of the array with a different shape.
+ ``.resize({...})`` is also available.
+
+- ``.index_at(i, j, ...)`` gets the count from the beginning to a given index.
+
+
+There are also several methods for getting references (described below).
+
Structured types
================
@@ -231,8 +258,8 @@ by the compiler. The result is returned as a NumPy array of type
.. code-block:: pycon
- >>> x = np.array([[1, 3],[5, 7]])
- >>> y = np.array([[2, 4],[6, 8]])
+ >>> x = np.array([[1, 3], [5, 7]])
+ >>> y = np.array([[2, 4], [6, 8]])
>>> z = 3
>>> result = vectorized_func(x, y, z)
@@ -343,21 +370,21 @@ The returned proxy object supports some of the same methods as ``py::array`` so
that it can be used as a drop-in replacement for some existing, index-checked
uses of ``py::array``:
-- ``r.ndim()`` returns the number of dimensions
+- ``.ndim()`` returns the number of dimensions
-- ``r.data(1, 2, ...)`` and ``r.mutable_data(1, 2, ...)``` returns a pointer to
+- ``.data(1, 2, ...)`` and ``r.mutable_data(1, 2, ...)``` returns a pointer to
the ``const T`` or ``T`` data, respectively, at the given indices. The
latter is only available to proxies obtained via ``a.mutable_unchecked()``.
-- ``itemsize()`` returns the size of an item in bytes, i.e. ``sizeof(T)``.
+- ``.itemsize()`` returns the size of an item in bytes, i.e. ``sizeof(T)``.
-- ``ndim()`` returns the number of dimensions.
+- ``.ndim()`` returns the number of dimensions.
-- ``shape(n)`` returns the size of dimension ``n``
+- ``.shape(n)`` returns the size of dimension ``n``
-- ``size()`` returns the total number of elements (i.e. the product of the shapes).
+- ``.size()`` returns the total number of elements (i.e. the product of the shapes).
-- ``nbytes()`` returns the number of bytes used by the referenced elements
+- ``.nbytes()`` returns the number of bytes used by the referenced elements
(i.e. ``itemsize()`` times ``size()``).
.. seealso::
@@ -368,15 +395,13 @@ uses of ``py::array``:
Ellipsis
========
-Python 3 provides a convenient ``...`` ellipsis notation that is often used to
+Python provides a convenient ``...`` ellipsis notation that is often used to
slice multidimensional arrays. For instance, the following snippet extracts the
middle dimensions of a tensor with the first and last index set to zero.
-In Python 2, the syntactic sugar ``...`` is not available, but the singleton
-``Ellipsis`` (of type ``ellipsis``) can still be used directly.
.. code-block:: python
- a = # a NumPy array
+ a = ... # a NumPy array
b = a[0, ..., 0]
The function ``py::ellipsis()`` function can be used to perform the same
@@ -387,8 +412,6 @@ operation on the C++ side:
py::array a = /* A NumPy array */;
py::array b = a[py::make_tuple(0, py::ellipsis(), 0)];
-.. versionchanged:: 2.6
- ``py::ellipsis()`` is now also avaliable in Python 2.
Memory view
===========
@@ -410,7 +433,7 @@ following:
{ 2, 4 }, // shape (rows, cols)
{ sizeof(uint8_t) * 4, sizeof(uint8_t) } // strides in bytes
);
- })
+ });
This approach is meant for providing a ``memoryview`` for a C/C++ buffer not
managed by Python. The user is responsible for managing the lifetime of the
@@ -426,11 +449,7 @@ We can also use ``memoryview::from_memory`` for a simple 1D contiguous buffer:
buffer, // buffer pointer
sizeof(uint8_t) * 8 // buffer size
);
- })
-
-.. note::
-
- ``memoryview::from_memory`` is not available in Python 2.
+ });
.. versionchanged:: 2.6
``memoryview::from_memory`` added.
diff --git a/3rdparty/pybind11/docs/advanced/pycpp/object.rst b/3rdparty/pybind11/docs/advanced/pycpp/object.rst
index 6c7525ce..93e1a94d 100644
--- a/3rdparty/pybind11/docs/advanced/pycpp/object.rst
+++ b/3rdparty/pybind11/docs/advanced/pycpp/object.rst
@@ -20,6 +20,40 @@ Available types include :class:`handle`, :class:`object`, :class:`bool_`,
Be sure to review the :ref:`pytypes_gotchas` before using this heavily in
your C++ API.
+.. _instantiating_compound_types:
+
+Instantiating compound Python types from C++
+============================================
+
+Dictionaries can be initialized in the :class:`dict` constructor:
+
+.. code-block:: cpp
+
+ using namespace pybind11::literals; // to bring in the `_a` literal
+ py::dict d("spam"_a=py::none(), "eggs"_a=42);
+
+A tuple of python objects can be instantiated using :func:`py::make_tuple`:
+
+.. code-block:: cpp
+
+ py::tuple tup = py::make_tuple(42, py::none(), "spam");
+
+Each element is converted to a supported Python type.
+
+A `simple namespace`_ can be instantiated using
+
+.. code-block:: cpp
+
+ using namespace pybind11::literals; // to bring in the `_a` literal
+ 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`,
+:func:`py::getattr`, and :func:`py::setattr` functions. Simple namespaces can
+be useful as lightweight stand-ins for class instances.
+
+.. _simple namespace: https://docs.python.org/3/library/types.html#types.SimpleNamespace
+
.. _casting_back_and_forth:
Casting back and forth
@@ -30,7 +64,7 @@ types to Python, which can be done using :func:`py::cast`:
.. code-block:: cpp
- MyClass *cls = ..;
+ MyClass *cls = ...;
py::object obj = py::cast(cls);
The reverse direction uses the following syntax:
@@ -132,6 +166,7 @@ Keyword arguments are also supported. In Python, there is the usual call syntax:
def f(number, say, to):
... # function code
+
f(1234, say="hello", to=some_instance) # keyword call in Python
In C++, the same call can be made using:
diff --git a/3rdparty/pybind11/docs/advanced/pycpp/utilities.rst b/3rdparty/pybind11/docs/advanced/pycpp/utilities.rst
index c15051fb..af0f9cb2 100644
--- a/3rdparty/pybind11/docs/advanced/pycpp/utilities.rst
+++ b/3rdparty/pybind11/docs/advanced/pycpp/utilities.rst
@@ -28,7 +28,7 @@ Capturing standard output from ostream
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``
-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
redirects output to the corresponding Python streams:
@@ -47,15 +47,26 @@ redirects output to the corresponding Python streams:
call_noisy_func();
});
+.. warning::
+
+ The implementation in ``pybind11/iostream.h`` is NOT thread safe. Multiple
+ threads writing to a redirected ostream concurrently cause data races
+ and potentially buffer overflows. Therefore it is currently a requirement
+ that all (possibly) concurrent redirected ostream writes are protected by
+ a mutex. #HelpAppreciated: Work on iostream.h thread safety. For more
+ background see the discussions under
+ `PR #2982 <https://github.com/pybind/pybind11/pull/2982>`_ and
+ `PR #2995 <https://github.com/pybind/pybind11/pull/2995>`_.
+
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
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
-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
-`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:: py
+.. code-block:: cpp
// Alternative: Call single function using call guard
m.def("noisy_func", &call_noisy_function,
@@ -63,7 +74,7 @@ except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful wi
py::scoped_estream_redirect>());
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
@@ -92,7 +103,7 @@ arguments to disable one of the streams if needed.
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
can be used.