21
21
# input. The resulting code (xx 90 90) would appear to be interpreted as an
22
22
# escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
23
23
#
24
- import sys
24
+ import io
25
25
import os
26
+ import sys
26
27
import struct
27
28
import binascii
28
29
@@ -80,13 +81,10 @@ def __init__(self):
80
81
81
82
def getfileinfo (name ):
82
83
finfo = FInfo ()
84
+ fp = io .open (name , 'rb' )
83
85
# Quick check for textfile
84
- fp = open (name )
85
- data = open (name ).read (256 )
86
- for c in data :
87
- if not c .isspace () and (c < ' ' or ord (c ) > 0x7f ):
88
- break
89
- else :
86
+ data = fp .read (512 )
87
+ if 0 not in data :
90
88
finfo .Type = 'TEXT'
91
89
fp .seek (0 , 2 )
92
90
dsize = fp .tell ()
@@ -100,7 +98,7 @@ def __init__(self, *args):
100
98
pass
101
99
102
100
def read (self , * args ):
103
- return ''
101
+ return b ''
104
102
105
103
def write (self , * args ):
106
104
pass
@@ -113,8 +111,8 @@ class _Hqxcoderengine:
113
111
114
112
def __init__ (self , ofp ):
115
113
self .ofp = ofp
116
- self .data = ''
117
- self .hqxdata = ''
114
+ self .data = b ''
115
+ self .hqxdata = b ''
118
116
self .linelen = LINELEN - 1
119
117
120
118
def write (self , data ):
@@ -132,12 +130,12 @@ def _flush(self, force):
132
130
first = 0
133
131
while first <= len (self .hqxdata )- self .linelen :
134
132
last = first + self .linelen
135
- self .ofp .write (self .hqxdata [first :last ]+ '\n ' )
133
+ self .ofp .write (self .hqxdata [first :last ]+ b '\n ' )
136
134
self .linelen = LINELEN
137
135
first = last
138
136
self .hqxdata = self .hqxdata [first :]
139
137
if force :
140
- self .ofp .write (self .hqxdata + ':\n ' )
138
+ self .ofp .write (self .hqxdata + b ':\n ' )
141
139
142
140
def close (self ):
143
141
if self .data :
@@ -152,15 +150,15 @@ class _Rlecoderengine:
152
150
153
151
def __init__ (self , ofp ):
154
152
self .ofp = ofp
155
- self .data = ''
153
+ self .data = b ''
156
154
157
155
def write (self , data ):
158
156
self .data = self .data + data
159
157
if len (self .data ) < REASONABLY_LARGE :
160
158
return
161
159
rledata = binascii .rlecode_hqx (self .data )
162
160
self .ofp .write (rledata )
163
- self .data = ''
161
+ self .data = b ''
164
162
165
163
def close (self ):
166
164
if self .data :
@@ -172,7 +170,7 @@ def close(self):
172
170
class BinHex :
173
171
def __init__ (self , name_finfo_dlen_rlen , ofp ):
174
172
name , finfo , dlen , rlen = name_finfo_dlen_rlen
175
- if type (ofp ) == type ( '' ):
173
+ if isinstance (ofp , basestring ):
176
174
ofname = ofp
177
175
ofp = open (ofname , 'w' )
178
176
if os .name == 'mac' :
@@ -193,8 +191,8 @@ def _writeinfo(self, name, finfo):
193
191
nl = len (name )
194
192
if nl > 63 :
195
193
raise Error , 'Filename too long'
196
- d = chr (nl ) + name + '\0 '
197
- d2 = finfo .Type + finfo .Creator
194
+ d = bytes ( chr (nl )) + bytes ( name ) + b '\0 '
195
+ d2 = bytes ( finfo .Type , "latin-1" ) + bytes ( finfo .Creator , "latin-1" )
198
196
199
197
# Force all structs to be packed with big-endian
200
198
d3 = struct .pack ('>h' , finfo .Flags )
@@ -281,7 +279,7 @@ def __init__(self, ifp):
281
279
282
280
def read (self , totalwtd ):
283
281
"""Read at least wtd bytes (or until EOF)"""
284
- decdata = ''
282
+ decdata = b ''
285
283
wtd = totalwtd
286
284
#
287
285
# The loop here is convoluted, since we don't really now how
@@ -321,8 +319,8 @@ class _Rledecoderengine:
321
319
322
320
def __init__ (self , ifp ):
323
321
self .ifp = ifp
324
- self .pre_buffer = ''
325
- self .post_buffer = ''
322
+ self .pre_buffer = b ''
323
+ self .post_buffer = b ''
326
324
self .eof = 0
327
325
328
326
def read (self , wtd ):
@@ -337,7 +335,7 @@ def _fill(self, wtd):
337
335
if self .ifp .eof :
338
336
self .post_buffer = self .post_buffer + \
339
337
binascii .rledecode_hqx (self .pre_buffer )
340
- self .pre_buffer = ''
338
+ self .pre_buffer = b ''
341
339
return
342
340
343
341
#
@@ -372,7 +370,7 @@ def close(self):
372
370
373
371
class HexBin :
374
372
def __init__ (self , ifp ):
375
- if type (ifp ) == type ( '' ):
373
+ if isinstance (ifp , basestring ):
376
374
ifp = open (ifp )
377
375
#
378
376
# Find initial colon.
@@ -438,7 +436,7 @@ def read(self, *n):
438
436
n = min (n , self .dlen )
439
437
else :
440
438
n = self .dlen
441
- rv = ''
439
+ rv = b ''
442
440
while len (rv ) < n :
443
441
rv = rv + self ._read (n - len (rv ))
444
442
self .dlen = self .dlen - n
0 commit comments