<s> """<STR_LIT>""" <EOL> import struct <EOL> try : <EOL> from cStringIO import StringIO as _StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO as _StringIO <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg ) : <EOL> self . msg = msg <EOL> def __repr__ ( self ) : <EOL> return repr ( self . msg ) <EOL> def __str__ ( self ) : <EOL> return str ( self . msg ) <EOL> class ConversionError ( Error ) : <EOL> pass <EOL> class Packer : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . reset ( ) <EOL> def reset ( self ) : <EOL> self . __buf = _StringIO ( ) <EOL> def get_buffer ( self ) : <EOL> return self . __buf . getvalue ( ) <EOL> get_buf = get_buffer <EOL> def pack_uint ( self , x ) : <EOL> self . __buf . write ( struct . pack ( '<STR_LIT>' , x ) ) <EOL> pack_int = pack_uint <EOL> pack_enum = pack_int <EOL> def pack_bool ( self , x ) : <EOL> if x : self . __buf . write ( '<STR_LIT>' ) <EOL> else : self . __buf . write ( '<STR_LIT>' ) <EOL> def pack_uhyper ( self , x ) : <EOL> self . pack_uint ( x >> <NUM_LIT:32> & <NUM_LIT> L ) <EOL> self . pack_uint ( x & <NUM_LIT> L ) <EOL> pack_hyper = pack_uhyper <EOL> def pack_float ( self , x ) : <EOL> try : self . __buf . write ( struct . pack ( '<STR_LIT>' , x ) ) <EOL> except struct . error , msg : <EOL> raise ConversionError , msg <EOL> def pack_double ( self , x ) : <EOL> try : self . __buf . write ( struct . pack ( '<STR_LIT>' , x ) ) <EOL> except struct . error , msg : <EOL> raise ConversionError , msg <EOL> def pack_fstring ( self , n , s ) : <EOL> if n < <NUM_LIT:0> : <EOL> raise ValueError , '<STR_LIT>' <EOL> data = s [ : n ] <EOL> n = ( ( n + <NUM_LIT:3> ) // <NUM_LIT:4> ) * <NUM_LIT:4> <EOL> data = data + ( n - len ( data ) ) * '<STR_LIT>' <EOL> self . __buf . write ( data ) <EOL> pack_fopaque = pack_fstring <EOL> def pack_string ( self , s ) : <EOL> n = len ( s ) <EOL> self . pack_uint ( n ) <EOL> self . pack_fstring ( n , s ) <EOL> pack_opaque = pack_string <EOL> pack_bytes = pack_string <EOL> def pack_list ( self , list , pack_item ) : <EOL> for item in list : <EOL> self . pack_uint ( <NUM_LIT:1> ) <EOL> pack_item ( item ) <EOL> self . pack_uint ( <NUM_LIT:0> ) <EOL> def pack_farray ( self , n , list , pack_item ) : <EOL> if len ( list ) != n : <EOL> raise ValueError , '<STR_LIT>' <EOL> for item in list : <EOL> pack_item ( item ) <EOL> def pack_array ( self , list , pack_item ) : <EOL> n = len ( list ) <EOL> self . pack_uint ( n ) <EOL> self . pack_farray ( n , list , pack_item ) <EOL> class Unpacker : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data ) : <EOL> self . reset ( data ) <EOL> def reset ( self , data ) : <EOL> self . __buf = data <EOL> self . __pos = <NUM_LIT:0> <EOL> def get_position ( self ) : <EOL> return self . __pos <EOL> def set_position ( self , position ) : <EOL> self . __pos = position <EOL> def get_buffer ( self ) : <EOL> return self . __buf <EOL> def done ( self ) : <EOL> if self . __pos < len ( self . __buf ) : <EOL> raise Error ( '<STR_LIT>' ) <EOL> def unpack_uint ( self ) : <EOL> i = self . __pos <EOL> self . __pos = j = i + <NUM_LIT:4> <EOL> data = self . __buf [ i : j ] <EOL> if len ( data ) < <NUM_LIT:4> : <EOL> raise EOFError <EOL> x = struct . unpack ( '<STR_LIT>' , data ) [ <NUM_LIT:0> ] <EOL> try : <EOL> return int ( x ) <EOL> except OverflowError : <EOL> return x <EOL> def unpack_int ( self ) : <EOL> i = self . __pos <EOL> self . __pos = j = i + <NUM_LIT:4> <EOL> data = self . __buf [ i : j ] <EOL> if len ( data ) < <NUM_LIT:4> : <EOL> raise EOFError <EOL> return struct . unpack ( '<STR_LIT>' , data ) [ <NUM_LIT:0> ] <EOL> unpack_enum = unpack_int <EOL> def unpack_bool ( self ) : <EOL> return bool ( self . unpack_int ( ) ) <EOL> def unpack_uhyper ( self ) : <EOL> hi = self . unpack_uint ( ) <EOL> lo = self . unpack_uint ( ) <EOL> return long ( hi ) << <NUM_LIT:32> | lo <EOL> def unpack_hyper ( self ) : <EOL> x = self . unpack_uhyper ( ) <EOL> if x >= <NUM_LIT> L : <EOL> x = x - <NUM_LIT> L <EOL> return x <EOL> def unpack_float ( self ) : <EOL> i = self . __pos <EOL> self . __pos = j = i + <NUM_LIT:4> <EOL> data = self . __buf [ i : j ] <EOL> if len ( data ) < <NUM_LIT:4> : <EOL> raise EOFError <EOL> return struct . unpack ( '<STR_LIT>' , data ) [ <NUM_LIT:0> ] <EOL> def unpack_double ( self ) : <EOL> i = self . __pos <EOL> self . __pos = j = i + <NUM_LIT:8> <EOL> data = self . __buf [ i : j ] <EOL> if len ( data ) < <NUM_LIT:8> : <EOL> raise EOFError <EOL> return struct . unpack ( '<STR_LIT>' , data ) [ <NUM_LIT:0> ] <EOL> def unpack_fstring ( self , n ) : <EOL> if n < <NUM_LIT:0> : <EOL> raise ValueError , '<STR_LIT>' <EOL> i = self . __pos <EOL> j = i + ( n + <NUM_LIT:3> ) // <NUM_LIT:4> * <NUM_LIT:4> <EOL> if j > len ( self . __buf ) : <EOL> raise EOFError <EOL> self . __pos = j <EOL> return self . __buf [ i : i + n ] <EOL> unpack_fopaque = unpack_fstring <EOL> def unpack_string ( self ) : <EOL> n = self . unpack_uint ( ) <EOL> return self . unpack_fstring ( n ) <EOL> unpack_opaque = unpack_string <EOL> unpack_bytes = unpack_string <EOL> def unpack_list ( self , unpack_item ) : <EOL> list = [ ] <EOL> while <NUM_LIT:1> : <EOL> x = self . unpack_uint ( ) <EOL> if x == <NUM_LIT:0> : break <EOL> if x != <NUM_LIT:1> : <EOL> raise ConversionError , '<STR_LIT>' % ( x , ) <EOL> item = unpack_item ( ) <EOL> list . append ( item ) <EOL> return list <EOL> def unpack_farray ( self , n , unpack_item ) : <EOL> list = [ ] <EOL> for i in range ( n ) : <EOL> list . append ( unpack_item ( ) ) <EOL> return list <EOL> def unpack_array ( self , unpack_item ) : <EOL> n = self . unpack_uint ( ) <EOL> return self . unpack_farray ( n , unpack_item ) <EOL> def _test ( ) : <EOL> p = Packer ( ) <EOL> packtest = [ <EOL> ( p . pack_uint , ( <NUM_LIT:9> , ) ) , <EOL> ( p . pack_bool , ( True , ) ) , <EOL> ( p . pack_bool , ( False , ) ) , <EOL> ( p . pack_uhyper , ( <NUM_LIT> L , ) ) , <EOL> ( p . pack_float , ( <NUM_LIT> , ) ) , <EOL> ( p . pack_double , ( <NUM_LIT> , ) ) , <EOL> ( p . pack_string , ( '<STR_LIT>' , ) ) , <EOL> ( p . pack_list , ( range ( <NUM_LIT:5> ) , p . pack_uint ) ) , <EOL> ( p . pack_array , ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , p . pack_string ) ) , <EOL> ] <EOL> succeedlist = [ <NUM_LIT:1> ] * len ( packtest ) <EOL> count = <NUM_LIT:0> <EOL> for method , args in packtest : <EOL> print '<STR_LIT>' , count , <EOL> try : <EOL> method ( * args ) <EOL> print '<STR_LIT>' <EOL> except ConversionError , var : <EOL> print '<STR_LIT>' , var . msg <EOL> succeedlist [ count ] = <NUM_LIT:0> <EOL> count = count + <NUM_LIT:1> <EOL> data = p . get_buffer ( ) <EOL> up = Unpacker ( data ) <EOL> unpacktest = [ <EOL> ( up . unpack_uint , ( ) , lambda x : x == <NUM_LIT:9> ) , <EOL> ( up . unpack_bool , ( ) , lambda x : x is True ) , <EOL> ( up . unpack_bool , ( ) , lambda x : x is False ) , <EOL> ( up . unpack_uhyper , ( ) , lambda x : x == <NUM_LIT> L ) , <EOL> ( up . unpack_float , ( ) , lambda x : <NUM_LIT> < x < <NUM_LIT> ) , <EOL> ( up . unpack_double , ( ) , lambda x : <NUM_LIT> < x < <NUM_LIT> ) , <EOL> ( up . unpack_string , ( ) , lambda x : x == '<STR_LIT>' ) , <EOL> ( up . unpack_list , ( up . unpack_uint , ) , lambda x : x == range ( <NUM_LIT:5> ) ) , <EOL> ( up . unpack_array , ( up . unpack_string , ) , <EOL> lambda x : x == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ] <EOL> count = <NUM_LIT:0> <EOL> for method , args , pred in unpacktest : <EOL> print '<STR_LIT>' , count , <EOL> try : <EOL> if succeedlist [ count ] : <EOL> x = method ( * args ) <EOL> print pred ( x ) and '<STR_LIT>' or '<STR_LIT>' , '<STR_LIT::>' , x <EOL> else : <EOL> print '<STR_LIT>' <EOL> except ConversionError , var : <EOL> print '<STR_LIT>' , var . msg <EOL> count = count + <NUM_LIT:1> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
