Copy length bytes from fileobj src to fileobj dst. If length is None, copy the entire content.
(src, dst, length=None, exception=OSError, bufsize=None)
| 4 | |
| 5 | |
| 6 | def copyfileobj(src, dst, length=None, exception=OSError, bufsize=None): |
| 7 | """Copy length bytes from fileobj src to fileobj dst. |
| 8 | If length is None, copy the entire content. |
| 9 | """ |
| 10 | if bufsize is None: |
| 11 | bufsize = pipebuf.PIPE_BUF_BYTES |
| 12 | |
| 13 | if length == 0: |
| 14 | return |
| 15 | if length is None: |
| 16 | shutil.copyfileobj(src, dst, bufsize) |
| 17 | return |
| 18 | |
| 19 | blocks, remainder = divmod(length, bufsize) |
| 20 | for b in range(blocks): |
| 21 | buf = src.read(bufsize) |
| 22 | if len(buf) < bufsize: |
| 23 | raise exception("unexpected end of data") |
| 24 | dst.write(buf) |
| 25 | |
| 26 | if remainder != 0: |
| 27 | buf = src.read(remainder) |
| 28 | if len(buf) < remainder: |
| 29 | raise exception("unexpected end of data") |
| 30 | dst.write(buf) |
| 31 | return |