summaryrefslogtreecommitdiffstats
path: root/Lib/binhex.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2011-03-17 02:11:09 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2011-03-17 02:11:09 (GMT)
commitbab07a652d811f2621538ffed6c46a41be2da888 (patch)
treefdc4fb419aec17f4db492c06e0b97955dbaf7057 /Lib/binhex.py
parentc9dacc27cfdd45d80c5d6c4a2e481df7042be0c5 (diff)
parentb52c0be4d284782475a96ce070075b5e912bccaf (diff)
downloadcpython-bab07a652d811f2621538ffed6c46a41be2da888.zip
cpython-bab07a652d811f2621538ffed6c46a41be2da888.tar.gz
cpython-bab07a652d811f2621538ffed6c46a41be2da888.tar.bz2
Tidy up merge with remote
Diffstat (limited to 'Lib/binhex.py')
-rw-r--r--Lib/binhex.py42
1 files changed, 24 insertions, 18 deletions
diff --git a/Lib/binhex.py b/Lib/binhex.py
index 4b7997a..999a675 100644
--- a/Lib/binhex.py
+++ b/Lib/binhex.py
@@ -52,14 +52,13 @@ class FInfo:
def getfileinfo(name):
finfo = FInfo()
- fp = io.open(name, 'rb')
- # Quick check for textfile
- data = fp.read(512)
- if 0 not in data:
- finfo.Type = 'TEXT'
- fp.seek(0, 2)
- dsize = fp.tell()
- fp.close()
+ with io.open(name, 'rb') as fp:
+ # Quick check for textfile
+ data = fp.read(512)
+ if 0 not in data:
+ finfo.Type = 'TEXT'
+ fp.seek(0, 2)
+ dsize = fp.tell()
dir, file = os.path.split(name)
file = file.replace(':', '-', 1)
return file, finfo, dsize, 0
@@ -140,19 +139,26 @@ class _Rlecoderengine:
class BinHex:
def __init__(self, name_finfo_dlen_rlen, ofp):
name, finfo, dlen, rlen = name_finfo_dlen_rlen
+ close_on_error = False
if isinstance(ofp, str):
ofname = ofp
ofp = io.open(ofname, 'wb')
- ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:')
- hqxer = _Hqxcoderengine(ofp)
- self.ofp = _Rlecoderengine(hqxer)
- self.crc = 0
- if finfo is None:
- finfo = FInfo()
- self.dlen = dlen
- self.rlen = rlen
- self._writeinfo(name, finfo)
- self.state = _DID_HEADER
+ close_on_error = True
+ try:
+ ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:')
+ hqxer = _Hqxcoderengine(ofp)
+ self.ofp = _Rlecoderengine(hqxer)
+ self.crc = 0
+ if finfo is None:
+ finfo = FInfo()
+ self.dlen = dlen
+ self.rlen = rlen
+ self._writeinfo(name, finfo)
+ self.state = _DID_HEADER
+ except:
+ if close_on_error:
+ ofp.close()
+ raise
def _writeinfo(self, name, finfo):
nl = len(name)