Make test_binhex pass. (Do we really want to support it still?)

This commit is contained in:
Guido van Rossum 2007-05-22 22:25:42 +00:00
parent 4581ae5fa2
commit dcee3c0d1f
2 changed files with 26 additions and 31 deletions

View File

@ -21,8 +21,9 @@ hexbin(inputfilename, outputfilename)
# input. The resulting code (xx 90 90) would appear to be interpreted as an # input. The resulting code (xx 90 90) would appear to be interpreted as an
# escaped *value* of 0x90. All coders I've seen appear to ignore this nicety... # escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
# #
import sys import io
import os import os
import sys
import struct import struct
import binascii import binascii
@ -80,13 +81,10 @@ except ImportError:
def getfileinfo(name): def getfileinfo(name):
finfo = FInfo() finfo = FInfo()
fp = io.open(name, 'rb')
# Quick check for textfile # Quick check for textfile
fp = open(name) data = fp.read(512)
data = open(name).read(256) if 0 not in data:
for c in data:
if not c.isspace() and (c<' ' or ord(c) > 0x7f):
break
else:
finfo.Type = 'TEXT' finfo.Type = 'TEXT'
fp.seek(0, 2) fp.seek(0, 2)
dsize = fp.tell() dsize = fp.tell()
@ -100,7 +98,7 @@ except ImportError:
pass pass
def read(self, *args): def read(self, *args):
return '' return b''
def write(self, *args): def write(self, *args):
pass pass
@ -113,8 +111,8 @@ class _Hqxcoderengine:
def __init__(self, ofp): def __init__(self, ofp):
self.ofp = ofp self.ofp = ofp
self.data = '' self.data = b''
self.hqxdata = '' self.hqxdata = b''
self.linelen = LINELEN-1 self.linelen = LINELEN-1
def write(self, data): def write(self, data):
@ -132,12 +130,12 @@ class _Hqxcoderengine:
first = 0 first = 0
while first <= len(self.hqxdata)-self.linelen: while first <= len(self.hqxdata)-self.linelen:
last = first + self.linelen last = first + self.linelen
self.ofp.write(self.hqxdata[first:last]+'\n') self.ofp.write(self.hqxdata[first:last]+b'\n')
self.linelen = LINELEN self.linelen = LINELEN
first = last first = last
self.hqxdata = self.hqxdata[first:] self.hqxdata = self.hqxdata[first:]
if force: if force:
self.ofp.write(self.hqxdata + ':\n') self.ofp.write(self.hqxdata + b':\n')
def close(self): def close(self):
if self.data: if self.data:
@ -152,7 +150,7 @@ class _Rlecoderengine:
def __init__(self, ofp): def __init__(self, ofp):
self.ofp = ofp self.ofp = ofp
self.data = '' self.data = b''
def write(self, data): def write(self, data):
self.data = self.data + data self.data = self.data + data
@ -160,7 +158,7 @@ class _Rlecoderengine:
return return
rledata = binascii.rlecode_hqx(self.data) rledata = binascii.rlecode_hqx(self.data)
self.ofp.write(rledata) self.ofp.write(rledata)
self.data = '' self.data = b''
def close(self): def close(self):
if self.data: if self.data:
@ -172,7 +170,7 @@ class _Rlecoderengine:
class BinHex: class BinHex:
def __init__(self, name_finfo_dlen_rlen, ofp): def __init__(self, name_finfo_dlen_rlen, ofp):
name, finfo, dlen, rlen = name_finfo_dlen_rlen name, finfo, dlen, rlen = name_finfo_dlen_rlen
if type(ofp) == type(''): if isinstance(ofp, basestring):
ofname = ofp ofname = ofp
ofp = open(ofname, 'w') ofp = open(ofname, 'w')
if os.name == 'mac': if os.name == 'mac':
@ -193,8 +191,8 @@ class BinHex:
nl = len(name) nl = len(name)
if nl > 63: if nl > 63:
raise Error, 'Filename too long' raise Error, 'Filename too long'
d = chr(nl) + name + '\0' d = bytes(chr(nl)) + bytes(name) + b'\0'
d2 = finfo.Type + finfo.Creator d2 = bytes(finfo.Type, "latin-1") + bytes(finfo.Creator, "latin-1")
# Force all structs to be packed with big-endian # Force all structs to be packed with big-endian
d3 = struct.pack('>h', finfo.Flags) d3 = struct.pack('>h', finfo.Flags)
@ -281,7 +279,7 @@ class _Hqxdecoderengine:
def read(self, totalwtd): def read(self, totalwtd):
"""Read at least wtd bytes (or until EOF)""" """Read at least wtd bytes (or until EOF)"""
decdata = '' decdata = b''
wtd = totalwtd wtd = totalwtd
# #
# The loop here is convoluted, since we don't really now how # The loop here is convoluted, since we don't really now how
@ -321,8 +319,8 @@ class _Rledecoderengine:
def __init__(self, ifp): def __init__(self, ifp):
self.ifp = ifp self.ifp = ifp
self.pre_buffer = '' self.pre_buffer = b''
self.post_buffer = '' self.post_buffer = b''
self.eof = 0 self.eof = 0
def read(self, wtd): def read(self, wtd):
@ -337,7 +335,7 @@ class _Rledecoderengine:
if self.ifp.eof: if self.ifp.eof:
self.post_buffer = self.post_buffer + \ self.post_buffer = self.post_buffer + \
binascii.rledecode_hqx(self.pre_buffer) binascii.rledecode_hqx(self.pre_buffer)
self.pre_buffer = '' self.pre_buffer = b''
return return
# #
@ -372,7 +370,7 @@ class _Rledecoderengine:
class HexBin: class HexBin:
def __init__(self, ifp): def __init__(self, ifp):
if type(ifp) == type(''): if isinstance(ifp, basestring):
ifp = open(ifp) ifp = open(ifp)
# #
# Find initial colon. # Find initial colon.
@ -438,7 +436,7 @@ class HexBin:
n = min(n, self.dlen) n = min(n, self.dlen)
else: else:
n = self.dlen n = self.dlen
rv = '' rv = b''
while len(rv) < n: while len(rv) < n:
rv = rv + self._read(n-len(rv)) rv = rv + self._read(n-len(rv))
self.dlen = self.dlen - n self.dlen = self.dlen - n

View File

@ -17,16 +17,13 @@ class BinHexTestCase(unittest.TestCase):
self.fname2 = test_support.TESTFN + "2" self.fname2 = test_support.TESTFN + "2"
def tearDown(self): def tearDown(self):
try: os.unlink(self.fname1) test_support.unlink(self.fname1)
except OSError: pass test_support.unlink(self.fname2)
try: os.unlink(self.fname2) DATA = b'Jack is my hero'
except OSError: pass
DATA = 'Jack is my hero'
def test_binhex(self): def test_binhex(self):
f = open(self.fname1, 'w') f = open(self.fname1, 'wb')
f.write(self.DATA) f.write(self.DATA)
f.close() f.close()
@ -34,7 +31,7 @@ class BinHexTestCase(unittest.TestCase):
binhex.hexbin(self.fname2, self.fname1) binhex.hexbin(self.fname2, self.fname1)
f = open(self.fname1, 'r') f = open(self.fname1, 'rb')
finish = f.readline() finish = f.readline()
f.close() f.close()