Avoid ResourceWarning in tests in Python 3.2

This commit is contained in:
Daniele Varrazzo 2011-01-08 00:40:27 +00:00
parent 43c8fce45c
commit f8c4335f35
2 changed files with 20 additions and 4 deletions

View File

@ -182,7 +182,11 @@ class LargeObjectTests(LargeObjectMixin, unittest.TestCase):
filename = os.path.join(self.tmpdir, "data.txt")
lo.export(filename)
self.assertTrue(os.path.exists(filename))
self.assertEqual(open(filename, "rb").read(), b("some data"))
f = open(filename, "rb")
try:
self.assertEqual(f.read(), b("some data"))
finally:
f.close()
def test_close_twice(self):
lo = self.conn.lobject()
@ -224,7 +228,11 @@ class LargeObjectTests(LargeObjectMixin, unittest.TestCase):
filename = os.path.join(self.tmpdir, "data.txt")
lo.export(filename)
self.assertTrue(os.path.exists(filename))
self.assertEqual(open(filename, "rb").read(), b("some data"))
f = open(filename, "rb")
try:
self.assertEqual(f.read(), b("some data"))
finally:
f.close()
def test_close_after_commit(self):
lo = self.conn.lobject()
@ -279,7 +287,11 @@ class LargeObjectTests(LargeObjectMixin, unittest.TestCase):
filename = os.path.join(self.tmpdir, "data.txt")
lo.export(filename)
self.assertTrue(os.path.exists(filename))
self.assertEqual(open(filename, "rb").read(), b("some data"))
f = open(filename, "rb")
try:
self.assertEqual(f.read(), b("some data"))
finally:
f.close()
decorate_all_tests(LargeObjectTests, skip_if_no_lo)
decorate_all_tests(LargeObjectTests, skip_if_green)

View File

@ -121,5 +121,9 @@ def script_to_py3(script):
if main("lib2to3.fixes", ['--no-diffs', '-w', '-n', f.name]):
raise Exception('py3 conversion failed')
return open(f.name).read()
f2 = open(f.name)
try:
return f2.read()
finally:
f2.close()