From 57e17f2b11c334ba02b41745c87092e47750a986 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 8 Sep 2019 10:31:27 -0700 Subject: [PATCH 1/2] CLN: fix bare excepts on close --- pandas/_libs/parsers.pyx | 9 ++++----- pandas/io/json/_json.py | 6 ++---- pandas/io/parquet.py | 5 +---- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx index 62a3568932def..0bb4d0c25a032 100644 --- a/pandas/_libs/parsers.pyx +++ b/pandas/_libs/parsers.pyx @@ -566,11 +566,10 @@ cdef class TextReader: def close(self): # 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 + if self.handle is not None and hasattr(self.handle, "close"): + # UTF8Recoder does not have `close` attr # TODO: should it? + self.handle.close() + # also preemptively free all allocated memory parser_free(self.parser) if self.true_set: 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 From b7d533cbd8fad180871c3ece2e255b0e5e8ba043 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 9 Sep 2019 14:34:23 -0700 Subject: [PATCH 2/2] define UTF8Recoder.close --- pandas/_libs/parsers.pyx | 3 +-- pandas/io/common.py | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx index 0bb4d0c25a032..0aab4fdc8c537 100644 --- a/pandas/_libs/parsers.pyx +++ b/pandas/_libs/parsers.pyx @@ -566,8 +566,7 @@ cdef class TextReader: def close(self): # we need to properly close an open derived # filehandle here, e.g. and UTFRecoder - if self.handle is not None and hasattr(self.handle, "close"): - # UTF8Recoder does not have `close` attr # TODO: should it? + if self.handle is not None: self.handle.close() # also preemptively free all allocated memory 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