Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bcfb35f

Browse files
committed
Issue #26384: Fix UnboundLocalError in socket._sendfile_use_sendfile
1 parent 9a1c91a commit bcfb35f

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

Lib/socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def _sendfile_use_sendfile(self, file, offset=0, count=None):
258258
raise _GiveupOnSendfile(err) # not a regular file
259259
try:
260260
fsize = os.fstat(fileno).st_size
261-
except OSError:
261+
except OSError as err:
262262
raise _GiveupOnSendfile(err) # not a regular file
263263
if not fsize:
264264
return 0 # empty file

Lib/test/test_socket.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,25 @@ def test_uknown_socket_family_repr(self):
14471447
self.assertEqual(s.family, 42424)
14481448
self.assertEqual(s.type, 13331)
14491449

1450+
@unittest.skipUnless(hasattr(os, 'sendfile'), 'test needs os.sendfile()')
1451+
def test__sendfile_use_sendfile(self):
1452+
class File:
1453+
def __init__(self, fd):
1454+
self.fd = fd
1455+
1456+
def fileno(self):
1457+
return self.fd
1458+
with socket.socket() as sock:
1459+
fd = os.open(os.curdir, os.O_RDONLY)
1460+
os.close(fd)
1461+
with self.assertRaises(socket._GiveupOnSendfile):
1462+
sock._sendfile_use_sendfile(File(fd))
1463+
with self.assertRaises(OverflowError):
1464+
sock._sendfile_use_sendfile(File(2**1000))
1465+
with self.assertRaises(TypeError):
1466+
sock._sendfile_use_sendfile(File(None))
1467+
1468+
14501469
@unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.')
14511470
class BasicCANTest(unittest.TestCase):
14521471

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Core and Builtins
7171
Library
7272
-------
7373

74+
- Fix UnboundLocalError in socket._sendfile_use_sendfile.
75+
7476
- Issue #28075: Check for ERROR_ACCESS_DENIED in Windows implementation of
7577
os.stat(). Patch by Eryk Sun.
7678

0 commit comments

Comments
 (0)