Skip to content

Commit

Permalink
Merge pull request #109 from jaraco/feature/functools
Browse files Browse the repository at this point in the history
Re-use functionality from jaraco.functools
  • Loading branch information
jaraco committed Sep 13, 2024
2 parents e23cbba + 83444fb commit 466f535
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions newsfragments/+275f3051.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rely on save_method_args to save method args.
8 changes: 5 additions & 3 deletions zipp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from .compat.py310 import text_encoding
from .glob import Translator

from ._functools import save_method_args


__all__ = ['Path']


Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions zipp/_functools.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 466f535

Please sign in to comment.