Skip to content

Commit 1e45904

Browse files
authored
Merge pull request #155 from jaesivsm/remove-unecessary-close
Remove unecessary file close
2 parents 31a4811 + 3c9133d commit 1e45904

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

adafruit_platformdetect/__init__.py

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

4949
with open("/proc/cpuinfo", "r") as infile:
5050
cpuinfo = infile.read().split("\n")
51-
infile.close()
52-
for line in cpuinfo:
53-
match = re.search(pattern, line, flags=re.IGNORECASE)
54-
if match:
55-
return match.group(1)
51+
for line in cpuinfo:
52+
match = re.search(pattern, line, flags=re.IGNORECASE)
53+
if match:
54+
return match.group(1)
5655
return None
5756

5857
def check_dt_compatible_value(self, value):
@@ -82,7 +81,6 @@ def get_armbian_release_field(self, field):
8281
match = re.search(pattern, line)
8382
if match:
8483
field_value = match.group(1)
85-
release_file.close()
8684
except FileNotFoundError:
8785
pass
8886

@@ -93,60 +91,44 @@ def get_device_model(self):
9391
Search /proc/device-tree/model for the device model and return its value, if found,
9492
otherwise None.
9593
"""
96-
model = None
97-
9894
try:
9995
with open("/proc/device-tree/model", "r") as model_file:
100-
model = model_file.read()
101-
model_file.close()
96+
return model_file.read()
10297
except FileNotFoundError:
10398
pass
104-
105-
return model
99+
return None
106100

107101
def get_device_compatible(self):
108102
"""
109103
Search /proc/device-tree/compatible for the compatible chip name.
110104
"""
111-
model = None
112-
113105
try:
114106
with open("/proc/device-tree/compatible", "r") as model_file:
115-
model = model_file.read()
116-
model_file.close()
107+
return model_file.read()
117108
except FileNotFoundError:
118109
pass
119-
120-
return model
110+
return None
121111

122112
def check_board_asset_tag_value(self):
123113
"""
124114
Search /sys/devices/virtual/dmi/id for the device model and return its value, if found,
125115
otherwise None.
126116
"""
127-
tag = None
128-
129117
try:
130118
with open("/sys/devices/virtual/dmi/id/board_asset_tag", "r") as tag_file:
131-
tag = tag_file.read().strip()
132-
tag_file.close()
119+
return tag_file.read().strip()
133120
except FileNotFoundError:
134121
pass
135-
136-
return tag
122+
return None
137123

138124
def check_board_name_value(self):
139125
"""
140126
Search /sys/devices/virtual/dmi/id for the board name and return its value, if found,
141127
otherwise None. Debian/ubuntu based
142128
"""
143-
board_name = None
144-
145129
try:
146130
with open("/sys/devices/virtual/dmi/id/board_name", "r") as board_name_file:
147-
board_name = board_name_file.read().strip()
148-
board_name.close()
131+
return board_name_file.read().strip()
149132
except FileNotFoundError:
150133
pass
151-
152-
return board_name
134+
return None

0 commit comments

Comments
 (0)