Skip to content

Commit 25bde6f

Browse files
committed
Apply suggestions to Python code
1 parent 94e522e commit 25bde6f

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

Lib/socket.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ def makefile(self, mode="r", buffering=None, *,
349349
text.mode = mode
350350
return text
351351

352-
def _sendfile_zerocopy(self, zerocopy_func, giveup_err, file, offset=0, count=None):
352+
def _sendfile_zerocopy(self, zerocopy_func, giveup_exc_type, file,
353+
offset=0, count=None):
353354
"""
354355
Send a file using a zero-copy function.
355356
"""
@@ -360,11 +361,11 @@ def _sendfile_zerocopy(self, zerocopy_func, giveup_err, file, offset=0, count=No
360361
try:
361362
fileno = file.fileno()
362363
except (AttributeError, io.UnsupportedOperation) as err:
363-
raise giveup_err(err) # not a regular file
364+
raise giveup_exc_type(err) # not a regular file
364365
try:
365366
fsize = os.fstat(fileno).st_size
366367
except OSError as err:
367-
raise giveup_err(err) # not a regular file
368+
raise giveup_exc_type(err) # not a regular file
368369
if not fsize:
369370
return 0 # empty file
370371
# Truncate to 1GiB to avoid OverflowError, see bpo-38319.
@@ -406,7 +407,7 @@ def _sendfile_zerocopy(self, zerocopy_func, giveup_err, file, offset=0, count=No
406407
# one being 'file' is not a regular mmap(2)-like
407408
# file, in which case we'll fall back on using
408409
# plain send().
409-
raise giveup_err(err)
410+
raise giveup_exc_type(err)
410411
raise err from None
411412
else:
412413
if sent == 0:
@@ -418,17 +419,17 @@ def _sendfile_zerocopy(self, zerocopy_func, giveup_err, file, offset=0, count=No
418419
if total_sent > 0 and hasattr(file, 'seek'):
419420
file.seek(offset)
420421

421-
def _sendfile_use_sendfile(self, file, offset=0, count=None):
422-
if not (sendfile := getattr(os, 'sendfile', None)):
422+
if hasattr(os, 'sendfile'):
423+
def _sendfile_use_sendfile(self, file, offset=0, count=None):
424+
return self._sendfile_zerocopy(
425+
partial(os.sendfile, self.fileno()),
426+
_GiveupOnSendfile,
427+
file, offset, count,
428+
)
429+
else:
430+
def _sendfile_use_sendfile(self, file, offset=0, count=None):
423431
raise _GiveupOnSendfile(
424432
"os.sendfile() not available on this platform")
425-
return self._sendfile_zerocopy(
426-
zerocopy_func=partial(sendfile, self.fileno()),
427-
giveup_err=_GiveupOnSendfile,
428-
file=file,
429-
offset=offset,
430-
count=count,
431-
)
432433

433434
def _sendfile_use_send(self, file, offset=0, count=None):
434435
self._check_sendfile_params(file, offset, count)

Lib/ssl.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ def _sendfile_use_ssl_sendfile(self, file, offset=0, count=None):
12761276
)
12771277
return self._sendfile_zerocopy(
12781278
zerocopy_func=ssl_sendfile,
1279-
giveup_err=_GiveupOnSSLSendfile,
1279+
giveup_exc_type=_GiveupOnSSLSendfile,
12801280
file=file,
12811281
offset=offset,
12821282
count=count,
@@ -1288,12 +1288,20 @@ def sendfile(self, file, offset=0, count=None):
12881288
"""
12891289
if self._sslobj is None:
12901290
return super().sendfile(file, offset, count)
1291-
if self._sslobj.uses_ktls_for_send():
1292-
try:
1293-
return self._sendfile_use_ssl_sendfile(file, offset, count)
1294-
except _GiveupOnSSLSendfile:
1295-
pass
1296-
return self._sendfile_use_send(file, offset, count)
1291+
1292+
if not self._sslobj.uses_ktls_for_send():
1293+
return self._sendfile_use_send(file, offset, count)
1294+
1295+
sendfile = getattr(self._sslobj, "sendfile", None)
1296+
if sendfile is None:
1297+
return self._sendfile_use_send(file, offset, count)
1298+
1299+
try:
1300+
return self._sendfile_zerocopy(
1301+
sendfile, _GiveupOnSSLSendfile, file, offset, count,
1302+
)
1303+
except _GiveupOnSSLSendfile:
1304+
return self._sendfile_use_send(file, offset, count)
12971305

12981306
def recv(self, buflen=1024, flags=0):
12991307
self._checkClosed()

0 commit comments

Comments
 (0)