From dba4ed0d766680a084b02c35692bd2b76448f2ba Mon Sep 17 00:00:00 2001 From: "Zong-han, Xie" Date: Sun, 17 May 2026 21:06:28 +0800 Subject: [PATCH 1/4] Fixed issue-786 Modified if statement to handle PosixPath instance file name in the same way with handling string file name. --- modmesh/track/dataframe.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modmesh/track/dataframe.py b/modmesh/track/dataframe.py index e8844012c..a4e29eb85 100644 --- a/modmesh/track/dataframe.py +++ b/modmesh/track/dataframe.py @@ -67,7 +67,8 @@ def read_from_text_file( :return: None """ - if isinstance(fname, str): + import pathlib + if isinstance(fname, str) or isinstance(fname, pathlib.PosixPath): if not os.path.exists(fname): raise Exception("Text file '{}' does not exist".format(fname)) fid = open(fname, 'rt') From 9930fd0f73054828162e24cadadbce31fb4c641e Mon Sep 17 00:00:00 2001 From: "Zong-han, Xie" Date: Sat, 23 May 2026 12:28:56 +0800 Subject: [PATCH 2/4] This commits is to better address issue 786. Changes made in this commit: 1. Cross-platform type check with os.PathLike 2. Combine isinstance calls with isinstance(fname, (str, os.PathLike)) 3. Added tests for different types of path 4. Tighten error type by using FileNotFoundErrorn not Exceptions --- modmesh/track/dataframe.py | 7 +++--- tests/test_timeseries_dataframe.py | 39 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/modmesh/track/dataframe.py b/modmesh/track/dataframe.py index a4e29eb85..193b6b74c 100644 --- a/modmesh/track/dataframe.py +++ b/modmesh/track/dataframe.py @@ -67,10 +67,11 @@ def read_from_text_file( :return: None """ - import pathlib - if isinstance(fname, str) or isinstance(fname, pathlib.PosixPath): + if isinstance(fname, (str, os.PathLike)): if not os.path.exists(fname): - raise Exception("Text file '{}' does not exist".format(fname)) + raise FileNotFoundError( + "Text file '{}' does not exist".format(fname) + ) fid = open(fname, 'rt') fid_ctx = contextlib.closing(fid) else: diff --git a/tests/test_timeseries_dataframe.py b/tests/test_timeseries_dataframe.py index a1a81170b..be20f0e13 100644 --- a/tests/test_timeseries_dataframe.py +++ b/tests/test_timeseries_dataframe.py @@ -25,6 +25,9 @@ # POSSIBILITY OF SUCH DAMAGE. import io +import os +import pathlib +import tempfile import unittest import numpy as np @@ -163,3 +166,39 @@ def test_dataframe_sort(self): col_data = reordered_tsdf['DELTA_VEL[1]'] nd_arr = np.genfromtxt(io.StringIO(self.dlc_data), delimiter=',')[1:] self.assertEqual(list(col_data), list(nd_arr[:, 1])) + + def test_read_from_text_file_accepts_str_path(self): + tsdf = dataframe.DataFrame() + with tempfile.NamedTemporaryFile( + mode='w', suffix='.csv', delete=False, + ) as fh: + fh.write(self.dlc_data) + path = fh.name + try: + tsdf.read_from_text_file(path) + self.assertEqual(tsdf._columns, self.col_sol) + self.assertEqual(tsdf._index_name, 'EPOCH') + finally: + os.unlink(path) + + def test_read_from_text_file_accepts_pathlib_path(self): + tsdf = dataframe.DataFrame() + with tempfile.NamedTemporaryFile( + mode='w', suffix='.csv', delete=False, + ) as fh: + fh.write(self.dlc_data) + path = pathlib.Path(fh.name) + try: + tsdf.read_from_text_file(path) + self.assertEqual(tsdf._columns, self.col_sol) + self.assertEqual(tsdf._index_name, 'EPOCH') + finally: + path.unlink() + + def test_read_from_text_file_missing_raises_filenotfound(self): + tsdf = dataframe.DataFrame() + missing = pathlib.Path(tempfile.gettempdir()) / 'no_such_file.csv' + with self.assertRaises(FileNotFoundError): + tsdf.read_from_text_file(missing) + +# vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4 tw=79: From 01e0293c05755fc45a99e6026b63a9e1b39e9029 Mon Sep 17 00:00:00 2001 From: "Zong-han, Xie" Date: Sun, 24 May 2026 06:55:01 +0800 Subject: [PATCH 3/4] Added a comment to a test case Added comment to `test_read_from_text_file_accepts_pathlib_path` to illustrate the background and purpose of this test case. --- tests/test_timeseries_dataframe.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_timeseries_dataframe.py b/tests/test_timeseries_dataframe.py index be20f0e13..21ded1b0b 100644 --- a/tests/test_timeseries_dataframe.py +++ b/tests/test_timeseries_dataframe.py @@ -181,6 +181,12 @@ def test_read_from_text_file_accepts_str_path(self): finally: os.unlink(path) +# Theis test is for a bug reported in issue #786: +# https://github.com/solvcon/modmesh/issues/786 +# Issue #786: If user uses time series dataframe under Linux, +# it is likely to be given a `pathlib.PosixPath` instance to +# indicate the path to text file. This test is to make sure +# dataframe support path which is from `pathlib` def test_read_from_text_file_accepts_pathlib_path(self): tsdf = dataframe.DataFrame() with tempfile.NamedTemporaryFile( From 0d3c9ef79cc17580ca6ed296ebf526f6b3deab56 Mon Sep 17 00:00:00 2001 From: "Zong-han, Xie" Date: Sun, 24 May 2026 07:15:58 +0800 Subject: [PATCH 4/4] fix lint error in CI/CD --- tests/test_timeseries_dataframe.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_timeseries_dataframe.py b/tests/test_timeseries_dataframe.py index 21ded1b0b..7a0409e74 100644 --- a/tests/test_timeseries_dataframe.py +++ b/tests/test_timeseries_dataframe.py @@ -183,10 +183,10 @@ def test_read_from_text_file_accepts_str_path(self): # Theis test is for a bug reported in issue #786: # https://github.com/solvcon/modmesh/issues/786 -# Issue #786: If user uses time series dataframe under Linux, -# it is likely to be given a `pathlib.PosixPath` instance to -# indicate the path to text file. This test is to make sure -# dataframe support path which is from `pathlib` +# Issue #786: If user uses time series dataframe under Linux, +# it is likely to be given a `pathlib.PosixPath` instance to +# indicate the path to text file. This test is to make sure +# dataframe support a path which is from `pathlib` def test_read_from_text_file_accepts_pathlib_path(self): tsdf = dataframe.DataFrame() with tempfile.NamedTemporaryFile(