Repository: modin-project/modin

Issue:
Fix code scanning alert - File is not always closed
<!-- Warning: The suggested title contains the alert rule name. This can expose security information. -->

Tracking issue for:
- [ ] https://github.com/modin-project/modin/security/code-scanning/329



Discussion:

Resolving patch:
diff --git a/modin/_version.py b/modin/_version.py
--- a/modin/_version.py
+++ b/modin/_version.py
@@ -161,21 +161,20 @@ def git_get_keywords(versionfile_abs: str) -> Dict:
     # _version.py.
     keywords = {}
     try:
-        f = open(versionfile_abs, "r")
-        for line in f.readlines():
-            if line.strip().startswith("git_refnames ="):
-                mo = re.search(r'=\s*"(.*)"', line)
-                if mo:
-                    keywords["refnames"] = mo.group(1)
-            if line.strip().startswith("git_full ="):
-                mo = re.search(r'=\s*"(.*)"', line)
-                if mo:
-                    keywords["full"] = mo.group(1)
-            if line.strip().startswith("git_date ="):
-                mo = re.search(r'=\s*"(.*)"', line)
-                if mo:
-                    keywords["date"] = mo.group(1)
-        f.close()
+        with open(versionfile_abs, "r") as _f:
+            for line in _f.readlines():
+                if line.strip().startswith("git_refnames ="):
+                    mo = re.search(r'=\s*"(.*)"', line)
+                    if mo:
+                        keywords["refnames"] = mo.group(1)
+                if line.strip().startswith("git_full ="):
+                    mo = re.search(r'=\s*"(.*)"', line)
+                    if mo:
+                        keywords["full"] = mo.group(1)
+                if line.strip().startswith("git_date ="):
+                    mo = re.search(r'=\s*"(.*)"', line)
+                    if mo:
+                        keywords["date"] = mo.group(1)
     except EnvironmentError:
         pass
     return keywords
diff --git a/modin/experimental/core/storage_formats/pyarrow/parsers.py b/modin/experimental/core/storage_formats/pyarrow/parsers.py
--- a/modin/experimental/core/storage_formats/pyarrow/parsers.py
+++ b/modin/experimental/core/storage_formats/pyarrow/parsers.py
@@ -53,12 +53,12 @@ def parse(self, fname, num_splits, start, end, header, **kwargs):
         import pyarrow as pa
         import pyarrow.csv as csv
 
-        bio = open(fname, "rb")
-        # The header line for the CSV file
-        first_line = bio.readline()
-        bio.seek(start)
-        to_read = header + first_line + bio.read(end - start)
-        bio.close()
+        with open(fname, "rb") as bio:
+            # The header line for the CSV file
+            first_line = bio.readline()
+            bio.seek(start)
+            to_read = header + first_line + bio.read(end - start)
+
         table = csv.read_csv(
             BytesIO(to_read), parse_options=csv.ParseOptions(header_rows=1)
         )


Test patch:
diff --git a/modin/pandas/test/test_io.py b/modin/pandas/test/test_io.py
--- a/modin/pandas/test/test_io.py
+++ b/modin/pandas/test/test_io.py
@@ -1034,9 +1034,8 @@ def test_read_csv_default_to_pandas(self):
             # This tests that we default to pandas on a buffer
             from io import StringIO
 
-            pd.read_csv(
-                StringIO(open(pytest.csvs_names["test_read_csv_regular"], "r").read())
-            )
+            with open(pytest.csvs_names["test_read_csv_regular"], "r") as _f:
+                pd.read_csv(StringIO(_f.read()))
 
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",