Skip to content

Commit 4e343cb

Browse files
committed
Fix REXPaint error handling and deprecate an old function.
1 parent 3387f45 commit 4e343cb

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ v2.0.0
88

99
Unreleased
1010
------------------
11+
Deprecated
12+
- `tcod.console_load_xp` has been deprecated, `tcod.console_from_xp` can load
13+
these files without modifying an existing console.
14+
15+
Fixed
16+
- `tcod.console_from_xp` now has better error handling (instead of crashing.)
1117

1218
11.18.2 - 2020-12-03
1319
--------------------

tcod/_internal.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,19 @@ def _raise_tcod_error() -> NoReturn:
6363

6464

6565
def _check(error: int) -> int:
66-
"""Detect and convert a libtcod error code it into an exception."""
66+
"""Detect and convert a libtcod error code into an exception."""
6767
if error < 0:
6868
_raise_tcod_error()
6969
return error
7070

7171

72+
def _check_p(pointer: Any) -> Any:
73+
"""Treats NULL pointers as errors and raises a libtcod exception."""
74+
if not pointer:
75+
_raise_tcod_error()
76+
return pointer
77+
78+
7279
def _check_warn(error: int, stacklevel: int = 2) -> int:
7380
"""Like _check, but raises a warning on positive error codes."""
7481
if _check(error) > 0:

tcod/libtcodpy.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
_bytes,
3434
_CDataWrapper,
3535
_check,
36+
_check_p,
3637
_check_warn,
3738
_console,
3839
_fmt,
@@ -1853,8 +1854,12 @@ def console_from_file(filename: str) -> tcod.console.Console:
18531854
18541855
Returns: A new :any`Console` instance.
18551856
"""
1857+
if not os.path.exists(filename):
1858+
raise RuntimeError(
1859+
"File not found:\n\t%s" % (os.path.realpath(filename),)
1860+
)
18561861
return tcod.console.Console._from_cdata(
1857-
lib.TCOD_console_from_file(filename.encode("utf-8"))
1862+
_check_p(lib.TCOD_console_from_file(filename.encode("utf-8")))
18581863
)
18591864

18601865

@@ -2068,8 +2073,14 @@ def console_save_apf(con: tcod.console.Console, filename: str) -> bool:
20682073
)
20692074

20702075

2076+
@deprecate("Use tcod.console_from_xp to load this file.")
20712077
def console_load_xp(con: tcod.console.Console, filename: str) -> bool:
2072-
"""Update a console from a REXPaint `.xp` file."""
2078+
"""Update a console from a REXPaint `.xp` file.
2079+
2080+
.. deprecated:: 11.18
2081+
Functions modifying console objects in-place are deprecated.
2082+
Use :any:`tcod.console_from_xp` to load a Console from a file.
2083+
"""
20732084
return bool(
20742085
lib.TCOD_console_load_xp(_console(con), filename.encode("utf-8"))
20752086
)
@@ -2088,15 +2099,23 @@ def console_save_xp(
20882099

20892100
def console_from_xp(filename: str) -> tcod.console.Console:
20902101
"""Return a single console from a REXPaint `.xp` file."""
2102+
if not os.path.exists(filename):
2103+
raise RuntimeError(
2104+
"File not found:\n\t%s" % (os.path.realpath(filename),)
2105+
)
20912106
return tcod.console.Console._from_cdata(
2092-
lib.TCOD_console_from_xp(filename.encode("utf-8"))
2107+
_check_p(lib.TCOD_console_from_xp(filename.encode("utf-8")))
20932108
)
20942109

20952110

20962111
def console_list_load_xp(
20972112
filename: str,
20982113
) -> Optional[List[tcod.console.Console]]:
20992114
"""Return a list of consoles from a REXPaint `.xp` file."""
2115+
if not os.path.exists(filename):
2116+
raise RuntimeError(
2117+
"File not found:\n\t%s" % (os.path.realpath(filename),)
2118+
)
21002119
tcod_list = lib.TCOD_console_list_from_xp(filename.encode("utf-8"))
21012120
if tcod_list == ffi.NULL:
21022121
return None

0 commit comments

Comments
 (0)