diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 3adf8d7bbdd11..dab1cd243a343 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,11 @@ 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, 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") 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)