Skip to content

Commit dbb5751

Browse files
committed
Don't leak open handles
1 parent b971d45 commit dbb5751

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

q2cli/builtin/tools.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -560,40 +560,42 @@ def view(result_path, port, verbose):
560560
class Handler(http.server.SimpleHTTPRequestHandler):
561561
def do_GET(self):
562562
# Redirect the output from these requests to devnull if not verbose
563-
with contextlib.redirect_stderr(
564-
sys.stderr if verbose else open(os.devnull, 'w')):
565-
# Determine if this is a request for the file we are supposed
566-
# to be viewing
567-
if self.path == result_path:
568-
if not os.path.exists(self.path):
569-
self.send_error(404)
563+
with open(os.devnull, 'w') as devnull:
564+
with contextlib.redirect_stderr(
565+
sys.stderr if verbose else devnull):
566+
# Determine if this is a request for the file we are
567+
# supposed to be viewing
568+
if self.path == result_path:
569+
if not os.path.exists(self.path):
570+
self.send_error(404)
571+
else:
572+
self.send_response(200)
573+
with open(self.path, 'rb') as file:
574+
self.wfile.write(file.read())
575+
# Determine if this is a request for a file within the
576+
# visualization
577+
elif self.path.startswith(f'/_/{session}/{result.uuid}/'):
578+
file_path = self.path.split(str(result.uuid))[1]
579+
file_path = extracted_path + file_path
580+
file_path = os.path.abspath(file_path)
581+
582+
if not os.path.exists(file_path) or \
583+
not file_path.startswith(extracted_path):
584+
self.send_error(404)
585+
else:
586+
self.send_response(200)
587+
self.send_header('Access-Control-Allow-Origin',
588+
'*')
589+
self.end_headers()
590+
591+
with open(file_path, 'rb') as file:
592+
self.wfile.write(file.read())
593+
# Otherwise default to super class. This will respond
594+
# appropriately to requests for assets that are part of the
595+
# vendored view app and will reject any requests for files
596+
# outside the served directory
570597
else:
571-
self.send_response(200)
572-
with open(self.path, 'rb') as file:
573-
self.wfile.write(file.read())
574-
# Determine if this is a request for a file within the
575-
# visualization
576-
elif self.path.startswith(f'/_/{session}/{result.uuid}/'):
577-
file_path = self.path.split(str(result.uuid))[1]
578-
file_path = extracted_path + file_path
579-
file_path = os.path.abspath(file_path)
580-
581-
if not os.path.exists(file_path) or \
582-
not file_path.startswith(extracted_path):
583-
self.send_error(404)
584-
else:
585-
self.send_response(200)
586-
self.send_header('Access-Control-Allow-Origin', '*')
587-
self.end_headers()
588-
589-
with open(file_path, 'rb') as file:
590-
self.wfile.write(file.read())
591-
# Otherwise default to super class. This will respond
592-
# appropriately to requests for assets that are part of the
593-
# vendored view app and will reject any requests for files
594-
# outside the served directory
595-
else:
596-
super().do_GET()
598+
super().do_GET()
597599

598600
VENDOR_PATH = 'q2cli/assets/view/'
599601

0 commit comments

Comments
 (0)