From mboxrd@z Thu Jan 1 00:00:00 1970 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 D8B343A9620 for ; Tue, 20 Jan 2026 07:44:40 +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=1768895084; cv=none; b=YSPrnztIc1jro0YHSz7JnLASKRGJ5XdIK1VRnu6ZYzl8ZJS/4uiaFwQdx3AokweL81q/z0GUr0C2550hA2ThkVXuUn8I6fsy/19H0Ae19cuqAE9YCtHJQunQ1txjKDHjlg0q6r/ijLIMHsH3xS1TFzwQvmoZTFlButog4wNypTE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768895084; c=relaxed/simple; bh=ipg+mSoJEdk5zjE+os8QYlY0SM6gJ7gF/I8SjyIouwA=; h=Message-ID:Subject:From:To:Date:In-Reply-To:References: Content-Type:MIME-Version; b=CQkZgOLjMWw0SqZKdfAyoXt1IYGegrTaxao69UF82wJYstNXt0sQvjLXK+S7v8m1CruaL8VlYE8bInxvnFMbMjMiKvfuwFINEdcnlKzfTvQ6Dhpv07RrHa7tKNitBx52h9MjDSiIh1oYxibOVvjdwW7mtzZCIc2TptoW+KFhLB4= 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=dzxezR5n; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=ThhAqvf2; 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="dzxezR5n"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="ThhAqvf2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768895079; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=ipg+mSoJEdk5zjE+os8QYlY0SM6gJ7gF/I8SjyIouwA=; b=dzxezR5nA6fadbrlg79vRUfRYgWJzs+vNYAU3ea5Qri9gkWSzy9C4lk8tExS2kVvucMoTV RvUgHcJQ4XUHVg3K1XZE9VkKfWfBYodP4yq7yfdLVGX9ub8BvNKlzyO1TRPBBBaIPhEmir T9uhIxcZjOEpldA3HH0BIhvJ3zSy3JI= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-361-TPM1zqPeOlqmo4sEEejnaQ-1; Tue, 20 Jan 2026 02:44:35 -0500 X-MC-Unique: TPM1zqPeOlqmo4sEEejnaQ-1 X-Mimecast-MFC-AGG-ID: TPM1zqPeOlqmo4sEEejnaQ_1768895074 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-47d3c9b8c56so59443745e9.0 for ; Mon, 19 Jan 2026 23:44:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1768895074; x=1769499874; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:autocrypt :references:in-reply-to:date:to:from:subject:message-id:from:to:cc :subject:date:message-id:reply-to; bh=ipg+mSoJEdk5zjE+os8QYlY0SM6gJ7gF/I8SjyIouwA=; b=ThhAqvf21v4dmw7421FYAWSYUusox2arMB/mvkXmwgl2ltJ67m0kaMMEotvok3bnGW dgYZNPWgj7tCCexP8D6XVYSxfQAiP1n6b72B26qi+wnGv6+he/OcX/5gTvpZlokNd3hM jo7QsnwynQSiOlDFjr7ArzCi015P0NKMjUNrFJqZn+AP/3yVbKaft8QLBz1R5jlRltPN RIY9B2SfW8IHnngb+a15i8UvGk75+urWZyv77sEnqZMI+x9HQSYXq+US/Wf43G0IRLNs sN/mqjCVzDpMCzb0mrhJkCOdOeY4ZESfFgjJk+jpF9CBdYdXFWhz3g4VA3aXkh7PzaTY isVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768895074; x=1769499874; h=mime-version:user-agent:content-transfer-encoding:autocrypt :references:in-reply-to:date:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ipg+mSoJEdk5zjE+os8QYlY0SM6gJ7gF/I8SjyIouwA=; b=AkCCYBB0miHYuP9rEFqyzBfapMqnk/1js+0xg+LxNxapbCCZxwlEdT3qSLt2LNRMGd Jm9Yk2tBvfzBJrBmN+y7nf1OWuxhPMtFjKCLoHHFl8mxonGoVPYysGqt5scDsWh+uOVj UoNrR8Ig4e8xJQ6FRSqfNcMRmdIyHX9lj6HZiS8jniJDkEvhoon+/G5Y5BwVnMiQnaDy zRDmovVVkpJh4X2c1j4wPio1WikGzRitC+pbE7v85z6Y8eB0+cNTak1g09SPxza936vu kHci1G0WcC8VQbUgdcfSdBBsMzfbgFR8ufkn1LgNlPEOMHP9Va31GGRurkjdeOkP8mLl BxoQ== X-Forwarded-Encrypted: i=1; AJvYcCXr2gbGLMKHO3SNNTr7cLYDCmigPsAlWibL8Iw0u2mgAhbGHwfxNwPhwhb4yfTMB8gt+xym/nIXl52CutE=@vger.kernel.org X-Gm-Message-State: AOJu0YzjAcLRrV2WtQt+z/IKqaCt89+WTyFE8d60PO1teYHBF3MnckT6 e4dio5wfmfRxTgQM+SqtNYvSVImNq4d55WffRCdywjaReUCWX7ZxgydsbxVmqz0Sd59m9HQpLxC ydkoMGHre4WwfW14qmkbuBnisCkrrnzEWuNyB5nLwCRwEEOaTHB5r8/iVe915H4vqsg== X-Gm-Gg: AY/fxX7o+S8Ac9x/RO3HBLgfENcGq9NmA/WY41qW0SwTuLAVLtJPu2YxF96S4Xjc3v5 tEHpWnoEc/lZifoKzoBIT1H7oFQ5JorEBOh4Gz6+1tpLztdfzb2J/uxs4Hgl6hWl34ql0eo9pFr Z2qbhoDEdtb06dukXzGhX7ZYSqjA28TUiEgs3E7brD6VuUTbvOcxESECDCJHpc8/pLLOJv/ebfe /3bFb5+N+cT3taTL4jMXEuQ7GjXS0Mlp4CAxyIu3KbnB60F8KgSDc9aXYR7PfJy2POkgwN67KNg RzjwlRdZ7o4YFU9LvK/lKhWRbB6LGrcsJwPFdfKMseK2CPFiOIQYGscRCZX99CUdKIJCBupEeMj Hqu1yG+9411OlgavMSDRSxxsXzzs/L6x/Zn1BQXM+OEM/Y2jKnF0Tesre7iZeViZzeFzQDZ/0xN umjEXKC+WL X-Received: by 2002:a05:600c:474a:b0:477:9b4a:a82 with SMTP id 5b1f17b1804b1-4801eb14b36mr181412405e9.35.1768895074070; Mon, 19 Jan 2026 23:44:34 -0800 (PST) X-Received: by 2002:a05:600c:474a:b0:477:9b4a:a82 with SMTP id 5b1f17b1804b1-4801eb14b36mr181412165e9.35.1768895073650; Mon, 19 Jan 2026 23:44:33 -0800 (PST) Received: from gmonaco-thinkpadt14gen3.rmtit.csb (185-132-178-103.hosted-by-worldstream.net. [185.132.178.103]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47f4289b789sm291325845e9.1.2026.01.19.23.44.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jan 2026 23:44:33 -0800 (PST) Message-ID: <9ed5639ec750349acfd243a7f8358acd1d13c2de.camel@redhat.com> Subject: Re: [PATCH 06/26] rv/rvgen: use context managers for file operations From: Gabriele Monaco To: Wander Lairson Costa , Steven Rostedt , Nam Cao , open list , "open list:RUNTIME VERIFICATION (RV)" Date: Tue, 20 Jan 2026 08:44:32 +0100 In-Reply-To: <20260119205601.105821-7-wander@redhat.com> References: <20260119205601.105821-1-wander@redhat.com> <20260119205601.105821-7-wander@redhat.com> Autocrypt: addr=gmonaco@redhat.com; prefer-encrypt=mutual; keydata=mDMEZuK5YxYJKwYBBAHaRw8BAQdAmJ3dM9Sz6/Hodu33Qrf8QH2bNeNbOikqYtxWFLVm0 1a0JEdhYnJpZWxlIE1vbmFjbyA8Z21vbmFjb0BrZXJuZWwub3JnPoiZBBMWCgBBFiEEysoR+AuB3R Zwp6j270psSVh4TfIFAmjKX2MCGwMFCQWjmoAFCwkIBwICIgIGFQoJCAsCBBYCAwECHgcCF4AACgk Q70psSVh4TfIQuAD+JulczTN6l7oJjyroySU55Fbjdvo52xiYYlMjPG7dCTsBAMFI7dSL5zg98I+8 cXY1J7kyNsY6/dcipqBM4RMaxXsOtCRHYWJyaWVsZSBNb25hY28gPGdtb25hY29AcmVkaGF0LmNvb T6InAQTFgoARAIbAwUJBaOagAULCQgHAgIiAgYVCgkICwIEFgIDAQIeBwIXgBYhBMrKEfgLgd0WcK eo9u9KbElYeE3yBQJoymCyAhkBAAoJEO9KbElYeE3yjX4BAJ/ETNnlHn8OjZPT77xGmal9kbT1bC1 7DfrYVISWV2Y1AP9HdAMhWNAvtCtN2S1beYjNybuK6IzWYcFfeOV+OBWRDQ== Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.2 (3.58.2-1.fc43) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Mon, 2026-01-19 at 17:45 -0300, Wander Lairson Costa wrote: > 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. >=20 > 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. >=20 > 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. >=20 > Signed-off-by: Wander Lairson Costa Looks reasonable, thanks! Reviewed-by: Gabriele Monaco > --- > =C2=A0tools/verification/rvgen/rvgen/automata.py=C2=A0 |=C2=A0 6 ++---- > =C2=A0tools/verification/rvgen/rvgen/generator.py | 12 ++++-------- > =C2=A02 files changed, 6 insertions(+), 12 deletions(-) >=20 > diff --git a/tools/verification/rvgen/rvgen/automata.py > b/tools/verification/rvgen/rvgen/automata.py > index ed02d0c69b410..70ff98abea751 100644 > --- a/tools/verification/rvgen/rvgen/automata.py > +++ b/tools/verification/rvgen/rvgen/automata.py > @@ -51,13 +51,11 @@ class Automata: > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cursor =3D 0 > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 dot_lines =3D [] > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 try: > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 dot_f= ile =3D open(self.__dot_path) > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 with = open(self.__dot_path) as dot_file: > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 dot_lines =3D dot_file.read().splitlines() > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 except OSError as exc: > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = raise AutomataError(f"Cannot open the file: {self.__dot_path}") > from exc > =C2=A0 > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 dot_lines =3D dot_file.read()= .splitlines() > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 dot_file.close() > - > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # checking the first lin= e: > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 line =3D dot_lines[curso= r].split() > =C2=A0 > diff --git a/tools/verification/rvgen/rvgen/generator.py > b/tools/verification/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: > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 raise FileNotFoundError(= "Could not find the rv directory, do you have > the kernel source installed?") > =C2=A0 > =C2=A0=C2=A0=C2=A0=C2=A0 def _read_file(self, path): > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 fd =3D open(path, 'r') > - > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 content =3D fd.read() > - > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 fd.close() > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 with open(path, 'r') as fd: > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 conte= nt =3D fd.read() > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return content > =C2=A0 > =C2=A0=C2=A0=C2=A0=C2=A0 def _read_template_file(self, file): > @@ -199,9 +196,8 @@ obj-$(CONFIG_RV_MON_{name_up}) +=3D monitors/{name}/{= name}.o > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = return > =C2=A0 > =C2=A0=C2=A0=C2=A0=C2=A0 def __write_file(self, file_name, content): > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 file =3D open(file_name, 'w') > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 file.write(content) > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 file.close() > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 with open(file_name, 'w') as = file: > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 file.= write(content) > =C2=A0 > =C2=A0=C2=A0=C2=A0=C2=A0 def _create_file(self, file_name, content): > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 path =3D f"{self.name}/{= file_name}"