comparison hgext/convert/cvs.py @ 51687:1eab9e40c0c8

convert: fix various leaked file descriptors Some of these only leaked if an exception occurred between the open and close, but a lot of these leaked unconditionally. A type hint is added to `parsesplicemap` because otherwise this change caused pytype to change the return type from this to `Dict[nothing, nothing]`.
author Matt Harbison <matt_harbison@yahoo.com>
date Thu, 11 Jul 2024 21:54:02 -0400
parents 18c8c18993f0
children f4733654f144
comparison
equal deleted inserted replaced
51686:39033e7a6e0a 51687:1eab9e40c0c8
9 import os 9 import os
10 import re 10 import re
11 import socket 11 import socket
12 12
13 from mercurial.i18n import _ 13 from mercurial.i18n import _
14 from mercurial.pycompat import (
15 open,
16 )
17 from mercurial import ( 14 from mercurial import (
18 encoding, 15 encoding,
19 error, 16 error,
20 util, 17 util,
21 ) 18 )
50 self.changeset = None 47 self.changeset = None
51 self.files = {} 48 self.files = {}
52 self.tags = {} 49 self.tags = {}
53 self.lastbranch = {} 50 self.lastbranch = {}
54 self.socket = None 51 self.socket = None
55 self.cvsroot = open(os.path.join(cvs, b"Root"), b'rb').read()[:-1] 52 self.cvsroot = util.readfile(os.path.join(cvs, b"Root"))[:-1]
56 self.cvsrepo = open(os.path.join(cvs, b"Repository"), b'rb').read()[:-1] 53 self.cvsrepo = util.readfile(os.path.join(cvs, b"Repository"))[:-1]
57 self.encoding = encoding.encoding 54 self.encoding = encoding.encoding
58 55
59 self._connect() 56 self._connect()
60 57
61 def _parse(self): 58 def _parse(self):
158 155
159 if not passw: 156 if not passw:
160 passw = b"A" 157 passw = b"A"
161 cvspass = os.path.expanduser(b"~/.cvspass") 158 cvspass = os.path.expanduser(b"~/.cvspass")
162 try: 159 try:
163 pf = open(cvspass, b'rb') 160 for line in util.readfile(cvspass).splitlines():
164 for line in pf.read().splitlines():
165 part1, part2 = line.split(b' ', 1) 161 part1, part2 = line.split(b' ', 1)
166 # /1 :pserver:user@example.com:2401/cvsroot/foo 162 # /1 :pserver:user@example.com:2401/cvsroot/foo
167 # Ah<Z 163 # Ah<Z
168 if part1 == b'/1': 164 if part1 == b'/1':
169 part1, part2 = part2.split(b' ', 1) 165 part1, part2 = part2.split(b' ', 1)
172 else: 168 else:
173 format = format0 169 format = format0
174 if part1 == format: 170 if part1 == format:
175 passw = part2 171 passw = part2
176 break 172 break
177 pf.close()
178 except IOError as inst: 173 except IOError as inst:
179 if inst.errno != errno.ENOENT: 174 if inst.errno != errno.ENOENT:
180 if not getattr(inst, 'filename', None): 175 if not getattr(inst, 'filename', None):
181 inst.filename = cvspass 176 inst.filename = cvspass
182 raise 177 raise