Skip to content

Commit

Permalink
[Feature] Support shallow copy for Config (#1796)
Browse files Browse the repository at this point in the history
* Add .owners.yml to mark daily issue shift

* Update .owners.yml

fix end of file

* Add __copy__() to Config

* fix format in config.py

* fix format in test_copy

* fix format in config.py

* Update config.py

* Update tests/test_utils/test_config.py

Co-authored-by: Zaida Zhou <[email protected]>

Co-authored-by: Zaida Zhou <[email protected]>
  • Loading branch information
imabackstabber and zhouzaida authored Mar 15, 2022
1 parent b5d550f commit 1cd864d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mmcv/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,13 @@ def __iter__(self):
def __getstate__(self):
return (self._cfg_dict, self._filename, self._text)

def __copy__(self):
cls = self.__class__
other = cls.__new__(cls)
other.__dict__.update(self.__dict__)

return other

def __deepcopy__(self, memo):
cls = self.__class__
other = cls.__new__(cls)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,15 @@ def test_deepcopy():
assert new_cfg._cfg_dict is not cfg._cfg_dict
assert new_cfg._filename == cfg._filename
assert new_cfg._text == cfg._text


def test_copy():
cfg_file = osp.join(data_path, 'config/n.py')
cfg = Config.fromfile(cfg_file)
new_cfg = copy.copy(cfg)

assert isinstance(new_cfg, Config)
assert new_cfg is not cfg
assert new_cfg._cfg_dict is cfg._cfg_dict
assert new_cfg._filename == cfg._filename
assert new_cfg._text == cfg._text

0 comments on commit 1cd864d

Please sign in to comment.