From 64f1745e3e9f10ecf3e40e9f2125f258999ef89f Mon Sep 17 00:00:00 2001 From: zhouzaida Date: Wed, 1 Feb 2023 10:32:35 +0800 Subject: [PATCH] [Fix] backend_args should not be modified by get_file_backend --- mmengine/fileio/io.py | 6 ++++-- tests/test_fileio/test_io.py | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/mmengine/fileio/io.py b/mmengine/fileio/io.py index 62f9a4ef7a..17104ffa69 100644 --- a/mmengine/fileio/io.py +++ b/mmengine/fileio/io.py @@ -91,8 +91,10 @@ def _get_file_backend(prefix: str, backend_args: dict): """ # backend name has a higher priority if 'backend' in backend_args: - backend_name = backend_args.pop('backend') - backend = backends[backend_name](**backend_args) + # backend_args should not be modified + backend_args_bak = backend_args.copy() + backend_name = backend_args_bak.pop('backend') + backend = backends[backend_name](**backend_args_bak) else: backend = prefix_to_backends[prefix](**backend_args) return backend diff --git a/tests/test_fileio/test_io.py b/tests/test_fileio/test_io.py index 1e8698cc68..c34af47e0b 100644 --- a/tests/test_fileio/test_io.py +++ b/tests/test_fileio/test_io.py @@ -107,10 +107,13 @@ def test_get_file_backend(): backend_args = {'backend': 'local'} backend = fileio.get_file_backend(uri=None, backend_args=backend_args) assert isinstance(backend, fileio.backends.LocalBackend) + # backend_args should not be modified + assert backend_args == {'backend': 'local'} backend_args = {'backend': 'petrel', 'enable_mc': True} backend = fileio.get_file_backend(uri=None, backend_args=backend_args) assert isinstance(backend, fileio.backends.PetrelBackend) + assert backend_args == {'backend': 'petrel', 'enable_mc': True} # backend name has a higher priority backend_args = {'backend': 'http'}