mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Made more extensive usage of context managers with open.
This commit is contained in:
27
django/core/cache/backends/filebased.py
vendored
27
django/core/cache/backends/filebased.py
vendored
@@ -31,16 +31,13 @@ class FileBasedCache(BaseCache):
|
||||
|
||||
fname = self._key_to_file(key)
|
||||
try:
|
||||
f = open(fname, 'rb')
|
||||
try:
|
||||
with open(fname, 'rb') as f:
|
||||
exp = pickle.load(f)
|
||||
now = time.time()
|
||||
if exp < now:
|
||||
self._delete(fname)
|
||||
else:
|
||||
return pickle.load(f)
|
||||
finally:
|
||||
f.close()
|
||||
except (IOError, OSError, EOFError, pickle.PickleError):
|
||||
pass
|
||||
return default
|
||||
@@ -61,13 +58,10 @@ class FileBasedCache(BaseCache):
|
||||
if not os.path.exists(dirname):
|
||||
os.makedirs(dirname)
|
||||
|
||||
f = open(fname, 'wb')
|
||||
try:
|
||||
with open(fname, 'wb') as f:
|
||||
now = time.time()
|
||||
pickle.dump(now + timeout, f, pickle.HIGHEST_PROTOCOL)
|
||||
pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
|
||||
finally:
|
||||
f.close()
|
||||
except (IOError, OSError):
|
||||
pass
|
||||
|
||||
@@ -94,17 +88,14 @@ class FileBasedCache(BaseCache):
|
||||
self.validate_key(key)
|
||||
fname = self._key_to_file(key)
|
||||
try:
|
||||
f = open(fname, 'rb')
|
||||
try:
|
||||
with open(fname, 'rb') as f:
|
||||
exp = pickle.load(f)
|
||||
now = time.time()
|
||||
if exp < now:
|
||||
self._delete(fname)
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
finally:
|
||||
f.close()
|
||||
now = time.time()
|
||||
if exp < now:
|
||||
self._delete(fname)
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
except (IOError, OSError, EOFError, pickle.PickleError):
|
||||
return False
|
||||
|
||||
|
||||
@@ -9,10 +9,9 @@ Cookbook, licensed under the Python Software License.
|
||||
Example Usage::
|
||||
|
||||
>>> from django.core.files import locks
|
||||
>>> f = open('./file', 'wb')
|
||||
>>> locks.lock(f, locks.LOCK_EX)
|
||||
>>> f.write('Django')
|
||||
>>> f.close()
|
||||
>>> with open('./file', 'wb') as f:
|
||||
>>> locks.lock(f, locks.LOCK_EX)
|
||||
>>> f.write('Django')
|
||||
"""
|
||||
|
||||
__all__ = ('LOCK_EX','LOCK_SH','LOCK_NB','lock','unlock')
|
||||
|
||||
@@ -59,8 +59,7 @@ def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_ove
|
||||
pass
|
||||
|
||||
# first open the old file, so that it won't go away
|
||||
old_file = open(old_file_name, 'rb')
|
||||
try:
|
||||
with open(old_file_name, 'rb') as old_file:
|
||||
# now open the new file, not forgetting allow_overwrite
|
||||
fd = os.open(new_file_name, os.O_WRONLY | os.O_CREAT | getattr(os, 'O_BINARY', 0) |
|
||||
(not allow_overwrite and os.O_EXCL or 0))
|
||||
@@ -73,8 +72,6 @@ def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_ove
|
||||
finally:
|
||||
locks.unlock(fd)
|
||||
os.close(fd)
|
||||
finally:
|
||||
old_file.close()
|
||||
copystat(old_file_name, new_file_name)
|
||||
|
||||
try:
|
||||
|
||||
@@ -265,7 +265,8 @@ class EmailMessage(object):
|
||||
def attach_file(self, path, mimetype=None):
|
||||
"""Attaches a file from the filesystem."""
|
||||
filename = os.path.basename(path)
|
||||
content = open(path, 'rb').read()
|
||||
with open(path, 'rb') as f:
|
||||
content = f.read()
|
||||
self.attach(filename, content, mimetype)
|
||||
|
||||
def _create_message(self, msg):
|
||||
|
||||
@@ -5,8 +5,8 @@ from optparse import make_option
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
def has_bom(fn):
|
||||
f = open(fn, 'r')
|
||||
sample = f.read(4)
|
||||
with open(fn, 'r') as f:
|
||||
sample = f.read(4)
|
||||
return sample[:3] == '\xef\xbb\xbf' or \
|
||||
sample.startswith(codecs.BOM_UTF16_LE) or \
|
||||
sample.startswith(codecs.BOM_UTF16_BE)
|
||||
|
||||
@@ -112,7 +112,8 @@ def copy_plural_forms(msgs, locale, domain, verbosity, stdout=sys.stdout):
|
||||
for domain in domains:
|
||||
django_po = os.path.join(django_dir, 'conf', 'locale', locale, 'LC_MESSAGES', '%s.po' % domain)
|
||||
if os.path.exists(django_po):
|
||||
m = plural_forms_re.search(open(django_po, 'rU').read())
|
||||
with open(django_po, 'rU') as fp:
|
||||
m = plural_forms_re.search(fp.read())
|
||||
if m:
|
||||
if verbosity > 1:
|
||||
stdout.write("copying plural forms: %s\n" % m.group('value'))
|
||||
@@ -141,11 +142,8 @@ def write_pot_file(potfile, msgs, file, work_file, is_templatized):
|
||||
msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
|
||||
else:
|
||||
msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
|
||||
f = open(potfile, 'ab')
|
||||
try:
|
||||
f.write(msgs)
|
||||
finally:
|
||||
f.close()
|
||||
with open(potfile, 'ab') as fp:
|
||||
fp.write(msgs)
|
||||
|
||||
def process_file(file, dirpath, potfile, domain, verbosity,
|
||||
extensions, wrap, location, stdout=sys.stdout):
|
||||
@@ -164,15 +162,13 @@ def process_file(file, dirpath, potfile, domain, verbosity,
|
||||
if domain == 'djangojs' and file_ext in extensions:
|
||||
is_templatized = True
|
||||
orig_file = os.path.join(dirpath, file)
|
||||
src_data = open(orig_file).read()
|
||||
with open(orig_file) as fp:
|
||||
src_data = fp.read()
|
||||
src_data = prepare_js_for_gettext(src_data)
|
||||
thefile = '%s.c' % file
|
||||
work_file = os.path.join(dirpath, thefile)
|
||||
f = open(work_file, "w")
|
||||
try:
|
||||
f.write(src_data)
|
||||
finally:
|
||||
f.close()
|
||||
with open(work_file, "w") as fp:
|
||||
fp.write(src_data)
|
||||
cmd = (
|
||||
'xgettext -d %s -L C %s %s --keyword=gettext_noop '
|
||||
'--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
|
||||
@@ -184,14 +180,12 @@ def process_file(file, dirpath, potfile, domain, verbosity,
|
||||
orig_file = os.path.join(dirpath, file)
|
||||
is_templatized = file_ext in extensions
|
||||
if is_templatized:
|
||||
src_data = open(orig_file, "rU").read()
|
||||
with open(orig_file, "rU") as fp:
|
||||
src_data = fp.read()
|
||||
thefile = '%s.py' % file
|
||||
content = templatize(src_data, orig_file[2:])
|
||||
f = open(os.path.join(dirpath, thefile), "w")
|
||||
try:
|
||||
f.write(content)
|
||||
finally:
|
||||
f.close()
|
||||
with open(os.path.join(dirpath, thefile), "w") as fp:
|
||||
fp.write(content)
|
||||
work_file = os.path.join(dirpath, thefile)
|
||||
cmd = (
|
||||
'xgettext -d %s -L Python %s %s --keyword=gettext_noop '
|
||||
@@ -232,11 +226,8 @@ def write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
|
||||
os.unlink(potfile)
|
||||
raise CommandError("errors happened while running msguniq\n%s" % errors)
|
||||
if os.path.exists(pofile):
|
||||
f = open(potfile, 'w')
|
||||
try:
|
||||
f.write(msgs)
|
||||
finally:
|
||||
f.close()
|
||||
with open(potfile, 'w') as fp:
|
||||
fp.write(msgs)
|
||||
msgs, errors = _popen('msgmerge %s %s -q "%s" "%s"' %
|
||||
(wrap, location, pofile, potfile))
|
||||
if errors:
|
||||
@@ -247,11 +238,8 @@ def write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
|
||||
msgs = copy_plural_forms(msgs, locale, domain, verbosity, stdout)
|
||||
msgs = msgs.replace(
|
||||
"#. #-#-#-#-# %s.pot (PACKAGE VERSION) #-#-#-#-#\n" % domain, "")
|
||||
f = open(pofile, 'wb')
|
||||
try:
|
||||
f.write(msgs)
|
||||
finally:
|
||||
f.close()
|
||||
with open(pofile, 'wb') as fp:
|
||||
fp.write(msgs)
|
||||
os.unlink(potfile)
|
||||
if no_obsolete:
|
||||
msgs, errors = _popen('msgattrib %s %s -o "%s" --no-obsolete "%s"' %
|
||||
|
||||
@@ -157,13 +157,12 @@ def custom_sql_for_model(model, style, connection):
|
||||
os.path.join(app_dir, "%s.sql" % opts.object_name.lower())]
|
||||
for sql_file in sql_files:
|
||||
if os.path.exists(sql_file):
|
||||
fp = open(sql_file, 'U')
|
||||
for statement in statements.split(fp.read().decode(settings.FILE_CHARSET)):
|
||||
# Remove any comments from the file
|
||||
statement = re.sub(ur"--.*([\n\Z]|$)", "", statement)
|
||||
if statement.strip():
|
||||
output.append(statement + u";")
|
||||
fp.close()
|
||||
with open(sql_file, 'U') as fp:
|
||||
for statement in statements.split(fp.read().decode(settings.FILE_CHARSET)):
|
||||
# Remove any comments from the file
|
||||
statement = re.sub(ur"--.*([\n\Z]|$)", "", statement)
|
||||
if statement.strip():
|
||||
output.append(statement + u";")
|
||||
|
||||
return output
|
||||
|
||||
|
||||
@@ -176,9 +176,8 @@ def runfastcgi(argset=[], **kwargs):
|
||||
become_daemon(our_home_dir=options["workdir"], **daemon_kwargs)
|
||||
|
||||
if options["pidfile"]:
|
||||
fp = open(options["pidfile"], "w")
|
||||
fp.write("%d\n" % os.getpid())
|
||||
fp.close()
|
||||
with open(options["pidfile"], "w") as fp:
|
||||
fp.write("%d\n" % os.getpid())
|
||||
|
||||
WSGIServer(get_internal_wsgi_application(), **wsgi_opts).run()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user