From 9ae54a77076cfc9e5b33c25ab5c9a27e0c3cf4cc Mon Sep 17 00:00:00 2001 From: "Jonathan B. Coe" Date: Mon, 9 Mar 2026 00:56:03 +0000 Subject: [PATCH 1/2] Add fixture to disable ttyrec recording by default in tests --- nle/tests/test_nethack.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/nle/tests/test_nethack.py b/nle/tests/test_nethack.py index aa80b0618..93982c08f 100644 --- a/nle/tests/test_nethack.py +++ b/nle/tests/test_nethack.py @@ -32,6 +32,20 @@ ] +@pytest.fixture(autouse=True) +def disable_ttyrec_by_default(monkeypatch): + """Monkeypatches Nethack to disable ttyrec recording unless specified.""" + original_init = nethack.Nethack.__init__ + + def new_init(self, *args, **kwargs): + # If ttyrec is not explicitly passed, disable it. + if "ttyrec" not in kwargs: + kwargs["ttyrec"] = None + original_init(self, *args, **kwargs) + + monkeypatch.setattr(nethack.Nethack, "__init__", new_init) + + class TestNetHack: @pytest.fixture def game(self): # Make sure we close even on test failure. @@ -141,9 +155,11 @@ def test_set_seed_after_reset(self, game): class TestNetHackFurther: - def test_run(self): + def test_run(self, tmpdir): + ttyrec_path = os.path.join(tmpdir, "nle.ttyrec%i.bz2" % nethack.TTYREC_VERSION) game = nethack.Nethack( - observation_keys=("glyphs", "chars", "colors", "blstats", "program_state") + ttyrec=ttyrec_path, + observation_keys=("glyphs", "chars", "colors", "blstats", "program_state"), ) _, _, _, _, program_state = game.reset() actions = [ @@ -186,9 +202,7 @@ def test_run(self): assert class_sym.explain == "human or elf" game.close() - assert os.path.isfile( - os.path.join(os.getcwd(), "nle.ttyrec%i.bz2" % nethack.TTYREC_VERSION) - ) + assert os.path.isfile(ttyrec_path) def test_illegal_filename(self): with pytest.raises(IOError): From eb4f08d157b2aecc00479eafa9a558d145830f62 Mon Sep 17 00:00:00 2001 From: "Jonathan B. Coe" Date: Fri, 20 Mar 2026 00:08:52 +0000 Subject: [PATCH 2/2] More intrusive but simpler fix --- nle/tests/test_nethack.py | 44 +++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/nle/tests/test_nethack.py b/nle/tests/test_nethack.py index 93982c08f..0365a6985 100644 --- a/nle/tests/test_nethack.py +++ b/nle/tests/test_nethack.py @@ -32,35 +32,21 @@ ] -@pytest.fixture(autouse=True) -def disable_ttyrec_by_default(monkeypatch): - """Monkeypatches Nethack to disable ttyrec recording unless specified.""" - original_init = nethack.Nethack.__init__ - - def new_init(self, *args, **kwargs): - # If ttyrec is not explicitly passed, disable it. - if "ttyrec" not in kwargs: - kwargs["ttyrec"] = None - original_init(self, *args, **kwargs) - - monkeypatch.setattr(nethack.Nethack, "__init__", new_init) - - class TestNetHack: @pytest.fixture def game(self): # Make sure we close even on test failure. - g = nethack.Nethack(observation_keys=("chars", "blstats")) + g = nethack.Nethack(ttyrec=None, observation_keys=("chars", "blstats")) try: yield g finally: g.close() def test_close_and_restart(self): - game = nethack.Nethack() + game = nethack.Nethack(ttyrec=None) game.reset() game.close() - game = nethack.Nethack() + game = nethack.Nethack(ttyrec=None) game.reset() game.close() @@ -117,7 +103,7 @@ def test_run_n_episodes(self, tmpdir, game, episodes=3): def test_several_nethacks(self, game): game.reset() - game1 = nethack.Nethack() + game1 = nethack.Nethack(ttyrec=None) game1.reset() try: @@ -133,7 +119,7 @@ def test_several_nethacks(self, game): game1.close() def test_set_initial_seeds(self): - game = nethack.Nethack(copy=True) + game = nethack.Nethack(ttyrec=None, copy=True) game.set_initial_seeds(core=42, disp=666) obs0 = game.reset() try: @@ -207,28 +193,29 @@ def test_run(self, tmpdir): def test_illegal_filename(self): with pytest.raises(IOError): nethack.Nethack(ttyrec="") - game = nethack.Nethack() + game = nethack.Nethack(ttyrec=None) with pytest.raises(IOError): game.reset("") def test_set_buffers_after_reset(self): - game = nethack.Nethack() + game = nethack.Nethack(ttyrec=None) game.reset() with pytest.raises(RuntimeError, match=r"set_buffers called after reset()"): game._pynethack.set_buffers() def test_nethack_random_character(self): - game = nethack.Nethack(playername="Hugo-@") + game = nethack.Nethack(ttyrec=None, playername="Hugo-@") assert "race:random" in game.options assert "gender:random" in game.options assert "align:random" in game.options - game = nethack.Nethack(playername="Jurgen-wiz-gno-cha-mal") + game = nethack.Nethack(ttyrec=None, playername="Jurgen-wiz-gno-cha-mal") assert "race:random" not in game.options assert "gender:random" not in game.options assert "align:random" not in game.options game = nethack.Nethack( + ttyrec=None, playername="Albert-@", options=list(nethack.NETHACKOPTIONS) + ["align:lawful"], ) @@ -238,6 +225,7 @@ def test_nethack_random_character(self): assert "align:lawful" in game.options game = nethack.Nethack( + ttyrec=None, playername="Rachel", options=list(nethack.NETHACKOPTIONS) + ["gender:female"], ) @@ -250,7 +238,9 @@ def test_nethack_random_character(self): class TestNethackSomeObs: @pytest.fixture def game(self): # Make sure we close even on test failure. - g = nethack.Nethack(observation_keys=("program_state", "message", "internal")) + g = nethack.Nethack( + ttyrec=None, observation_keys=("program_state", "message", "internal") + ) try: yield g finally: @@ -591,6 +581,7 @@ class TestNethackGlanceObservation: @pytest.fixture def game(self): # Make sure we close even on test failure. g = nethack.Nethack( + ttyrec=None, playername="MonkBot-mon-hum-neu-mal", observation_keys=("screen_descriptions", "glyphs", "chars"), ) @@ -642,6 +633,7 @@ class TestNethackTerminalObservation: @pytest.fixture def game(self): # Make sure we close even on test failure. g = nethack.Nethack( + ttyrec=None, playername="MonkBot-mon-hum-neu-mal", observation_keys=( "tty_chars", @@ -695,7 +687,9 @@ class TestNethackMiscObservation: @pytest.fixture def game(self): # Make sure we close even on test failure. g = nethack.Nethack( - playername="MonkBot-mon-hum-neu-mal", observation_keys=("misc", "internal") + ttyrec=None, + playername="MonkBot-mon-hum-neu-mal", + observation_keys=("misc", "internal"), ) try: yield g