diff options
| author | Benjamin Peterson <benjamin@python.org> | 2011-05-20 16:41:13 (GMT) |
|---|---|---|
| committer | Benjamin Peterson <benjamin@python.org> | 2011-05-20 16:41:13 (GMT) |
| commit | 30e10d8114b68613f35e98159eb428f0be0cb769 (patch) | |
| tree | 304c8d916391d6848d1dde8515e219c8ff94ba57 | |
| parent | 52f63eabeb324dd3f262257626bf5cdcfefee6c0 (diff) | |
| download | cpython-30e10d8114b68613f35e98159eb428f0be0cb769.zip cpython-30e10d8114b68613f35e98159eb428f0be0cb769.tar.gz cpython-30e10d8114b68613f35e98159eb428f0be0cb769.tar.bz2 | |
add example for not using access
| -rw-r--r-- | Doc/library/os.rst | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/Doc/library/os.rst b/Doc/library/os.rst index b16fb1f..0466da4 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:: |
