comparison mercurial/archival.py @ 13400:14f3795a5ed7

explicitly close files Add missing calls to close() to many places where files are opened. Relying on reference counting to catch them soon-ish is not portable and fails in environments with a proper GC, such as PyPy.
author Dan Villiom Podlaski Christiansen <danchr@gmail.com>
date Fri, 24 Dec 2010 15:23:01 +0100
parents aae2d5cbde64
children 9a41af6b9f29
comparison
equal deleted inserted replaced
13399:eff102facb15 13400:14f3795a5ed7
82 if fname: 82 if fname:
83 self.fileobj.write(fname + '\000') 83 self.fileobj.write(fname + '\000')
84 84
85 def __init__(self, dest, mtime, kind=''): 85 def __init__(self, dest, mtime, kind=''):
86 self.mtime = mtime 86 self.mtime = mtime
87 self.fileobj = None
87 88
88 def taropen(name, mode, fileobj=None): 89 def taropen(name, mode, fileobj=None):
89 if kind == 'gz': 90 if kind == 'gz':
90 mode = mode[0] 91 mode = mode[0]
91 if not fileobj: 92 if not fileobj:
92 fileobj = open(name, mode + 'b') 93 fileobj = open(name, mode + 'b')
93 gzfileobj = self.GzipFileWithTime(name, mode + 'b', 94 gzfileobj = self.GzipFileWithTime(name, mode + 'b',
94 zlib.Z_BEST_COMPRESSION, 95 zlib.Z_BEST_COMPRESSION,
95 fileobj, timestamp=mtime) 96 fileobj, timestamp=mtime)
97 self.fileobj = gzfileobj
96 return tarfile.TarFile.taropen(name, mode, gzfileobj) 98 return tarfile.TarFile.taropen(name, mode, gzfileobj)
97 else: 99 else:
100 self.fileobj = fileobj
98 return tarfile.open(name, mode + kind, fileobj) 101 return tarfile.open(name, mode + kind, fileobj)
99 102
100 if isinstance(dest, str): 103 if isinstance(dest, str):
101 self.z = taropen(dest, mode='w:') 104 self.z = taropen(dest, mode='w:')
102 else: 105 else:
118 data = cStringIO.StringIO(data) 121 data = cStringIO.StringIO(data)
119 self.z.addfile(i, data) 122 self.z.addfile(i, data)
120 123
121 def done(self): 124 def done(self):
122 self.z.close() 125 self.z.close()
126 if self.fileobj:
127 self.fileobj.close()
123 128
124 class tellable(object): 129 class tellable(object):
125 '''provide tell method for zipfile.ZipFile when writing to http 130 '''provide tell method for zipfile.ZipFile when writing to http
126 response file object.''' 131 response file object.'''
127 132