|
7 | 7 | from inspect import signature |
8 | 8 | from io import StringIO |
9 | 9 | import os |
10 | | -from pathlib import Path |
11 | 10 | import sys |
12 | 11 |
|
13 | 12 | import numpy as np |
@@ -615,16 +614,16 @@ def test_sub_character(all_parsers, csv_dir_path): |
615 | 614 |
|
616 | 615 |
|
617 | 616 | @pytest.mark.parametrize("filename", ["sé-es-vé.csv", "ru-sй.csv", "中文文件名.csv"]) |
618 | | -def test_filename_with_special_chars(all_parsers, filename): |
| 617 | +def test_filename_with_special_chars(all_parsers, filename, tmp_path): |
619 | 618 | # see gh-15086. |
620 | 619 | parser = all_parsers |
621 | 620 | df = DataFrame({"a": [1, 2, 3]}) |
622 | 621 |
|
623 | | - with tm.ensure_clean(filename) as path: |
624 | | - df.to_csv(path, index=False) |
| 622 | + path = tmp_path / filename |
| 623 | + df.to_csv(path, index=False) |
625 | 624 |
|
626 | | - result = parser.read_csv(path) |
627 | | - tm.assert_frame_equal(result, df) |
| 625 | + result = parser.read_csv(path) |
| 626 | + tm.assert_frame_equal(result, df) |
628 | 627 |
|
629 | 628 |
|
630 | 629 | def test_read_table_same_signature_as_read_csv(all_parsers): |
@@ -786,22 +785,22 @@ def test_dict_keys_as_names(all_parsers): |
786 | 785 |
|
787 | 786 | @pytest.mark.xfail(using_string_dtype() and HAS_PYARROW, reason="TODO(infer_string)") |
788 | 787 | @xfail_pyarrow # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 0 |
789 | | -def test_encoding_surrogatepass(all_parsers): |
| 788 | +def test_encoding_surrogatepass(all_parsers, tmp_path): |
790 | 789 | # GH39017 |
791 | 790 | parser = all_parsers |
792 | 791 | content = b"\xed\xbd\xbf" |
793 | 792 | decoded = content.decode("utf-8", errors="surrogatepass") |
794 | 793 | expected = DataFrame({decoded: [decoded]}, index=[decoded * 2]) |
795 | 794 | expected.index.name = decoded * 2 |
796 | 795 |
|
797 | | - with tm.ensure_clean() as path: |
798 | | - Path(path).write_bytes( |
799 | | - content * 2 + b"," + content + b"\n" + content * 2 + b"," + content |
800 | | - ) |
801 | | - df = parser.read_csv(path, encoding_errors="surrogatepass", index_col=0) |
802 | | - tm.assert_frame_equal(df, expected) |
803 | | - with pytest.raises(UnicodeDecodeError, match="'utf-8' codec can't decode byte"): |
804 | | - parser.read_csv(path) |
| 796 | + path = tmp_path / "test_encoding.csv" |
| 797 | + path.write_bytes( |
| 798 | + content * 2 + b"," + content + b"\n" + content * 2 + b"," + content |
| 799 | + ) |
| 800 | + df = parser.read_csv(path, encoding_errors="surrogatepass", index_col=0) |
| 801 | + tm.assert_frame_equal(df, expected) |
| 802 | + with pytest.raises(UnicodeDecodeError, match="'utf-8' codec can't decode byte"): |
| 803 | + parser.read_csv(path) |
805 | 804 |
|
806 | 805 |
|
807 | 806 | def test_malformed_second_line(all_parsers): |
@@ -835,15 +834,15 @@ def test_short_multi_line(all_parsers): |
835 | 834 | tm.assert_frame_equal(result, expected) |
836 | 835 |
|
837 | 836 |
|
838 | | -def test_read_seek(all_parsers): |
| 837 | +def test_read_seek(all_parsers, tmp_path): |
839 | 838 | # GH48646 |
840 | 839 | parser = all_parsers |
841 | 840 | prefix = "### DATA\n" |
842 | 841 | content = "nkey,value\ntables,rectangular\n" |
843 | | - with tm.ensure_clean() as path: |
844 | | - Path(path).write_text(prefix + content, encoding="utf-8") |
845 | | - with open(path, encoding="utf-8") as file: |
846 | | - file.readline() |
847 | | - actual = parser.read_csv(file) |
848 | | - expected = parser.read_csv(StringIO(content)) |
| 842 | + path = tmp_path / "test_seek.csv" |
| 843 | + path.write_text(prefix + content, encoding="utf-8") |
| 844 | + with open(path, encoding="utf-8") as file: |
| 845 | + file.readline() |
| 846 | + actual = parser.read_csv(file) |
| 847 | + expected = parser.read_csv(StringIO(content)) |
849 | 848 | tm.assert_frame_equal(actual, expected) |
0 commit comments