From 83444fbd262022cf843ddb6f65b6556bac2ecf2e Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 26 Nov 2022 10:23:12 -0500 Subject: [PATCH] Rely on save_method_args to save method args. --- newsfragments/+275f3051.feature.rst | 1 + zipp/__init__.py | 8 +++++--- zipp/_functools.py | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 newsfragments/+275f3051.feature.rst create mode 100644 zipp/_functools.py diff --git a/newsfragments/+275f3051.feature.rst b/newsfragments/+275f3051.feature.rst new file mode 100644 index 0000000..8078ac0 --- /dev/null +++ b/newsfragments/+275f3051.feature.rst @@ -0,0 +1 @@ +Rely on save_method_args to save method args. \ No newline at end of file diff --git a/zipp/__init__.py b/zipp/__init__.py index d5b96b8..cb4f42c 100644 --- a/zipp/__init__.py +++ b/zipp/__init__.py @@ -20,6 +20,9 @@ from .compat.py310 import text_encoding from .glob import Translator +from ._functools import save_method_args + + __all__ = ['Path'] @@ -86,13 +89,12 @@ class InitializedState: Mix-in to save the initialization state for pickling. """ + @save_method_args def __init__(self, *args, **kwargs): - self.__args = args - self.__kwargs = kwargs super().__init__(*args, **kwargs) def __getstate__(self): - return self.__args, self.__kwargs + return self._saved___init__.args, self._saved___init__.kwargs def __setstate__(self, state): args, kwargs = state diff --git a/zipp/_functools.py b/zipp/_functools.py new file mode 100644 index 0000000..f75ae2b --- /dev/null +++ b/zipp/_functools.py @@ -0,0 +1,20 @@ +import collections +import functools + + +# from jaraco.functools 4.0.2 +def save_method_args(method): + """ + Wrap a method such that when it is called, the args and kwargs are + saved on the method. + """ + args_and_kwargs = collections.namedtuple('args_and_kwargs', 'args kwargs') + + @functools.wraps(method) + def wrapper(self, /, *args, **kwargs): + attr_name = '_saved_' + method.__name__ + attr = args_and_kwargs(args, kwargs) + setattr(self, attr_name, attr) + return method(self, *args, **kwargs) + + return wrapper