I noticed this while working on issue 16915: Traceback (most recent call last): ... File "/home/berker/projects/cpython/default/Lib/socket.py", line 262, in _sendfile_use_sendfile raise _GiveupOnSendfile(err) # not a regular file UnboundLocalError: local variable 'err' referenced before assignment Here's a patch.
This issue can be tested without moking os.fstat(): class F: def fileno(self): return fd with socket.socket() as sock: fd = os.open(os.curdir, os.O_RDONLY) os.close(fd) self.assertRaises(socket._GiveupOnSendfile, sock._sendfile_use_sendfile, F()) os.fstat() can raise not only OSError. It may be worth to test also with fd = 2**1000 and fd = None.