diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx index 62a3568932def..0aab4fdc8c537 100644 --- a/pandas/_libs/parsers.pyx +++ b/pandas/_libs/parsers.pyx @@ -567,10 +567,8 @@ cdef class TextReader: # we need to properly close an open derived # filehandle here, e.g. and UTFRecoder if self.handle is not None: - try: - self.handle.close() - except: - pass + self.handle.close() + # also preemptively free all allocated memory parser_free(self.parser) if self.true_set: diff --git a/pandas/io/common.py b/pandas/io/common.py index 0bbac8a8b7c1c..2ca2007e2925f 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -600,6 +600,9 @@ def readline(self) -> bytes: def next(self) -> bytes: return next(self.reader).encode("utf-8") + def close(self): + self.reader.close() + # Keeping these class for now because it provides a necessary convenience # for "dropping" the "encoding" argument from our I/O arguments when diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index 4af362d8343f2..a10d31e1f4f02 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -587,10 +587,8 @@ def read_json( result = json_reader.read() if should_close: - try: - filepath_or_buffer.close() - except: # noqa: flake8 - pass + filepath_or_buffer.close() + return result diff --git a/pandas/io/parquet.py b/pandas/io/parquet.py index 6fc70e9f4a737..334fb592cb0c1 100644 --- a/pandas/io/parquet.py +++ b/pandas/io/parquet.py @@ -125,10 +125,7 @@ def read(self, path, columns=None, **kwargs): path, columns=columns, **kwargs ).to_pandas() if should_close: - try: - path.close() - except: # noqa: flake8 - pass + path.close() return result