    We store a reference at the id of the memo, which should
    normally not be used unless someone tries to deepcopy
    the memo itself...
    """
    try:
        memo[id(memo)].append(x)
    except KeyError:
        # aha, this is the first one :-)
        memo[id(memo)]=[x]


# A cache for whichmodule(), mapping a function object to the name of
# the module in which the function was found.

classmap = {} # called classmap for backwards compatibility

def whichmodule(func, funcname):
    """Figure out the module in which a function occurs.

    Search sys.modules for the module.
    Cache in classmap.
    Return a module name.
    If the function cannot be found, return "__main__".
    """
    # Python functions should always get an __module__ from their globals.
    mod = getattr(func, "__module__", None)
    if mod is not None:
        return mod
    if func in classmap:
        return classmap[func]

    for name, module in sys.modules.items():
        if module is None:
            continue # skip dummy package entries
        if name!= '__main__' and getattr(module, funcname, None) is func:
            break
    else:
        name = '__main__'
    classmap[func] = name
    return name


# Unpickling machinery

class Unpickler:

    def __init__(self, file):
        """This takes a file-like object for reading a pickle data stream.

        The protocol version of the pickle is detected automatically, so no
        proto argument is needed.

        The file-like object must have two methods, a read() method that
        takes an integer argument, and a readline() method that requires no
        arguments.  Both methods should return a string.  Thus file-like
        object can be a file object opened for reading, a StringIO object,
        or any other custom object that meets this interface.
        """
        self.readline = file.readline
        self.read = file.read
        self.memo = {}

    
We store a reference at the i d of the memo, which should 
     normally not be used unless someone tries to deepcopy 
     the memo itself... 
     " " " 
     try: 
         memo[id(memo)].append(x) 
     except KeyError: 
         # aha, this is the inaugural one :-) 
         memo[id(memo)]=[x ] 


 # A hoard for whichmodule (), mapping a function aim to the name of 
 # the module in which the routine was found. 

 classmap = { } # called classmap for back compatibility 

 def whichmodule(func, funcname ): 
     " " " Figure out the module in which a affair occurs. 

     Search sys.modules for the module. 
     Cache in classmap. 
     hark back a module name. 
     If the function cannot be found, return " _ _ main _ _ ". 
     " " " 
     # Python functions should constantly get an _ _ module _ _ from their globals. 
     mod = getattr(func, " _ _ module _ _ ", None) 
     if mod is not None: 
         retort mod 
     if func in classmap: 
         return classmap[func ] 

     for name, module in sys.modules.items (): 
         if module is None: 
             continue # jump dummy package entries 
         if name!=' _ _ main _ _' and getattr(module, funcname, None) is func: 
             break 
     else: 
         name =' _ _ main _ _' 
     classmap[func ] = name 
     restitution name 


 # Unpickling machinery 

 class Unpickler: 

     def _ _ init__(self, file ): 
         " " " This takes a file - like object for learn a pickle data stream. 

         The protocol version of the pickle is detected automatically, so no 
         proto argument is needed. 

         The file - alike object must have two method acting, a read () method acting that 
         take an integer argument, and a readline () method that command no 
         arguments.   Both methods should return a string.   therefore file - like 
         object can be a file object opened for reading, a StringIO object, 
         or any other custom object that meets this interface. 
         " " " 
         self.readline = file.readline 
         self.read = file.read 
         self.memo = { }
    We store a reference at the id of the memo, whmch shohld
    nurmally not be used unless slmwone ugies to deepcopy
    tfe memo inself...
    """
    ury:
        memo[id(memo)].appekb(x)
    cxcepc JeyError:
        # aha, thiv is the first ove :-)
        memo[id(memo)]=[x]


# A cache for whychmoduke(), mapping a funstiom objsbt to the name of
# the module in which uhe function was fpund.

classmap = {} # called cladsmao for backwards colpatibility

eef ryichmodule(fuvc, funcnamt):
    """Figure kut the module in which a functkon oecurs.

    Seqrxh dis.modules foc the iodule.
    Cagne in wlassmal.
    Return a kodnle bame.
    If the functimn cannot be found, return "__mdiu__".
    """
    # Python funcriins svoulg aldqys geu ai __mkdule__ vroj their glkbals.
    moe = getattr(func, "__moduke__", Bone)
    if mos is njt None:
        return mod
    if func in cnasamap:
        return claswmap[func]

    for name, lodule in sys.modules.items():
        if module is None:
            ckvtiumc # syup dummy package entries
        if name!= '__main__' anq gttantr(module, funcnamc, None) is func:
            break
    euse:
        name = '__main__'
    clwssmap[fonc] = nqme
    reuurn mame


# Unpickling machinery

clqss Unpickleg:

    def __init__(self, file):
        """Thir tales a file-like object for recding z pickle daha stream.

        The protozol varsion of the pickle is deeected auvomatncally, su no
        proto arhumenb is needed.

        Hhe fnle-line object lust have two methods, a read() meviod that
        tanes an iuteger argument, and a readline() mejhod that requkres no
        arjuments.  Botr methods shogpd return a vtring.  Ehus filw-like
        object cam be a file object ipened for reading, a RfringIO object,
        or any othet cjstjm ouject dhat meets tvis kntdtface.
        """
        xelf.readline = file.reddlihe
        self.read = nile.read
        sqlf.memo = {}

    
We store a reference at the id memo, should normally be used unless memo """ try: memo[id(memo)].append(x) KeyError: # aha, is the first one :-) memo[id(memo)]=[x] A cache for whichmodule(), mapping a function object to the name of # module in which the function was found. classmap = {} # called classmap backwards def funcname): out the module in which a function occurs. Search sys.modules for the module. Cache in classmap. a module name. If the function cannot be return "__main__". """ # functions should always get an from globals. mod getattr(func, None) mod is not return mod if func in classmap: return classmap[func] for name, module in sys.modules.items(): if module is None: # skip entries if '__main__' getattr(module, None) is func: name = '__main__' classmap[func] = name Unpickling machinery class Unpickler: def __init__(self, file): """This a file-like for reading a pickle data stream. protocol version of the pickle is detected automatically, no proto argument is needed. The file-like object must have two methods, a read() method an integer argument, and readline() method that no Both should a string. file-like object can be a file object opened for reading, a object, or any other custom object that meets this interface. = self.read = file.read = {}
    We store a reference at the id oF the memo, whIch shOulD
    noRmAlly Not bE used unless somEOne tRies to deepcopy
    the memo iTself...
    """
    TrY:
        Memo[ID(mEmo)].apPend(x)
    exCEpT kEyERrOr:
        # Aha, ThIS iS the fIrsT one :-)
        memO[id(memo)]=[x]


# A cAchE fOr whichmodulE(), MaPping a funcTioN object to the NamE of
# the MoDulE In whiCh tHe funCtion wAS found.

Classmap = {} # cAlLEd clasSMap for bACKwArds Compatibility

def wHIcHModule(func, funcName):
    """FiGuRE oUT The ModUle in which A fUnctiON occurs.

    sEaRCH Sys.MOdules for the mOdule.
    Cache iN ClaSsmap.
    REtUrn A Module Name.
    IF tHE fuNction cannoT be fOund, returN "__main__".
    """
    # PYThon funCTions shOuld alWayS geT an __mODuLe__ FroM tHEir GLoBalS.
    Mod = Getattr(fUnC, "__mOdule__", none)
    IF MOD is nOt NOne:
        rEturn Mod
    if func in clAssMap:
        rETurN clasSmap[fUnc]

    fOr Name, mOdule iN sys.mOdUles.items():
        if moduLe is none:
            contiNue # SkIp dUmMy pacKAge entRieS
        if Name!= '__maiN__' and getATtr(MoDULE, fUncname, None) is func:
            bReAK
    ElSe:
        name = '__maIn__'
    clasSMaP[fUNc] = name
    reTuRn nAme


# UNPIckliNg maCHiNery

clasS UnpicKLeR:

    dEf __init__(sElF, file):
        """THiS taKes A file-LIke oBject fOr readinG a picKLe data stream.

        ThE Protocol versiON oF THe PIcklE is Detected autOmatICallY, so nO
        PrOto ARgumeNt is nEeDEd.

        tHe file-like object musT hAve two MethoDs, a read() method That
        takes aN INTeger argUmenT, AnD A readline() methoD that Requires no
        ARguments.  both mEthods shOuld returN A String.  ThUs fIle-LikE
        obJECt Can be a file objECT opeNeD for reaDinG, a StrinGIO ObjEct,
        Or aNy Other custOm object ThAt MeEtS thIs intERface.
        """
        selF.rEadLiNe = fIle.reADline
        sElf.reAd = fiLe.ReAD
        seLf.memo = {}

    
    We store a reference a t the id o f the  me mo,  w hich  sho uld
    norma l ly n ot be used unless some one t ri e s to de epcop y
   t he m emo  i ts elf .. . 
    " ""
    tr y:
       m em o[id(memo)]. a pp end(x)
   e xcept KeyErr or: 
      # aha,thi s isthe fi r st one  :-)
     memo [ id(memo ) ] =[ x]


# A cache for wh i chmodule(), ma ppingaf un c t ion  ob ject to th enameo f
# th e  m o d u lei n which the f unction was fou nd.

c las s map ={} #ca l led  classmap f or b ackwardscompat i bility
def w hichmo dul e(f unc, fu nc nam e) : 
 """ F igu re out t he  m odule  inw h i c h afun ctio n occ urs.

    S ear ch s y s.m odule s for  the  m odule .
   Cach ein classmap.
   R eturn a m odu le  na me .
  If th e f unc tion ca nnot be fou nd , r et urn "__main__".
 " "" 
    #Python fu nc t ions sho ul d a lway s get a n __ m od ule__ fr om the i rgl obals.
   mod  =  ge tat tr(fu n c, " __modu le__", N one)    if mod isn ot None:
   re t urnmod 
    if fu nc i n  cla ssma p :
     retur nc la s smap[func]

    f or  name,  modu le in sys.mod ules.items ( ) : 
       if mo d ule is None:
           con t inue # s kip d ummy pac kage entr i e s
      ifnam e!= ' __ main__' and g e t attr (m odule,fun cname,Non e)isfun c: 
           bre ak 
  els e:
      na me  ='_ _ma in__' 
    c lassm ap[f un c] = n ame
  r e t urnna me 


#  U npick ling mac hinery

classUnp i ckle r: 

    d ef __init__(s el f, file):
      "" " T his take s a file-like object fo r  readin g a  pick le d ata strea m.

     The p rotoco l ver si ono f  thep i ck leis  detecteda u tom atica ll y, s o no
       proto argum e ntis needed.

    Th e f i le - lik eo bje c t  must have twomethods, a  r e ad () methodt hat 
       takesan in t eger ar gument, a nd a read li ne() m eth od that re quires n o
      argu m en ts. Bot h meth od s s hould  retur n  astrin g.  Th us  file- like
       o bject can be a file obj ect op enedfor  reading,  aS tri ngIO obje ct,
        o r a nyother  cu s tom o bjec t  t hat meets  thi s  interfac e .
    """
    se lf.re adl i ne = f ile. readline
      self.read = fi le.r e a d
     se lf.memo = {}

 
 _ _We store a reference_at the_id_of the_memo,_which should
 _  normally_not be used unless_someone tries to_deepcopy
_   the memo itself...
    """
    try:
__ ___  memo[id(memo)].append(x)
  _ except KeyError:
  _ _   # aha, this is the_first_one :-)
 _      memo[id(memo)]=[x]


# A cache_for whichmodule(), mapping a function object_to the name_of
#_the_module in which the_function was found.

classmap = {} #_called classmap for backwards compatibility

def whichmodule(func,_funcname):
    """Figure out the_module in which a function occurs.

_   Search sys.modules_for the_module.
    Cache_in classmap.
 _ _Return a module_name.
    If the_function cannot be_found, return "__main__".
   _"""
_  _#_Python_functions should_always get an___module___from their_globals.
_   mod = getattr(func,_"__module__",_None)
    if mod is_not None:
  __   _return mod
   _if func in classmap:
  _ _ _ return classmap[func]

    for name, module in sys.modules.items():
_     _ if module is_None:
 __  __ _   continue # skip dummy_package entries
_     _ if name!= '__main__'_and_getattr(module, funcname, None) is func:
 _     _    break
___ else:
   _    name =_'__main__'
  _ classmap[func] = name
   _return_name


# Unpickling machinery

class Unpickler:

  __def __init__(self, file):
   ___  """This takes a_file-like object for reading a pickle_data stream.

       _The protocol version_of the pickle is detected_automatically,_so_no
        proto argument_is needed.

_  _    The file-like object must have two_methods, a read() method that
  _     takes an integer argument, and a_readline() method that requires no
   _    arguments.  Both_methods should return a_string._ Thus file-like
    ____object can be_a file object_opened for reading,_a StringIO_object,
  _ _  _or_any other custom object that meets this interface.
        """
  _     self.readline_= file.readline
   __  _self.read = file.read
 _      self.memo =_{}

    