summaryrefslogtreecommitdiffstats
path: root/Lib/pickle.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/pickle.py')
-rw-r--r--Lib/pickle.py9
1 files changed, 6 insertions, 3 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index ec19e24..e365bd1 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -787,9 +787,8 @@ class Pickler:
if name is None:
name = obj.__name__
- try:
- module = obj.__module__
- except AttributeError:
+ module = getattr(obj, "__module__", None)
+ if module is None:
module = whichmodule(obj, name)
try:
@@ -876,6 +875,10 @@ def whichmodule(func, funcname):
Return a module name.
If the function cannot be found, return "__main__".
"""
+ # Python functions should always get an __module__ from their globals.
+ mod = getattr(func, "__module__", None)
+ if mod is not None:
+ return mod
if func in classmap:
return classmap[func]