<s> import base64 <EOL> import quopri <EOL> import sys <EOL> import jsonpickle . util as util <EOL> import jsonpickle . tags as tags <EOL> import jsonpickle . handlers as handlers <EOL> from jsonpickle . compat import numeric_types , set , unicode <EOL> from jsonpickle . backend import JSONBackend <EOL> def decode ( string , backend = None , context = None , keys = False , reset = True , <EOL> safe = False ) : <EOL> backend = _make_backend ( backend ) <EOL> if context is None : <EOL> context = Unpickler ( keys = keys , backend = backend , safe = safe ) <EOL> return context . restore ( backend . decode ( string ) , reset = reset ) <EOL> def _make_backend ( backend ) : <EOL> if backend is None : <EOL> return JSONBackend ( ) <EOL> else : <EOL> return backend <EOL> class _Proxy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . instance = None <EOL> def get ( self ) : <EOL> return self . instance <EOL> def reset ( self , instance ) : <EOL> self . instance = instance <EOL> def _obj_setattr ( obj , attr , proxy ) : <EOL> setattr ( obj , attr , proxy . get ( ) ) <EOL> def _obj_setvalue ( obj , idx , proxy ) : <EOL> obj [ idx ] = proxy . get ( ) <EOL> class Unpickler ( object ) : <EOL> def __init__ ( self , backend = None , keys = False , safe = False ) : <EOL> self . backend = _make_backend ( backend ) <EOL> self . keys = keys <EOL> self . safe = safe <EOL> self . reset ( ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _namedict = { } <EOL> self . _namestack = [ ] <EOL> self . _obj_to_idx = { } <EOL> self . _objs = [ ] <EOL> self . _proxies = [ ] <EOL> def restore ( self , obj , reset = True ) : <EOL> """<STR_LIT>""" <EOL> if reset : <EOL> self . reset ( ) <EOL> value = self . _restore ( obj ) <EOL> if reset : <EOL> self . _swap_proxies ( ) <EOL> return value <EOL> def _swap_proxies ( self ) : <EOL> """<STR_LIT>""" <EOL> for ( obj , attr , proxy , method ) in self . _proxies : <EOL> method ( obj , attr , proxy ) <EOL> self . _proxies = [ ] <EOL> def _restore ( self , obj ) : <EOL> if has_tag ( obj , tags . B64 ) : <EOL> restore = self . _restore_base64 <EOL> elif has_tag ( obj , tags . BYTES ) : <EOL> restore = self . _restore_quopri <EOL> elif has_tag ( obj , tags . ID ) : <EOL> restore = self . _restore_id <EOL> elif has_tag ( obj , tags . REF ) : <EOL> restore = self . _restore_ref <EOL> elif has_tag ( obj , tags . ITERATOR ) : <EOL> restore = self . _restore_iterator <EOL> elif has_tag ( obj , tags . TYPE ) : <EOL> restore = self . _restore_type <EOL> elif has_tag ( obj , tags . REPR ) : <EOL> restore = self . _restore_repr <EOL> elif has_tag ( obj , tags . REDUCE ) : <EOL> restore = self . _restore_reduce <EOL> elif has_tag ( obj , tags . OBJECT ) : <EOL> restore = self . _restore_object <EOL> elif has_tag ( obj , tags . FUNCTION ) : <EOL> restore = self . _restore_function <EOL> elif util . is_list ( obj ) : <EOL> restore = self . _restore_list <EOL> elif has_tag ( obj , tags . TUPLE ) : <EOL> restore = self . _restore_tuple <EOL> elif has_tag ( obj , tags . SET ) : <EOL> restore = self . _restore_set <EOL> elif util . is_dictionary ( obj ) : <EOL> restore = self . _restore_dict <EOL> else : <EOL> restore = lambda x : x <EOL> return restore ( obj ) <EOL> def _restore_base64 ( self , obj ) : <EOL> return base64 . decodestring ( obj [ tags . B64 ] . encode ( '<STR_LIT:utf-8>' ) ) <EOL> def _restore_quopri ( self , obj ) : <EOL> return quopri . decodestring ( obj [ tags . BYTES ] . encode ( '<STR_LIT:utf-8>' ) ) <EOL> def _restore_iterator ( self , obj ) : <EOL> return iter ( self . _restore_list ( obj [ tags . ITERATOR ] ) ) <EOL> def _restore_reduce ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> reduce_val = obj [ tags . REDUCE ] <EOL> f , args , state , listitems , dictitems = map ( self . _restore , reduce_val ) <EOL> if f == tags . NEWOBJ or f . __name__ == '<STR_LIT>' : <EOL> cls = args [ <NUM_LIT:0> ] <EOL> stage1 = cls . __new__ ( cls , * args [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> stage1 = f ( * args ) <EOL> if state : <EOL> try : <EOL> stage1 . __setstate__ ( state ) <EOL> except AttributeError : <EOL> try : <EOL> stage1 . __dict__ . update ( state ) <EOL> except AttributeError : <EOL> for k , v in state . items ( ) : <EOL> setattr ( stage1 , k , v ) <EOL> if listitems : <EOL> try : <EOL> stage1 . extend ( listitems ) <EOL> except AttributeError : <EOL> for x in listitems : <EOL> stage1 . append ( x ) <EOL> if dictitems : <EOL> for k , v in dictitems : <EOL> stage1 . __setitem__ ( k , v ) <EOL> self . _mkref ( stage1 ) <EOL> return stage1 <EOL> def _restore_id ( self , obj ) : <EOL> return self . _objs [ obj [ tags . ID ] ] <EOL> def _restore_ref ( self , obj ) : <EOL> return self . _namedict . get ( obj [ tags . REF ] ) <EOL> def _restore_type ( self , obj ) : <EOL> typeref = loadclass ( obj [ tags . TYPE ] ) <EOL> if typeref is None : <EOL> return obj <EOL> return typeref <EOL> def _restore_repr ( self , obj ) : <EOL> if self . safe : <EOL> return None <EOL> obj = loadrepr ( obj [ tags . REPR ] ) <EOL> return self . _mkref ( obj ) <EOL> def _restore_object ( self , obj ) : <EOL> class_name = obj [ tags . OBJECT ] <EOL> cls = loadclass ( class_name ) <EOL> handler = handlers . get ( cls , handlers . get ( class_name ) ) <EOL> if handler is not None : <EOL> proxy = _Proxy ( ) <EOL> self . _mkref ( proxy ) <EOL> instance = handler ( self ) . restore ( obj ) <EOL> proxy . reset ( instance ) <EOL> self . _swapref ( proxy , instance ) <EOL> return instance <EOL> if cls is None : <EOL> return self . _mkref ( obj ) <EOL> return self . _restore_object_instance ( obj , cls ) <EOL> def _restore_function ( self , obj ) : <EOL> return loadclass ( obj [ tags . FUNCTION ] ) <EOL> def _loadfactory ( self , obj ) : <EOL> try : <EOL> default_factory = obj [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> return None <EOL> del obj [ '<STR_LIT>' ] <EOL> return self . _restore ( default_factory ) <EOL> def _restore_object_instance ( self , obj , cls ) : <EOL> proxy = _Proxy ( ) <EOL> self . _mkref ( proxy ) <EOL> factory = self . _loadfactory ( obj ) <EOL> if has_tag ( obj , tags . NEWARGSEX ) : <EOL> args , kwargs = obj [ tags . NEWARGSEX ] <EOL> else : <EOL> args = getargs ( obj ) <EOL> kwargs = { } <EOL> if args : <EOL> args = self . _restore ( args ) <EOL> if kwargs : <EOL> kwargs = self . _restore ( kwargs ) <EOL> is_oldstyle = not ( isinstance ( cls , type ) or getattr ( cls , '<STR_LIT>' , None ) ) <EOL> try : <EOL> if ( not is_oldstyle ) and hasattr ( cls , '<STR_LIT>' ) : <EOL> if factory : <EOL> instance = cls . __new__ ( cls , factory , * args , ** kwargs ) <EOL> instance . default_factory = factory <EOL> else : <EOL> instance = cls . __new__ ( cls , * args , ** kwargs ) <EOL> else : <EOL> instance = object . __new__ ( cls ) <EOL> except TypeError : <EOL> is_oldstyle = True <EOL> if is_oldstyle : <EOL> try : <EOL> instance = cls ( * args ) <EOL> except TypeError : <EOL> try : <EOL> instance = make_blank_classic ( cls ) <EOL> except : <EOL> return self . _mkref ( obj ) <EOL> proxy . reset ( instance ) <EOL> self . _swapref ( proxy , instance ) <EOL> if isinstance ( instance , tuple ) : <EOL> return instance <EOL> if ( hasattr ( instance , '<STR_LIT>' ) and <EOL> isinstance ( instance . default_factory , _Proxy ) ) : <EOL> instance . default_factory = instance . default_factory . get ( ) <EOL> return self . _restore_object_instance_variables ( obj , instance ) <EOL> def _restore_from_dict ( self , obj , instance , ignorereserved = True ) : <EOL> restore_key = self . _restore_key_fn ( ) <EOL> method = _obj_setattr <EOL> for k , v in sorted ( obj . items ( ) , key = util . itemgetter ) : <EOL> if ignorereserved and k in tags . RESERVED : <EOL> continue <EOL> if isinstance ( k , numeric_types ) : <EOL> str_k = unicode ( k ) <EOL> else : <EOL> str_k = k <EOL> self . _namestack . append ( str_k ) <EOL> k = restore_key ( k ) <EOL> value = self . _restore ( v ) <EOL> if ( util . is_noncomplex ( instance ) or <EOL> util . is_dictionary_subclass ( instance ) ) : <EOL> instance [ k ] = value <EOL> else : <EOL> setattr ( instance , k , value ) <EOL> if isinstance ( value , _Proxy ) : <EOL> self . _proxies . append ( ( instance , k , value , method ) ) <EOL> self . _namestack . pop ( ) <EOL> def _restore_object_instance_variables ( self , obj , instance ) : <EOL> self . _restore_from_dict ( obj , instance ) <EOL> if has_tag ( obj , tags . SEQ ) : <EOL> if hasattr ( instance , '<STR_LIT>' ) : <EOL> for v in obj [ tags . SEQ ] : <EOL> instance . append ( self . _restore ( v ) ) <EOL> if hasattr ( instance , '<STR_LIT>' ) : <EOL> for v in obj [ tags . SEQ ] : <EOL> instance . add ( self . _restore ( v ) ) <EOL> if has_tag ( obj , tags . STATE ) : <EOL> instance = self . _restore_state ( obj , instance ) <EOL> return instance <EOL> def _restore_state ( self , obj , instance ) : <EOL> state = self . _restore ( obj [ tags . STATE ] ) <EOL> has_slots = ( isinstance ( state , tuple ) and len ( state ) == <NUM_LIT:2> <EOL> and isinstance ( state [ <NUM_LIT:1> ] , dict ) ) <EOL> has_slots_and_dict = has_slots and isinstance ( state [ <NUM_LIT:0> ] , dict ) <EOL> if hasattr ( instance , '<STR_LIT>' ) : <EOL> instance . __setstate__ ( state ) <EOL> elif isinstance ( state , dict ) : <EOL> self . _restore_from_dict ( state , instance , ignorereserved = False ) <EOL> elif has_slots : <EOL> self . _restore_from_dict ( state [ <NUM_LIT:1> ] , instance , ignorereserved = False ) <EOL> if has_slots_and_dict : <EOL> self . _restore_from_dict ( state [ <NUM_LIT:0> ] , <EOL> instance , ignorereserved = False ) <EOL> elif ( not hasattr ( instance , '<STR_LIT>' ) <EOL> and not hasattr ( instance , '<STR_LIT>' ) ) : <EOL> instance = state <EOL> return instance <EOL> def _restore_list ( self , obj ) : <EOL> parent = [ ] <EOL> self . _mkref ( parent ) <EOL> children = [ self . _restore ( v ) for v in obj ] <EOL> parent . extend ( children ) <EOL> method = _obj_setvalue <EOL> proxies = [ ( parent , idx , value , method ) <EOL> for idx , value in enumerate ( parent ) <EOL> if isinstance ( value , _Proxy ) ] <EOL> self . _proxies . extend ( proxies ) <EOL> return parent <EOL> def _restore_tuple ( self , obj ) : <EOL> return tuple ( [ self . _restore ( v ) for v in obj [ tags . TUPLE ] ] ) <EOL> def _restore_set ( self , obj ) : <EOL> return set ( [ self . _restore ( v ) for v in obj [ tags . SET ] ] ) <EOL> def _restore_dict ( self , obj ) : <EOL> data = { } <EOL> restore_key = self . _restore_key_fn ( ) <EOL> for k , v in sorted ( obj . items ( ) , key = util . itemgetter ) : <EOL> if isinstance ( k , numeric_types ) : <EOL> str_k = unicode ( k ) <EOL> else : <EOL> str_k = k <EOL> self . _namestack . append ( str_k ) <EOL> k = restore_key ( k ) <EOL> data [ k ] = self . _restore ( v ) <EOL> self . _namestack . pop ( ) <EOL> return data <EOL> def _restore_key_fn ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . keys : <EOL> restore_key = self . _restore_pickled_key <EOL> else : <EOL> restore_key = lambda key : key <EOL> return restore_key <EOL> def _restore_pickled_key ( self , key ) : <EOL> if key . startswith ( tags . JSON_KEY ) : <EOL> key = decode ( key [ len ( tags . JSON_KEY ) : ] , <EOL> backend = self . backend , context = self , <EOL> keys = True , reset = False ) <EOL> return key <EOL> def _refname ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:/>' + '<STR_LIT:/>' . join ( self . _namestack ) <EOL> def _mkref ( self , obj ) : <EOL> obj_id = id ( obj ) <EOL> try : <EOL> self . _obj_to_idx [ obj_id ] <EOL> except KeyError : <EOL> self . _obj_to_idx [ obj_id ] = len ( self . _objs ) <EOL> self . _objs . append ( obj ) <EOL> self . _namedict [ self . _refname ( ) ] = obj <EOL> return obj <EOL> def _swapref ( self , proxy , instance ) : <EOL> proxy_id = id ( proxy ) <EOL> instance_id = id ( instance ) <EOL> instance_index = self . _obj_to_idx [ proxy_id ] <EOL> self . _obj_to_idx [ instance_id ] = instance_index <EOL> del self . _obj_to_idx [ proxy_id ] <EOL> self . _objs [ instance_index ] = instance <EOL> self . _namedict [ self . _refname ( ) ] = instance <EOL> def loadclass ( module_and_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> module , name = module_and_name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> module = util . untranslate_module_name ( module ) <EOL> __import__ ( module ) <EOL> return getattr ( sys . modules [ module ] , name ) <EOL> except : <EOL> return None <EOL> def getargs ( obj ) : <EOL> """<STR_LIT>""" <EOL> if has_tag ( obj , tags . NEWARGSEX ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if has_tag ( obj , tags . NEWARGS ) : <EOL> return obj [ tags . NEWARGS ] <EOL> if has_tag ( obj , tags . INITARGS ) : <EOL> return obj [ tags . INITARGS ] <EOL> try : <EOL> seq_list = obj [ tags . SEQ ] <EOL> obj_dict = obj [ tags . OBJECT ] <EOL> except KeyError : <EOL> return [ ] <EOL> typeref = loadclass ( obj_dict ) <EOL> if not typeref : <EOL> return [ ] <EOL> if hasattr ( typeref , '<STR_LIT>' ) : <EOL> if len ( typeref . _fields ) == len ( seq_list ) : <EOL> return seq_list <EOL> return [ ] <EOL> class _trivialclassic : <EOL> """<STR_LIT>""" <EOL> def make_blank_classic ( cls ) : <EOL> """<STR_LIT>""" <EOL> instance = _trivialclassic ( ) <EOL> instance . __class__ = cls <EOL> return instance <EOL> def loadrepr ( reprstr ) : <EOL> """<STR_LIT>""" <EOL> module , evalstr = reprstr . split ( '<STR_LIT:/>' ) <EOL> mylocals = locals ( ) <EOL> localname = module <EOL> if '<STR_LIT:.>' in localname : <EOL> localname = module . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> mylocals [ localname ] = __import__ ( module ) <EOL> return eval ( evalstr ) <EOL> def has_tag ( obj , tag ) : <EOL> """<STR_LIT>""" <EOL> return type ( obj ) is dict and tag in obj </s>
