Skip to content

Commit 8ae5286

Browse files
committed
Improve PathLike handling.
1 parent e12c417 commit 8ae5286

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Changes relevant to the users of python-tcod are documented here.
44
This project adheres to [Semantic Versioning](https://semver.org/) since version `2.0.0`.
55

66
## [Unreleased]
7+
### Fixed
8+
- Fixed handling of non-Path PathLike parameters and filepath encodings.
79

810
## [13.3.0] - 2022-01-07
911
### Added

tcod/console.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"""
66
from __future__ import annotations
77

8-
import os
98
import warnings
109
from os import PathLike
10+
from pathlib import Path
1111
from typing import Any, Iterable, Optional, Sequence, Tuple, Union
1212

1313
import numpy as np
@@ -1300,11 +1300,12 @@ def load_xp(path: Union[str, PathLike[str]], order: Literal["C", "F"] = "C") ->
13001300
is_transparent = (console.rgb["bg"] == KEY_COLOR).all(axis=-1)
13011301
console.rgba[is_transparent] = (ord(" "), (0,), (0,))
13021302
"""
1303-
if not os.path.exists(path):
1304-
raise FileNotFoundError(f"File not found:\n\t{os.path.abspath(path)}")
1305-
layers = _check(tcod.lib.TCOD_load_xp(str(path).encode("utf-8"), 0, ffi.NULL))
1303+
path = Path(path)
1304+
if not path.exists():
1305+
raise FileNotFoundError(f"File not found:\n\t{path.resolve()}")
1306+
layers = _check(tcod.lib.TCOD_load_xp(bytes(path), 0, ffi.NULL))
13061307
consoles = ffi.new("TCOD_Console*[]", layers)
1307-
_check(tcod.lib.TCOD_load_xp(str(path).encode("utf-8"), layers, consoles))
1308+
_check(tcod.lib.TCOD_load_xp(bytes(path), layers, consoles))
13081309
return tuple(Console._from_cdata(console_p, order=order) for console_p in consoles)
13091310

13101311

@@ -1358,12 +1359,13 @@ def save_xp(
13581359
13591360
tcod.console.save_xp("example.xp", [console])
13601361
"""
1362+
path = Path(path)
13611363
consoles_c = ffi.new("TCOD_Console*[]", [c.console_c for c in consoles])
13621364
_check(
13631365
tcod.lib.TCOD_save_xp(
13641366
len(consoles_c),
13651367
consoles_c,
1366-
str(path).encode("utf-8"),
1368+
bytes(path),
13671369
compress_level,
13681370
)
13691371
)

tcod/image.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from __future__ import annotations
99

1010
from os import PathLike
11+
from pathlib import Path
1112
from typing import Any, Dict, Tuple, Union
1213

1314
import numpy as np
@@ -345,7 +346,7 @@ def load(filename: Union[str, PathLike[str]]) -> NDArray[np.uint8]:
345346
346347
.. versionadded:: 11.4
347348
"""
348-
image = Image._from_cdata(ffi.gc(lib.TCOD_image_load(str(filename).encode()), lib.TCOD_image_delete))
349+
image = Image._from_cdata(ffi.gc(lib.TCOD_image_load(bytes(Path(filename))), lib.TCOD_image_delete))
349350
array: NDArray[np.uint8] = np.asarray(image, dtype=np.uint8)
350351
height, width, depth = array.shape
351352
if depth == 3:

tcod/tileset.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from __future__ import annotations
1414

1515
import itertools
16-
import os
1716
from os import PathLike
17+
from pathlib import Path
1818
from typing import Any, Iterable, Optional, Tuple, Union
1919

2020
import numpy as np
@@ -258,9 +258,10 @@ def load_truetype_font(path: Union[str, PathLike[str]], tile_width: int, tile_he
258258
259259
This function is provisional. The API may change.
260260
"""
261-
if not os.path.exists(path):
262-
raise RuntimeError("File not found:\n\t%s" % (os.path.realpath(path),))
263-
cdata = lib.TCOD_load_truetype_font_(str(path).encode(), tile_width, tile_height)
261+
path = Path(path)
262+
if not path.exists():
263+
raise RuntimeError(f"File not found:\n\t{path.resolve()}")
264+
cdata = lib.TCOD_load_truetype_font_(bytes(path), tile_width, tile_height)
264265
if not cdata:
265266
raise RuntimeError(ffi.string(lib.TCOD_get_error()))
266267
return Tileset._claim(cdata)
@@ -287,9 +288,10 @@ def set_truetype_font(path: Union[str, PathLike[str]], tile_width: int, tile_hei
287288
This function does not support contexts.
288289
Use :any:`load_truetype_font` instead.
289290
"""
290-
if not os.path.exists(path):
291-
raise RuntimeError("File not found:\n\t%s" % (os.path.realpath(path),))
292-
if lib.TCOD_tileset_load_truetype_(str(path).encode(), tile_width, tile_height):
291+
path = Path(path)
292+
if not path.exists():
293+
raise RuntimeError(f"File not found:\n\t{path.resolve()}")
294+
if lib.TCOD_tileset_load_truetype_(bytes(path), tile_width, tile_height):
293295
raise RuntimeError(ffi.string(lib.TCOD_get_error()))
294296

295297

@@ -306,9 +308,10 @@ def load_bdf(path: Union[str, PathLike[str]]) -> Tileset:
306308
307309
.. versionadded:: 11.10
308310
""" # noqa: E501
309-
if not os.path.exists(path):
310-
raise RuntimeError("File not found:\n\t%s" % (os.path.realpath(path),))
311-
cdata = lib.TCOD_load_bdf(str(path).encode())
311+
path = Path(path)
312+
if not path.exists():
313+
raise RuntimeError(f"File not found:\n\t{path.resolve()}")
314+
cdata = lib.TCOD_load_bdf(bytes(path))
312315
if not cdata:
313316
raise RuntimeError(ffi.string(lib.TCOD_get_error()).decode())
314317
return Tileset._claim(cdata)
@@ -335,12 +338,13 @@ def load_tilesheet(
335338
336339
.. versionadded:: 11.12
337340
"""
338-
if not os.path.exists(path):
339-
raise RuntimeError("File not found:\n\t%s" % (os.path.realpath(path),))
341+
path = Path(path)
342+
if not path.exists():
343+
raise RuntimeError(f"File not found:\n\t{path.resolve()}")
340344
mapping = []
341345
if charmap is not None:
342346
mapping = list(itertools.islice(charmap, columns * rows))
343-
cdata = lib.TCOD_tileset_load(str(path).encode(), columns, rows, len(mapping), mapping)
347+
cdata = lib.TCOD_tileset_load(bytes(path), columns, rows, len(mapping), mapping)
344348
if not cdata:
345349
_raise_tcod_error()
346350
return Tileset._claim(cdata)

0 commit comments

Comments
 (0)