Skip to content

Commit e78ec67

Browse files
committed
Ensure that the new pickle protocol support works with 'local names' (e.g. dotted method names).
1 parent 829fbe4 commit e78ec67

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

typed_python/SerializationContext.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,16 @@ def pickledByStr(module_name: str, name: str) -> None:
4444
This mimics pickle's behavior when given a string from __reduce__. The
4545
string is interpreted as the name of a global variable, and pickle.whichmodules
4646
is used to search the module namespace, generating module_name.
47+
48+
Note that 'name' might contain '.' inside of it, since its a 'local name'.
4749
"""
4850
module = importlib.import_module(module_name)
49-
return getattr(module, name)
51+
52+
instance = module
53+
for subName in name.split('.'):
54+
instance = getattr(instance, subName)
55+
56+
return instance
5057

5158

5259
def createFunctionWithLocalsAndGlobals(code, globals):

typed_python/types_serialization_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import sys
1616
import os
1717
import importlib
18+
from functools import lru_cache
19+
1820
from abc import ABC, abstractmethod, ABCMeta
1921
from typed_python.test_util import callFunctionInFreshProcess
2022
import typed_python.compiler.python_ast_util as python_ast_util
@@ -57,6 +59,13 @@
5759
module_level_testfun = dummy_test_module.testfunction
5860

5961

62+
class GlobalClassWithLruCache:
63+
@staticmethod
64+
@lru_cache(maxsize=None)
65+
def f(x):
66+
return x
67+
68+
6069
def moduleLevelFunctionUsedByExactlyOneSerializationTest():
6170
return "please don't touch me"
6271

@@ -3085,3 +3094,10 @@ def f(self):
30853094
x = callFunctionInFreshProcess(getX, (), showStdout=True)
30863095

30873096
assert x == SerializationContext().deserialize(SerializationContext().serialize(x))
3097+
3098+
def test_serialize_class_static_lru_cache(self):
3099+
s = SerializationContext()
3100+
3101+
assert (
3102+
s.deserialize(s.serialize(GlobalClassWithLruCache.f)) is GlobalClassWithLruCache.f
3103+
)

0 commit comments

Comments
 (0)