commit: 8d8ab363b5eef5c105cd5c37e8663d8f01e0b8c9 Author: Brian Harring <ferringb <AT> gmail <DOT> com> AuthorDate: Fri Oct 24 16:26:57 2025 +0000 Commit: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> CommitDate: Sat Oct 25 06:18:10 2025 +0000 URL: https://gitweb.gentoo.org/proj/pkgcore/snakeoil.git/commit/?id=8d8ab363
chore: restructure snakeoil.klass No API break for any code I'm aware of, anything that was using _immutable_* was accessing private, so thems the breaks.. Signed-off-by: Brian Harring <ferringb <AT> gmail.com> Part-of: https://github.com/pkgcore/snakeoil/pull/107 Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org> src/snakeoil/{klass.py => klass/__init__.py} | 49 ++-------------------------- src/snakeoil/klass/immutable.py | 47 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/src/snakeoil/klass.py b/src/snakeoil/klass/__init__.py similarity index 94% rename from src/snakeoil/klass.py rename to src/snakeoil/klass/__init__.py index 08daaab..d271c96 100644 --- a/src/snakeoil/klass.py +++ b/src/snakeoil/klass/__init__.py @@ -40,8 +40,9 @@ from functools import partial, wraps from importlib import import_module from operator import attrgetter -from .caching import WeakInstMeta -from .currying import post_curry +from ..caching import WeakInstMeta +from ..currying import post_curry +from .immutable import ImmutableInstance, immutable_instance, inject_immutable_instance sentinel = object() @@ -720,50 +721,6 @@ def patch(target, external_decorator=None): return decorator -def _immutable_setattr(self, attr, value): - raise AttributeError(self, attr) - - -def _immutable_delattr(self, attr): - raise AttributeError(self, attr) - - -def immutable_instance(name, bases, scope, real_type=type): - """metaclass that makes instances of this class effectively immutable - - It still is possible to do object.__setattr__ to get around it during - initialization, but usage of this class effectively prevents accidental - modification, instead requiring explicit modification.""" - inject_immutable_instance(scope) - return real_type(name, bases, scope) - - -def inject_immutable_instance(scope): - """inject immutable __setattr__ and __delattr__ implementations - - see immutable_instance for further details - - :param scope: mapping to modify, inserting __setattr__ and __delattr__ - methods if they're not yet defined. - """ - scope.setdefault("__setattr__", _immutable_setattr) - scope.setdefault("__delattr__", _immutable_delattr) - - -class ImmutableInstance: - """Class that disables surface-level attribute modifications.""" - - __setattr__ = _immutable_setattr - __delattr__ = _immutable_delattr - - def __getstate__(self): - return self.__dict__.copy() - - def __setstate__(self, state): - for k, v in state.items(): - object.__setattr__(self, k, v) - - def alias_method(attr, name=None, doc=None): """at runtime, redirect to another method diff --git a/src/snakeoil/klass/immutable.py b/src/snakeoil/klass/immutable.py new file mode 100644 index 0000000..1e28def --- /dev/null +++ b/src/snakeoil/klass/immutable.py @@ -0,0 +1,47 @@ +"""Implementations of immutable instance metaclasses""" + +__all__ = ("immutable_instance", "inject_immutable_instance", "ImmutableInstance") + + +def _immutable_setattr(self, attr, value): + raise AttributeError(self, attr) + + +def _immutable_delattr(self, attr): + raise AttributeError(self, attr) + + +def immutable_instance(name, bases, scope, real_type=type): + """metaclass that makes instances of this class effectively immutable + + It still is possible to do object.__setattr__ to get around it during + initialization, but usage of this class effectively prevents accidental + modification, instead requiring explicit modification.""" + inject_immutable_instance(scope) + return real_type(name, bases, scope) + + +def inject_immutable_instance(scope): + """inject immutable __setattr__ and __delattr__ implementations + + see immutable_instance for further details + + :param scope: mapping to modify, inserting __setattr__ and __delattr__ + methods if they're not yet defined. + """ + scope.setdefault("__setattr__", _immutable_setattr) + scope.setdefault("__delattr__", _immutable_delattr) + + +class ImmutableInstance: + """Class that disables surface-level attribute modifications.""" + + __setattr__ = _immutable_setattr + __delattr__ = _immutable_delattr + + def __getstate__(self): + return self.__dict__.copy() + + def __setstate__(self, state): + for k, v in state.items(): + object.__setattr__(self, k, v)
