[PATCH v4 09/11] scripts: generate_rust_analyzer.py: avoid FD leak

Tamir Duberstein posted 11 patches 9 months ago
There is a newer version of this series
[PATCH v4 09/11] scripts: generate_rust_analyzer.py: avoid FD leak
Posted by Tamir Duberstein 9 months ago
Use a context manager to avoid leaking file descriptors.

Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
 scripts/generate_rust_analyzer.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index bd6e321a6aa5..ccb15aa66929 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -211,7 +211,8 @@ def generate_crates(
 
     def is_root_crate(build_file: pathlib.Path, target: str) -> bool:
         try:
-            return f"{target}.o" in open(build_file).read()
+            with open(build_file) as f:
+                return f"{target}.o" in f.read()
         except FileNotFoundError:
             return False
 

-- 
2.48.1
Re: [PATCH v4 09/11] scripts: generate_rust_analyzer.py: avoid FD leak
Posted by Fiona Behrens 8 months, 4 weeks ago
Tamir Duberstein <tamird@gmail.com> writes:

> Use a context manager to avoid leaking file descriptors.
>
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>

Reviewed-by: Fiona Behrens <me@kloenk.dev>

> ---
>  scripts/generate_rust_analyzer.py | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> index bd6e321a6aa5..ccb15aa66929 100755
> --- a/scripts/generate_rust_analyzer.py
> +++ b/scripts/generate_rust_analyzer.py
> @@ -211,7 +211,8 @@ def generate_crates(
>  
>      def is_root_crate(build_file: pathlib.Path, target: str) -> bool:
>          try:
> -            return f"{target}.o" in open(build_file).read()
> +            with open(build_file) as f:
> +                return f"{target}.o" in f.read()
>          except FileNotFoundError:
>              return False
Re: [PATCH v4 09/11] scripts: generate_rust_analyzer.py: avoid FD leak
Posted by Daniel Almeida 8 months, 4 weeks ago

> On 22 Mar 2025, at 10:23, Tamir Duberstein <tamird@gmail.com> wrote:
> 
> Use a context manager to avoid leaking file descriptors.
> 
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> ---
> scripts/generate_rust_analyzer.py | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> index bd6e321a6aa5..ccb15aa66929 100755
> --- a/scripts/generate_rust_analyzer.py
> +++ b/scripts/generate_rust_analyzer.py
> @@ -211,7 +211,8 @@ def generate_crates(
> 
>     def is_root_crate(build_file: pathlib.Path, target: str) -> bool:
>         try:
> -            return f"{target}.o" in open(build_file).read()
> +            with open(build_file) as f:
> +                return f"{target}.o" in f.read()
>         except FileNotFoundError:
>             return False
> 
> 
> -- 
> 2.48.1
> 
> 

Oh, nice!

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>