Show More
@@ -1516,6 +1516,15 b' class atomictempfile(object):' | |||
|
1516 | 1516 | if safehasattr(self, '_fp'): # constructor actually did something |
|
1517 | 1517 | self.discard() |
|
1518 | 1518 | |
|
1519 | def __enter__(self): | |
|
1520 | return self | |
|
1521 | ||
|
1522 | def __exit__(self, exctype, excvalue, traceback): | |
|
1523 | if exctype is not None: | |
|
1524 | self.discard() | |
|
1525 | else: | |
|
1526 | self.close() | |
|
1527 | ||
|
1519 | 1528 | def makedirs(name, mode=None, notindexed=False): |
|
1520 | 1529 | """recursive directory creation with parent mode inheritance |
|
1521 | 1530 |
@@ -96,6 +96,24 b' class testatomictempfile(unittest.TestCa' | |||
|
96 | 96 | self.assertTrue(file.read(), b'foobar\n') |
|
97 | 97 | file.discard() |
|
98 | 98 | |
|
99 | def testcontextmanagersuccess(self): | |
|
100 | """When the context closes, the file is closed""" | |
|
101 | with atomictempfile('foo') as f: | |
|
102 | self.assertFalse(os.path.isfile('foo')) | |
|
103 | f.write(b'argh\n') | |
|
104 | self.assertTrue(os.path.isfile('foo')) | |
|
105 | ||
|
106 | def testcontextmanagerfailure(self): | |
|
107 | """On exception, the file is discarded""" | |
|
108 | try: | |
|
109 | with atomictempfile('foo') as f: | |
|
110 | self.assertFalse(os.path.isfile('foo')) | |
|
111 | f.write(b'argh\n') | |
|
112 | raise ValueError | |
|
113 | except ValueError: | |
|
114 | pass | |
|
115 | self.assertFalse(os.path.isfile('foo')) | |
|
116 | ||
|
99 | 117 | if __name__ == '__main__': |
|
100 | 118 | import silenttestrunner |
|
101 | 119 | silenttestrunner.main(__name__) |
General Comments 0
You need to be logged in to leave comments.
Login now