summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2012-05-05 14:01:38 +0200
committerClaude Paroz <claude@2xlibre.net>2012-05-05 14:06:36 +0200
commit865cd35c9b357e20994f6c6a51f2ae000ba0a3ee (patch)
tree9db42053673c856a2527d5573d7f8fd682334182 /docs
parentec5423df05dedeee6651c36dd4d90fec0d8cca7c (diff)
Made more extensive usage of context managers with open.
Diffstat (limited to 'docs')
-rw-r--r--docs/_ext/applyxrefs.py27
-rw-r--r--docs/_ext/djangodocs.py9
-rw-r--r--docs/_ext/literals_to_xrefs.py6
-rw-r--r--docs/ref/utils.txt5
-rw-r--r--docs/topics/http/file-uploads.txt7
-rw-r--r--docs/topics/serialization.txt4
-rw-r--r--docs/topics/testing.txt5
7 files changed, 23 insertions, 40 deletions
diff --git a/docs/_ext/applyxrefs.py b/docs/_ext/applyxrefs.py
index 14d18bd856..7181480e0f 100644
--- a/docs/_ext/applyxrefs.py
+++ b/docs/_ext/applyxrefs.py
@@ -18,33 +18,18 @@ def process_file(fn, lines):
lines.insert(0, '\n')
lines.insert(0, '.. %s:\n' % target_name(fn))
try:
- f = open(fn, 'w')
+ with open(fn, 'w') as fp:
+ fp.writelines(lines)
except IOError:
print("Can't open %s for writing. Not touching it." % fn)
- return
- try:
- f.writelines(lines)
- except IOError:
- print("Can't write to %s. Not touching it." % fn)
- finally:
- f.close()
def has_target(fn):
try:
- f = open(fn, 'r')
+ with open(fn, 'r') as fp:
+ lines = fp.readlines()
except IOError:
- print("Can't open %s. Not touching it." % fn)
+ print("Can't open or read %s. Not touching it." % fn)
return (True, None)
- readok = True
- try:
- lines = f.readlines()
- except IOError:
- print("Can't read %s. Not touching it." % fn)
- readok = False
- finally:
- f.close()
- if not readok:
- return (True, None)
#print fn, len(lines)
if len(lines) < 1:
@@ -82,7 +67,7 @@ def main(argv=None):
print("Adding xref to %s" % fn)
process_file(fn, lines)
else:
- print "Skipping %s: already has a xref" % fn
+ print("Skipping %s: already has a xref" % fn)
if __name__ == '__main__':
sys.exit(main())
diff --git a/docs/_ext/djangodocs.py b/docs/_ext/djangodocs.py
index d4b0b43ca7..3d85147952 100644
--- a/docs/_ext/djangodocs.py
+++ b/docs/_ext/djangodocs.py
@@ -210,8 +210,7 @@ class DjangoStandaloneHTMLBuilder(StandaloneHTMLBuilder):
if t == "templatefilter" and l == "ref/templates/builtins"],
}
outfilename = os.path.join(self.outdir, "templatebuiltins.js")
- f = open(outfilename, 'wb')
- f.write('var django_template_builtins = ')
- json.dump(templatebuiltins, f)
- f.write(';\n')
- f.close();
+ with open(outfilename, 'wb') as fp:
+ fp.write('var django_template_builtins = ')
+ json.dump(templatebuiltins, fp)
+ fp.write(';\n')
diff --git a/docs/_ext/literals_to_xrefs.py b/docs/_ext/literals_to_xrefs.py
index d1487ca729..d7b3cfc549 100644
--- a/docs/_ext/literals_to_xrefs.py
+++ b/docs/_ext/literals_to_xrefs.py
@@ -38,7 +38,8 @@ ALWAYS_SKIP = [
]
def fixliterals(fname):
- data = open(fname).read()
+ with open(fname) as fp:
+ data = fp.read()
last = 0
new = []
@@ -101,7 +102,8 @@ def fixliterals(fname):
lastvalues[m.group(1)] = replace_value
new.append(data[last:])
- open(fname, "w").write("".join(new))
+ with open(fname, "w") as fp:
+ fp.write("".join(new))
storage["lastvalues"] = lastvalues
storage.close()
diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt
index 2525690869..f045f4bf5a 100644
--- a/docs/ref/utils.txt
+++ b/docs/ref/utils.txt
@@ -239,9 +239,8 @@ Sample usage::
... link=u"http://www.holovaty.com/test/",
... description="Testing."
... )
- >>> fp = open('test.rss', 'w')
- >>> feed.write(fp, 'utf-8')
- >>> fp.close()
+ >>> with open('test.rss', 'w') as fp:
+ >>> feed.write(fp, 'utf-8')
For simplifying the selection of a generator use ``feedgenerator.DefaultFeed``
which is currently ``Rss201rev2Feed``
diff --git a/docs/topics/http/file-uploads.txt b/docs/topics/http/file-uploads.txt
index 1c1ef776bb..877d3b4100 100644
--- a/docs/topics/http/file-uploads.txt
+++ b/docs/topics/http/file-uploads.txt
@@ -101,10 +101,9 @@ objects; see `UploadedFile objects`_ for a complete reference.
Putting it all together, here's a common way you might handle an uploaded file::
def handle_uploaded_file(f):
- destination = open('some/file/name.txt', 'wb+')
- for chunk in f.chunks():
- destination.write(chunk)
- destination.close()
+ with open('some/file/name.txt', 'wb+') as destination:
+ for chunk in f.chunks():
+ destination.write(chunk)
Looping over ``UploadedFile.chunks()`` instead of using ``read()`` ensures that
large files don't overwhelm your system's memory.
diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt
index 6bda32dece..73239cb483 100644
--- a/docs/topics/serialization.txt
+++ b/docs/topics/serialization.txt
@@ -36,8 +36,8 @@ You can also use a serializer object directly::
This is useful if you want to serialize data directly to a file-like object
(which includes an :class:`~django.http.HttpResponse`)::
- out = open("file.xml", "w")
- xml_serializer.serialize(SomeModel.objects.all(), stream=out)
+ with open("file.xml", "w") as out:
+ xml_serializer.serialize(SomeModel.objects.all(), stream=out)
.. note::
diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
index 7287b04da4..3971958dc7 100644
--- a/docs/topics/testing.txt
+++ b/docs/topics/testing.txt
@@ -770,9 +770,8 @@ arguments at time of construction:
wish to upload as a value. For example::
>>> c = Client()
- >>> f = open('wishlist.doc')
- >>> c.post('/customers/wishes/', {'name': 'fred', 'attachment': f})
- >>> f.close()
+ >>> with open('wishlist.doc') as fp:
+ >>> c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp})
(The name ``attachment`` here is not relevant; use whatever name your
file-processing code expects.)