summaryrefslogtreecommitdiffstats
path: root/Doc/library/os.rst
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2011-05-20 16:41:13 (GMT)
committerBenjamin Peterson <benjamin@python.org>2011-05-20 16:41:13 (GMT)
commit249b508c98d99597c170bdb9d6a4b1d56d839ebd (patch)
treef118d442335543b93e678065f7dbe0b6701957c3 /Doc/library/os.rst
parent261d855fd6e67bfe04aaaabfaff27f091b6d008f (diff)
downloadcpython-249b508c98d99597c170bdb9d6a4b1d56d839ebd.zip
cpython-249b508c98d99597c170bdb9d6a4b1d56d839ebd.tar.gz
cpython-249b508c98d99597c170bdb9d6a4b1d56d839ebd.tar.bz2
add example for not using access
Diffstat (limited to 'Doc/library/os.rst')
-rw-r--r--Doc/library/os.rst21
1 files changed, 20 insertions, 1 deletions
diff --git a/Doc/library/os.rst b/Doc/library/os.rst
index 3f6d69d..e6bafce 100644
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -772,7 +772,26 @@ Files and Directories
Using :func:`access` to check if a user is authorized to e.g. open a file
before actually doing so using :func:`open` creates a security hole,
because the user might exploit the short time interval between checking
- and opening the file to manipulate it.
+ and opening the file to manipulate it. It's preferable to use :term:`EAFP`
+ techniques. For example::
+
+ if os.access("myfile", os.R_OK):
+ with open("myfile") as fp:
+ return fp.read()
+ return "some default data"
+
+ is better written as::
+
+ try:
+ fp = open("myfile")
+ except OSError as e:
+ if e.errno == errno.EACCESS:
+ return "some default data"
+ # Not a permission error.
+ raise
+ else:
+ with fp:
+ return fp.read()
.. note::