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)
def copyfileobj(src, dest, length=512):
buf = bytearray(length)
while True:
sz = src.readinto(buf)
if not sz:
break
if sz == length:
if hasattr(src, "readinto"):
buf = bytearray(length)
while True:
sz = src.readinto(buf)
if not sz:
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)
else:
b = memoryview(buf)[:sz]
dest.write(b)