shutil: copyfileobj(): Support file objects without readinto() method.

pull/20/merge
Paul Sokolovsky 2015-02-04 02:50:04 +02:00
rodzic 94b2afdf8b
commit f3dd9abcda
1 zmienionych plików z 16 dodań i 9 usunięć

Wyświetl plik

@ -9,13 +9,20 @@ def rmtree(top):
os.rmdir(path) os.rmdir(path)
def copyfileobj(src, dest, length=512): def copyfileobj(src, dest, length=512):
buf = bytearray(length) if hasattr(src, "readinto"):
while True: buf = bytearray(length)
sz = src.readinto(buf) while True:
if not sz: sz = src.readinto(buf)
break if not sz:
if sz == length: break
if sz == length:
dest.write(buf)
else:
b = memoryview(buf)[:sz]
dest.write(b)
else:
while True:
buf = src.read(length)
if not buf:
break
dest.write(buf) dest.write(buf)
else:
b = memoryview(buf)[:sz]
dest.write(b)