This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: UnboundLocalError in socket._sendfile_use_sendfile
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2016-02-18 15:50 by berker.peksag, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
socket_unboundlocalerror.diff berker.peksag, 2016-02-18 15:50 review
issue26384_v2.diff berker.peksag, 2016-09-17 19:18 review
issue26384_v3.diff berker.peksag, 2016-09-17 20:00 review
Messages (5)
msg260464 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-02-18 15:50
I noticed this while working on issue 16915:

Traceback (most recent call last):
  File "/home/berker/projects/cpython/default/Lib/", line 262, in _sendfile_use_sendfile
    raise _GiveupOnSendfile(err)  # not a regular file
UnboundLocalError: local variable 'err' referenced before assignment

Here's a patch.
msg260806 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-02-24 14:46
This issue can be tested without moking os.fstat():

        class F:
            def fileno(self):
                return fd
        with socket.socket() as sock:
            fd =, os.O_RDONLY)
                              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.
msg276822 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-09-17 19:18
Thanks, Serhiy. Here's an updated patch.
msg276828 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-09-17 20:01
msg276837 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-09-17 20:22
New changeset 78efbf499611 by Berker Peksag in branch '3.5':
Issue #26384: Fix UnboundLocalError in socket._sendfile_use_sendfile

New changeset 2156aa4050c7 by Berker Peksag in branch '3.6':
Issue #26384: Merge from 3.5

New changeset 0d440fda8604 by Berker Peksag in branch 'default':
Issue #26384: Merge from 3.6
Date User Action Args
2022-04-11 14:58:27adminsetgithub: 70572
2016-09-17 20:23:45berker.peksagsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2016-09-17 20:22:35python-devsetnosy: + python-dev
messages: + msg276837
2016-09-17 20:01:17serhiy.storchakasetmessages: + msg276828
2016-09-17 20:00:10berker.peksagsetfiles: + issue26384_v3.diff
2016-09-17 19:18:55berker.peksagsetfiles: + issue26384_v2.diff

messages: + msg276822
versions: + Python 3.7
2016-07-01 10:42:42berker.peksaglinkissue27433 superseder
2016-02-24 14:46:23serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg260806
2016-02-18 15:50:52berker.peksagcreate