Skip to content

Commit

Permalink
Updated registration.py (#2375)
Browse files Browse the repository at this point in the history
* Updated registration.py

* Blacken
  • Loading branch information
Rohan138 authored Sep 1, 2021
1 parent 6e42458 commit a7b6462
Showing 1 changed file with 3 additions and 44 deletions.
47 changes: 3 additions & 44 deletions gym/envs/registration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import re
import copy
import importlib
import warnings

from gym import error, logger

Expand Down Expand Up @@ -104,16 +103,6 @@ def make(self, path, **kwargs):
logger.info("Making new env: %s", path)
spec = self.spec(path)
env = spec.make(**kwargs)
# We used to have people override _reset/_step rather than
# reset/step. Set _gym_disable_underscore_compat = True on
# your environment if you use these methods and don't want
# compatibility code to be invoked.
if (
hasattr(env, "_reset")
and hasattr(env, "_step")
and not getattr(env, "_gym_disable_underscore_compat", False)
):
patch_deprecated_methods(env)
if env.spec.max_episode_steps is not None:
from gym.wrappers.time_limit import TimeLimit

Expand All @@ -125,11 +114,10 @@ def all(self):

def spec(self, path):
if ":" in path:
mod_name, _sep, id = path.partition(":")
mod_name, _, id = path.partition(":")
try:
importlib.import_module(mod_name)
# catch ImportError for python2.7 compatibility
except ImportError:
except ModuleNotFoundError:
raise error.Error(
"A module ({}) was specified for the environment but was not found, make sure the package is installed with `pip install` before calling `gym.make()`".format(
mod_name
Expand Down Expand Up @@ -182,7 +170,7 @@ def spec(self, path):

def register(self, id, **kwargs):
if id in self.env_specs:
raise error.Error("Cannot re-register id: {}".format(id))
logger.warn("Overriding environment {}".format(id))
self.env_specs[id] = EnvSpec(id, **kwargs)


Expand All @@ -200,32 +188,3 @@ def make(id, **kwargs):

def spec(id):
return registry.spec(id)


warn_once = True


def patch_deprecated_methods(env):
"""
Methods renamed from '_method' to 'method', render() no longer has 'close' parameter, close is a separate method.
For backward compatibility, this makes it possible to work with unmodified environments.
"""
global warn_once
if warn_once:
logger.warn(
"Environment '%s' has deprecated methods '_step' and '_reset' rather than 'step' and 'reset'. Compatibility code invoked. Set _gym_disable_underscore_compat = True to disable this behavior."
% str(type(env))
)
warn_once = False
env.reset = env._reset
env.step = env._step
env.seed = env._seed

def render(mode):
return env._render(mode, close=False)

def close():
env._render("human", close=True)

env.render = render
env.close = close

0 comments on commit a7b6462

Please sign in to comment.