Skip to content

Commit edc2347

Browse files
committed
Remove explicit close() of opened file resource if context manager is used
Signed-off-by: sschulz92 <[email protected]>
1 parent 2c2fdd0 commit edc2347

File tree

5 files changed

+17
-23
lines changed

5 files changed

+17
-23
lines changed

build.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ def install():
4141
def create_setup_file():
4242
with open("setup.tmpl", "r", encoding="utf-8") as tmpl:
4343
tmpl_content = tmpl.read()
44-
tmpl.close()
4544
with open("setup.py", "w+", encoding="utf-8") as setup:
4645
v = get_version()
47-
setup.write(tmpl_content.format(
48-
v, "{\n\t\t':python_version == \"2.7\"': ['futures']\n\t}"))
49-
setup.close()
46+
setup.write(tmpl_content.format(v))
5047

5148

5249
def generate_package():
@@ -55,7 +52,6 @@ def generate_package():
5552
create_setup_file()
5653
with open(os.devnull, 'w', encoding="utf-8") as fnull:
5754
call([sys.executable, 'setup.py', 'sdist'], stdout=fnull, stderr=fnull)
58-
fnull.close()
5955

6056

6157
def create_zip():

getgauge/registry.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ def capture_to_file():
265265
content = registry.screenshot_provider()()
266266
with open(screenshot_file, "wb") as file:
267267
file.write(content)
268-
file.close()
269268
return os.path.basename(screenshot_file)
270269
screenshot_file = registry.screenshot_provider()()
271270
if not os.path.isabs(screenshot_file):

getgauge/util.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def read_file_contents(file_name):
3636
if os.path.isfile(file_name):
3737
with open(file_name, "r", encoding="utf-8") as f:
3838
content = f.read().replace('\r\n', '\n')
39-
f.close()
4039
return content
4140
return None
4241

setup.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ setup(
1818
'Programming Language :: Python :: 3 :: Only',
1919
],
2020
install_requires=['redBaron', 'debugpy', 'grpcio>=1.39.0', 'protobuf>=3.5.2'],
21-
extras_require={1},
2221
)

tests/test_refactor.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ class RefactorTests(unittest.TestCase):
1515
def setUp(self):
1616
self.preservesNewlines = True
1717
RefactorTests.path = os.path.join(tempfile.gettempdir(), 'step_impl.py')
18-
RefactorTests.file = open(RefactorTests.path, 'w')
19-
RefactorTests.file.write("""@step("Vowels in English language are <vowels>.")
20-
def assert_default_vowels(arg0):
21-
Messages.write_message("Given vowels are {0}".format(given_vowels))
22-
assert given_vowels == "".join(vowels)\n""")
23-
RefactorTests.file.close()
24-
RefactorTests.file = open(RefactorTests.path, 'r')
25-
RefactorTests.data = RefactorTests.file.read()
26-
RefactorTests.file.close()
18+
with open(RefactorTests.path, 'w', encoding="utf-8") as refactor_file:
19+
RefactorTests.file = refactor_file
20+
RefactorTests.file.write("""@step("Vowels in English language are <vowels>.")
21+
def assert_default_vowels(arg0):
22+
Messages.write_message("Given vowels are {0}".format(given_vowels))
23+
assert given_vowels == "".join(vowels)\n""")
24+
25+
with open(RefactorTests.path, 'r', encoding="utf-8") as refactor_file:
26+
RefactorTests.file = refactor_file
27+
RefactorTests.data = RefactorTests.file.read()
28+
2729
registry.add_step('Vowels in English language are <vowels>.', None,
2830
RefactorTests.path)
2931

@@ -337,10 +339,9 @@ def assert_default_vowels(arg0, arg1):
337339
self.assertIn('arg0, arg1', diff_contents)
338340

339341
def getActualText(self):
340-
_file = open(RefactorTests.path, 'r+')
341-
actual_data = _file.read()
342-
_file.seek(0)
343-
_file.truncate()
344-
_file.write(RefactorTests.data)
345-
_file.close()
342+
with open(RefactorTests.path, 'r+', encoding="utf-8") as _file:
343+
actual_data = _file.read()
344+
_file.seek(0)
345+
_file.truncate()
346+
_file.write(RefactorTests.data)
346347
return actual_data

0 commit comments

Comments
 (0)