[Python-checkins] cpython (merge 3.2 -> default): Tidy up merge with remote

nick.coghlan python-checkins at python.org
Thu Mar 17 02:54:37 CET 2011


http://hg.python.org/cpython/rev/2b3fb2398f45
changeset: 68637:2b3fb2398f45
parent: 68635:917f74ff5782
parent: 68636:21fe6d393242
user: Nick Coghlan <ncoghlan at gmail.com>
date: Wed Mar 16 22:11:09 2011 -0400
summary:
 Tidy up merge with remote
files:
 Misc/ACKS
 Misc/NEWS
diff --git a/Lib/binhex.py b/Lib/binhex.py
--- a/Lib/binhex.py
+++ b/Lib/binhex.py
@@ -52,14 +52,13 @@
 
 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 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)
diff --git a/Lib/test/test_binhex.py b/Lib/test/test_binhex.py
--- a/Lib/test/test_binhex.py
+++ b/Lib/test/test_binhex.py
@@ -15,10 +15,12 @@
 def setUp(self):
 self.fname1 = support.TESTFN + "1"
 self.fname2 = support.TESTFN + "2"
+ self.fname3 = support.TESTFN + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__"
 
 def tearDown(self):
 support.unlink(self.fname1)
 support.unlink(self.fname2)
+ support.unlink(self.fname3)
 
 DATA = b'Jack is my hero'
 
@@ -37,6 +39,15 @@
 
 self.assertEqual(self.DATA, finish)
 
+ def test_binhex_error_on_long_filename(self):
+ """
+ The testcase fails if no exception is raised when a filename parameter provided to binhex.binhex()
+ is too long, or if the exception raised in binhex.binhex() is not an instance of binhex.Error.
+ """
+ f3 = open(self.fname3, 'wb')
+ f3.close()
+
+ self.assertRaises(binhex.Error, binhex.binhex, self.fname3, self.fname2)
 
 def test_main():
 support.run_unittest(BinHexTestCase)
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -451,6 +451,7 @@
 Peter van Kampen
 Rafe Kaplan
 Jacob Kaplan-Moss
+Arkady Koplyarov
 Lou Kates
 Hiroaki Kawai
 Sebastien Keim
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -72,6 +72,8 @@
 Library
 -------
 
+- Issue #11577: fix ResourceWarning triggered by improved binhex test coverage
+
 - Issue #11243: fix the parameter querying methods of Message to work if
 the headers contain un-encoded non-ASCII data.
 
@@ -242,6 +244,8 @@
 Tests
 -----
 
+- Issue #11577: improve test coverage of binhex.py. Patch by Arkady Koplyarov.
+
 - New test_crashers added to exercise the scripts in the Lib/test/crashers
 directory and confirm they fail as expected
 
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /