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

Method _function_getnewargs

cloudpickle/cloudpickle.py:1292–1323  ·  view source on GitHub ↗
(self, func)

Source from the content-addressed store, hash-verified

1290 return self._dynamic_function_reduce(obj)
1291
1292 def _function_getnewargs(self, func):
1293 code = func.__code__
1294
1295 # base_globals represents the future global namespace of func at
1296 # unpickling time. Looking it up and storing it in
1297 # cloudpickle.Pickler.globals_ref allow functions sharing the same
1298 # globals at pickling time to also share them once unpickled, at one
1299 # condition: since globals_ref is an attribute of a cloudpickle.Pickler
1300 # instance, and that a new cloudpickle.Pickler is created each time
1301 # cloudpickle.dump or cloudpickle.dumps is called, functions also need
1302 # to be saved within the same invocation of
1303 # cloudpickle.dump/cloudpickle.dumps (for example:
1304 # cloudpickle.dumps([f1, f2])). There is no such limitation when using
1305 # cloudpickle.Pickler.dump, as long as the multiple invocations are
1306 # bound to the same cloudpickle.Pickler instance.
1307 base_globals = self.globals_ref.setdefault(id(func.__globals__), {})
1308
1309 if base_globals == {}:
1310 # Add module attributes used to resolve relative imports
1311 # instructions inside func.
1312 for k in ["__package__", "__name__", "__path__", "__file__"]:
1313 if k in func.__globals__:
1314 base_globals[k] = func.__globals__[k]
1315
1316 # Do not bind the free variables before the function is created to
1317 # avoid infinite recursion.
1318 if func.__closure__ is None:
1319 closure = None
1320 else:
1321 closure = tuple(_make_empty_cell() for _ in range(len(code.co_freevars)))
1322
1323 return code, base_globals, None, None, closure
1324
1325 def dump(self, obj):
1326 try:

Callers 1

Calls 1

_make_empty_cellFunction · 0.85

Tested by

no test coverage detected