Skip to content

Commit 43d8c68

Browse files
committed
Fix BSP.split_recursive crash when passing Random
Random was passed directly to CFFI instead of fetching its C pointer Fixes #168
1 parent 87ecd62 commit 43d8c68

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

tcod/bsp.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,17 @@ def split_recursive( # noqa: PLR0913
148148
"""Divide this partition recursively.
149149
150150
Args:
151-
depth (int): The maximum depth to divide this object recursively.
152-
min_width (int): The minimum width of any individual partition.
153-
min_height (int): The minimum height of any individual partition.
154-
max_horizontal_ratio (float):
155-
Prevent creating a horizontal ratio more extreme than this.
156-
max_vertical_ratio (float):
157-
Prevent creating a vertical ratio more extreme than this.
158-
seed (Optional[tcod.random.Random]):
159-
The random number generator to use.
151+
depth: The maximum depth to divide this object recursively.
152+
min_width: The minimum width of any individual partition.
153+
min_height: The minimum height of any individual partition.
154+
max_horizontal_ratio: Prevent creating a horizontal ratio more extreme than this.
155+
max_vertical_ratio: Prevent creating a vertical ratio more extreme than this.
156+
seed: The random number generator to use.
160157
"""
161158
cdata = self._as_cdata()
162159
lib.TCOD_bsp_split_recursive(
163160
cdata,
164-
seed or ffi.NULL,
161+
seed.random_c if seed is not None else ffi.NULL,
165162
depth,
166163
min_width,
167164
min_height,

tests/test_tcod.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
from numpy.typing import DTypeLike, NDArray
1010

1111
import tcod
12+
import tcod.bsp
1213
import tcod.console
14+
import tcod.context
15+
import tcod.map
16+
import tcod.path
17+
import tcod.random
1318
from tcod import libtcodpy
1419

1520

@@ -20,7 +25,7 @@ def raise_Exception(*_args: object) -> NoReturn:
2025
def test_line_error() -> None:
2126
"""Test exception propagation."""
2227
with pytest.raises(RuntimeError), pytest.warns():
23-
tcod.line(0, 0, 10, 10, py_callback=raise_Exception)
28+
libtcodpy.line(0, 0, 10, 10, py_callback=raise_Exception)
2429

2530

2631
@pytest.mark.filterwarnings("ignore:Iterate over nodes using")
@@ -44,7 +49,7 @@ def test_tcod_bsp() -> None:
4449

4550
# test that operations on deep BSP nodes preserve depth
4651
sub_bsp = bsp.children[0]
47-
sub_bsp.split_recursive(3, 2, 2, 1, 1)
52+
sub_bsp.split_recursive(3, 2, 2, 1, 1, seed=tcod.random.Random(seed=42))
4853
assert sub_bsp.children[0].level == 2 # noqa: PLR2004
4954

5055
# cover find_node method

0 commit comments

Comments
 (0)