<s> """<STR_LIT>""" <EOL> import types <EOL> import weakref <EOL> from copy_reg import dispatch_table <EOL> class Error ( Exception ) : <EOL> pass <EOL> error = Error <EOL> try : <EOL> from org . python . core import PyStringMap <EOL> except ImportError : <EOL> PyStringMap = None <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def copy ( x ) : <EOL> """<STR_LIT>""" <EOL> cls = type ( x ) <EOL> copier = _copy_dispatch . get ( cls ) <EOL> if copier : <EOL> return copier ( x ) <EOL> copier = getattr ( cls , "<STR_LIT>" , None ) <EOL> if copier : <EOL> return copier ( x ) <EOL> reductor = dispatch_table . get ( cls ) <EOL> if reductor : <EOL> rv = reductor ( x ) <EOL> else : <EOL> reductor = getattr ( x , "<STR_LIT>" , None ) <EOL> if reductor : <EOL> rv = reductor ( <NUM_LIT:2> ) <EOL> else : <EOL> reductor = getattr ( x , "<STR_LIT>" , None ) <EOL> if reductor : <EOL> rv = reductor ( ) <EOL> else : <EOL> raise Error ( "<STR_LIT>" % cls ) <EOL> return _reconstruct ( x , rv , <NUM_LIT:0> ) <EOL> _copy_dispatch = d = { } <EOL> def _copy_immutable ( x ) : <EOL> return x <EOL> for t in ( type ( None ) , int , long , float , bool , str , tuple , <EOL> frozenset , type , xrange , types . ClassType , <EOL> types . BuiltinFunctionType , type ( Ellipsis ) , <EOL> types . FunctionType , weakref . ref ) : <EOL> d [ t ] = _copy_immutable <EOL> for name in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> t = getattr ( types , name , None ) <EOL> if t is not None : <EOL> d [ t ] = _copy_immutable <EOL> def _copy_with_constructor ( x ) : <EOL> return type ( x ) ( x ) <EOL> for t in ( list , dict , set ) : <EOL> d [ t ] = _copy_with_constructor <EOL> def _copy_with_copy_method ( x ) : <EOL> return x . copy ( ) <EOL> if PyStringMap is not None : <EOL> d [ PyStringMap ] = _copy_with_copy_method <EOL> def _copy_inst ( x ) : <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> return x . __copy__ ( ) <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> args = x . __getinitargs__ ( ) <EOL> y = x . __class__ ( * args ) <EOL> else : <EOL> y = _EmptyClass ( ) <EOL> y . __class__ = x . __class__ <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> state = x . __getstate__ ( ) <EOL> else : <EOL> state = x . __dict__ <EOL> if hasattr ( y , '<STR_LIT>' ) : <EOL> y . __setstate__ ( state ) <EOL> else : <EOL> y . __dict__ . update ( state ) <EOL> return y <EOL> d [ types . InstanceType ] = _copy_inst <EOL> del d <EOL> def deepcopy ( x , memo = None , _nil = [ ] ) : <EOL> """<STR_LIT>""" <EOL> if memo is None : <EOL> memo = { } <EOL> d = id ( x ) <EOL> y = memo . get ( d , _nil ) <EOL> if y is not _nil : <EOL> return y <EOL> cls = type ( x ) <EOL> copier = _deepcopy_dispatch . get ( cls ) <EOL> if copier : <EOL> y = copier ( x , memo ) <EOL> else : <EOL> try : <EOL> issc = issubclass ( cls , type ) <EOL> except TypeError : <EOL> issc = <NUM_LIT:0> <EOL> if issc : <EOL> y = _deepcopy_atomic ( x , memo ) <EOL> else : <EOL> copier = getattr ( x , "<STR_LIT>" , None ) <EOL> if copier : <EOL> y = copier ( memo ) <EOL> else : <EOL> reductor = dispatch_table . get ( cls ) <EOL> if reductor : <EOL> rv = reductor ( x ) <EOL> else : <EOL> reductor = getattr ( x , "<STR_LIT>" , None ) <EOL> if reductor : <EOL> rv = reductor ( <NUM_LIT:2> ) <EOL> else : <EOL> reductor = getattr ( x , "<STR_LIT>" , None ) <EOL> if reductor : <EOL> rv = reductor ( ) <EOL> else : <EOL> raise Error ( <EOL> "<STR_LIT>" % cls ) <EOL> y = _reconstruct ( x , rv , <NUM_LIT:1> , memo ) <EOL> memo [ d ] = y <EOL> _keep_alive ( x , memo ) <EOL> return y <EOL> _deepcopy_dispatch = d = { } <EOL> def _deepcopy_atomic ( x , memo ) : <EOL> return x <EOL> d [ type ( None ) ] = _deepcopy_atomic <EOL> d [ type ( Ellipsis ) ] = _deepcopy_atomic <EOL> d [ int ] = _deepcopy_atomic <EOL> d [ long ] = _deepcopy_atomic <EOL> d [ float ] = _deepcopy_atomic <EOL> d [ bool ] = _deepcopy_atomic <EOL> try : <EOL> d [ complex ] = _deepcopy_atomic <EOL> except NameError : <EOL> pass <EOL> d [ str ] = _deepcopy_atomic <EOL> try : <EOL> d [ unicode ] = _deepcopy_atomic <EOL> except NameError : <EOL> pass <EOL> try : <EOL> d [ types . CodeType ] = _deepcopy_atomic <EOL> except AttributeError : <EOL> pass <EOL> d [ type ] = _deepcopy_atomic <EOL> d [ xrange ] = _deepcopy_atomic <EOL> d [ types . ClassType ] = _deepcopy_atomic <EOL> d [ types . BuiltinFunctionType ] = _deepcopy_atomic <EOL> d [ types . FunctionType ] = _deepcopy_atomic <EOL> d [ weakref . ref ] = _deepcopy_atomic <EOL> def _deepcopy_list ( x , memo ) : <EOL> y = [ ] <EOL> memo [ id ( x ) ] = y <EOL> for a in x : <EOL> y . append ( deepcopy ( a , memo ) ) <EOL> return y <EOL> d [ list ] = _deepcopy_list <EOL> def _deepcopy_tuple ( x , memo ) : <EOL> y = [ ] <EOL> for a in x : <EOL> y . append ( deepcopy ( a , memo ) ) <EOL> d = id ( x ) <EOL> try : <EOL> return memo [ d ] <EOL> except KeyError : <EOL> pass <EOL> for i in range ( len ( x ) ) : <EOL> if x [ i ] is not y [ i ] : <EOL> y = tuple ( y ) <EOL> break <EOL> else : <EOL> y = x <EOL> memo [ d ] = y <EOL> return y <EOL> d [ tuple ] = _deepcopy_tuple <EOL> def _deepcopy_dict ( x , memo ) : <EOL> y = { } <EOL> memo [ id ( x ) ] = y <EOL> for key , value in x . iteritems ( ) : <EOL> y [ deepcopy ( key , memo ) ] = deepcopy ( value , memo ) <EOL> return y <EOL> d [ dict ] = _deepcopy_dict <EOL> if PyStringMap is not None : <EOL> d [ PyStringMap ] = _deepcopy_dict <EOL> def _deepcopy_method ( x , memo ) : <EOL> return type ( x ) ( x . im_func , deepcopy ( x . im_self , memo ) , x . im_class ) <EOL> _deepcopy_dispatch [ types . MethodType ] = _deepcopy_method <EOL> def _keep_alive ( x , memo ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> memo [ id ( memo ) ] . append ( x ) <EOL> except KeyError : <EOL> memo [ id ( memo ) ] = [ x ] <EOL> def _deepcopy_inst ( x , memo ) : <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> return x . __deepcopy__ ( memo ) <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> args = x . __getinitargs__ ( ) <EOL> args = deepcopy ( args , memo ) <EOL> y = x . __class__ ( * args ) <EOL> else : <EOL> y = _EmptyClass ( ) <EOL> y . __class__ = x . __class__ <EOL> memo [ id ( x ) ] = y <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> state = x . __getstate__ ( ) <EOL> else : <EOL> state = x . __dict__ <EOL> state = deepcopy ( state , memo ) <EOL> if hasattr ( y , '<STR_LIT>' ) : <EOL> y . __setstate__ ( state ) <EOL> else : <EOL> y . __dict__ . update ( state ) <EOL> return y <EOL> d [ types . InstanceType ] = _deepcopy_inst <EOL> def _reconstruct ( x , info , deep , memo = None ) : <EOL> if isinstance ( info , str ) : <EOL> return x <EOL> assert isinstance ( info , tuple ) <EOL> if memo is None : <EOL> memo = { } <EOL> n = len ( info ) <EOL> assert n in ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) <EOL> callable , args = info [ : <NUM_LIT:2> ] <EOL> if n > <NUM_LIT:2> : <EOL> state = info [ <NUM_LIT:2> ] <EOL> else : <EOL> state = { } <EOL> if n > <NUM_LIT:3> : <EOL> listiter = info [ <NUM_LIT:3> ] <EOL> else : <EOL> listiter = None <EOL> if n > <NUM_LIT:4> : <EOL> dictiter = info [ <NUM_LIT:4> ] <EOL> else : <EOL> dictiter = None <EOL> if deep : <EOL> args = deepcopy ( args , memo ) <EOL> y = callable ( * args ) <EOL> memo [ id ( x ) ] = y <EOL> if state : <EOL> if deep : <EOL> state = deepcopy ( state , memo ) <EOL> if hasattr ( y , '<STR_LIT>' ) : <EOL> y . __setstate__ ( state ) <EOL> else : <EOL> if isinstance ( state , tuple ) and len ( state ) == <NUM_LIT:2> : <EOL> state , slotstate = state <EOL> else : <EOL> slotstate = None <EOL> if state is not None : <EOL> y . __dict__ . update ( state ) <EOL> if slotstate is not None : <EOL> for key , value in slotstate . iteritems ( ) : <EOL> setattr ( y , key , value ) <EOL> if listiter is not None : <EOL> for item in listiter : <EOL> if deep : <EOL> item = deepcopy ( item , memo ) <EOL> y . append ( item ) <EOL> if dictiter is not None : <EOL> for key , value in dictiter : <EOL> if deep : <EOL> key = deepcopy ( key , memo ) <EOL> value = deepcopy ( value , memo ) <EOL> y [ key ] = value <EOL> return y <EOL> del d <EOL> del types <EOL> class _EmptyClass : <EOL> pass <EOL> def _test ( ) : <EOL> l = [ None , <NUM_LIT:1> , <NUM_LIT:2> L , <NUM_LIT> , '<STR_LIT>' , ( <NUM_LIT:1> , <NUM_LIT:2> L ) , [ <NUM_LIT> , '<STR_LIT:abc>' ] , <EOL> { '<STR_LIT:abc>' : '<STR_LIT>' } , ( ) , [ ] , { } ] <EOL> l1 = copy ( l ) <EOL> print l1 == l <EOL> l1 = map ( copy , l ) <EOL> print l1 == l <EOL> l1 = deepcopy ( l ) <EOL> print l1 == l <EOL> class C : <EOL> def __init__ ( self , arg = None ) : <EOL> self . a = <NUM_LIT:1> <EOL> self . arg = arg <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> file = sys . argv [ <NUM_LIT:0> ] <EOL> else : <EOL> file = __file__ <EOL> self . fp = open ( file ) <EOL> self . fp . close ( ) <EOL> def __getstate__ ( self ) : <EOL> return { '<STR_LIT:a>' : self . a , '<STR_LIT>' : self . arg } <EOL> def __setstate__ ( self , state ) : <EOL> for key , value in state . iteritems ( ) : <EOL> setattr ( self , key , value ) <EOL> def __deepcopy__ ( self , memo = None ) : <EOL> new = self . __class__ ( deepcopy ( self . arg , memo ) ) <EOL> new . a = self . a <EOL> return new <EOL> c = C ( '<STR_LIT>' ) <EOL> l . append ( c ) <EOL> l2 = copy ( l ) <EOL> print l == l2 <EOL> print l <EOL> print l2 <EOL> l2 = deepcopy ( l ) <EOL> print l == l2 <EOL> print l <EOL> print l2 <EOL> l . append ( { l [ <NUM_LIT:1> ] : l , '<STR_LIT>' : l [ <NUM_LIT:2> ] } ) <EOL> l3 = copy ( l ) <EOL> import repr <EOL> print map ( repr . repr , l ) <EOL> print map ( repr . repr , l1 ) <EOL> print map ( repr . repr , l2 ) <EOL> print map ( repr . repr , l3 ) <EOL> l3 = deepcopy ( l ) <EOL> import repr <EOL> print map ( repr . repr , l ) <EOL> print map ( repr . repr , l1 ) <EOL> print map ( repr . repr , l2 ) <EOL> print map ( repr . repr , l3 ) <EOL> class odict ( dict ) : <EOL> def __init__ ( self , d = { } ) : <EOL> self . a = <NUM_LIT> <EOL> dict . __init__ ( self , d ) <EOL> def __setitem__ ( self , k , i ) : <EOL> dict . __setitem__ ( self , k , i ) <EOL> self . a <EOL> o = odict ( { "<STR_LIT:A>" : "<STR_LIT:B>" } ) <EOL> x = deepcopy ( o ) <EOL> print ( o , x ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
