summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorWander Lairson Costa <wander@redhat.com>2026-02-23 19:17:49 +0300
committerGabriele Monaco <gmonaco@redhat.com>2026-03-31 17:47:51 +0300
commit6c7e548e313dcfbb8a4965b9b93c5c59537b35d9 (patch)
treef8e7ef39220d337e0b3aa8a8ef778402bd92496a /tools
parentc4258d8160b2a40732f3fe5272a9ec524e0a5e94 (diff)
downloadlinux-6c7e548e313dcfbb8a4965b9b93c5c59537b35d9.tar.xz
rv/rvgen: use context managers for file operations
Replace manual file open and close operations with context managers throughout the rvgen codebase. The previous implementation used explicit open() and close() calls, which could lead to resource leaks if exceptions occurred between opening and closing the file handles. This change affects three file operations: reading DOT specification files in the automata parser, reading template files in the generator base class, and writing generated monitor files. All now use the with statement to ensure proper resource cleanup even in error conditions. Context managers provide automatic cleanup through the with statement, which guarantees that file handles are closed when the with block exits regardless of whether an exception occurred. This follows PEP 343 recommendations and is the standard Python idiom for resource management. The change also reduces code verbosity while improving safety and maintainability. Signed-off-by: Wander Lairson Costa <wander@redhat.com> Reviewed-by: Nam Cao <namcao@linutronix.de> Reviewed-by: Gabriele Monaco <gmonaco@redhat.com> Link: https://lore.kernel.org/r/20260223162407.147003-7-wander@redhat.com Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/verification/rvgen/rvgen/automata.py6
-rw-r--r--tools/verification/rvgen/rvgen/generator.py12
2 files changed, 6 insertions, 12 deletions
diff --git a/tools/verification/rvgen/rvgen/automata.py b/tools/verification/rvgen/rvgen/automata.py
index 4f5681265ee2..10146b6061ed 100644
--- a/tools/verification/rvgen/rvgen/automata.py
+++ b/tools/verification/rvgen/rvgen/automata.py
@@ -91,13 +91,11 @@ class Automata:
cursor = 0
dot_lines = []
try:
- dot_file = open(self.__dot_path)
+ with open(self.__dot_path) as dot_file:
+ dot_lines = dot_file.read().splitlines()
except OSError as exc:
raise AutomataError(exc.strerror) from exc
- dot_lines = dot_file.read().splitlines()
- dot_file.close()
-
# checking the first line:
line = dot_lines[cursor].split()
diff --git a/tools/verification/rvgen/rvgen/generator.py b/tools/verification/rvgen/rvgen/generator.py
index 61174b139123..d932e96dd66d 100644
--- a/tools/verification/rvgen/rvgen/generator.py
+++ b/tools/verification/rvgen/rvgen/generator.py
@@ -51,11 +51,8 @@ class RVGenerator:
raise FileNotFoundError("Could not find the rv directory, do you have the kernel source installed?")
def _read_file(self, path):
- fd = open(path, 'r')
-
- content = fd.read()
-
- fd.close()
+ with open(path, 'r') as fd:
+ content = fd.read()
return content
def _read_template_file(self, file):
@@ -199,9 +196,8 @@ obj-$(CONFIG_RV_MON_{name_up}) += monitors/{name}/{name}.o
return
def __write_file(self, file_name, content):
- file = open(file_name, 'w')
- file.write(content)
- file.close()
+ with open(file_name, 'w') as file:
+ file.write(content)
def _create_file(self, file_name, content):
path = f"{self.name}/{file_name}"