Skip to content

Commit

Permalink
Fix #544: Port to Python 3.14
Browse files Browse the repository at this point in the history
* _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.
  • Loading branch information
vstinner committed Dec 4, 2024
1 parent 6220b0c commit 17c40c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
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 @@ -2234,6 +2234,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 @@ -2278,11 +2282,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 17c40c7

Please sign in to comment.