Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/util.py @ 27778:4d10600c3f08
util: simplify file I/O functions using context managers
author | Bryan O'Sullivan <bryano@fb.com> |
---|---|
date | Tue, 12 Jan 2016 14:49:35 -0800 |
parents | 5ef99738a562 |
children | ba427b51f1d8 |
comparison
equal
deleted
inserted
replaced
27777:47ac135113ec | 27778:4d10600c3f08 |
---|---|
1443 raise | 1443 raise |
1444 if mode is not None: | 1444 if mode is not None: |
1445 os.chmod(name, mode) | 1445 os.chmod(name, mode) |
1446 | 1446 |
1447 def readfile(path): | 1447 def readfile(path): |
1448 fp = open(path, 'rb') | 1448 with open(path, 'rb') as fp: |
1449 try: | |
1450 return fp.read() | 1449 return fp.read() |
1451 finally: | |
1452 fp.close() | |
1453 | 1450 |
1454 def writefile(path, text): | 1451 def writefile(path, text): |
1455 fp = open(path, 'wb') | 1452 with open(path, 'wb') as fp: |
1456 try: | |
1457 fp.write(text) | 1453 fp.write(text) |
1458 finally: | |
1459 fp.close() | |
1460 | 1454 |
1461 def appendfile(path, text): | 1455 def appendfile(path, text): |
1462 fp = open(path, 'ab') | 1456 with open(path, 'ab') as fp: |
1463 try: | |
1464 fp.write(text) | 1457 fp.write(text) |
1465 finally: | |
1466 fp.close() | |
1467 | 1458 |
1468 class chunkbuffer(object): | 1459 class chunkbuffer(object): |
1469 """Allow arbitrary sized chunks of data to be efficiently read from an | 1460 """Allow arbitrary sized chunks of data to be efficiently read from an |
1470 iterator over chunks of arbitrary size.""" | 1461 iterator over chunks of arbitrary size.""" |
1471 | 1462 |