summaryrefslogtreecommitdiff
path: root/Lib/binhex.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-04-10 13:29:28 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2015-04-10 13:29:28 +0300
commit2116b12da59f77358cc539b90f58a3cdea43c2fd (patch)
tree693d26652b1dbf8b2ac058c5725f9726c3d14a0b /Lib/binhex.py
parentfcbf8f3e3d46eb023dd9a3954c6f9ed9a533d427 (diff)
parent7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd (diff)
downloadcpython-git-2116b12da59f77358cc539b90f58a3cdea43c2fd.tar.gz
Issue #23865: close() methods in multiple modules now are idempotent and more
robust at shutdown. If needs to release multiple resources, they are released even if errors are occured.
Diffstat (limited to 'Lib/binhex.py')
-rw-r--r--Lib/binhex.py42
1 files changed, 26 insertions, 16 deletions
diff --git a/Lib/binhex.py b/Lib/binhex.py
index 14badb71aa..56b5f852c0 100644
--- a/Lib/binhex.py
+++ b/Lib/binhex.py
@@ -32,7 +32,8 @@ class Error(Exception):
pass
# States (what have we written)
-[_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
+_DID_HEADER = 0
+_DID_DATA = 1
# Various constants
REASONABLY_LARGE = 32768 # Minimal amount we pass the rle-coder
@@ -213,16 +214,21 @@ class BinHex:
self._write(data)
def close(self):
- if self.state < _DID_DATA:
- self.close_data()
- if self.state != _DID_DATA:
- raise Error('Close at the wrong time')
- if self.rlen != 0:
- raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
- self._writecrc()
- self.ofp.close()
- self.state = None
- del self.ofp
+ if self.state is None:
+ return
+ try:
+ if self.state < _DID_DATA:
+ self.close_data()
+ if self.state != _DID_DATA:
+ raise Error('Close at the wrong time')
+ if self.rlen != 0:
+ raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
+ self._writecrc()
+ finally:
+ self.state = None
+ ofp = self.ofp
+ del self.ofp
+ ofp.close()
def binhex(inp, out):
"""binhex(infilename, outfilename): create binhex-encoded copy of a file"""
@@ -435,11 +441,15 @@ class HexBin:
return self._read(n)
def close(self):
- if self.rlen:
- dummy = self.read_rsrc(self.rlen)
- self._checkcrc()
- self.state = _DID_RSRC
- self.ifp.close()
+ if self.state is None:
+ return
+ try:
+ if self.rlen:
+ dummy = self.read_rsrc(self.rlen)
+ self._checkcrc()
+ finally:
+ self.state = None
+ self.ifp.close()
def hexbin(inp, out):
"""hexbin(infilename, outfilename) - Decode binhexed file"""