diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-03-30 08:33:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-30 08:33:02 +0200 |
commit | 172bb39452ae8b3ccdf5d1f23ead46f44200cd49 (patch) | |
tree | 5e1effbca3664b839a81eb7a7d62fa4974cfbfb1 /Tools/scripts/pdeps.py | |
parent | afbb7a371fb44edc731344eab5b474ad8f7b57d7 (diff) | |
download | cpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.tar.gz cpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.zip |
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)
Diffstat (limited to 'Tools/scripts/pdeps.py')
-rwxr-xr-x | Tools/scripts/pdeps.py | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/Tools/scripts/pdeps.py b/Tools/scripts/pdeps.py index f8218ac5243..4e8e930948f 100755 --- a/Tools/scripts/pdeps.py +++ b/Tools/scripts/pdeps.py @@ -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) |