MCPcopy Index your code
hub / github.com/cloudpipe/cloudpickle / _extract_code_globals

Function _extract_code_globals

cloudpickle/cloudpickle.py:313–335  ·  view source on GitHub ↗

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

(co)

Source from the content-addressed store, hash-verified

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…