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

Function _code_reduce

cloudpickle/cloudpickle.py:834–949  ·  view source on GitHub ↗

code object reducer.

(obj)

Source from the content-addressed store, hash-verified

832
833
834def _code_reduce(obj):
835 """code object reducer."""
836 # If you are not sure about the order of arguments, take a look at help
837 # of the specific type from types, for example:
838 # >>> from types import CodeType
839 # >>> help(CodeType)
840
841 # Hack to circumvent non-predictable memoization caused by string interning.
842 # See the inline comment in _class_setstate for details.
843 co_name = "".join(obj.co_name)
844
845 # co_filename is not used in the constructor of code objects, so we can
846 # safely set it to indicate that this is dynamic code. This also makes
847 # the payload deterministic, independent of where the function is defined
848 # which is especially useful when defining classes in jupyter/ipython
849 # cells which do not have a deterministic filename.
850 co_filename = "".join("<dynamic-code>")
851
852 # Create shallow copies of these tuple to make cloudpickle payload deterministic.
853 # When creating a code object during load, copies of these four tuples are
854 # created, while in the main process, these tuples can be shared.
855 # By always creating copies, we make sure the resulting payload is deterministic.
856 co_names = tuple(name for name in obj.co_names)
857 co_varnames = tuple(name for name in obj.co_varnames)
858 co_freevars = tuple(name for name in obj.co_freevars)
859 co_cellvars = tuple(name for name in obj.co_cellvars)
860 if hasattr(obj, "co_exceptiontable"):
861 # Python 3.11 and later: there are some new attributes
862 # related to the enhanced exceptions.
863 args = (
864 obj.co_argcount,
865 obj.co_posonlyargcount,
866 obj.co_kwonlyargcount,
867 obj.co_nlocals,
868 obj.co_stacksize,
869 obj.co_flags,
870 obj.co_code,
871 obj.co_consts,
872 co_names,
873 co_varnames,
874 co_filename,
875 co_name,
876 obj.co_qualname,
877 obj.co_firstlineno,
878 obj.co_linetable,
879 obj.co_exceptiontable,
880 co_freevars,
881 co_cellvars,
882 )
883 elif hasattr(obj, "co_linetable"):
884 # Python 3.10 and later: obj.co_lnotab is deprecated and constructor
885 # expects obj.co_linetable instead.
886 args = (
887 obj.co_argcount,
888 obj.co_posonlyargcount,
889 obj.co_kwonlyargcount,
890 obj.co_nlocals,
891 obj.co_stacksize,

Callers

nothing calls this directly

Calls 1

joinMethod · 0.80

Tested by

no test coverage detected