summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2011-05-20 11:41:13 -0500
committerBenjamin Peterson <benjamin@python.org>2011-05-20 11:41:13 -0500
commit80f71e2ae7fae2761fb394df0ff4a3e01c310c3a (patch)
tree341c7c580bc7f7a156aede4b4dbbdf9c76c60558
parent8eb7dfc56c306cc700effe0ec38109541cf79a78 (diff)
downloadcpython-80f71e2ae7fae2761fb394df0ff4a3e01c310c3a.tar.gz
add example for not using access
-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 b16fb1fa36..0466da4487 100644
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -916,7 +916,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::