From nobody Sun Feb 8 18:49:25 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BFCDE413224 for ; Wed, 4 Feb 2026 14:51:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770216712; cv=none; b=FJxVgwILjkLbSaKEFc2QhN8l2AtoL2CAYxPLJyQhDi/LgFp3TXT3xGdCJLuoyc5WRSpNPxHzGBmeDLmEqIbKzydI2eGPMMtDg7tx9f5bqEzUEYnLIoIuCAPOqPCyXWnwUkh8xAV6DklihSUMvdzzO/65bAgYZRo3Yya8vWxZO00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770216712; c=relaxed/simple; bh=vKGoCud62RvTn/RnPIPbJSk8pA3OGV7VeKLMQKjyh+w=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SLUScfH3OS3d3qXavpQea1yi6KwtQ0mjcfYLedT4GIpNFyFPcZKulJG5ZuBCcIGVzz5L0wFWk5IIJk/FOXJGDb3lIhsnpGVJVwj+S6RApCJi04Kw27Le/JWe+h50ZU31ypjTzwB4w3CQ4lBxL+wgs0Xpp67rFXPDgskVf6eSLJs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Zsr7/lni; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Zsr7/lni" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770216710; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=keHT6bdZvc3LtAXA8IGY3q2fOIXGt3Nvqkknb+sVMEQ=; b=Zsr7/lniluhdeZPfpdNKmjds8IUQ7TPJt6MXZwvlMslEebKjemJD8ZVngeEU+LxhUGcrL6 2q4jVNtMBi4+Hy66qP9hOnougbQsX+pOBIKEuONt458uclJHG8DpXaYseF6D71KvYmMB9Y H8JdGhUXpDiRhxfUQiXmTxOpLys8pKQ= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-417-scbrbT51NtWHnT0oR8H5Sw-1; Wed, 04 Feb 2026 09:51:47 -0500 X-MC-Unique: scbrbT51NtWHnT0oR8H5Sw-1 X-Mimecast-MFC-AGG-ID: scbrbT51NtWHnT0oR8H5Sw_1770216706 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B3F371956059; Wed, 4 Feb 2026 14:51:46 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.88.140]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3242A30001B9; Wed, 4 Feb 2026 14:51:43 +0000 (UTC) From: Wander Lairson Costa To: Steven Rostedt , Gabriele Monaco , Nam Cao , Wander Lairson Costa , linux-trace-kernel@vger.kernel.org (open list:RUNTIME VERIFICATION (RV)), linux-kernel@vger.kernel.org (open list) Subject: [PATCH v2 06/20] rv/rvgen: use context managers for file operations Date: Wed, 4 Feb 2026 11:42:32 -0300 Message-ID: <20260204144914.104028-7-wander@redhat.com> In-Reply-To: <20260204144914.104028-1-wander@redhat.com> References: <20260204144914.104028-1-wander@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Nam Cao Reviewed-by: Gabriele Monaco --- tools/verification/rvgen/rvgen/automata.py | 6 ++---- tools/verification/rvgen/rvgen/generator.py | 12 ++++-------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/tools/verification/rvgen/rvgen/automata.py b/tools/verificatio= n/rvgen/rvgen/automata.py index 07ae81e9a49fa..1feb5f0c0bc3e 100644 --- a/tools/verification/rvgen/rvgen/automata.py +++ b/tools/verification/rvgen/rvgen/automata.py @@ -51,13 +51,11 @@ class Automata: cursor =3D 0 dot_lines =3D [] try: - dot_file =3D open(self.__dot_path) + with open(self.__dot_path) as dot_file: + dot_lines =3D dot_file.read().splitlines() except OSError as exc: raise AutomataError(f"Cannot open the file: {self.__dot_path}"= ) from exc =20 - dot_lines =3D dot_file.read().splitlines() - dot_file.close() - # checking the first line: line =3D dot_lines[cursor].split() =20 diff --git a/tools/verification/rvgen/rvgen/generator.py b/tools/verificati= on/rvgen/rvgen/generator.py index 6d16fb68798a7..ee75e111feef1 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 h= ave the kernel source installed?") =20 def _read_file(self, path): - fd =3D open(path, 'r') - - content =3D fd.read() - - fd.close() + with open(path, 'r') as fd: + content =3D fd.read() return content =20 def _read_template_file(self, file): @@ -199,9 +196,8 @@ obj-$(CONFIG_RV_MON_{name_up}) +=3D monitors/{name}/{na= me}.o return =20 def __write_file(self, file_name, content): - file =3D open(file_name, 'w') - file.write(content) - file.close() + with open(file_name, 'w') as file: + file.write(content) =20 def _create_file(self, file_name, content): path =3D f"{self.name}/{file_name}" --=20 2.52.0