Skip to content

Commit 04b60dd

Browse files
committed
docs: cleanup for Python 3
1 parent cf5a439 commit 04b60dd

File tree

14 files changed

+39
-102
lines changed

14 files changed

+39
-102
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Goodies
7878
In addition to the core functionality, pybind11 provides some extra
7979
goodies:
8080

81-
- Python 2.7, 3.5+, and PyPy/PyPy3 7.3 are supported with an
82-
implementation-agnostic interface.
81+
- Python 3.5+, and PyPy3 7.3 are supported with an
82+
implementation-agnostic interface (pybind11 2.9 for Python 2).
8383

8484
- It is possible to bind C++11 lambda functions with captured
8585
variables. The lambda capture data is stored inside the resulting

docs/advanced/cast/strings.rst

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
Strings, bytes and Unicode conversions
22
######################################
33

4-
.. note::
5-
6-
This section discusses string handling in terms of Python 3 strings. For
7-
Python 2.7, replace all occurrences of ``str`` with ``unicode`` and
8-
``bytes`` with ``str``. Python 2.7 users may find it best to use ``from
9-
__future__ import unicode_literals`` to avoid unintentionally using ``str``
10-
instead of ``unicode``.
11-
124
Passing Python strings to C++
135
=============================
146

@@ -58,9 +50,9 @@ Passing bytes to C++
5850
--------------------
5951

6052
A Python ``bytes`` object will be passed to C++ functions that accept
61-
``std::string`` or ``char*`` *without* conversion. On Python 3, in order to
62-
make a function *only* accept ``bytes`` (and not ``str``), declare it as taking
63-
a ``py::bytes`` argument.
53+
``std::string`` or ``char*`` *without* conversion. In order to make a function
54+
*only* accept ``bytes`` (and not ``str``), declare it as taking a ``py::bytes``
55+
argument.
6456

6557

6658
Returning C++ strings to Python
@@ -204,11 +196,6 @@ decoded to Python ``str``.
204196
}
205197
);
206198

207-
.. warning::
208-
209-
Wide character strings may not work as described on Python 2.7 or Python
210-
3.3 compiled with ``--enable-unicode=ucs2``.
211-
212199
Strings in multibyte encodings such as Shift-JIS must transcoded to a
213200
UTF-8/16/32 before being returned to Python.
214201

docs/advanced/classes.rst

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -813,26 +813,21 @@ An instance can now be pickled as follows:
813813

814814
.. code-block:: python
815815
816-
try:
817-
import cPickle as pickle # Use cPickle on Python 2.7
818-
except ImportError:
819-
import pickle
816+
import pickle
820817
821818
p = Pickleable("test_value")
822819
p.setExtra(15)
823-
data = pickle.dumps(p, 2)
820+
data = pickle.dumps(p)
824821
825822
826823
.. note::
827-
Note that only the cPickle module is supported on Python 2.7.
828-
829-
The second argument to ``dumps`` is also crucial: it selects the pickle
830-
protocol version 2, since the older version 1 is not supported. Newer
831-
versions are also fine—for instance, specify ``-1`` to always use the
832-
latest available version. Beware: failure to follow these instructions
833-
will cause important pybind11 memory allocation routines to be skipped
834-
during unpickling, which will likely lead to memory corruption and/or
835-
segmentation faults.
824+
If given, the second argument to ``dumps`` must be 2 or larger - 0 and 1 are
825+
not supported. Newer versions are also fine; for instance, specify ``-1`` to
826+
always use the latest available version. Beware: failure to follow these
827+
instructions will cause important pybind11 memory allocation routines to be
828+
skipped during unpickling, which will likely lead to memory corruption
829+
and/or segmentation faults. Python defaults to version 3 (Python 3-3.7) and
830+
version 4 for Python 3.8+.
836831

837832
.. seealso::
838833

@@ -849,11 +844,9 @@ Python normally uses references in assignments. Sometimes a real copy is needed
849844
to prevent changing all copies. The ``copy`` module [#f5]_ provides these
850845
capabilities.
851846

852-
On Python 3, a class with pickle support is automatically also (deep)copy
847+
A class with pickle support is automatically also (deep)copy
853848
compatible. However, performance can be improved by adding custom
854-
``__copy__`` and ``__deepcopy__`` methods. With Python 2.7, these custom methods
855-
are mandatory for (deep)copy compatibility, because pybind11 only supports
856-
cPickle.
849+
``__copy__`` and ``__deepcopy__`` methods.
857850

858851
For simple classes (deep)copy can be enabled by using the copy constructor,
859852
which should look as follows:

docs/advanced/exceptions.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ an invalid state.
328328
Chaining exceptions ('raise from')
329329
==================================
330330

331-
In Python 3.3 a mechanism for indicating that exceptions were caused by other
332-
exceptions was introduced:
331+
Python has a mechanism for indicating that exceptions were caused by other
332+
exceptions:
333333

334334
.. code-block:: py
335335
@@ -340,7 +340,7 @@ exceptions was introduced:
340340
341341
To do a similar thing in pybind11, you can use the ``py::raise_from`` function. It
342342
sets the current python error indicator, so to continue propagating the exception
343-
you should ``throw py::error_already_set()`` (Python 3 only).
343+
you should ``throw py::error_already_set()``.
344344

345345
.. code-block:: cpp
346346

docs/advanced/pycpp/numpy.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,6 @@ Ellipsis
398398
Python 3 provides a convenient ``...`` ellipsis notation that is often used to
399399
slice multidimensional arrays. For instance, the following snippet extracts the
400400
middle dimensions of a tensor with the first and last index set to zero.
401-
In Python 2, the syntactic sugar ``...`` is not available, but the singleton
402-
``Ellipsis`` (of type ``ellipsis``) can still be used directly.
403401

404402
.. code-block:: python
405403
@@ -414,8 +412,6 @@ operation on the C++ side:
414412
py::array a = /* A NumPy array */;
415413
py::array b = a[py::make_tuple(0, py::ellipsis(), 0)];
416414
417-
.. versionchanged:: 2.6
418-
``py::ellipsis()`` is now also available in Python 2.
419415
420416
Memory view
421417
===========
@@ -455,9 +451,5 @@ We can also use ``memoryview::from_memory`` for a simple 1D contiguous buffer:
455451
);
456452
})
457453
458-
.. note::
459-
460-
``memoryview::from_memory`` is not available in Python 2.
461-
462454
.. versionchanged:: 2.6
463455
``memoryview::from_memory`` added.

docs/basics.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ load and execute the example:
166166
.. code-block:: pycon
167167
168168
$ python
169-
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
170-
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
169+
Python 3.9.10 (main, Jan 15 2022, 11:48:04)
170+
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
171171
Type "help", "copyright", "credits" or "license" for more information.
172172
>>> import example
173173
>>> example.add(1, 2)
174-
3L
174+
3
175175
>>>
176176
177177
.. _keyword_args:

docs/compiling.rst

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,8 @@ available in all modes. The targets provided are:
462462
``pybind11::headers``
463463
Just the pybind11 headers and minimum compile requirements
464464

465-
``pybind11::python2_no_register``
466-
Quiets the warning/error when mixing C++14 or higher and Python 2
467-
468465
``pybind11::pybind11``
469-
Python headers + ``pybind11::headers`` + ``pybind11::python2_no_register`` (Python 2 only)
466+
Python headers + ``pybind11::headers``
470467

471468
``pybind11::python_link_helper``
472469
Just the "linking" part of pybind11:module
@@ -583,12 +580,6 @@ using ``pip`` or ``conda``. If it hasn't, you can also manually specify
583580
``-I <path-to-pybind11>/include`` together with the Python includes path
584581
``python3-config --includes``.
585582

586-
Note that Python 2.7 modules don't use a special suffix, so you should simply
587-
use ``example.so`` instead of ``example$(python3-config --extension-suffix)``.
588-
Besides, the ``--extension-suffix`` option may or may not be available, depending
589-
on the distribution; in the latter case, the module extension can be manually
590-
set to ``.so``.
591-
592583
On macOS: the build command is almost the same but it also requires passing
593584
the ``-undefined dynamic_lookup`` flag so as to ignore missing symbols when
594585
building the module:

docs/faq.rst

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ Frequently asked questions
88
filename of the extension library (without suffixes such as ``.so``).
99

1010
2. If the above did not fix the issue, you are likely using an incompatible
11-
version of Python (for instance, the extension library was compiled against
12-
Python 2, while the interpreter is running on top of some version of Python
13-
3, or vice versa).
11+
version of Python that does not match what you compiled with.
1412

1513
"Symbol not found: ``__Py_ZeroStruct`` / ``_PyInstanceMethod_Type``"
1614
========================================================================
@@ -289,27 +287,7 @@ Conflicts can arise, however, when using pybind11 in a project that *also* uses
289287
the CMake Python detection in a system with several Python versions installed.
290288

291289
This difference may cause inconsistencies and errors if *both* mechanisms are
292-
used in the same project. Consider the following CMake code executed in a
293-
system with Python 2.7 and 3.x installed:
294-
295-
.. code-block:: cmake
296-
297-
find_package(PythonInterp)
298-
find_package(PythonLibs)
299-
find_package(pybind11)
300-
301-
It will detect Python 2.7 and pybind11 will pick it as well.
302-
303-
In contrast this code:
304-
305-
.. code-block:: cmake
306-
307-
find_package(pybind11)
308-
find_package(PythonInterp)
309-
find_package(PythonLibs)
310-
311-
will detect Python 3.x for pybind11 and may crash on
312-
``find_package(PythonLibs)`` afterwards.
290+
used in the same project.
313291

314292
There are three possible solutions:
315293

include/pybind11/cast.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class type_caster<bool> {
325325
res = 0; // None is implicitly converted to False
326326
}
327327
#if defined(PYPY_VERSION)
328-
// On PyPy, check that "__bool__" (or "__nonzero__" on Python 2.7) attr exists
328+
// On PyPy, check that "__bool__" attr exists
329329
else if (hasattr(src, PYBIND11_BOOL_ATTR)) {
330330
res = PyObject_IsTrue(src.ptr());
331331
}
@@ -383,8 +383,8 @@ struct string_caster {
383383
return load_bytes(load_src);
384384
}
385385

386-
// On Python 3.3, for UTF-8 we avoid the need for a temporary `bytes`
387-
// object by using `PyUnicode_AsUTF8AndSize`.
386+
// For UTF-8 we avoid the need for a temporary `bytes` object by using
387+
// `PyUnicode_AsUTF8AndSize`.
388388
if (PYBIND11_SILENCE_MSVC_C4127(UTF_N == 8)) {
389389
Py_ssize_t size = -1;
390390
const auto *buffer
@@ -464,7 +464,7 @@ struct string_caster {
464464
template <typename C = CharT>
465465
bool load_bytes(enable_if_t<std::is_same<C, char>::value, handle> src) {
466466
if (PYBIND11_BYTES_CHECK(src.ptr())) {
467-
// We were passed a Python 3 raw bytes; accept it into a std::string or char*
467+
// We were passed raw bytes; accept it into a std::string or char*
468468
// without any encoding attempt.
469469
const char *bytes = PYBIND11_BYTES_AS_STRING(src.ptr());
470470
if (bytes) {
@@ -1279,7 +1279,7 @@ struct arg_v : arg {
12791279

12801280
/// \ingroup annotations
12811281
/// Annotation indicating that all following arguments are keyword-only; the is the equivalent of
1282-
/// an unnamed '*' argument (in Python 3)
1282+
/// an unnamed '*' argument
12831283
struct kw_only {};
12841284

12851285
/// \ingroup annotations

include/pybind11/detail/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
// If UNDEFINED, pybind11::str can only hold PyUnicodeObject, and
270270
// pybind11::isinstance<str>() is true only for pybind11::str.
271271
// However, for Python 2 only (!), the pybind11::str caster
272-
// implicitly decodes bytes to PyUnicodeObject. This is to ease
272+
// implicitly decoded bytes to PyUnicodeObject. This was to ease
273273
// the transition from the legacy behavior to the non-permissive
274274
// behavior.
275275

0 commit comments

Comments
 (0)