Skip to content

Python 2 removal #3688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d5321df
`#error BYE_BYE_GOLDEN_SNAKE`
rwgk Feb 5, 2022
0fd580f
Removing everything related to 2.7 from ci.yml
rwgk Feb 5, 2022
474f977
Commenting-out Centos7
rwgk Feb 5, 2022
a574f10
Removing `PYTHON: 27` from .appveyor.yml
rwgk Feb 5, 2022
29739f6
"PY2" removal, mainly from tests. C++ code is not touched.
rwgk Feb 5, 2022
b3251d0
Systematic removal of `u` prefix from `u"..."` and `u'...'` literals.…
rwgk Feb 5, 2022
e1237f9
Cleaning up around case-insensitive hits for `[^a-z]py.*2` in tests/.
rwgk Feb 5, 2022
209da4f
Removing obsolete Python 2 mention in compiling.rst
rwgk Feb 5, 2022
a59b497
Proper `#error` for Python 2.
rwgk Feb 5, 2022
bf48589
Using PY_VERSION_HEX to guard `#error "PYTHON 2 IS NO LONGER SUPPORTED.`
rwgk Feb 6, 2022
3a8278c
chore: bump pre-commit
henryiii Feb 6, 2022
0d13993
style: run pre-commit for pyupgrade 3+
henryiii Feb 6, 2022
421ee5e
tests: use sys.version_info, not PY
henryiii Feb 6, 2022
91b1203
chore: more Python 2 removal
henryiii Feb 6, 2022
4898aef
Uncommenting Centos7 block (PR #3691 showed that it is working again).
rwgk Feb 6, 2022
d50fc8a
Update pre-commit hooks
Skylion007 Feb 6, 2022
77c9f8d
Fix pre-commit hook
Skylion007 Feb 6, 2022
319b9f3
refactor: remove Python 2 from CMake
henryiii Feb 7, 2022
98d92e3
refactor: remove Python 2 from setup code
henryiii Feb 10, 2022
61abe9b
refactor: simplify, better static typing
henryiii Feb 10, 2022
bfe715d
feat: fail with nice messages
henryiii Feb 10, 2022
cf5a439
refactor: drop Python 2 C++ code
henryiii Feb 10, 2022
04b60dd
docs: cleanup for Python 3
henryiii Feb 10, 2022
f5ff6f1
revert: intree
henryiii Feb 11, 2022
f87bff3
docs: minor touchup to py2 statement
henryiii Feb 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ environment:
matrix:
- PYTHON: 36
CONFIG: Debug
- PYTHON: 27
CONFIG: Debug
install:
- ps: |
$env:CMAKE_GENERATOR = "Visual Studio 14 2015"
Expand Down
27 changes: 0 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ jobs:
matrix:
runs-on: [ubuntu-latest, windows-2022, macos-latest]
python:
- '2.7'
- '3.5'
- '3.6'
- '3.9'
Expand All @@ -49,13 +48,9 @@ jobs:
python: '3.6'
args: >
-DPYBIND11_FINDPYTHON=ON
- runs-on: macos-latest
python: 'pypy-2.7'
# Inject a couple Windows 2019 runs
- runs-on: windows-2019
python: '3.9'
- runs-on: windows-2019
python: '2.7'

name: "🐍 ${{ matrix.python }} • ${{ matrix.runs-on }} • x64 ${{ matrix.args }}"
runs-on: ${{ matrix.runs-on }}
Expand Down Expand Up @@ -168,22 +163,6 @@ jobs:
- name: Interface test
run: cmake --build build2 --target test_cmake_build

# Eventually Microsoft might have an action for setting up
# MSVC, but for now, this action works:
- name: Prepare compiler environment for Windows 🐍 2.7
if: matrix.python == 2.7 && runner.os == 'Windows'
uses: ilammy/msvc-dev-cmd@v1.10.0
with:
arch: x64

# This makes two environment variables available in the following step(s)
- name: Set Windows 🐍 2.7 environment variables
if: matrix.python == 2.7 && runner.os == 'Windows'
shell: bash
run: |
echo "DISTUTILS_USE_SDK=1" >> $GITHUB_ENV
echo "MSSdk=1" >> $GITHUB_ENV

# This makes sure the setup_helpers module can build packages using
# setuptools
- name: Setuptools helpers test
Expand Down Expand Up @@ -784,7 +763,6 @@ jobs:
fail-fast: false
matrix:
python:
- 2.7
- 3.6
- 3.7
# todo: check/cpptest does not support 3.8+ yet
Expand Down Expand Up @@ -832,17 +810,12 @@ jobs:
fail-fast: false
matrix:
python:
- 2.7
- 3.5
- 3.7
std:
- 14

include:
- python: 2.7
std: 17
args: >
-DCMAKE_CXX_FLAGS="/permissive- /EHsc /GR"
- python: 3.7
std: 17
args: >
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ env:

jobs:
# 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 3.6, since that is often the most
# challenging matrix element.
test-packaging:
name: 🐍 2.7 • 📦 tests • windows-latest
name: 🐍 3.6 • 📦 tests • windows-latest
runs-on: windows-latest

steps:
- uses: actions/checkout@v2

- name: Setup 🐍 2.7
- name: Setup 🐍 3.6
uses: actions/setup-python@v2
with:
python-version: 2.7
python-version: 3.6

- name: Prepare env
run: |
Expand Down
33 changes: 21 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,37 @@ repos:
- id: mixed-line-ending
- id: requirements-txt-fixer
- id: trailing-whitespace
- id: fix-encoding-pragma
exclude: ^noxfile.py$

# Upgrade old Python syntax
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.0
hooks:
- id: pyupgrade
args: [--py3-plus]

# Nicely sort includes
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort

# Black, the code formatter, natively supports pre-commit
- repo: https://github.com/psf/black
rev: 21.12b0 # Keep in sync with blacken-docs
rev: 22.1.0 # Keep in sync with blacken-docs
hooks:
- id: black

# Also code format the docs
- repo: https://github.com/asottile/blacken-docs
rev: v1.12.0
rev: v1.12.1
hooks:
- id: blacken-docs
additional_dependencies:
- black==21.12b0 # keep in sync with black hook
- black==22.1.0 # keep in sync with black hook

# Changes tabs to spaces
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.1.10
rev: v1.1.11
hooks:
- id: remove-tabs

Expand All @@ -67,12 +69,15 @@ repos:
hooks:
- id: pycln

# Checking for common mistakes
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
hooks:
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
- id: python-no-log-warn
# Python 3.6
# - id: python-use-type-annotations
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal
Expand All @@ -87,6 +92,7 @@ repos:
- pep8-naming
exclude: ^(docs/.*|tools/.*)$

# Automatically remove noqa that are not used
- repo: https://github.com/asottile/yesqa
rev: v1.3.0
hooks:
Expand All @@ -107,9 +113,9 @@ repos:
rev: v0.931
hooks:
- id: mypy
# Running per-file misbehaves a bit, so just run on all files, it's fast
pass_filenames: false
additional_dependencies: [typed_ast]
args: [--show-error-codes]
exclude: ^(tests|docs)/
additional_dependencies: [nox, rich]

# Checks the manifest for missing files (native support)
- repo: https://github.com/mgedmin/check-manifest
Expand All @@ -120,27 +126,30 @@ repos:
stages: [manual]
additional_dependencies: [cmake, ninja]

# Check for spelling
- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
hooks:
- id: codespell
exclude: ".supp$"
args: ["-L", "nd,ot,thist"]

# Check for common shell mistakes
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.8.0.3
rev: v0.8.0.4
hooks:
- id: shellcheck

# The original pybind11 checks for a few C++ style items
# Disallow some common capitalization mistakes
- repo: local
hooks:
- id: disallow-caps
name: Disallow improper capitalization
language: pygrep
entry: PyBind|Numpy|Cmake|CCache|PyTest
exclude: .pre-commit-config.yaml
exclude: ^\.pre-commit-config.yaml$

# Clang format the codebase automatically
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: "v13.0.0"
hooks:
Expand Down
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
recursive-include pybind11/include/pybind11 *.h
recursive-include pybind11 *.py
recursive-include pybind11 py.typed
recursive-include pybind11 *.pyi
include pybind11/share/cmake/pybind11/*.cmake
include LICENSE README.rst pyproject.toml setup.py setup.cfg
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependency.
Think of this library as a tiny self-contained version of Boost.Python
with everything stripped away that isn’t relevant for binding
generation. Without comments, the core header files only require ~4K
lines of code and depend on Python (2.7 or 3.5+, or PyPy) and the C++
lines of code and depend on Python (3.5+, or PyPy) and the C++
standard library. This compact implementation was possible thanks to
some of the new C++11 language features (specifically: tuples, lambda
functions and variadic templates). Since its creation, this library has
Expand Down Expand Up @@ -78,8 +78,8 @@ Goodies
In addition to the core functionality, pybind11 provides some extra
goodies:

- Python 2.7, 3.5+, and PyPy/PyPy3 7.3 are supported with an
implementation-agnostic interface.
- Python 3.5+, and PyPy3 7.3 are supported with an implementation-agnostic
interface (pybind11 2.9 was the last version to support Python 2).

- It is possible to bind C++11 lambda functions with captured
variables. The lambda capture data is stored inside the resulting
Expand Down
3 changes: 1 addition & 2 deletions docs/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ ALIASES += "endrst=\endverbatim"
QUIET = YES
WARNINGS = YES
WARN_IF_UNDOCUMENTED = NO
PREDEFINED = PY_MAJOR_VERSION=3 \
PYBIND11_NOINLINE
PREDEFINED = PYBIND11_NOINLINE
19 changes: 3 additions & 16 deletions docs/advanced/cast/strings.rst
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
Strings, bytes and Unicode conversions
######################################

.. note::

This section discusses string handling in terms of Python 3 strings. For
Python 2.7, replace all occurrences of ``str`` with ``unicode`` and
``bytes`` with ``str``. Python 2.7 users may find it best to use ``from
__future__ import unicode_literals`` to avoid unintentionally using ``str``
instead of ``unicode``.

Passing Python strings to C++
=============================

Expand Down Expand Up @@ -58,9 +50,9 @@ Passing bytes to C++
--------------------

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


Returning C++ strings to Python
Expand Down Expand Up @@ -204,11 +196,6 @@ decoded to Python ``str``.
}
);

.. warning::

Wide character strings may not work as described on Python 2.7 or Python
3.3 compiled with ``--enable-unicode=ucs2``.

Strings in multibyte encodings such as Shift-JIS must transcoded to a
UTF-8/16/32 before being returned to Python.

Expand Down
33 changes: 13 additions & 20 deletions docs/advanced/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ a virtual method call.
>>> from example import *
>>> d = Dog()
>>> call_go(d)
u'woof! woof! woof! '
'woof! woof! woof! '
>>> class Cat(Animal):
... def go(self, n_times):
... return "meow! " * n_times
...
>>> c = Cat()
>>> call_go(c)
u'meow! meow! meow! '
'meow! meow! meow! '

If you are defining a custom constructor in a derived Python class, you *must*
ensure that you explicitly call the bound C++ constructor using ``__init__``,
Expand Down Expand Up @@ -813,26 +813,21 @@ An instance can now be pickled as follows:

.. code-block:: python

try:
import cPickle as pickle # Use cPickle on Python 2.7
except ImportError:
import pickle
import pickle

p = Pickleable("test_value")
p.setExtra(15)
data = pickle.dumps(p, 2)
data = pickle.dumps(p)


.. note::
Note that only the cPickle module is supported on Python 2.7.

The second argument to ``dumps`` is also crucial: it selects the pickle
protocol version 2, since the older version 1 is not supported. Newer
versions are also fine—for instance, specify ``-1`` to always use the
latest available version. Beware: failure to follow these instructions
will cause important pybind11 memory allocation routines to be skipped
during unpickling, which will likely lead to memory corruption and/or
segmentation faults.
If given, the second argument to ``dumps`` must be 2 or larger - 0 and 1 are
not supported. Newer versions are also fine; for instance, specify ``-1`` to
always use the latest available version. Beware: failure to follow these
instructions will cause important pybind11 memory allocation routines to be
skipped during unpickling, which will likely lead to memory corruption
and/or segmentation faults. Python defaults to version 3 (Python 3-3.7) and
version 4 for Python 3.8+.

.. seealso::

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

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

For simple classes (deep)copy can be enabled by using the copy constructor,
which should look as follows:
Expand Down
6 changes: 3 additions & 3 deletions docs/advanced/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ an invalid state.
Chaining exceptions ('raise from')
==================================

In Python 3.3 a mechanism for indicating that exceptions were caused by other
exceptions was introduced:
Python has a mechanism for indicating that exceptions were caused by other
exceptions:

.. code-block:: py

Expand All @@ -340,7 +340,7 @@ exceptions was introduced:

To do a similar thing in pybind11, you can use the ``py::raise_from`` function. It
sets the current python error indicator, so to continue propagating the exception
you should ``throw py::error_already_set()`` (Python 3 only).
you should ``throw py::error_already_set()``.

.. code-block:: cpp

Expand Down
Loading