(self, file, offset=0, count=None)
| 423 | "os.sendfile() not available on this platform") |
| 424 | |
| 425 | def _sendfile_use_send(self, file, offset=0, count=None): |
| 426 | self._check_sendfile_params(file, offset, count) |
| 427 | if self.gettimeout() == 0: |
| 428 | raise ValueError("non-blocking sockets are not supported") |
| 429 | if offset: |
| 430 | file.seek(offset) |
| 431 | blocksize = min(count, 8192) if count else 8192 |
| 432 | total_sent = 0 |
| 433 | # localize variable access to minimize overhead |
| 434 | file_read = file.read |
| 435 | sock_send = self.send |
| 436 | try: |
| 437 | while True: |
| 438 | if count: |
| 439 | blocksize = min(count - total_sent, blocksize) |
| 440 | if blocksize <= 0: |
| 441 | break |
| 442 | data = memoryview(file_read(blocksize)) |
| 443 | if not data: |
| 444 | break # EOF |
| 445 | while True: |
| 446 | try: |
| 447 | sent = sock_send(data) |
| 448 | except BlockingIOError: |
| 449 | continue |
| 450 | else: |
| 451 | total_sent += sent |
| 452 | if sent < len(data): |
| 453 | data = data[sent:] |
| 454 | else: |
| 455 | break |
| 456 | return total_sent |
| 457 | finally: |
| 458 | if total_sent > 0 and hasattr(file, 'seek'): |
| 459 | file.seek(offset + total_sent) |
| 460 | |
| 461 | def _check_sendfile_params(self, file, offset, count): |
| 462 | if 'b' not in getattr(file, 'mode', 'b'): |
no test coverage detected