<s> from pypy . interpreter . error import OperationError <EOL> from pypy . rlib . rstruct . error import StructError <EOL> from pypy . rlib . rstruct . standardfmttable import PACK_ACCEPTS_BROKEN_INPUT <EOL> from pypy . rlib . rstruct . formatiterator import FormatIterator , CalcSizeFormatIterator <EOL> class PackFormatIterator ( FormatIterator ) : <EOL> def __init__ ( self , space , args_w ) : <EOL> self . space = space <EOL> self . args_w = args_w <EOL> self . args_index = <NUM_LIT:0> <EOL> self . result = [ ] <EOL> def operate ( self , fmtdesc , repetitions ) : <EOL> if fmtdesc . needcount : <EOL> fmtdesc . pack ( self , repetitions ) <EOL> else : <EOL> for i in range ( repetitions ) : <EOL> fmtdesc . pack ( self ) <EOL> operate . _annspecialcase_ = '<STR_LIT>' <EOL> _operate_is_specialized_ = True <EOL> def align ( self , mask ) : <EOL> pad = ( - len ( self . result ) ) & mask <EOL> for i in range ( pad ) : <EOL> self . result . append ( '<STR_LIT:\x00>' ) <EOL> def finished ( self ) : <EOL> if self . args_index != len ( self . args_w ) : <EOL> raise StructError ( "<STR_LIT>" ) <EOL> def accept_obj_arg ( self ) : <EOL> try : <EOL> w_obj = self . args_w [ self . args_index ] <EOL> except IndexError : <EOL> raise StructError ( "<STR_LIT>" ) <EOL> self . args_index += <NUM_LIT:1> <EOL> return w_obj <EOL> if PACK_ACCEPTS_BROKEN_INPUT : <EOL> def accept_int_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> try : <EOL> return self . space . int_w ( w_obj ) <EOL> except OperationError , e : <EOL> return self . space . int_w ( self . _maybe_float ( e , w_obj ) ) <EOL> def accept_uint_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> try : <EOL> return self . space . uint_w ( w_obj ) <EOL> except OperationError , e : <EOL> return self . space . uint_w ( self . _maybe_float ( e , w_obj ) ) <EOL> def accept_longlong_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> try : <EOL> return self . space . r_longlong_w ( w_obj ) <EOL> except OperationError , e : <EOL> return self . space . r_longlong_w ( self . _maybe_float ( e , w_obj ) ) <EOL> def accept_ulonglong_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> try : <EOL> return self . space . r_ulonglong_w ( w_obj ) <EOL> except OperationError , e : <EOL> return self . space . r_ulonglong_w ( self . _maybe_float ( e , w_obj ) ) <EOL> def _maybe_float ( self , e , w_obj ) : <EOL> space = self . space <EOL> if not e . match ( space , space . w_TypeError ) : <EOL> raise e <EOL> if not space . is_true ( space . isinstance ( w_obj , space . w_float ) ) : <EOL> raise e <EOL> return space . int ( w_obj ) <EOL> else : <EOL> def accept_int_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> return self . space . int_w ( w_obj ) <EOL> def accept_uint_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> return self . space . uint_w ( w_obj ) <EOL> def accept_longlong_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> return self . space . r_longlong_w ( w_obj ) <EOL> def accept_ulonglong_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> return self . space . r_ulonglong_w ( w_obj ) <EOL> def accept_str_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> return self . space . str_w ( w_obj ) <EOL> def accept_unicode_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> return self . space . unicode_w ( w_obj ) <EOL> def accept_float_arg ( self ) : <EOL> w_obj = self . accept_obj_arg ( ) <EOL> return self . space . float_w ( w_obj ) <EOL> class UnpackFormatIterator ( FormatIterator ) : <EOL> def __init__ ( self , space , input ) : <EOL> self . space = space <EOL> self . input = input <EOL> self . inputpos = <NUM_LIT:0> <EOL> self . result_w = [ ] <EOL> def operate ( self , fmtdesc , repetitions ) : <EOL> if fmtdesc . needcount : <EOL> fmtdesc . unpack ( self , repetitions ) <EOL> else : <EOL> for i in range ( repetitions ) : <EOL> fmtdesc . unpack ( self ) <EOL> operate . _annspecialcase_ = '<STR_LIT>' <EOL> _operate_is_specialized_ = True <EOL> def align ( self , mask ) : <EOL> self . inputpos = ( self . inputpos + mask ) & ~ mask <EOL> def finished ( self ) : <EOL> if self . inputpos != len ( self . input ) : <EOL> raise StructError ( "<STR_LIT>" ) <EOL> def read ( self , count ) : <EOL> end = self . inputpos + count <EOL> if end > len ( self . input ) : <EOL> raise StructError ( "<STR_LIT>" ) <EOL> s = self . input [ self . inputpos : end ] <EOL> self . inputpos = end <EOL> return s <EOL> def appendobj ( self , value ) : <EOL> self . result_w . append ( self . space . wrap ( value ) ) <EOL> appendobj . _annspecialcase_ = '<STR_LIT>' </s>
