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