From 5dac884a39459e27672617bf4a234efe7df9d9ea Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 4 Apr 2023 16:04:12 +0000 Subject: [PATCH] class Lockable: _lock -> _locked, lock() -> _lock() --- mmgen/base_obj.py | 20 ++++++++++---------- mmgen/flags.py | 4 ++-- mmgen/opts.py | 2 +- test/unit_tests_d/ut_flags.py | 8 +++++++- test/unit_tests_d/ut_lockable.py | 14 +++++++------- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/mmgen/base_obj.py b/mmgen/base_obj.py index b8859515..dccb3660 100755 --- a/mmgen/base_obj.py +++ b/mmgen/base_obj.py @@ -30,7 +30,7 @@ class AttrCtrlMeta(type): def __call__(cls,*args,**kwargs): instance = super().__call__(*args,**kwargs) if instance._autolock: - instance.lock() + instance._lock() return instance class AttrCtrl(metaclass=AttrCtrlMeta): @@ -46,24 +46,24 @@ class AttrCtrl(metaclass=AttrCtrlMeta): instead of raising AttributeError. """ _autolock = True - _lock = False + _locked = False _use_class_attr = False _default_to_none = False _skip_type_check = () - def lock(self): - self._lock = True + def _lock(self): + self._locked = True def __getattr__(self,name): - if self._lock and self._default_to_none: + if self._locked and self._default_to_none: return None else: raise AttributeError(f'{type(self).__name__} object has no attribute {name!r}') def __setattr__(self,name,value): - if self._lock: - assert name != '_lock', 'lock can be set only once' + if self._locked: + assert name != '_locked', 'lock can be set only once' def do_error(name,value,ref_val): raise AttributeError( @@ -103,15 +103,15 @@ class Lockable(AttrCtrl): _set_ok = () _reset_ok = () - def lock(self): + def _lock(self): for name in ('_set_ok','_reset_ok'): for attr in getattr(self,name): assert hasattr(self,attr), ( f'attribute {attr!r} in {name!r} not found in {type(self).__name__} object {id(self)}' ) - super().lock() + super()._lock() def __setattr__(self,name,value): - if self._lock and (name in self.__dict__ or hasattr(type(self),name)): + if self._locked and (name in self.__dict__ or hasattr(type(self),name)): val = getattr(self,name) if name not in (self._set_ok + self._reset_ok): raise AttributeError(f'attribute {name!r} of {type(self).__name__} object is read-only') diff --git a/mmgen/flags.py b/mmgen/flags.py index bf267de1..fd68dcd0 100755 --- a/mmgen/flags.py +++ b/mmgen/flags.py @@ -26,7 +26,7 @@ from .util import fmt_list,die class ClassFlags(AttrCtrl): _name = 'flags' _desc = 'flag' - reserved_attrs = ('lock',) + reserved_attrs = () def __init__(self,parent,arg): self._parent = parent @@ -59,7 +59,7 @@ class ClassFlags(AttrCtrl): def __setattr__(self,name,val): - if self._lock: + if self._locked: if name not in self._available: self.not_available_error(name) diff --git a/mmgen/opts.py b/mmgen/opts.py index f821c804..361bdbbe 100755 --- a/mmgen/opts.py +++ b/mmgen/opts.py @@ -400,7 +400,7 @@ def init( cfg._uopts = po.user_opts cfg._args = po.cmd_args - cfg.lock() + cfg._lock() if need_proto: from .protocol import warn_trustlevel,init_proto_from_cfg diff --git a/test/unit_tests_d/ut_flags.py b/test/unit_tests_d/ut_flags.py index 7d1afe01..40f8bffb 100755 --- a/test/unit_tests_d/ut_flags.py +++ b/test/unit_tests_d/ut_flags.py @@ -12,6 +12,9 @@ class unit_test(object): def run_test(self,name,ut): + class MyClassOpts(ClassOpts): + reserved_attrs = ('foo',) + class cls1: avail_opts = () avail_flags = () @@ -27,7 +30,10 @@ class unit_test(object): avail_opts = ('_foo',) class cls4(cls1): - avail_opts = ('lock',) + avail_opts = ('foo',) + def __init__(self,opts=None,flags=None): + self.opt = MyClassOpts(self,opts) + self.flag = MyClassFlags(self,flags) def test_flags(): def gen(): diff --git a/test/unit_tests_d/ut_lockable.py b/test/unit_tests_d/ut_lockable.py index f2681a97..a17065d1 100755 --- a/test/unit_tests_d/ut_lockable.py +++ b/test/unit_tests_d/ut_lockable.py @@ -20,7 +20,7 @@ class unit_test(object): _autolock = False foo = 'fooval' ac = MyAttrCtrl() - ac.lock() + ac._lock() ac.foo = 'new fooval' ac.foo = 'new fooval2' @@ -34,7 +34,7 @@ class unit_test(object): foo = 'fooval' bar = None acc = MyAttrCtrlClsCheck() - acc.lock() + acc._lock() acc.foo = 'new_fooval' acc.foo = 'new_fooval2' @@ -72,7 +72,7 @@ class unit_test(object): lc.delta = 0.0 # yes lc.epsilon = [] # no - lc.lock() + lc._lock() lc.foo = 'fooval2' lc.bar = 'barval2' @@ -93,7 +93,7 @@ class unit_test(object): qux = 'quxval' lcc = MyLockableClsCheck() - lcc.lock() + lcc._lock() lcc.foo = 'fooval2' # class attribute foo is None, so can be set to any type lcc.bar = 2 @@ -110,7 +110,7 @@ class unit_test(object): lca = MyLockableAutolock() assert lca._autolock == True - assert lca._lock == True + assert lca._locked == True assert lca.foo == True class MyLockableAutolockDflNone(Lockable): @@ -148,7 +148,7 @@ class unit_test(object): def bad16(): lca.foo = None def bad17(): lb = MyLockableBad() - def bad18(): aca.lock() + def bad18(): aca._lock() def bad19(): acdn.baz = None def bad20(): lcdn.foo = 1 @@ -175,7 +175,7 @@ class unit_test(object): ("attr (can't reset)", 'AttributeError', 'reset', bad15 ), ("attr (can't set)", 'AttributeError', 'read-only', bad16 ), ("attr (bad _set_ok)", 'AssertionError', 'not found in',bad17 ), - ("call to lock()", 'AssertionError', 'only once', bad18 ), + ("call to _lock()", 'AssertionError', 'only once', bad18 ), )) qmsg('OK')