|
8 | 8 | # |
9 | 9 | # Licensed under GNU Lesser General Public License v3.0 |
10 | 10 | # |
11 | | -from pathlib import Path |
| 11 | +import os |
| 12 | +from pathlib import Path, PurePosixPath, PureWindowsPath |
12 | 13 | from unittest.mock import patch |
13 | 14 |
|
14 | 15 | import pytest |
@@ -349,3 +350,25 @@ def test_project_path_updated_when_moved(self, tmp_path): |
349 | 350 |
|
350 | 351 | expected = project_dir.absolute() |
351 | 352 | assert cfg["project_path"] == expected |
| 353 | + |
| 354 | + |
| 355 | +def _write_path_pickle(path: Path, module: str, cls_name: str) -> Path: |
| 356 | + # Protocol 0 pickle of ``{"project_path": <module>.<cls_name>("a", "b")}``; written by hand |
| 357 | + # because a concrete path of the other OS cannot be created to pickle it |
| 358 | + path.write_bytes(f"(dVproject_path\nc{module}\n{cls_name}\n(Va\nVb\ntRs.".encode()) |
| 359 | + return path |
| 360 | + |
| 361 | + |
| 362 | +@pytest.mark.parametrize("module", ["pathlib", "pathlib._local"]) |
| 363 | +def test_read_pickle_loads_foreign_paths_as_pure(tmp_path, module): |
| 364 | + """Paths pickled on another OS load as pure paths instead of raising (#3562).""" |
| 365 | + foreign_name, pure_type = ("PosixPath", PurePosixPath) if os.name == "nt" else ("WindowsPath", PureWindowsPath) |
| 366 | + data = auxiliaryfunctions.read_pickle(_write_path_pickle(tmp_path / "meta.pickle", module, foreign_name)) |
| 367 | + assert type(data["project_path"]) is pure_type |
| 368 | + assert data["project_path"].parts == ("a", "b") |
| 369 | + |
| 370 | + |
| 371 | +def test_read_pickle_keeps_native_paths(tmp_path): |
| 372 | + native_type = type(Path()) |
| 373 | + data = auxiliaryfunctions.read_pickle(_write_path_pickle(tmp_path / "meta.pickle", "pathlib", native_type.__name__)) |
| 374 | + assert type(data["project_path"]) is native_type |
0 commit comments