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)
| 232 | return unsigned_chksum, signed_chksum |
| 233 | |
| 234 | def copyfileobj(src, dst, length=None, exception=OSError, bufsize=None): |
| 235 | """Copy length bytes from fileobj src to fileobj dst. |
| 236 | If length is None, copy the entire content. |
| 237 | """ |
| 238 | bufsize = bufsize or 16 * 1024 |
| 239 | if length == 0: |
| 240 | return |
| 241 | if length is None: |
| 242 | shutil.copyfileobj(src, dst, bufsize) |
| 243 | return |
| 244 | |
| 245 | blocks, remainder = divmod(length, bufsize) |
| 246 | for b in range(blocks): |
| 247 | buf = src.read(bufsize) |
| 248 | if len(buf) < bufsize: |
| 249 | raise exception("unexpected end of data") |
| 250 | dst.write(buf) |
| 251 | |
| 252 | if remainder != 0: |
| 253 | buf = src.read(remainder) |
| 254 | if len(buf) < remainder: |
| 255 | raise exception("unexpected end of data") |
| 256 | dst.write(buf) |
| 257 | return |
| 258 | |
| 259 | def _safe_print(s): |
| 260 | encoding = getattr(sys.stdout, 'encoding', None) |