1
1
from __future__ import absolute_import
2
2
from builtins import range
3
+ from future import standard_library
4
+ standard_library .install_aliases ()
3
5
from .profile import ProfileImpl
4
- import StringIO
5
6
import bz2
6
7
import copy
7
8
import io
@@ -92,7 +93,7 @@ def readNum(fobj):
92
93
n = 0
93
94
shift = 0
94
95
while True :
95
- b = ord (fobj .read (1 ))
96
+ b = bytearray (fobj .read (1 ))[ 0 ]
96
97
n |= (b & 0x7F ) << shift
97
98
shift += 7
98
99
if (b & 0x80 ) == 0 :
@@ -108,7 +109,7 @@ def writeNum(fobj, n):
108
109
n >>= 7
109
110
if n != 0 :
110
111
b |= 0x80
111
- fobj .write (chr ( b ))
112
+ fobj .write (bytearray ([ b ] ))
112
113
113
114
if n == 0 :
114
115
break
@@ -118,15 +119,15 @@ def readString(fobj):
118
119
"""
119
120
Read a string from a stream.
120
121
"""
121
- return fobj .readline ()[:- 1 ]
122
+ return fobj .readline ()[:- 1 ]. decode ()
122
123
123
124
124
125
def writeString (fobj , s ):
125
126
"""
126
127
Write a string to a stream.
127
128
"""
128
- fobj .write (str ( s ))
129
- fobj .write ('\n ' )
129
+ fobj .write (s . encode ( ))
130
+ fobj .write (u '\n '. encode () )
130
131
131
132
132
133
def readFloat (fobj ):
@@ -183,7 +184,7 @@ def copy(self):
183
184
class CompressedSection (Section ):
184
185
def read (self , fobj ):
185
186
fobj .seek (self .offset + self .start )
186
- _io = StringIO . StringIO (bz2 .decompress (fobj .read (self .size )))
187
+ _io = io . BytesIO (bz2 .decompress (fobj .read (self .size )))
187
188
return self .deserialize (_io )
188
189
189
190
def write (self , fobj ):
@@ -214,12 +215,12 @@ def read(self, fobj):
214
215
raise NotImplementedError ()
215
216
216
217
else :
217
- _io = StringIO . StringIO (bz2 .decompress (fobj .read (self .size )))
218
+ _io = io . BytesIO (bz2 .decompress (fobj .read (self .size )))
218
219
self .size = len (_io .getvalue ())
219
220
return self .deserialize (_io )
220
221
221
222
def write (self , fobj ):
222
- _io = StringIO . StringIO ()
223
+ _io = io . BytesIO ()
223
224
if self .pool_fname :
224
225
raise NotImplementedError ()
225
226
@@ -334,7 +335,7 @@ def setOffsetFor(self, fname, value):
334
335
335
336
def extractForFunction (self , fname , counters ):
336
337
offset = self .function_offsets [fname ]
337
- _io = StringIO . StringIO (self .data )
338
+ _io = io . BytesIO (self .data )
338
339
_io .seek (offset )
339
340
counters .sort ()
340
341
while True :
@@ -384,7 +385,7 @@ def setOffsetFor(self, fname, value):
384
385
385
386
def extractForFunction (self , fname ):
386
387
offset = self .function_offsets [fname ]
387
- _io = StringIO . StringIO (self .data )
388
+ _io = io . BytesIO (self .data )
388
389
_io .seek (offset )
389
390
last_address = 0
390
391
while True :
@@ -436,7 +437,7 @@ def setOffsetFor(self, fname, value):
436
437
def extractForFunction (self , fname ):
437
438
offset = self .function_offsets [fname ]
438
439
439
- _io = StringIO . StringIO (self .data )
440
+ _io = io . BytesIO (self .data )
440
441
_io .seek (offset )
441
442
while True :
442
443
n = readNum (_io )
@@ -455,7 +456,7 @@ def __init__(self):
455
456
# Populate data with a single character initially so that zero is
456
457
# never a valid string pool index. LineText relies upon this to use
457
458
# zero as a sentinel.
458
- self .data = StringIO . StringIO ( '\n ' )
459
+ self .data = io . BytesIO ( u '\n '. encode () )
459
460
self .pool_read = False
460
461
461
462
def serialize (self , fobj ):
@@ -464,7 +465,7 @@ def serialize(self, fobj):
464
465
465
466
def deserialize (self , fobj ):
466
467
# FIXME: Make this lazy!
467
- self .data = StringIO . StringIO (fobj .read (self .size ))
468
+ self .data = io . BytesIO (fobj .read (self .size ))
468
469
469
470
def upgrade (self , impl ):
470
471
pass
@@ -591,7 +592,7 @@ def deserialize(fobj):
591
592
def serialize (self , fname = None ):
592
593
# If we're not writing to a file, emulate a file object instead.
593
594
if fname is None :
594
- fobj = StringIO . StringIO ()
595
+ fobj = io . BytesIO ()
595
596
else :
596
597
fobj = open (fname , 'wb' )
597
598
@@ -613,7 +614,7 @@ def serialize(self, fname=None):
613
614
614
615
# We need to write all sections first, so we know their offset
615
616
# before we write the header.
616
- tmpio = StringIO . StringIO ()
617
+ tmpio = io . BytesIO ()
617
618
offsets = {}
618
619
sizes = {}
619
620
for section in sections :
0 commit comments