misc: close files (#46992)

This commit is contained in:
Frédéric Péters 2020-09-24 08:38:49 +02:00
parent 08de8e3cb7
commit c97a3e4fc2
7 changed files with 38 additions and 21 deletions

View File

@ -116,6 +116,7 @@ def create_temporary_pub(sql_mode=False, templates_mode=False, lazy_mode=False):
fd.write('force-lazy-mode = true\n')
if sql_mode:
fd.write('postgresql = true\n')
fd.close()
# make sure site options are not cached
pub.site_options = None
@ -175,8 +176,6 @@ def create_temporary_pub(sql_mode=False, templates_mode=False, lazy_mode=False):
conn.close()
fd.close()
return pub
@ -325,6 +324,8 @@ class HttpRequestsMocking(object):
scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
base_url = urlparse.urlunparse((scheme, netloc, path, '', '', ''))
with open(os.path.join(os.path.dirname(__file__), 'idp_metadata.xml')) as fd:
metadata = fd.read()
status, data, headers = {
'http://remote.example.net/204': (204, None, None),
'http://remote.example.net/400': (400, 'bad request', None),
@ -350,8 +351,7 @@ class HttpRequestsMocking(object):
'http://remote.example.net/xml-errheader': (200, '<?xml version="1.0"><foo/>',
{'content-type': 'text/xml', 'x-error-code': '1'}),
'http://remote.example.net/connection-error': (None, None, None),
'http://authentic.example.net/idp/saml2/metadata': (
200, open(os.path.join(os.path.dirname(__file__), 'idp_metadata.xml')).read(), None),
'http://authentic.example.net/idp/saml2/metadata': (200, metadata, None),
}.get(base_url, (200, '', {}))
if url.startswith('file://'):

View File

@ -55,7 +55,8 @@ def _find_vc_version():
revision = None
try:
setup_content = open(os.path.join(srcdir, 'setup.py')).read()
with open(os.path.join(srcdir, 'setup.py')) as fd:
setup_content = fd.read()
version_line = [x for x in setup_content.splitlines() if 'version' in x][0]
version = re.split('"|\'', version_line.split()[2])[1]
except:

View File

@ -56,9 +56,10 @@ class BotFilter(logging.Filter):
botfile = os.path.join(get_publisher().data_dir, 'webbots')
if os.path.exists(botfile) and get_request():
user_agent = get_request().get_environ('HTTP_USER_AGENT', '')
for bot_ua_string in [x.strip() for x in open(botfile).readlines()]:
if bot_ua_string in user_agent:
return True
with open(botfile) as fd:
for bot_ua_string in [x.strip() for x in fd.readlines()]:
if bot_ua_string in user_agent:
return True
return False

View File

@ -731,7 +731,8 @@ class QommonPublisher(Publisher, object):
def reload_cfg(self):
filename = os.path.join(self.app_dir, 'config.pck')
try:
self.cfg = cPickle.load(open(filename, 'rb'), encoding='utf-8')
with open(filename, 'rb') as fd:
self.cfg = cPickle.load(fd, encoding='utf-8')
except:
self.cfg = {}

View File

@ -380,7 +380,8 @@ class StorableObject(object):
def get_new_id(cls, create=False):
objects_dir = cls.get_objects_dir()
try:
max_id = int(open(os.path.join(objects_dir, '.max_id')).read())
with open(os.path.join(objects_dir, '.max_id')) as fd:
max_id = int(fd.read())
except (IOError, OSError, ValueError):
max_id = 0
keys = cls.keys()
@ -449,7 +450,8 @@ class StorableObject(object):
return [x for x in values if getattr(x, index) == value]
if not os.path.exists(index_file):
return []
return pickle.load(open(index_file, 'rb'))
with open(index_file, 'rb') as fd:
return pickle.load(fd)
@classmethod
def get_with_indexed_value(cls, index, value, ignore_errors = False):
@ -469,6 +471,7 @@ class StorableObject(object):
@classmethod
def get_filename(cls, filename, ignore_errors=False, ignore_migration=False, **kwargs):
fd = None
try:
fd = open(force_bytes(filename, 'utf-8'), 'rb')
o = cls.storage_load(fd, **kwargs)
@ -483,7 +486,6 @@ class StorableObject(object):
except EOFError as e:
# maybe it's being written to, loop for a while to see
current_position = fd.tell()
fd.close()
for i in range(10):
time.sleep(0.01)
if current_position != os.stat(filename).st_size:
@ -492,6 +494,9 @@ class StorableObject(object):
if ignore_errors:
return None
raise KeyError()
finally:
if fd:
fd.close()
o.__class__ = cls
if any((isinstance(k, bytes) for k in o.__dict__)):
pickle_2to3_conversion(o)
@ -578,7 +583,8 @@ class StorableObject(object):
if index_key not in indexes:
continue
index_file = os.path.join(objects_dir, '.indexes', index_key, index)
pickle.dump(content, open(index_file, 'wb'), protocol=2)
with open(index_file, 'wb') as fd:
pickle.dump(content, fd, protocol=2)
for index in cls._hashed_indexes or []:
if index not in indexes:
@ -762,10 +768,12 @@ class StorableObject(object):
old_index_name = '%s-%s' % (index, fix_key(oldv))
old_index_file = os.path.join(index_dir, old_index_name)
if os.path.exists(old_index_file):
ids = [str(x) for x in pickle.load(open(old_index_file, 'rb'))]
with open(old_index_file, 'rb') as fd:
ids = [str(x) for x in pickle.load(fd)]
if str(self.id) in ids:
ids.remove(str(self.id))
pickle.dump(ids, open(old_index_file, 'wb'), protocol=2)
with open(old_index_file, 'wb') as fd:
pickle.dump(ids, fd, protocol=2)
for newv in new_value:
if newv in old_value:
@ -773,12 +781,14 @@ class StorableObject(object):
index_name = '%s-%s' % (index, fix_key(newv))
index_file = os.path.join(index_dir, index_name)
if os.path.exists(index_file):
ids = [str(x) for x in pickle.load(open(index_file, 'rb'))]
with open(index_file, 'rb') as fd:
ids = [str(x) for x in pickle.load(fd)]
else:
ids = []
if not str(self.id) in ids:
ids.append(str(self.id))
pickle.dump(ids, open(index_file, 'wb'), protocol=2)
with open(index_file, 'wb') as fd:
pickle.dump(ids, fd, protocol=2)
@classmethod
def volatile(cls):
@ -812,10 +822,12 @@ class StorableObject(object):
index_name = '%s-%s' % (index, attr_value)
index_file = os.path.join(index_dir, index, index_name)
if os.path.exists(index_file):
ids = [str(x) for x in pickle.load(open(index_file, 'rb'))]
with open(index_file, 'rb') as fd:
ids = [str(x) for x in pickle.load(fd)]
if str(object.id) in ids:
ids.remove(str(object.id))
pickle.dump(ids, open(index_file, 'wb'), protocol=2)
with open(index_file, 'wb') as fd:
pickle.dump(ids, fd, protocol=2)
os.unlink(os.path.join(objects_dir, fix_key(id)))

View File

@ -71,7 +71,8 @@ class PicklableUpload(Upload):
return b''
if hasattr(self, 'qfilename'):
filename = os.path.join(get_publisher().app_dir, 'uploads', self.qfilename)
return open(filename, 'rb').read()
with open(filename, 'rb') as fd:
return fd.read()
return None
def get_base64_content(self):

View File

@ -195,4 +195,5 @@ WORKING_DAY_CALENDAR = 'workalendar.europe.France'
local_settings_file = os.environ.get('WCS_SETTINGS_FILE',
os.path.join(os.path.dirname(__file__), 'local_settings.py'))
if os.path.exists(local_settings_file):
exec(open(local_settings_file).read())
with open(local_settings_file) as fd:
exec(fd.read())