Skip to content

Commit f7f06c3

Browse files
authored
Merge pull request #135 from makermelissa/master
Explicitly close files after use
2 parents 64621ec + 27e60ee commit f7f06c3

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

adafruit_platformdetect/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ def get_cpuinfo_field(self, field):
4848

4949
with open("/proc/cpuinfo", "r") as infile:
5050
cpuinfo = infile.read().split("\n")
51+
infile.close()
5152
for line in cpuinfo:
5253
match = re.search(pattern, line, flags=re.IGNORECASE)
5354
if match:
5455
return match.group(1)
55-
5656
return None
5757

5858
def check_dt_compatible_value(self, value):
@@ -61,12 +61,8 @@ def check_dt_compatible_value(self, value):
6161
otherwise False.
6262
"""
6363
# Match a value like 'qcom,apq8016-sbc':
64-
try:
65-
with open("/proc/device-tree/compatible") as compatible:
66-
if value in compatible.read():
67-
return True
68-
except FileNotFoundError:
69-
pass
64+
if value in self.get_device_compatible():
65+
return True
7066

7167
return False
7268

@@ -85,6 +81,7 @@ def get_armbian_release_field(self, field):
8581
match = re.search(pattern, line)
8682
if match:
8783
field_value = match.group(1)
84+
release_file.close()
8885
except FileNotFoundError:
8986
pass
9087

@@ -100,6 +97,7 @@ def get_device_model(self):
10097
try:
10198
with open("/proc/device-tree/model", "r") as model_file:
10299
model = model_file.read()
100+
model_file.close()
103101
except FileNotFoundError:
104102
pass
105103

@@ -114,6 +112,7 @@ def get_device_compatible(self):
114112
try:
115113
with open("/proc/device-tree/compatible", "r") as model_file:
116114
model = model_file.read()
115+
model_file.close()
117116
except FileNotFoundError:
118117
pass
119118

@@ -129,21 +128,23 @@ def check_board_asset_tag_value(self):
129128
try:
130129
with open("/sys/devices/virtual/dmi/id/board_asset_tag", "r") as tag_file:
131130
tag = tag_file.read().strip()
131+
tag_file.close()
132132
except FileNotFoundError:
133133
pass
134134

135135
return tag
136136

137137
def check_board_name_value(self):
138138
"""
139-
Search /sys/devices/virtual/dmi/id for the device model and return its value, if found,
139+
Search /sys/devices/virtual/dmi/id for the board name and return its value, if found,
140140
otherwise None. Debian/ubuntu based
141141
"""
142142
board_name = None
143143

144144
try:
145145
with open("/sys/devices/virtual/dmi/id/board_name", "r") as board_name_file:
146146
board_name = board_name_file.read().strip()
147+
board_name.close()
147148
except FileNotFoundError:
148149
pass
149150

0 commit comments

Comments
 (0)