parameters: (self, obj, name=None)
docstring: Registered with the dispatch to handle all function types.
  
          Determines what kind of function obj is (e.g. lambda, defined at
          interactive prompt, etc) and handles the pickling appropriately.
docstring_summary: Registered with the dispatch to handle all function types.
docstring_tokens: Registered with the dispatch to handle all function types .
function: def save_function(self, obj, name=None):
          """ Registered with the dispatch to handle all function types.
  
          Determines what kind of function obj is (e.g. lambda, defined at
          interactive prompt, etc) and handles the pickling appropriately.
          """
          write = self.write
  
          if name is None:
              name = obj.__name__
          try:
              # whichmodule() could fail, see
              # https://bitbucket.org/gutworth/six/issues/63/importing-six-breaks-pickling
              modname = pickle.whichmodule(obj, name)
          except Exception:
              modname = None
          # print('which gives %s %s %s' % (modname, obj, name))
          try:
              themodule = sys.modules[modname]
          except KeyError:
              # eval'd items such as namedtuple give invalid items for their function __module__
              modname = "__main__"
  
          if modname == "__main__":
              themodule = None
  
          if themodule:
              self.modules.add(themodule)
              if getattr(themodule, name, None) is obj:
                  return self.save_global(obj, name)
  
          # a builtin_function_or_method which comes in as an attribute of some
          # object (e.g., object.__new__, itertools.chain.from_iterable) will end
          # up with modname "__main__" and so end up here. But these functions
          # have no __code__ attribute in CPython, so the handling for
          # user-defined functions below will fail.
          # So we pickle them here using save_reduce; have to do it differently
          # for different python versions.
          if not hasattr(obj, "__code__"):
              if PY3:
                  if sys.version_info < (3, 4):
                      raise pickle.PicklingError("Can't pickle %r" % obj)
                  else:
                      rv = obj.__reduce_ex__(self.proto)
              else:
                  if hasattr(obj, "__self__"):
                      rv = (getattr, (obj.__self__, name))
                  else:
                      raise pickle.PicklingError("Can't pickle %r" % obj)
              return Pickler.save_reduce(self, obj=obj, *rv)
  
          # if func is lambda, def'ed at prompt, is in main, or is nested, then
          # we'll pickle the actual function object rather than simply saving a
          # reference (as is done in default pickler), via save_function_tuple.
          if (
              islambda(obj)
              or getattr(obj.__code__, "co_filename", None) == "<stdin>"
              or themodule is None
          ):
              self.save_function_tuple(obj)
              return
          else:
              # func is nested
              klass = getattr(themodule, name, None)
              if klass is None or klass is not obj:
                  self.save_function_tuple(obj)
                  return
  
          if obj.__dict__:
              # essentially save_reduce, but workaround needed to avoid recursion
              self.save(_restore_attr)
              write(pickle.MARK + pickle.GLOBAL + modname + "\n" + name + "\n")
              self.memoize(obj)
              self.save(obj.__dict__)
              write(pickle.TUPLE + pickle.REDUCE)
          else:
              write(pickle.GLOBAL + modname + "\n" + name + "\n")
              self.memoize(obj)
function_tokens: def save_function ( self , obj , name = None ) : write = self . write if name is None : name = obj . __name__ try : # whichmodule() could fail, see # https://bitbucket.org/gutworth/six/issues/63/importing-six-breaks-pickling modname = pickle . whichmodule ( obj , name ) except Exception : modname = None # print('which gives %s %s %s' % (modname, obj, name)) try : themodule = sys . modules [ modname ] except KeyError : # eval'd items such as namedtuple give invalid items for their function __module__ modname = "__main__" if modname == "__main__" : themodule = None if themodule : self . modules . add ( themodule ) if getattr ( themodule , name , None ) is obj : return self . save_global ( obj , name ) # a builtin_function_or_method which comes in as an attribute of some # object (e.g., object.__new__, itertools.chain.from_iterable) will end # up with modname "__main__" and so end up here. But these functions # have no __code__ attribute in CPython, so the handling for # user-defined functions below will fail. # So we pickle them here using save_reduce; have to do it differently # for different python versions. if not hasattr ( obj , "__code__" ) : if PY3 : if sys . version_info < ( 3 , 4 ) : raise pickle . PicklingError ( "Can't pickle %r" % obj ) else : rv = obj . __reduce_ex__ ( self . proto ) else : if hasattr ( obj , "__self__" ) : rv = ( getattr , ( obj . __self__ , name ) ) else : raise pickle . PicklingError ( "Can't pickle %r" % obj ) return Pickler . save_reduce ( self , obj = obj , * rv ) # if func is lambda, def'ed at prompt, is in main, or is nested, then # we'll pickle the actual function object rather than simply saving a # reference (as is done in default pickler), via save_function_tuple. if ( islambda ( obj ) or getattr ( obj . __code__ , "co_filename" , None ) == "<stdin>" or themodule is None ) : self . save_function_tuple ( obj ) return else : # func is nested klass = getattr ( themodule , name , None ) if klass is None or klass is not obj : self . save_function_tuple ( obj ) return if obj . __dict__ : # essentially save_reduce, but workaround needed to avoid recursion self . save ( _restore_attr ) write ( pickle . MARK + pickle . GLOBAL + modname + "\n" + name + "\n" ) self . memoize ( obj ) self . save ( obj . __dict__ ) write ( pickle . TUPLE + pickle . REDUCE ) else : write ( pickle . GLOBAL + modname + "\n" + name + "\n" ) self . memoize ( obj )