MCPcopy Create free account
hub / github.com/cloudpipe/cloudpickle / _whichmodule

Function _whichmodule

cloudpickle/cloudpickle.py:205–236  ·  view source on GitHub ↗

Find the module an object belongs to. This function differs from ``pickle.whichmodule`` in two ways: - it does not mangle the cases where obj's module is __main__ and obj was not found in any module. - Errors arising during module introspection are ignored, as those errors a

(obj, name)

Source from the content-addressed store, hash-verified

203
204
205def _whichmodule(obj, name):
206 """Find the module an object belongs to.
207
208 This function differs from ``pickle.whichmodule`` in two ways:
209 - it does not mangle the cases where obj's module is __main__ and obj was
210 not found in any module.
211 - Errors arising during module introspection are ignored, as those errors
212 are considered unwanted side effects.
213 """
214 module_name = getattr(obj, "__module__", None)
215
216 if module_name is not None:
217 return module_name
218 # Protect the iteration by using a copy of sys.modules against dynamic
219 # modules that trigger imports of other modules upon calls to getattr or
220 # other threads importing at the same time.
221 for module_name, module in sys.modules.copy().items():
222 # Some modules such as coverage can inject non-module objects inside
223 # sys.modules
224 if (
225 module_name == "__main__"
226 or module_name == "__mp_main__"
227 or module is None
228 or not isinstance(module, types.ModuleType)
229 ):
230 continue
231 try:
232 if _getattribute(module, name) is obj:
233 return module_name
234 except Exception:
235 pass
236 return None
237
238
239def _should_pickle_by_reference(obj, name=None):

Calls 1

_getattributeFunction · 0.85