From 96801a2446374f0a29c3f933d5abd090fc740f81 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 3 Dec 2019 21:02:01 -0800 Subject: [PATCH 1/2] BUG: unclosed file warning when passing invalid encoding --- pandas/io/formats/format.py | 3 +-- pandas/tests/io/formats/test_format.py | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 3adf8d7bbdd11..d1d1d9947da39 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -3,7 +3,6 @@ and latex files. This module also applies to display formatting. """ -import codecs from contextlib import contextmanager from datetime import tzinfo import decimal @@ -495,7 +494,7 @@ def get_buffer( if hasattr(buf, "write"): yield buf elif isinstance(buf, str): - with codecs.open(buf, "w", encoding=encoding) as f: + with open(buf, "w", encoding=encoding) as f: yield f else: raise TypeError("buf is not a file name and it has no write method") diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 0f4a7a33dd115..004a1d184537d 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -3259,8 +3259,9 @@ def test_filepath_or_buffer_arg( ): getattr(df, method)(buf=filepath_or_buffer, encoding=encoding) elif encoding == "foo": - with pytest.raises(LookupError, match="unknown encoding"): - getattr(df, method)(buf=filepath_or_buffer, encoding=encoding) + with tm.assert_produces_warning(None): + with pytest.raises(LookupError, match="unknown encoding"): + getattr(df, method)(buf=filepath_or_buffer, encoding=encoding) else: expected = getattr(df, method)() getattr(df, method)(buf=filepath_or_buffer, encoding=encoding) From dc3d823aa8a5d0599b69ea7e719e621a75a84e3a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 4 Dec 2019 12:41:18 -0800 Subject: [PATCH 2/2] windows compat --- pandas/io/formats/format.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index d1d1d9947da39..dab1cd243a343 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -494,7 +494,11 @@ def get_buffer( if hasattr(buf, "write"): yield buf elif isinstance(buf, str): - with open(buf, "w", encoding=encoding) as f: + with open(buf, "w", encoding=encoding, newline="") as f: + # GH#30034 open instead of codecs.open prevents a file leak + # if we have an invalid encoding argument. + # newline="" is needed to roundtrip correctly on + # windows test_to_latex_filename yield f else: raise TypeError("buf is not a file name and it has no write method")