Skip to content

Commit 6493f49

Browse files
rwgkhenryiiiSkylion007
authored
Python 2 removal part 1: tests (C++ code is intentionally ~untouched) (#3688)
* `#error BYE_BYE_GOLDEN_SNAKE` * Removing everything related to 2.7 from ci.yml * Commenting-out Centos7 * Removing `PYTHON: 27` from .appveyor.yml * "PY2" removal, mainly from tests. C++ code is not touched. * Systematic removal of `u` prefix from `u"..."` and `u'...'` literals. Collateral cleanup of a couple minor other things. * Cleaning up around case-insensitive hits for `[^a-z]py.*2` in tests/. * Removing obsolete Python 2 mention in compiling.rst * Proper `#error` for Python 2. * Using PY_VERSION_HEX to guard `#error "PYTHON 2 IS NO LONGER SUPPORTED.` * chore: bump pre-commit * style: run pre-commit for pyupgrade 3+ * tests: use sys.version_info, not PY * chore: more Python 2 removal * Uncommenting Centos7 block (PR #3691 showed that it is working again). * Update pre-commit hooks * Fix pre-commit hook * refactor: remove Python 2 from CMake * refactor: remove Python 2 from setup code * refactor: simplify, better static typing * feat: fail with nice messages * refactor: drop Python 2 C++ code * docs: cleanup for Python 3 * revert: intree revert: intree * docs: minor touchup to py2 statement Co-authored-by: Henry Schreiner <[email protected]> Co-authored-by: Aaron Gokaslan <[email protected]>
1 parent 46dcd9b commit 6493f49

File tree

102 files changed

+530
-1335
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+530
-1335
lines changed

.appveyor.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ environment:
1111
matrix:
1212
- PYTHON: 36
1313
CONFIG: Debug
14-
- PYTHON: 27
15-
CONFIG: Debug
1614
install:
1715
- ps: |
1816
$env:CMAKE_GENERATOR = "Visual Studio 14 2015"

.github/workflows/ci.yml

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ jobs:
2525
matrix:
2626
runs-on: [ubuntu-latest, windows-2022, macos-latest]
2727
python:
28-
- '2.7'
2928
- '3.5'
3029
- '3.6'
3130
- '3.9'
@@ -49,13 +48,9 @@ jobs:
4948
python: '3.6'
5049
args: >
5150
-DPYBIND11_FINDPYTHON=ON
52-
- runs-on: macos-latest
53-
python: 'pypy-2.7'
5451
# Inject a couple Windows 2019 runs
5552
- runs-on: windows-2019
5653
python: '3.9'
57-
- runs-on: windows-2019
58-
python: '2.7'
5954

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

171-
# Eventually Microsoft might have an action for setting up
172-
# MSVC, but for now, this action works:
173-
- name: Prepare compiler environment for Windows 🐍 2.7
174-
if: matrix.python == 2.7 && runner.os == 'Windows'
175-
uses: ilammy/[email protected]
176-
with:
177-
arch: x64
178-
179-
# This makes two environment variables available in the following step(s)
180-
- name: Set Windows 🐍 2.7 environment variables
181-
if: matrix.python == 2.7 && runner.os == 'Windows'
182-
shell: bash
183-
run: |
184-
echo "DISTUTILS_USE_SDK=1" >> $GITHUB_ENV
185-
echo "MSSdk=1" >> $GITHUB_ENV
186-
187166
# This makes sure the setup_helpers module can build packages using
188167
# setuptools
189168
- name: Setuptools helpers test
@@ -784,7 +763,6 @@ jobs:
784763
fail-fast: false
785764
matrix:
786765
python:
787-
- 2.7
788766
- 3.6
789767
- 3.7
790768
# todo: check/cpptest does not support 3.8+ yet
@@ -832,17 +810,12 @@ jobs:
832810
fail-fast: false
833811
matrix:
834812
python:
835-
- 2.7
836813
- 3.5
837814
- 3.7
838815
std:
839816
- 14
840817

841818
include:
842-
- python: 2.7
843-
std: 17
844-
args: >
845-
-DCMAKE_CXX_FLAGS="/permissive- /EHsc /GR"
846819
- python: 3.7
847820
std: 17
848821
args: >

.github/workflows/pip.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ env:
1717

1818
jobs:
1919
# This builds the sdists and wheels and makes sure the files are exactly as
20-
# expected. Using Windows and Python 2.7, since that is often the most
20+
# expected. Using Windows and Python 3.6, since that is often the most
2121
# challenging matrix element.
2222
test-packaging:
23-
name: 🐍 2.7 • 📦 tests • windows-latest
23+
name: 🐍 3.6 • 📦 tests • windows-latest
2424
runs-on: windows-latest
2525

2626
steps:
2727
- uses: actions/checkout@v2
2828

29-
- name: Setup 🐍 2.7
29+
- name: Setup 🐍 3.6
3030
uses: actions/setup-python@v2
3131
with:
32-
python-version: 2.7
32+
python-version: 3.6
3333

3434
- name: Prepare env
3535
run: |

.pre-commit-config.yaml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,37 @@ repos:
2929
- id: mixed-line-ending
3030
- id: requirements-txt-fixer
3131
- id: trailing-whitespace
32-
- id: fix-encoding-pragma
33-
exclude: ^noxfile.py$
3432

33+
# Upgrade old Python syntax
3534
- repo: https://github.com/asottile/pyupgrade
3635
rev: v2.31.0
3736
hooks:
3837
- id: pyupgrade
38+
args: [--py3-plus]
3939

40+
# Nicely sort includes
4041
- repo: https://github.com/PyCQA/isort
4142
rev: 5.10.1
4243
hooks:
4344
- id: isort
4445

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

52+
# Also code format the docs
5153
- repo: https://github.com/asottile/blacken-docs
52-
rev: v1.12.0
54+
rev: v1.12.1
5355
hooks:
5456
- id: blacken-docs
5557
additional_dependencies:
56-
- black==21.12b0 # keep in sync with black hook
58+
- black==22.1.0 # keep in sync with black hook
5759

5860
# Changes tabs to spaces
5961
- repo: https://github.com/Lucas-C/pre-commit-hooks
60-
rev: v1.1.10
62+
rev: v1.1.11
6163
hooks:
6264
- id: remove-tabs
6365

@@ -67,12 +69,15 @@ repos:
6769
hooks:
6870
- id: pycln
6971

72+
# Checking for common mistakes
7073
- repo: https://github.com/pre-commit/pygrep-hooks
7174
rev: v1.9.0
7275
hooks:
7376
- id: python-check-blanket-noqa
7477
- id: python-check-blanket-type-ignore
7578
- id: python-no-log-warn
79+
# Python 3.6
80+
# - id: python-use-type-annotations
7681
- id: rst-backticks
7782
- id: rst-directive-colons
7883
- id: rst-inline-touching-normal
@@ -87,6 +92,7 @@ repos:
8792
- pep8-naming
8893
exclude: ^(docs/.*|tools/.*)$
8994

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

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

129+
# Check for spelling
123130
- repo: https://github.com/codespell-project/codespell
124131
rev: v2.1.0
125132
hooks:
126133
- id: codespell
127134
exclude: ".supp$"
128135
args: ["-L", "nd,ot,thist"]
129136

137+
# Check for common shell mistakes
130138
- repo: https://github.com/shellcheck-py/shellcheck-py
131-
rev: v0.8.0.3
139+
rev: v0.8.0.4
132140
hooks:
133141
- id: shellcheck
134142

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

152+
# Clang format the codebase automatically
144153
- repo: https://github.com/pre-commit/mirrors-clang-format
145154
rev: "v13.0.0"
146155
hooks:

MANIFEST.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
recursive-include pybind11/include/pybind11 *.h
22
recursive-include pybind11 *.py
33
recursive-include pybind11 py.typed
4-
recursive-include pybind11 *.pyi
54
include pybind11/share/cmake/pybind11/*.cmake
65
include LICENSE README.rst pyproject.toml setup.py setup.cfg

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dependency.
3434
Think of this library as a tiny self-contained version of Boost.Python
3535
with everything stripped away that isn’t relevant for binding
3636
generation. Without comments, the core header files only require ~4K
37-
lines of code and depend on Python (2.7 or 3.5+, or PyPy) and the C++
37+
lines of code and depend on Python (3.5+, or PyPy) and the C++
3838
standard library. This compact implementation was possible thanks to
3939
some of the new C++11 language features (specifically: tuples, lambda
4040
functions and variadic templates). Since its creation, this library has
@@ -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 implementation-agnostic
82+
interface (pybind11 2.9 was the last version to support 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/Doxyfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ ALIASES += "endrst=\endverbatim"
1818
QUIET = YES
1919
WARNINGS = YES
2020
WARN_IF_UNDOCUMENTED = NO
21-
PREDEFINED = PY_MAJOR_VERSION=3 \
22-
PYBIND11_NOINLINE
21+
PREDEFINED = PYBIND11_NOINLINE

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: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ a virtual method call.
133133
>>> from example import *
134134
>>> d = Dog()
135135
>>> call_go(d)
136-
u'woof! woof! woof! '
136+
'woof! woof! woof! '
137137
>>> class Cat(Animal):
138138
... def go(self, n_times):
139139
... return "meow! " * n_times
140140
...
141141
>>> c = Cat()
142142
>>> call_go(c)
143-
u'meow! meow! meow! '
143+
'meow! meow! meow! '
144144
145145
If you are defining a custom constructor in a derived Python class, you *must*
146146
ensure that you explicitly call the bound C++ constructor using ``__init__``,
@@ -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

0 commit comments

Comments
 (0)