#NEWLINE# def write_varlen_number( stream , value ) : #NEWLINE# #INDENT# if ( value == 0 ) : #NEWLINE# #INDENT# stream . write ( b'\x00' ) #NEWLINE# return #NEWLINE# #UNINDENT# pieces = [ ] #NEWLINE# while value : #NEWLINE# #INDENT# ( x , value ) = ( ( x & 127 ) , ( value >> 7 ) ) #NEWLINE# if len ( pieces ) : #NEWLINE# #INDENT# x |= 128 #NEWLINE# #UNINDENT# pieces . insert ( 0 , x ) #NEWLINE# if ( len ( pieces ) > 4096 ) : #NEWLINE# #INDENT# raise ValueError ( 'Number too large' ) #NEWLINE# #UNINDENT# #UNINDENT# stream . write ( struct . pack ( ( '%dB' % len ( pieces ) ) , * pieces ) )