Skip to content

Commit 65e4f3e

Browse files
committed
fix the automatic_class_in_file in windows
Signed-off-by: Benjamin Donnot <[email protected]>
1 parent d8004b0 commit 65e4f3e

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ jobs:
206206
name: darwin,
207207
os: macos-latest,
208208
}
209-
# - {
210-
# name: windows,
211-
# os: windows-2022,
212-
# }
209+
- {
210+
name: windows,
211+
os: windows-2022,
212+
}
213213
- {
214214
name: ubuntu,
215215
os: ubuntu-latest,

grid2op/Environment/baseEnv.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
import warnings
2121
import numpy as np
2222
from scipy.optimize import (minimize, LinearConstraint)
23-
2423
from abc import ABC, abstractmethod
24+
25+
from grid2op._glop_platform_info import _IS_WINDOWS
2526
from grid2op.Environment._env_prev_state import _EnvPreviousState
2627
from grid2op.Observation import (BaseObservation,
2728
ObservationSpace,
@@ -4117,14 +4118,25 @@ def close(self):
41174118
self._aux_close_local_dir_cls()
41184119

41194120
def _aux_close_local_dir_cls(self):
4120-
if self._local_dir_cls is not None:
4121-
# I am the "keeper" of the temporary directory
4122-
# deleting this env should also delete the temporary directory
4123-
if not (hasattr(self._local_dir_cls, "_RUNNER_DO_NOT_ERASE") and not self._local_dir_cls._RUNNER_DO_NOT_ERASE):
4124-
# BUT if a runner uses it, then I should not delete it !
4125-
self._local_dir_cls.cleanup()
4126-
self._local_dir_cls = None
4127-
# In this case it's likely that the OS will clean it for grid2op with a warning...
4121+
if self._local_dir_cls is None:
4122+
return
4123+
# I am the "keeper" of the temporary directory
4124+
# deleting this env should also delete the temporary directory
4125+
if not (hasattr(self._local_dir_cls, "_RUNNER_DO_NOT_ERASE") and not self._local_dir_cls._RUNNER_DO_NOT_ERASE):
4126+
# BUT if a runner uses it, then I should not delete it !
4127+
self._local_dir_cls.cleanup()
4128+
self._local_dir_cls = None
4129+
# In this case it's likely that the OS will clean it for grid2op with a warning...
4130+
4131+
# clear all imported modules (required for windows)
4132+
# otherwise it tries to import the classes when processes are spawned
4133+
# even the classes that have been "cleaned"
4134+
if _IS_WINDOWS and type(self)._PATH_GRID_CLASSES is not None:
4135+
# TODO find a better way here
4136+
module_name = os.path.split(type(self)._PATH_GRID_CLASSES)[-1]
4137+
for el in sys.modules.copy():
4138+
if module_name in el:
4139+
del sys.modules[el]
41284140

41294141
def attach_layout(self, grid_layout):
41304142
"""

grid2op/tests/automatic_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import numpy as np
1616
from gymnasium.vector import AsyncVectorEnv
1717

18-
1918
import grid2op
2019
from grid2op._glop_platform_info import _IS_WINDOWS
2120
from grid2op.Runner import Runner
@@ -615,6 +614,7 @@ def test_asynch_fork(self):
615614

616615
def test_asynch_spawn(self):
617616
# test I can reset everything on the same process
617+
# python -m unittest grid2op.tests.automatic_classes.GymEnvAutoClassTester.test_asynch_spawn
618618
env1 = GymEnv(self.env)
619619
env2 = GymEnv(self.env)
620620
obs1, info1 = env1.reset()

0 commit comments

Comments
 (0)