(original) (raw)

changeset: 103890:78efbf499611 branch: 3.5 parent: 103881:16d4bbda29c2 user: Berker Peksag berker.peksag@gmail.com date: Sat Sep 17 23:22:06 2016 +0300 files: Lib/socket.py Lib/test/test_socket.py Misc/NEWS description: Issue #26384: Fix UnboundLocalError in socket._sendfile_use_sendfile diff -r 16d4bbda29c2 -r 78efbf499611 Lib/socket.py --- a/Lib/socket.py Sat Sep 17 16:20:55 2016 +0300 +++ b/Lib/socket.py Sat Sep 17 23:22:06 2016 +0300 @@ -258,7 +258,7 @@ 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 -r 16d4bbda29c2 -r 78efbf499611 Lib/test/test_socket.py --- a/Lib/test/test_socket.py Sat Sep 17 16:20:55 2016 +0300 +++ b/Lib/test/test_socket.py Sat Sep 17 23:22:06 2016 +0300 @@ -1447,6 +1447,25 @@ 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 -r 16d4bbda29c2 -r 78efbf499611 Misc/NEWS --- a/Misc/NEWS Sat Sep 17 16:20:55 2016 +0300 +++ b/Misc/NEWS Sat Sep 17 23:22:06 2016 +0300 @@ -71,6 +71,8 @@ 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. /berker.peksag@gmail.com