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

Function _extract_code_globals

cloudpickle/cloudpickle.py:315–337  ·  view source on GitHub ↗

Find all globals names read or written to by codeblock co.

(co)

Source from the content-addressed store, hash-verified

313
314
315def _extract_code_globals(co):
316 """Find all globals names read or written to by codeblock co."""
317 out_names = _extract_code_globals_cache.get(co)
318 if out_names is None:
319 # We use a dict with None values instead of a set to get a
320 # deterministic order and avoid introducing non-deterministic pickle
321 # bytes as a results.
322 out_names = {name: None for name in _walk_global_ops(co)}
323
324 # Declaring a function inside another one using the "def ..." syntax
325 # generates a constant code object corresponding to the one of the
326 # nested function's As the nested function may itself need global
327 # variables, we need to introspect its code, extract its globals, (look
328 # for code object in it's co_consts attribute..) and add the result to
329 # code_globals
330 if co.co_consts:
331 for const in co.co_consts:
332 if isinstance(const, types.CodeType):
333 out_names.update(_extract_code_globals(const))
334
335 _extract_code_globals_cache[co] = out_names
336
337 return out_names
338
339
340def _find_imported_submodules(code, top_level_dependencies):

Callers 1

_function_getstateFunction · 0.85

Calls 1

_walk_global_opsFunction · 0.85

Tested by

no test coverage detected