mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	Use (c)StringIO for collecting bytes. Fixes bug #451622.
This commit is contained in:
		
							parent
							
								
									63a8d69476
								
							
						
					
					
						commit
						c47016ee74
					
				
					 1 changed files with 12 additions and 8 deletions
				
			
		| 
						 | 
					@ -5,6 +5,10 @@
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import struct
 | 
					import struct
 | 
				
			||||||
 | 
					try:
 | 
				
			||||||
 | 
					    from cStringIO import StringIO as _StringIO
 | 
				
			||||||
 | 
					except ImportError:
 | 
				
			||||||
 | 
					    from StringIO import StringIO as _StringIO
 | 
				
			||||||
 | 
					
 | 
				
			||||||
__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
 | 
					__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,22 +43,22 @@ def __init__(self):
 | 
				
			||||||
        self.reset()
 | 
					        self.reset()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def reset(self):
 | 
					    def reset(self):
 | 
				
			||||||
        self.__buf = ''
 | 
					        self.__buf = _StringIO()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_buffer(self):
 | 
					    def get_buffer(self):
 | 
				
			||||||
        return self.__buf
 | 
					        return self.__buf.getvalue()
 | 
				
			||||||
    # backwards compatibility
 | 
					    # backwards compatibility
 | 
				
			||||||
    get_buf = get_buffer
 | 
					    get_buf = get_buffer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def pack_uint(self, x):
 | 
					    def pack_uint(self, x):
 | 
				
			||||||
        self.__buf = self.__buf + struct.pack('>L', x)
 | 
					        self.__buf.write(struct.pack('>L', x))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pack_int = pack_uint
 | 
					    pack_int = pack_uint
 | 
				
			||||||
    pack_enum = pack_int
 | 
					    pack_enum = pack_int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def pack_bool(self, x):
 | 
					    def pack_bool(self, x):
 | 
				
			||||||
        if x: self.__buf = self.__buf + '\0\0\0\1'
 | 
					        if x: self.__buf.write('\0\0\0\1')
 | 
				
			||||||
        else: self.__buf = self.__buf + '\0\0\0\0'
 | 
					        else: self.__buf.write('\0\0\0\0')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def pack_uhyper(self, x):
 | 
					    def pack_uhyper(self, x):
 | 
				
			||||||
        self.pack_uint(x>>32 & 0xffffffffL)
 | 
					        self.pack_uint(x>>32 & 0xffffffffL)
 | 
				
			||||||
| 
						 | 
					@ -63,12 +67,12 @@ def pack_uhyper(self, x):
 | 
				
			||||||
    pack_hyper = pack_uhyper
 | 
					    pack_hyper = pack_uhyper
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def pack_float(self, x):
 | 
					    def pack_float(self, x):
 | 
				
			||||||
        try: self.__buf = self.__buf + struct.pack('>f', x)
 | 
					        try: self.__buf.write(struct.pack('>f', x))
 | 
				
			||||||
        except struct.error, msg:
 | 
					        except struct.error, msg:
 | 
				
			||||||
            raise ConversionError, msg
 | 
					            raise ConversionError, msg
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def pack_double(self, x):
 | 
					    def pack_double(self, x):
 | 
				
			||||||
        try: self.__buf = self.__buf + struct.pack('>d', x)
 | 
					        try: self.__buf.write(struct.pack('>d', x))
 | 
				
			||||||
        except struct.error, msg:
 | 
					        except struct.error, msg:
 | 
				
			||||||
            raise ConversionError, msg
 | 
					            raise ConversionError, msg
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -78,7 +82,7 @@ def pack_fstring(self, n, s):
 | 
				
			||||||
        n = ((n+3)/4)*4
 | 
					        n = ((n+3)/4)*4
 | 
				
			||||||
        data = s[:n]
 | 
					        data = s[:n]
 | 
				
			||||||
        data = data + (n - len(data)) * '\0'
 | 
					        data = data + (n - len(data)) * '\0'
 | 
				
			||||||
        self.__buf = self.__buf + data
 | 
					        self.__buf.write(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pack_fopaque = pack_fstring
 | 
					    pack_fopaque = pack_fstring
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue