MCPcopy Index your code
hub / github.com/RustPython/RustPython / whichmodule

Function whichmodule

Lib/pickle.py:320–354  ·  view source on GitHub ↗

Find the module an object belong to.

(obj, name)

Source from the content-addressed store, hash-verified

318 return obj
319
320def whichmodule(obj, name):
321 """Find the module an object belong to."""
322 dotted_path = name.split('.')
323 module_name = getattr(obj, '__module__', None)
324 if '<locals>' in dotted_path:
325 raise PicklingError(f"Can't pickle local object {obj!r}")
326 if module_name is None:
327 # Protect the iteration by using a list copy of sys.modules against dynamic
328 # modules that trigger imports of other modules upon calls to getattr.
329 for module_name, module in sys.modules.copy().items():
330 if (module_name == '__main__'
331 or module_name == '__mp_main__' # bpo-42406
332 or module is None):
333 continue
334 try:
335 if _getattribute(module, dotted_path) is obj:
336 return module_name
337 except AttributeError:
338 pass
339 module_name = '__main__'
340
341 try:
342 __import__(module_name, level=0)
343 module = sys.modules[module_name]
344 except (ImportError, ValueError, KeyError) as exc:
345 raise PicklingError(f"Can't pickle {obj!r}: {exc!s}")
346 try:
347 if _getattribute(module, dotted_path) is obj:
348 return module_name
349 except AttributeError:
350 raise PicklingError(f"Can't pickle {obj!r}: "
351 f"it's not found as {module_name}.{name}")
352
353 raise PicklingError(
354 f"Can't pickle {obj!r}: it's not the same object as {module_name}.{name}")
355
356def encode_long(x):
357 r"""Encode a long to a two&#x27;s complement little-endian binary string.

Callers 1

save_globalMethod · 0.85

Calls 7

getattrFunction · 0.85
PicklingErrorClass · 0.85
_getattributeFunction · 0.85
__import__Function · 0.50
splitMethod · 0.45
itemsMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected