Commit aec09788 authored by Tres Seaver's avatar Tres Seaver

Suppress Py3k resource warnings files from 'open()'.

parent 56319ef6
...@@ -231,7 +231,8 @@ This tests tries to provoke this bug by: ...@@ -231,7 +231,8 @@ This tests tries to provoke this bug by:
... print(record.name, record.levelname, end=' ') ... print(record.name, record.levelname, end=' ')
... print(handler.format(record)) ... print(handler.format(record))
... if bad: ... if bad:
... print(open('server-%s.log' % addr[1]).read()) ... with open('server-%s.log' % addr[1]) as f:
... print(f.read())
... #else: ... #else:
... # logging.getLogger('ZEO').debug('GOOD %s' % c) ... # logging.getLogger('ZEO').debug('GOOD %s' % c)
... db.close() ... db.close()
......
This diff is collapsed.
...@@ -46,9 +46,8 @@ class TestZEOOptions(TestZDOptions): ...@@ -46,9 +46,8 @@ class TestZEOOptions(TestZDOptions):
def setUp(self): def setUp(self):
self.tempfilename = tempfile.mktemp() self.tempfilename = tempfile.mktemp()
f = open(self.tempfilename, "w") with open(self.tempfilename, "w") as f:
f.write(self.configdata) f.write(self.configdata)
f.close()
def tearDown(self): def tearDown(self):
try: try:
......
...@@ -159,11 +159,10 @@ class CacheTests(ZODB.tests.util.TestCase): ...@@ -159,11 +159,10 @@ class CacheTests(ZODB.tests.util.TestCase):
path = tempfile.mktemp() path = tempfile.mktemp()
# Copy data from self.cache into path, reaching into the cache # Copy data from self.cache into path, reaching into the cache
# guts to make the copy. # guts to make the copy.
dst = open(path, "wb+") with open(path, "wb+") as dst:
src = self.cache.f src = self.cache.f
src.seek(0) src.seek(0)
dst.write(src.read(self.cache.maxsize)) dst.write(src.read(self.cache.maxsize))
dst.close()
copy = ZEO.cache.ClientCache(path) copy = ZEO.cache.ClientCache(path)
# Verify that internals of both objects are the same. # Verify that internals of both objects are the same.
...@@ -213,24 +212,22 @@ class CacheTests(ZODB.tests.util.TestCase): ...@@ -213,24 +212,22 @@ class CacheTests(ZODB.tests.util.TestCase):
cache.close() cache.close()
def testConversionOfLargeFreeBlocks(self): def testConversionOfLargeFreeBlocks(self):
f = open('cache', 'wb') with open('cache', 'wb') as f:
f.write(ZEO.cache.magic+ f.write(ZEO.cache.magic+
b'\0'*8 + b'\0'*8 +
b'f'+struct.pack(">I", (1<<32)-12) b'f'+struct.pack(">I", (1<<32)-12)
) )
f.seek((1<<32)-1) f.seek((1<<32)-1)
f.write(b'x') f.write(b'x')
f.close()
cache = ZEO.cache.ClientCache('cache', size=1<<32) cache = ZEO.cache.ClientCache('cache', size=1<<32)
cache.close() cache.close()
cache = ZEO.cache.ClientCache('cache', size=1<<32) cache = ZEO.cache.ClientCache('cache', size=1<<32)
cache.close() cache.close()
f = open('cache', 'rb') with open('cache', 'rb') as f:
f.seek(12) f.seek(12)
self.assertEquals(f.read(1), b'f') self.assertEquals(f.read(1), b'f')
self.assertEquals(struct.unpack(">I", f.read(4))[0], self.assertEquals(struct.unpack(">I", f.read(4))[0],
ZEO.cache.max_block_size) ZEO.cache.max_block_size)
f.close()
if not sys.platform.startswith('linux'): if not sys.platform.startswith('linux'):
# On platforms without sparse files, these tests are just way # On platforms without sparse files, these tests are just way
...@@ -347,8 +344,8 @@ isn't corrupted. To see this, we'll write a little script that ...@@ -347,8 +344,8 @@ isn't corrupted. To see this, we'll write a little script that
writes records to a cache file repeatedly. writes records to a cache file repeatedly.
>>> import os, random, sys, time >>> import os, random, sys, time
>>> with open('t', 'w') as file: >>> with open('t', 'w') as f:
... _ = file.write(''' ... _ = f.write('''
... import os, random, sys, time ... import os, random, sys, time
... try: ... try:
... import thread ... import thread
...@@ -1088,8 +1085,8 @@ def rename_bad_cache_file(): ...@@ -1088,8 +1085,8 @@ def rename_bad_cache_file():
""" """
An attempt to open a bad cache file will cause it to be dropped and recreated. An attempt to open a bad cache file will cause it to be dropped and recreated.
>>> with open('cache', 'w') as file: >>> with open('cache', 'w') as f:
... _ = file.write('x'*100) ... _ = f.write('x'*100)
>>> import logging, sys >>> import logging, sys
>>> handler = logging.StreamHandler(sys.stdout) >>> handler = logging.StreamHandler(sys.stdout)
>>> logging.getLogger().addHandler(handler) >>> logging.getLogger().addHandler(handler)
...@@ -1104,14 +1101,13 @@ An attempt to open a bad cache file will cause it to be dropped and recreated. ...@@ -1104,14 +1101,13 @@ An attempt to open a bad cache file will cause it to be dropped and recreated.
>>> cache.store(p64(1), p64(1), None, b'data') >>> cache.store(p64(1), p64(1), None, b'data')
>>> cache.close() >>> cache.close()
>>> f = open('cache') >>> with open('cache') as f:
>>> _ = f.seek(0, 2) ... _ = f.seek(0, 2)
>>> print(f.tell()) ... print(f.tell())
1000 1000
>>> f.close()
>>> with open('cache', 'w') as file: >>> with open('cache', 'w') as f:
... _ = file.write('x'*200) ... _ = f.write('x'*200)
>>> cache = ZEO.cache.ClientCache('cache', 1000) # doctest: +ELLIPSIS >>> cache = ZEO.cache.ClientCache('cache', 1000) # doctest: +ELLIPSIS
Removing bad cache file: 'cache' (prev bad exists). Removing bad cache file: 'cache' (prev bad exists).
Traceback (most recent call last): Traceback (most recent call last):
...@@ -1120,17 +1116,15 @@ An attempt to open a bad cache file will cause it to be dropped and recreated. ...@@ -1120,17 +1116,15 @@ An attempt to open a bad cache file will cause it to be dropped and recreated.
>>> cache.store(p64(1), p64(1), None, b'data') >>> cache.store(p64(1), p64(1), None, b'data')
>>> cache.close() >>> cache.close()
>>> f = open('cache') >>> with open('cache') as f:
>>> _ = f.seek(0, 2) ... _ = f.seek(0, 2)
>>> print(f.tell()) ... print(f.tell())
1000 1000
>>> f.close()
>>> f = open('cache.bad') >>> with open('cache.bad') as f:
>>> _ = f.seek(0, 2) ... _ = f.seek(0, 2)
>>> print(f.tell()) ... print(f.tell())
100 100
>>> f.close()
>>> logging.getLogger().removeHandler(handler) >>> logging.getLogger().removeHandler(handler)
>>> logging.getLogger().setLevel(old_level) >>> logging.getLogger().setLevel(old_level)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment