From 058de2f39ceac5c1bceef0a5bb6268f3173b9781 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Wed, 25 Nov 2020 02:56:35 +0100 Subject: [PATCH] Fix exception on transmission abort with python3 With python3 sys.exc_value does no longer exist, but we can replace it with sys.exc_info(). --- ChangeLog | 6 ++++++ servefile/servefile.py | 3 ++- tests/test_servefile.py | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index dcc5568..e9c9b27 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,12 @@ servefile changelog =================== +Unreleased +---------- + + * fixed bug where exception was shown on transmission abort with python3 + + 2020-10-30 v0.5.1 ----------------- diff --git a/servefile/servefile.py b/servefile/servefile.py index 466d0d1..17ed9fa 100755 --- a/servefile/servefile.py +++ b/servefile/servefile.py @@ -714,7 +714,8 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler): class ThreadedHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): def handle_error(self, request, client_address): - print("%s ABORTED transmission (Reason: %s)" % (client_address[0], sys.exc_value)) + _, exc_value, _ = sys.exc_info() + print("%s ABORTED transmission (Reason: %s)" % (client_address[0], exc_value)) def catchSSLErrors(BaseSSLClass): diff --git a/tests/test_servefile.py b/tests/test_servefile.py index 1cfdcf9..a18e3af 100644 --- a/tests/test_servefile.py +++ b/tests/test_servefile.py @@ -356,3 +356,24 @@ def test_https_big_download(run_servefile, datadir): urllib3.disable_warnings() check_download(data, protocol='https', verify=False) + + +def test_abort_download(run_servefile, datadir): + data = "x" * (10 * 1024 ** 2) + p = datadir({'testfile': data}) / 'testfile' + env = os.environ.copy() + env['PYTHONUNBUFFERED'] = '1' + proc = run_servefile(str(p), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) + + # provoke a connection abort + # hopefully the buffers will not fill up with all of the 10mb + sock = socket.socket(socket.AF_INET) + sock.connect(("localhost", 8080)) + sock.send(b"GET /testfile HTTP/1.0\n\n") + resp = sock.recv(100) + assert resp != b'' + sock.close() + time.sleep(0.1) + proc.kill() + out = proc.stdout.read().decode() + assert "127.0.0.1 ABORTED transmission" in out