Skip to content

Commit f758eb3

Browse files
Fix compatibility with Python 3.11 (#467)
Code objects have some new attributes. Those are related to the enhanced exceptions with code highlighting. CI job for Python 3.11 is no longer optional.
1 parent 9a0013e commit f758eb3

File tree

5 files changed

+29
-68
lines changed

5 files changed

+29
-68
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
strategy:
3030
matrix:
3131
os: [ubuntu-latest, windows-latest, macos-latest]
32-
python_version: [3.6, 3.7, 3.8, 3.9, "3.10-dev", "pypy3"]
32+
python_version: [3.6, 3.7, 3.8, 3.9, "3.10", "3.11-dev", "pypy3"]
3333
exclude:
3434
# Do not test all minor versions on all platforms, especially if they
3535
# are not the oldest/newest supported versions
@@ -94,48 +94,6 @@ jobs:
9494
token: ${{ secrets.CODECOV_TOKEN }}
9595
file: ./coverage.xml
9696

97-
python-nightly:
98-
runs-on: ubuntu-18.04
99-
# This entry is made optional for now, see https://github.com/cloudpipe/cloudpickle/pull/420
100-
if: "contains(github.event.pull_request.labels.*.name, 'ci python-nightly')"
101-
steps:
102-
- uses: actions/checkout@v1
103-
- name: Install Python from ppa:deadsnakes/nightly
104-
run: |
105-
sudo add-apt-repository ppa:deadsnakes/nightly
106-
sudo apt update
107-
sudo apt install python3.11 python3.11-venv python3.11-dev
108-
python3.11 -m venv nightly-venv
109-
echo "$PWD/nightly-venv/bin" >> $GITHUB_PATH
110-
- name: Display Python version
111-
run: python -c "import sys; print(sys.version)"
112-
- name: Install project and dependencies
113-
run: |
114-
set -e
115-
python -m pip install --upgrade pip
116-
python -m pip install -r dev-requirements.txt
117-
python -m pip install -e .
118-
python ci/install_coverage_subprocess_pth.py
119-
- name: Generate old pickles (backward compat)
120-
shell: bash
121-
run: |
122-
git_head=$(git rev-parse HEAD)
123-
cp tests/generate_old_pickles.py tests/_generate_old_pickles.py
124-
git checkout v1.4.1
125-
python tests/_generate_old_pickles.py
126-
git checkout ${git_head}
127-
- name: Test with pytest
128-
run: |
129-
COVERAGE_PROCESS_START=$GITHUB_WORKSPACE/.coveragerc \
130-
PYTHONPATH='.:tests' python -m pytest -r s
131-
coverage combine --append
132-
coverage xml -i
133-
- name: Publish coverage results
134-
uses: codecov/codecov-action@v1
135-
with:
136-
token: ${{ secrets.CODECOV_TOKEN }}
137-
file: ./coverage.xml
138-
13997
distributed-downstream-build:
14098
runs-on: ubuntu-latest
14199
if: "contains(github.event.pull_request.labels.*.name, 'ci distributed') || contains(github.event.pull_request.labels.*.name, 'ci downstream')"

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
- Support for pickling subclasses of generic classes.
99
([PR #448](https://github.com/cloudpipe/cloudpickle/pull/448))
1010

11+
- Support and CI configuration for Python 3.11.
12+
([PR #467](https://github.com/cloudpipe/cloudpickle/pull/467))
13+
1114
2.0.0
1215
=====
1316

cloudpickle/cloudpickle_fast.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,23 @@ def _enum_getstate(obj):
244244

245245
def _code_reduce(obj):
246246
"""codeobject reducer"""
247-
if hasattr(obj, "co_linetable"): # pragma: no branch
247+
# If you are not sure about the order of arguments, take a look at help
248+
# of the specific type from types, for example:
249+
# >>> from types import CodeType
250+
# >>> help(CodeType)
251+
if hasattr(obj, "co_columntable"): # pragma: no branch
252+
# Python 3.11 and later: there are some new attributes
253+
# related to the enhanced exceptions.
254+
args = (
255+
obj.co_argcount, obj.co_posonlyargcount,
256+
obj.co_kwonlyargcount, obj.co_nlocals, obj.co_stacksize,
257+
obj.co_flags, obj.co_code, obj.co_consts, obj.co_names,
258+
obj.co_varnames, obj.co_filename, obj.co_name, obj.co_qualname,
259+
obj.co_firstlineno, obj.co_linetable, obj.co_endlinetable,
260+
obj.co_columntable, obj.co_exceptiontable, obj.co_freevars,
261+
obj.co_cellvars,
262+
)
263+
elif hasattr(obj, "co_linetable"): # pragma: no branch
248264
# Python 3.10 and later: obj.co_lnotab is deprecated and constructor
249265
# expects obj.co_linetable instead.
250266
args = (

tests/cloudpickle_test.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,29 +2386,13 @@ def method(self, arg: type_) -> type_:
23862386

23872387
def check_annotations(obj, expected_type, expected_type_str):
23882388
assert obj.__annotations__["attribute"] == expected_type
2389-
if sys.version_info >= (3, 11):
2390-
# In Python 3.11, type annotations are stored as strings.
2391-
# See PEP 563 for more details:
2392-
# https://www.python.org/dev/peps/pep-0563/
2393-
# Originaly scheduled for 3.10, then postponed.
2394-
# See this for more details:
2395-
# https://mail.python.org/archives/list/[email protected]/thread/CLVXXPQ2T2LQ5MP2Y53VVQFCXYWQJHKZ/
2396-
assert (
2397-
obj.method.__annotations__["arg"]
2398-
== expected_type_str
2399-
)
2400-
assert (
2401-
obj.method.__annotations__["return"]
2402-
== expected_type_str
2403-
)
2404-
else:
2405-
assert (
2406-
obj.method.__annotations__["arg"] == expected_type
2407-
)
2408-
assert (
2409-
obj.method.__annotations__["return"]
2410-
== expected_type
2411-
)
2389+
assert (
2390+
obj.method.__annotations__["arg"] == expected_type
2391+
)
2392+
assert (
2393+
obj.method.__annotations__["return"]
2394+
== expected_type
2395+
)
24122396
return "ok"
24132397

24142398
obj = MyClass()

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py35, py36, py37, py38, py39, py310, pypy3
2+
envlist = py35, py36, py37, py38, py39, py310, py311, pypy3
33

44
[testenv]
55
deps = -rdev-requirements.txt

0 commit comments

Comments
 (0)