File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments