You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB

4 years ago
  1. # encoding: utf-8
  2. """Utilities to enable code objects to be pickled.
  3. Any process that import this module will be able to pickle code objects. This
  4. includes the func_code attribute of any function. Once unpickled, new
  5. functions can be built using new.function(code, globals()). Eventually
  6. we need to automate all of this so that functions themselves can be pickled.
  7. Reference: A. Tremols, P Cogolo, "Python Cookbook," p 302-305
  8. """
  9. # Copyright (c) IPython Development Team.
  10. # Distributed under the terms of the Modified BSD License.
  11. import warnings
  12. warnings.warn("ipykernel.codeutil is deprecated since IPykernel 4.3.1. It has moved to ipyparallel.serialize", DeprecationWarning)
  13. import sys
  14. import types
  15. try:
  16. import copyreg # Py 3
  17. except ImportError:
  18. import copy_reg as copyreg # Py 2
  19. def code_ctor(*args):
  20. return types.CodeType(*args)
  21. def reduce_code(co):
  22. args = [co.co_argcount, co.co_nlocals, co.co_stacksize,
  23. co.co_flags, co.co_code, co.co_consts, co.co_names,
  24. co.co_varnames, co.co_filename, co.co_name, co.co_firstlineno,
  25. co.co_lnotab, co.co_freevars, co.co_cellvars]
  26. if sys.version_info[0] >= 3:
  27. args.insert(1, co.co_kwonlyargcount)
  28. return code_ctor, tuple(args)
  29. copyreg.pickle(types.CodeType, reduce_code)