From 09c54eaee3aaaa75876d16913c82af5f09bee9e8 Mon Sep 17 00:00:00 2001 From: HAOCHENYE <21724054@zju.edu.cn> Date: Thu, 27 Jul 2023 10:53:43 +0800 Subject: [PATCH 1/5] Fix ConfigDict.items will be called during dump --- mmengine/config/config.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/mmengine/config/config.py b/mmengine/config/config.py index 97b16f6f54..915962d09d 100644 --- a/mmengine/config/config.py +++ b/mmengine/config/config.py @@ -264,15 +264,11 @@ def _merge_a_into_b(a, b): for key, value in merged.items(): self[key] = value - def __getstate__(self): - state = {} - for key, value in super().items(): - state[key] = value - return state - - def __setstate__(self, state): - for key, value in state.items(): - self[key] = value + def __reduce_ex__(self, proto): + # + return (self.__class__, ({k: v + for k, v in super().items()}, ), None, None, + None, None) def __eq__(self, other): if isinstance(other, ConfigDict): From 92dc2c8d7e2811d7fb1fac1a2466e397669c91e6 Mon Sep 17 00:00:00 2001 From: HAOCHENYE <21724054@zju.edu.cn> Date: Thu, 27 Jul 2023 10:55:59 +0800 Subject: [PATCH 2/5] add comment --- mmengine/config/config.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mmengine/config/config.py b/mmengine/config/config.py index 915962d09d..d90bff9363 100644 --- a/mmengine/config/config.py +++ b/mmengine/config/config.py @@ -265,7 +265,9 @@ def _merge_a_into_b(a, b): self[key] = value def __reduce_ex__(self, proto): - # + # Customize this dump related function to void `self.items` will be + # called by CPython interpreter during pickling. See more details in + # https://github.com/python/cpython/blob/8d61a71f9c81619e34d4a30b625922ebc83c561b/Objects/typeobject.c#L6196 # noqa: E501 return (self.__class__, ({k: v for k, v in super().items()}, ), None, None, None, None) From ce4b7abf20fc7aa4aa806b99880dd01ef170d6c3 Mon Sep 17 00:00:00 2001 From: HAOCHENYE <21724054@zju.edu.cn> Date: Thu, 27 Jul 2023 10:58:56 +0800 Subject: [PATCH 3/5] add comment --- mmengine/config/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmengine/config/config.py b/mmengine/config/config.py index d90bff9363..bb53b931ea 100644 --- a/mmengine/config/config.py +++ b/mmengine/config/config.py @@ -265,7 +265,7 @@ def _merge_a_into_b(a, b): self[key] = value def __reduce_ex__(self, proto): - # Customize this dump related function to void `self.items` will be + # Override __reduce_ex__ to void `self.items` will be # called by CPython interpreter during pickling. See more details in # https://github.com/python/cpython/blob/8d61a71f9c81619e34d4a30b625922ebc83c561b/Objects/typeobject.c#L6196 # noqa: E501 return (self.__class__, ({k: v From 9267402c92e55619785c5b308d3107c307c5cb42 Mon Sep 17 00:00:00 2001 From: HAOCHENYE <21724054@zju.edu.cn> Date: Thu, 27 Jul 2023 17:27:32 +0800 Subject: [PATCH 4/5] fix python 3.7 --- mmengine/config/config.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mmengine/config/config.py b/mmengine/config/config.py index bb53b931ea..db16d7c879 100644 --- a/mmengine/config/config.py +++ b/mmengine/config/config.py @@ -24,8 +24,9 @@ from mmengine.fileio import dump, load from mmengine.logging import print_log -from mmengine.utils import (check_file_exist, get_installed_path, - import_modules_from_strings, is_installed) +from mmengine.utils import (check_file_exist, digit_version, + get_installed_path, import_modules_from_strings, + is_installed) from .lazy import LazyAttr, LazyObject from .utils import (ConfigParsingError, ImportTransformer, RemoveAssignFromAST, _gather_abs_import_lazyobj, _get_external_cfg_base_path, @@ -268,9 +269,14 @@ def __reduce_ex__(self, proto): # Override __reduce_ex__ to void `self.items` will be # called by CPython interpreter during pickling. See more details in # https://github.com/python/cpython/blob/8d61a71f9c81619e34d4a30b625922ebc83c561b/Objects/typeobject.c#L6196 # noqa: E501 - return (self.__class__, ({k: v - for k, v in super().items()}, ), None, None, - None, None) + if digit_version(platform.python_version()) < digit_version('3.8'): + return (self.__class__, ({k: v + for k, v in super().items()}, ), None, + None, None) + else: + return (self.__class__, ({k: v + for k, v in super().items()}, ), None, + None, None, None) def __eq__(self, other): if isinstance(other, ConfigDict): From f9f5214acd9a5cd0ea589e8b0555ca8790d31071 Mon Sep 17 00:00:00 2001 From: HAOCHENYE <21724054@zju.edu.cn> Date: Thu, 27 Jul 2023 17:30:14 +0800 Subject: [PATCH 5/5] refine comments --- mmengine/config/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmengine/config/config.py b/mmengine/config/config.py index db16d7c879..18202b4d09 100644 --- a/mmengine/config/config.py +++ b/mmengine/config/config.py @@ -266,7 +266,7 @@ def _merge_a_into_b(a, b): self[key] = value def __reduce_ex__(self, proto): - # Override __reduce_ex__ to void `self.items` will be + # Override __reduce_ex__ to avoid `self.items` will be # called by CPython interpreter during pickling. See more details in # https://github.com/python/cpython/blob/8d61a71f9c81619e34d4a30b625922ebc83c561b/Objects/typeobject.c#L6196 # noqa: E501 if digit_version(platform.python_version()) < digit_version('3.8'):