diff options
author | Jeremy Hylton <jeremy@alum.mit.edu> | 2003-01-31 18:33:18 (GMT) |
---|---|---|
committer | Jeremy Hylton <jeremy@alum.mit.edu> | 2003-01-31 18:33:18 (GMT) |
commit | 4f0dcc9a9a4629607d4fff79912e8d0c86cd3914 (patch) | |
tree | bba33e033f6a51fec1c01a79a5f6e931f65e44f2 /Lib | |
parent | 8f24cdc0d5e30e2f924ed2e8a71400fa87a70983 (diff) | |
download | cpython-4f0dcc9a9a4629607d4fff79912e8d0c86cd3914.zip cpython-4f0dcc9a9a4629607d4fff79912e8d0c86cd3914.tar.gz cpython-4f0dcc9a9a4629607d4fff79912e8d0c86cd3914.tar.bz2 |
Provide __module__ attributes for functions defined in C and Python.
__module__ is the string name of the module the function was defined
in, just like __module__ of classes. In some cases, particularly for
C functions, the __module__ may be None.
Change PyCFunction_New() from a function to a macro, but keep an
unused copy of the function around so that we don't change the binary
API.
Change pickle's save_global() to use whichmodule() if __module__ is
None, but add the __module__ logic to whichmodule() since it might be
used outside of pickle.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/pickle.py | 9 | ||||
-rw-r--r-- | Lib/test/test_funcattrs.py | 4 |
2 files changed, 10 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] diff --git a/Lib/test/test_funcattrs.py b/Lib/test/test_funcattrs.py index 3dc888c..2c31d36 100644 --- a/Lib/test/test_funcattrs.py +++ b/Lib/test/test_funcattrs.py @@ -9,6 +9,10 @@ def b(): 'my docstring' pass +# __module__ is a special attribute +verify(b.__module__ == __name__) +verify(verify.__module__ == "test.test_support") + # setting attributes on functions try: b.publish |