Skip to content

Commit dcee3c0

Browse files
committed
Make test_binhex pass. (Do we really want to support it still?)
1 parent 4581ae5 commit dcee3c0

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

Lib/binhex.py

+21-23
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
# input. The resulting code (xx 90 90) would appear to be interpreted as an
2222
# escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
2323
#
24-
import sys
24+
import io
2525
import os
26+
import sys
2627
import struct
2728
import binascii
2829

@@ -80,13 +81,10 @@ def __init__(self):
8081

8182
def getfileinfo(name):
8283
finfo = FInfo()
84+
fp = io.open(name, 'rb')
8385
# 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:
9088
finfo.Type = 'TEXT'
9189
fp.seek(0, 2)
9290
dsize = fp.tell()
@@ -100,7 +98,7 @@ def __init__(self, *args):
10098
pass
10199

102100
def read(self, *args):
103-
return ''
101+
return b''
104102

105103
def write(self, *args):
106104
pass
@@ -113,8 +111,8 @@ class _Hqxcoderengine:
113111

114112
def __init__(self, ofp):
115113
self.ofp = ofp
116-
self.data = ''
117-
self.hqxdata = ''
114+
self.data = b''
115+
self.hqxdata = b''
118116
self.linelen = LINELEN-1
119117

120118
def write(self, data):
@@ -132,12 +130,12 @@ def _flush(self, force):
132130
first = 0
133131
while first <= len(self.hqxdata)-self.linelen:
134132
last = first + self.linelen
135-
self.ofp.write(self.hqxdata[first:last]+'\n')
133+
self.ofp.write(self.hqxdata[first:last]+b'\n')
136134
self.linelen = LINELEN
137135
first = last
138136
self.hqxdata = self.hqxdata[first:]
139137
if force:
140-
self.ofp.write(self.hqxdata + ':\n')
138+
self.ofp.write(self.hqxdata + b':\n')
141139

142140
def close(self):
143141
if self.data:
@@ -152,15 +150,15 @@ class _Rlecoderengine:
152150

153151
def __init__(self, ofp):
154152
self.ofp = ofp
155-
self.data = ''
153+
self.data = b''
156154

157155
def write(self, data):
158156
self.data = self.data + data
159157
if len(self.data) < REASONABLY_LARGE:
160158
return
161159
rledata = binascii.rlecode_hqx(self.data)
162160
self.ofp.write(rledata)
163-
self.data = ''
161+
self.data = b''
164162

165163
def close(self):
166164
if self.data:
@@ -172,7 +170,7 @@ def close(self):
172170
class BinHex:
173171
def __init__(self, name_finfo_dlen_rlen, ofp):
174172
name, finfo, dlen, rlen = name_finfo_dlen_rlen
175-
if type(ofp) == type(''):
173+
if isinstance(ofp, basestring):
176174
ofname = ofp
177175
ofp = open(ofname, 'w')
178176
if os.name == 'mac':
@@ -193,8 +191,8 @@ def _writeinfo(self, name, finfo):
193191
nl = len(name)
194192
if nl > 63:
195193
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")
198196

199197
# Force all structs to be packed with big-endian
200198
d3 = struct.pack('>h', finfo.Flags)
@@ -281,7 +279,7 @@ def __init__(self, ifp):
281279

282280
def read(self, totalwtd):
283281
"""Read at least wtd bytes (or until EOF)"""
284-
decdata = ''
282+
decdata = b''
285283
wtd = totalwtd
286284
#
287285
# The loop here is convoluted, since we don't really now how
@@ -321,8 +319,8 @@ class _Rledecoderengine:
321319

322320
def __init__(self, ifp):
323321
self.ifp = ifp
324-
self.pre_buffer = ''
325-
self.post_buffer = ''
322+
self.pre_buffer = b''
323+
self.post_buffer = b''
326324
self.eof = 0
327325

328326
def read(self, wtd):
@@ -337,7 +335,7 @@ def _fill(self, wtd):
337335
if self.ifp.eof:
338336
self.post_buffer = self.post_buffer + \
339337
binascii.rledecode_hqx(self.pre_buffer)
340-
self.pre_buffer = ''
338+
self.pre_buffer = b''
341339
return
342340

343341
#
@@ -372,7 +370,7 @@ def close(self):
372370

373371
class HexBin:
374372
def __init__(self, ifp):
375-
if type(ifp) == type(''):
373+
if isinstance(ifp, basestring):
376374
ifp = open(ifp)
377375
#
378376
# Find initial colon.
@@ -438,7 +436,7 @@ def read(self, *n):
438436
n = min(n, self.dlen)
439437
else:
440438
n = self.dlen
441-
rv = ''
439+
rv = b''
442440
while len(rv) < n:
443441
rv = rv + self._read(n-len(rv))
444442
self.dlen = self.dlen - n

Lib/test/test_binhex.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,21 @@ def setUp(self):
1717
self.fname2 = test_support.TESTFN + "2"
1818

1919
def tearDown(self):
20-
try: os.unlink(self.fname1)
21-
except OSError: pass
20+
test_support.unlink(self.fname1)
21+
test_support.unlink(self.fname2)
2222

23-
try: os.unlink(self.fname2)
24-
except OSError: pass
25-
26-
DATA = 'Jack is my hero'
23+
DATA = b'Jack is my hero'
2724

2825
def test_binhex(self):
29-
f = open(self.fname1, 'w')
26+
f = open(self.fname1, 'wb')
3027
f.write(self.DATA)
3128
f.close()
3229

3330
binhex.binhex(self.fname1, self.fname2)
3431

3532
binhex.hexbin(self.fname2, self.fname1)
3633

37-
f = open(self.fname1, 'r')
34+
f = open(self.fname1, 'rb')
3835
finish = f.readline()
3936
f.close()
4037

0 commit comments

Comments
 (0)