[Python-3000-checkins] r55518 - in python/branches/py3k-struni/Lib: binhex.py test/test_binhex.py

guido.van.rossum python-3000-checkins at python.org
Wed May 23 00:25:43 CEST 2007


Author: guido.van.rossum
Date: Wed May 23 00:25:42 2007
New Revision: 55518

Modified:
   python/branches/py3k-struni/Lib/binhex.py
   python/branches/py3k-struni/Lib/test/test_binhex.py
Log:
Make test_binhex pass.  (Do we really want to support it still?)


Modified: python/branches/py3k-struni/Lib/binhex.py
==============================================================================
--- python/branches/py3k-struni/Lib/binhex.py	(original)
+++ python/branches/py3k-struni/Lib/binhex.py	Wed May 23 00:25:42 2007
@@ -21,8 +21,9 @@
 # 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...
 #
-import sys
+import io
 import os
+import sys
 import struct
 import binascii
 
@@ -80,13 +81,10 @@
 
     def getfileinfo(name):
         finfo = FInfo()
+        fp = io.open(name, 'rb')
         # Quick check for textfile
-        fp = open(name)
-        data = open(name).read(256)
-        for c in data:
-            if not c.isspace() and (c<' ' or ord(c) > 0x7f):
-                break
-        else:
+        data = fp.read(512)
+        if 0 not in data:
             finfo.Type = 'TEXT'
         fp.seek(0, 2)
         dsize = fp.tell()
@@ -100,7 +98,7 @@
             pass
 
         def read(self, *args):
-            return ''
+            return b''
 
         def write(self, *args):
             pass
@@ -113,8 +111,8 @@
 
     def __init__(self, ofp):
         self.ofp = ofp
-        self.data = ''
-        self.hqxdata = ''
+        self.data = b''
+        self.hqxdata = b''
         self.linelen = LINELEN-1
 
     def write(self, data):
@@ -132,12 +130,12 @@
         first = 0
         while first <= len(self.hqxdata)-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
             first = last
         self.hqxdata = self.hqxdata[first:]
         if force:
-            self.ofp.write(self.hqxdata + ':\n')
+            self.ofp.write(self.hqxdata + b':\n')
 
     def close(self):
         if self.data:
@@ -152,7 +150,7 @@
 
     def __init__(self, ofp):
         self.ofp = ofp
-        self.data = ''
+        self.data = b''
 
     def write(self, data):
         self.data = self.data + data
@@ -160,7 +158,7 @@
             return
         rledata = binascii.rlecode_hqx(self.data)
         self.ofp.write(rledata)
-        self.data = ''
+        self.data = b''
 
     def close(self):
         if self.data:
@@ -172,7 +170,7 @@
 class BinHex:
     def __init__(self, name_finfo_dlen_rlen, ofp):
         name, finfo, dlen, rlen = name_finfo_dlen_rlen
-        if type(ofp) == type(''):
+        if isinstance(ofp, basestring):
             ofname = ofp
             ofp = open(ofname, 'w')
             if os.name == 'mac':
@@ -193,8 +191,8 @@
         nl = len(name)
         if nl > 63:
             raise Error, 'Filename too long'
-        d = chr(nl) + name + '\0'
-        d2 = finfo.Type + finfo.Creator
+        d = bytes(chr(nl)) + bytes(name) + b'\0'
+        d2 = bytes(finfo.Type, "latin-1") + bytes(finfo.Creator, "latin-1")
 
         # Force all structs to be packed with big-endian
         d3 = struct.pack('>h', finfo.Flags)
@@ -281,7 +279,7 @@
 
     def read(self, totalwtd):
         """Read at least wtd bytes (or until EOF)"""
-        decdata = ''
+        decdata = b''
         wtd = totalwtd
         #
         # The loop here is convoluted, since we don't really now how
@@ -321,8 +319,8 @@
 
     def __init__(self, ifp):
         self.ifp = ifp
-        self.pre_buffer = ''
-        self.post_buffer = ''
+        self.pre_buffer = b''
+        self.post_buffer = b''
         self.eof = 0
 
     def read(self, wtd):
@@ -337,7 +335,7 @@
         if self.ifp.eof:
             self.post_buffer = self.post_buffer + \
                 binascii.rledecode_hqx(self.pre_buffer)
-            self.pre_buffer = ''
+            self.pre_buffer = b''
             return
 
         #
@@ -372,7 +370,7 @@
 
 class HexBin:
     def __init__(self, ifp):
-        if type(ifp) == type(''):
+        if isinstance(ifp, basestring):
             ifp = open(ifp)
         #
         # Find initial colon.
@@ -438,7 +436,7 @@
             n = min(n, self.dlen)
         else:
             n = self.dlen
-        rv = ''
+        rv = b''
         while len(rv) < n:
             rv = rv + self._read(n-len(rv))
         self.dlen = self.dlen - n

Modified: python/branches/py3k-struni/Lib/test/test_binhex.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_binhex.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_binhex.py	Wed May 23 00:25:42 2007
@@ -17,16 +17,13 @@
         self.fname2 = test_support.TESTFN + "2"
 
     def tearDown(self):
-        try: os.unlink(self.fname1)
-        except OSError: pass
+        test_support.unlink(self.fname1)
+        test_support.unlink(self.fname2)
 
-        try: os.unlink(self.fname2)
-        except OSError: pass
-
-    DATA = 'Jack is my hero'
+    DATA = b'Jack is my hero'
 
     def test_binhex(self):
-        f = open(self.fname1, 'w')
+        f = open(self.fname1, 'wb')
         f.write(self.DATA)
         f.close()
 
@@ -34,7 +31,7 @@
 
         binhex.hexbin(self.fname2, self.fname1)
 
-        f = open(self.fname1, 'r')
+        f = open(self.fname1, 'rb')
         finish = f.readline()
         f.close()
 


More information about the Python-3000-checkins mailing list