mirror of
https://github.com/python/cpython.git
synced 2025-08-27 20:25:18 +00:00
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)
This commit is contained in:
parent
afbb7a371f
commit
172bb39452
27 changed files with 249 additions and 259 deletions
|
@ -64,29 +64,28 @@ m_from = re.compile('^[ \t]*import[ \t]+([^#]+)')
|
|||
# Collect data from one file
|
||||
#
|
||||
def process(filename, table):
|
||||
fp = open(filename, 'r')
|
||||
mod = os.path.basename(filename)
|
||||
if mod[-3:] == '.py':
|
||||
mod = mod[:-3]
|
||||
table[mod] = list = []
|
||||
while 1:
|
||||
line = fp.readline()
|
||||
if not line: break
|
||||
while line[-1:] == '\\':
|
||||
nextline = fp.readline()
|
||||
if not nextline: break
|
||||
line = line[:-1] + nextline
|
||||
m_found = m_import.match(line) or m_from.match(line)
|
||||
if m_found:
|
||||
(a, b), (a1, b1) = m_found.regs[:2]
|
||||
else: continue
|
||||
words = line[a1:b1].split(',')
|
||||
# print '#', line, words
|
||||
for word in words:
|
||||
word = word.strip()
|
||||
if word not in list:
|
||||
list.append(word)
|
||||
fp.close()
|
||||
with open(filename) as fp:
|
||||
mod = os.path.basename(filename)
|
||||
if mod[-3:] == '.py':
|
||||
mod = mod[:-3]
|
||||
table[mod] = list = []
|
||||
while 1:
|
||||
line = fp.readline()
|
||||
if not line: break
|
||||
while line[-1:] == '\\':
|
||||
nextline = fp.readline()
|
||||
if not nextline: break
|
||||
line = line[:-1] + nextline
|
||||
m_found = m_import.match(line) or m_from.match(line)
|
||||
if m_found:
|
||||
(a, b), (a1, b1) = m_found.regs[:2]
|
||||
else: continue
|
||||
words = line[a1:b1].split(',')
|
||||
# print '#', line, words
|
||||
for word in words:
|
||||
word = word.strip()
|
||||
if word not in list:
|
||||
list.append(word)
|
||||
|
||||
|
||||
# Compute closure (this is in fact totally general)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue