From 249b508c98d99597c170bdb9d6a4b1d56d839ebd Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Fri, 20 May 2011 11:41:13 -0500 Subject: add example for not using access --- Doc/library/os.rst | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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:: -- cgit v0.12