Skip to content

Commit a58ca53

Browse files
rakesh balusapombredanne
authored andcommitted
#253 Ensure that opened files are closed
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 0d4905b commit a58ca53

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/packagedcode/pypi.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,20 @@ def parse_pkg_info(location):
6464
parse a 'PKG-INFO' file at 'location' and return a PythonPackage object.
6565
"""
6666
infos = {}
67-
with open(location, 'rb') as txt_file:
68-
lines = []
69-
for line in txt_file:
70-
lines.append(line)
71-
lines = ''.join(lines)
72-
for attribute in PKG_INFO_ATTRIBUTES:
73-
infos[attribute] = re.findall('^'+attribute+'[\s:]*.*', lines, flags=re.MULTILINE)[0]
74-
infos[attribute] = re.sub('^'+attribute+'[\s:]*', '', infos[attribute], flags=re.MULTILINE)
75-
if infos[attribute] == 'UNKNOWN':
76-
infos[attribute] = None
77-
package = PythonPackage(
78-
name=infos.get('Name'),
79-
version=infos.get('Version'),
80-
summary=infos.get('Summary'),
81-
homepage_url=infos.get('Home-page'),
82-
asserted_licenses=[AssertedLicense(license=infos.get('License'))],
83-
authors=[infos.get('Author')],
84-
)
67+
file_text = open(location, 'rb').read()
68+
for attribute in PKG_INFO_ATTRIBUTES:
69+
infos[attribute] = re.findall('^'+attribute+'[\s:]*.*', file_text, flags=re.MULTILINE)[0]
70+
infos[attribute] = re.sub('^'+attribute+'[\s:]*', '', infos[attribute], flags=re.MULTILINE)
71+
if infos[attribute] == 'UNKNOWN':
72+
infos[attribute] = None
73+
package = PythonPackage(
74+
name=infos.get('Name'),
75+
version=infos.get('Version'),
76+
summary=infos.get('Summary'),
77+
homepage_url=infos.get('Home-page'),
78+
asserted_licenses=[AssertedLicense(license=infos.get('License'))],
79+
authors=[infos.get('Author')],
80+
)
8581
return package
8682

8783

0 commit comments

Comments
 (0)