From bcfb35f80d9d1f87d9fa6993c1d3bc35dd5db865 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Sat, 17 Sep 2016 23:22:06 +0300 Subject: [PATCH] Issue #26384: Fix UnboundLocalError in socket._sendfile_use_sendfile --- Lib/socket.py | 2 +- Lib/test/test_socket.py | 19 +++++++++++++++++++ Misc/NEWS | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Lib/socket.py b/Lib/socket.py index 6dddfe1d34b..a9cab32e31e 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -258,7 +258,7 @@ class socket(_socket.socket): raise _GiveupOnSendfile(err) # not a regular file try: fsize = os.fstat(fileno).st_size - except OSError: + except OSError as err: raise _GiveupOnSendfile(err) # not a regular file if not fsize: return 0 # empty file diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 4b047ee76ab..c5975c8c9e5 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1447,6 +1447,25 @@ class GeneralModuleTests(unittest.TestCase): self.assertEqual(s.family, 42424) self.assertEqual(s.type, 13331) + @unittest.skipUnless(hasattr(os, 'sendfile'), 'test needs os.sendfile()') + def test__sendfile_use_sendfile(self): + class File: + def __init__(self, fd): + self.fd = fd + + def fileno(self): + return self.fd + with socket.socket() as sock: + fd = os.open(os.curdir, os.O_RDONLY) + os.close(fd) + with self.assertRaises(socket._GiveupOnSendfile): + sock._sendfile_use_sendfile(File(fd)) + with self.assertRaises(OverflowError): + sock._sendfile_use_sendfile(File(2**1000)) + with self.assertRaises(TypeError): + sock._sendfile_use_sendfile(File(None)) + + @unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') class BasicCANTest(unittest.TestCase): diff --git a/Misc/NEWS b/Misc/NEWS index 8fbdd226340..d3e9fd72e69 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -71,6 +71,8 @@ Core and Builtins Library ------- +- Fix UnboundLocalError in socket._sendfile_use_sendfile. + - Issue #28075: Check for ERROR_ACCESS_DENIED in Windows implementation of os.stat(). Patch by Eryk Sun.