Skip to content

Commit e87994d

Browse files
ogriselrgbkrk
authored andcommitted
Simpler fix
1 parent c05c7bd commit e87994d

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

cloudpickle/cloudpickle.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,11 @@ def save_global(self, obj, name=None, pack=struct.pack):
620620
The name of this method is somewhat misleading: all types get
621621
dispatched here.
622622
"""
623+
if obj.__module__ == "__main__":
624+
return self.save_dynamic_class(obj)
625+
623626
try:
624-
if obj.__module__ == "__main__":
625-
raise ValueError("%r is defined in __main__" % obj)
626-
else:
627-
return Pickler.save_global(self, obj, name=name)
627+
return Pickler.save_global(self, obj, name=name)
628628
except Exception:
629629
if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
630630
if obj in _BUILTIN_TYPE_NAMES:

tests/cloudpickle_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,8 @@ class Foo(object):
749749
def method(self, x):
750750
return x
751751
752+
foo = Foo()
753+
752754
def f0(x):
753755
return x ** 2
754756
@@ -761,6 +763,9 @@ def f2(x):
761763
def f3():
762764
return Foo().method(CONSTANT)
763765
766+
def f4(x):
767+
return foo.method(x)
768+
764769
cloned = subprocess_pickle_echo(lambda x: x**2, protocol={protocol})
765770
assert cloned(3) == 9
766771
@@ -781,6 +786,9 @@ def f3():
781786
782787
cloned = subprocess_pickle_echo(f3, protocol={protocol})
783788
assert cloned() == f3()
789+
790+
cloned = subprocess_pickle_echo(f4, protocol={protocol})
791+
assert cloned(2) == f4(2)
784792
""".format(protocol=self.protocol)
785793
assert_run_python_script(textwrap.dedent(code))
786794

0 commit comments

Comments
 (0)