diff --git a/gym/envs/classic_control/acrobot.py b/gym/envs/classic_control/acrobot.py index 60e16abfb..8f4f7e5b8 100644 --- a/gym/envs/classic_control/acrobot.py +++ b/gym/envs/classic_control/acrobot.py @@ -375,14 +375,13 @@ class AcrobotEnv(core.Env): np.array(pygame.surfarray.pixels3d(self.screen)), axes=(1, 0, 2) ) + def close(self): + if self.screen is not None: + import pygame -def close(self): - if self.screen is not None: - import pygame - - pygame.display.quit() - pygame.quit() - self.isopen = False + pygame.display.quit() + pygame.quit() + self.isopen = False def wrap(x, m, M): diff --git a/gym/wrappers/monitoring/video_recorder.py b/gym/wrappers/monitoring/video_recorder.py index 187ff5294..266139a5d 100644 --- a/gym/wrappers/monitoring/video_recorder.py +++ b/gym/wrappers/monitoring/video_recorder.py @@ -221,6 +221,10 @@ class VideoRecorder: # TODO: remove with gym 1.0 if not self.enabled or self._closed: return + # First close the environment + self.env.close() + + # Close the encoder if self.encoder: logger.debug("Closing video encoder: path=%s", self.path) self.encoder.close() diff --git a/tests/wrappers/test_human_rendering.py b/tests/wrappers/test_human_rendering.py index ae34acf6a..0273292b2 100644 --- a/tests/wrappers/test_human_rendering.py +++ b/tests/wrappers/test_human_rendering.py @@ -21,10 +21,12 @@ def test_human_rendering(): env.close() + env = gym.make("CartPole-v1", render_mode="human") with pytest.raises( AssertionError, match=re.escape( "Expected env.render_mode to be one of 'rgb_array' or 'single_rgb_array' but got 'human'" ), ): - HumanRendering(gym.make("CartPole-v1", render_mode="human")) + HumanRendering(env) + env.close() diff --git a/tests/wrappers/test_video_recorder.py b/tests/wrappers/test_video_recorder.py index e057aaecf..d3a651626 100644 --- a/tests/wrappers/test_video_recorder.py +++ b/tests/wrappers/test_video_recorder.py @@ -8,7 +8,7 @@ import gym from gym.wrappers.monitoring.video_recorder import VideoRecorder -class BrokenRecordableEnv: +class BrokenRecordableEnv(gym.Env): metadata = {"render_modes": ["rgb_array"]} def __init__(self, render_mode="rgb_array"): @@ -18,7 +18,7 @@ class BrokenRecordableEnv: pass -class UnrecordableEnv: +class UnrecordableEnv(gym.Env): metadata = {"render_modes": [None]} def __init__(self, render_mode=None):