Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7dd737

Browse files
committed
merge 3.1
2 parents da5b852 + 249b508 commit c7dd737

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

Doc/library/os.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,26 @@ Files and Directories
895895
Using :func:`access` to check if a user is authorized to e.g. open a file
896896
before actually doing so using :func:`open` creates a security hole,
897897
because the user might exploit the short time interval between checking
898-
and opening the file to manipulate it.
898+
and opening the file to manipulate it. It's preferable to use :term:`EAFP`
899+
techniques. For example::
900+
901+
if os.access("myfile", os.R_OK):
902+
with open("myfile") as fp:
903+
return fp.read()
904+
return "some default data"
905+
906+
is better written as::
907+
908+
try:
909+
fp = open("myfile")
910+
except OSError as e:
911+
if e.errno == errno.EACCESS:
912+
return "some default data"
913+
# Not a permission error.
914+
raise
915+
else:
916+
with fp:
917+
return fp.read()
899918

900919
.. note::
901920

0 commit comments

Comments
 (0)