<s> """<STR_LIT>""" <EOL> import py <EOL> from struct import pack , unpack <EOL> from pypy . rlib . rstruct . formatiterator import FormatIterator <EOL> from pypy . rlib . rstruct . error import StructError <EOL> from pypy . rlib . rstruct . nativefmttable import native_is_bigendian <EOL> from pypy . rpython . extregistry import ExtRegistryEntry <EOL> class MasterReader ( object ) : <EOL> def __init__ ( self , s ) : <EOL> self . input = s <EOL> self . inputpos = <NUM_LIT:0> <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 align ( self , mask ) : <EOL> self . inputpos = ( self . inputpos + mask ) & ~ mask <EOL> class AbstractReader ( object ) : <EOL> pass <EOL> def reader_for_pos ( pos ) : <EOL> class ReaderForPos ( AbstractReader ) : <EOL> def __init__ ( self , mr ) : <EOL> self . mr = mr <EOL> self . bigendian = native_is_bigendian <EOL> def read ( self , count ) : <EOL> return self . mr . read ( count ) <EOL> def appendobj ( self , value ) : <EOL> self . value = value <EOL> ReaderForPos . __name__ = '<STR_LIT>' % pos <EOL> return ReaderForPos <EOL> class FrozenUnpackIterator ( FormatIterator ) : <EOL> def __init__ ( self , fmt ) : <EOL> self . formats = [ ] <EOL> self . fmt = fmt <EOL> def operate ( self , fmtdesc , repetitions ) : <EOL> if fmtdesc . needcount : <EOL> self . formats . append ( ( fmtdesc , repetitions , None ) ) <EOL> else : <EOL> for i in range ( repetitions ) : <EOL> self . formats . append ( ( fmtdesc , <NUM_LIT:1> , None ) ) <EOL> def align ( self , mask ) : <EOL> if self . formats : <EOL> fmt , rep , _ = self . formats . pop ( ) <EOL> self . formats . append ( ( fmt , rep , mask ) ) <EOL> def _create_unpacking_func ( self ) : <EOL> rg = range ( len ( self . formats ) ) <EOL> perform_lst = [ ] <EOL> miniglobals = { } <EOL> miniglobals . update ( globals ( ) ) <EOL> for i in rg : <EOL> fmtdesc , rep , mask = self . formats [ i ] <EOL> miniglobals [ '<STR_LIT>' % i ] = fmtdesc . unpack <EOL> if mask is not None : <EOL> perform_lst . append ( '<STR_LIT>' % mask ) <EOL> if not fmtdesc . needcount : <EOL> perform_lst . append ( '<STR_LIT>' % ( i , i ) ) <EOL> else : <EOL> perform_lst . append ( '<STR_LIT>' % ( i , i , rep ) ) <EOL> miniglobals [ '<STR_LIT>' % i ] = reader_for_pos ( i ) <EOL> readers = "<STR_LIT:;>" . join ( [ "<STR_LIT>" % ( i , i ) <EOL> for i in rg ] ) <EOL> perform = "<STR_LIT:;>" . join ( perform_lst ) <EOL> unpackers = '<STR_LIT:U+002C>' . join ( [ '<STR_LIT>' % i for i in rg ] ) <EOL> source = py . code . Source ( """<STR_LIT>""" % locals ( ) ) <EOL> exec source . compile ( ) in miniglobals <EOL> self . unpack = miniglobals [ '<STR_LIT>' ] <EOL> def unpack ( self , s ) : <EOL> res = unpack ( self . fmt , s ) <EOL> if len ( res ) == <NUM_LIT:1> : <EOL> return res [ <NUM_LIT:0> ] <EOL> return res <EOL> def _freeze_ ( self ) : <EOL> assert self . formats <EOL> self . _create_unpacking_func ( ) <EOL> return True <EOL> def create_unpacker ( unpack_str ) : <EOL> fmtiter = FrozenUnpackIterator ( unpack_str ) <EOL> fmtiter . interpret ( unpack_str ) <EOL> return fmtiter <EOL> create_unpacker . _annspecialcase_ = '<STR_LIT>' <EOL> def runpack ( fmt , input ) : <EOL> unpacker = create_unpacker ( fmt ) <EOL> return unpacker . unpack ( input ) <EOL> runpack . _annspecialcase_ = '<STR_LIT>' </s>
