Skip to content

Commit

Permalink
Fix #544: Port to Python 3.14 (#545)
Browse files Browse the repository at this point in the history
* Fix #544: Port to Python 3.14

* _getattribute() API changed in Python 3.14.
* itertools.count() no longer supports pickle on Python 3.14:
  https://docs.python.org/dev/whatsnew/3.14.html#itertools
* Fix annotations test.

---------

Co-authored-by: Olivier Grisel <[email protected]>
  • Loading branch information
vstinner and ogrisel authored Jan 14, 2025
1 parent caf55df commit 7468d72
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python_version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "pypy-3.9"]
python_version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14-dev", "pypy-3.9"]
exclude:
# Do not test all minor versions on all platforms, especially if they
# are not the oldest/newest supported versions
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
In development
==============

- Various fixes to support for Python 3.14 ([PR #545](
https://github.com/cloudpipe/cloudpickle/pull/545)).

3.1.0
=====
Expand Down
14 changes: 11 additions & 3 deletions cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
import logging
import opcode
import pickle
from pickle import _getattribute
from pickle import _getattribute as _pickle_getattribute
import platform
import struct
import sys
Expand Down Expand Up @@ -192,6 +192,14 @@ def _is_registered_pickle_by_value(module):
return False


if sys.version_info >= (3, 14):
def _getattribute(obj, name):
return _pickle_getattribute(obj, name.split('.'))
else:
def _getattribute(obj, name):
return _pickle_getattribute(obj, name)[0]


def _whichmodule(obj, name):
"""Find the module an object belongs to.
Expand Down Expand Up @@ -219,7 +227,7 @@ def _whichmodule(obj, name):
):
continue
try:
if _getattribute(module, name)[0] is obj:
if _getattribute(module, name) is obj:
return module_name
except Exception:
pass
Expand Down Expand Up @@ -293,7 +301,7 @@ def _lookup_module_and_qualname(obj, name=None):
return None

try:
obj2, parent = _getattribute(module, name)
obj2 = _getattribute(module, name)
except AttributeError:
# obj was not found inside the module it points to
return None
Expand Down
8 changes: 5 additions & 3 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,10 @@ def test_unhashable_function(self):
self.assertEqual(depickled_method("a"), 1)
self.assertEqual(depickled_method("b"), None)

@unittest.skipIf(
sys.version_info >= (3, 14),
"itertools.count() doesn't support pickle on Python 3.14+",
)
def test_itertools_count(self):
counter = itertools.count(1, step=2)

Expand Down Expand Up @@ -2279,11 +2283,9 @@ def g():
self.assertEqual(f2.__doc__, f.__doc__)

def test_wraps_preserves_function_annotations(self):
def f(x):
def f(x: int) -> float:
pass

f.__annotations__ = {"x": 1, "return": float}

@wraps(f)
def g(x):
f(x)
Expand Down

0 comments on commit 7468d72

Please sign in to comment.