Python types.ModuleType.__setattr__() Examples
The following are 24
code examples of types.ModuleType.__setattr__().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
types.ModuleType
, or try the search function
.
Example #1
Source File: __init__.py From lazy_import with GNU General Public License v3.0 | 6 votes |
def _reset_lazymodule(module, cls_attrs): """Resets a module's lazy state from cached data. """ modclass = type(module) del modclass.__getattribute__ del modclass.__setattr__ try: del modclass._LOADING except AttributeError: pass for cls_attr in _CLS_ATTRS: try: setattr(modclass, cls_attr, cls_attrs[cls_attr]) except KeyError: pass _reset_lazy_submod_refs(module)
Example #2
Source File: __init__.py From lazy_import with GNU General Public License v3.0 | 5 votes |
def __setattr__(self, attr, value): logger.debug("Setting attr {} to value {}, in LazyModule instance " "of {}".format(attr, value, super(LazyModule, self) .__getattribute__("__name__"))) _load_module(self) return super(LazyModule, self).__setattr__(attr, value)
Example #3
Source File: test_descr.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def carloverre(): if verbose: print "Testing prohibition of Carlo Verre's hack..." try: object.__setattr__(str, "foo", 42) except TypeError: pass else: raise TestFailed, "Carlo Verre __setattr__ suceeded!" try: object.__delattr__(str, "lower") except TypeError: pass else: raise TestFailed, "Carlo Verre __delattr__ succeeded!"
Example #4
Source File: test_descr.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def pymods(): if verbose: print "Testing Python subclass of module..." log = [] from types import ModuleType as MT class MM(MT): def __init__(self, name): MT.__init__(self, name) def __getattribute__(self, name): log.append(("getattr", name)) return MT.__getattribute__(self, name) def __setattr__(self, name, value): log.append(("setattr", name, value)) MT.__setattr__(self, name, value) def __delattr__(self, name): log.append(("delattr", name)) MT.__delattr__(self, name) a = MM("a") a.foo = 12 x = a.foo del a.foo vereq(log, [("setattr", "foo", 12), ("getattr", "foo"), ("delattr", "foo")]) # http://python.org/sf/1174712 try: class Module(types.ModuleType, str): pass except TypeError: pass else: raise TestFailed("inheriting from ModuleType and str at the " "same time should fail")
Example #5
Source File: magic.py From zipa with Apache License 2.0 | 5 votes |
def __setattr__(self, name, value): if hasattr(self, "env"): self.env[name] = value ModuleType.__setattr__(self, name, value)
Example #6
Source File: test_descr.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def carloverre(): if verbose: print "Testing prohibition of Carlo Verre's hack..." try: object.__setattr__(str, "foo", 42) except TypeError: pass else: raise TestFailed, "Carlo Verre __setattr__ suceeded!" try: object.__delattr__(str, "lower") except TypeError: pass else: raise TestFailed, "Carlo Verre __delattr__ succeeded!"
Example #7
Source File: test_descr.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def pymods(): if verbose: print "Testing Python subclass of module..." log = [] from types import ModuleType as MT class MM(MT): def __init__(self, name): MT.__init__(self, name) def __getattribute__(self, name): log.append(("getattr", name)) return MT.__getattribute__(self, name) def __setattr__(self, name, value): log.append(("setattr", name, value)) MT.__setattr__(self, name, value) def __delattr__(self, name): log.append(("delattr", name)) MT.__delattr__(self, name) a = MM("a") a.foo = 12 x = a.foo del a.foo vereq(log, [("setattr", "foo", 12), ("getattr", "foo"), ("delattr", "foo")]) # http://python.org/sf/1174712 try: class Module(types.ModuleType, str): pass except TypeError: pass else: raise TestFailed("inheriting from ModuleType and str at the " "same time should fail")
Example #8
Source File: __init__.py From lazy_import with GNU General Public License v3.0 | 5 votes |
def _reset_lazy_submod_refs(module): modclass = type(module) for deldict in _DELETION_DICT: try: resetnames = getattr(modclass, deldict) except AttributeError: continue for name, submod in resetnames.items(): super(LazyModule, module).__setattr__(name, submod)
Example #9
Source File: __init__.py From lazy_import with GNU General Public License v3.0 | 5 votes |
def _clean_lazymodule(module): """Removes all lazy behavior from a module's class, for loading. Also removes all module attributes listed under the module's class deletion dictionaries. Deletion dictionaries are class attributes with names specified in `_DELETION_DICT`. Parameters ---------- module: LazyModule Returns ------- dict A dictionary of deleted class attributes, that can be used to reset the lazy state using :func:`_reset_lazymodule`. """ modclass = type(module) _clean_lazy_submod_refs(module) modclass.__getattribute__ = ModuleType.__getattribute__ modclass.__setattr__ = ModuleType.__setattr__ cls_attrs = {} for cls_attr in _CLS_ATTRS: try: cls_attrs[cls_attr] = getattr(modclass, cls_attr) delattr(modclass, cls_attr) except AttributeError: pass return cls_attrs
Example #10
Source File: releases.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def __setattr__(self, name, value): #if hasattr(self, "instances"): self.instances[name] = value ModuleType.__setattr__(self, name, value)
Example #11
Source File: test_descr.py From medicare-demo with Apache License 2.0 | 5 votes |
def carloverre(): if verbose: print "Testing prohibition of Carlo Verre's hack..." try: object.__setattr__(str, "foo", 42) except TypeError: pass else: raise TestFailed, "Carlo Verre __setattr__ suceeded!" try: object.__delattr__(str, "lower") except TypeError: pass else: raise TestFailed, "Carlo Verre __delattr__ succeeded!"
Example #12
Source File: test_descr.py From medicare-demo with Apache License 2.0 | 5 votes |
def pymods(): if verbose: print "Testing Python subclass of module..." log = [] from types import ModuleType as MT class MM(MT): def __init__(self, name): MT.__init__(self, name) def __getattribute__(self, name): log.append(("getattr", name)) return MT.__getattribute__(self, name) def __setattr__(self, name, value): log.append(("setattr", name, value)) MT.__setattr__(self, name, value) def __delattr__(self, name): log.append(("delattr", name)) MT.__delattr__(self, name) a = MM("a") a.foo = 12 x = a.foo del a.foo vereq(log, [("setattr", "foo", 12), ("getattr", "foo"), ("delattr", "foo")]) # http://python.org/sf/1174712 try: class Module(types.ModuleType, str): pass except TypeError: pass else: raise TestFailed("inheriting from ModuleType and str at the " "same time should fail")
Example #13
Source File: releases.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def __setattr__(self, name, value): #if hasattr(self, "instances"): self.instances[name] = value ModuleType.__setattr__(self, name, value)
Example #14
Source File: releases.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def __setattr__(self, name, value): #if hasattr(self, "instances"): self.instances[name] = value ModuleType.__setattr__(self, name, value)
Example #15
Source File: releases.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def __setattr__(self, name, value): #if hasattr(self, "instances"): self.instances[name] = value ModuleType.__setattr__(self, name, value)
Example #16
Source File: releases.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def __setattr__(self, name, value): #if hasattr(self, "instances"): self.instances[name] = value ModuleType.__setattr__(self, name, value)
Example #17
Source File: releases.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def __setattr__(self, name, value): #if hasattr(self, "instances"): self.instances[name] = value ModuleType.__setattr__(self, name, value)
Example #18
Source File: releases.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def __setattr__(self, name, value): #if hasattr(self, "instances"): self.instances[name] = value ModuleType.__setattr__(self, name, value)
Example #19
Source File: releases.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def __setattr__(self, name, value): #if hasattr(self, "instances"): self.instances[name] = value ModuleType.__setattr__(self, name, value)
Example #20
Source File: releases.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def __setattr__(self, name, value): #if hasattr(self, "instances"): self.instances[name] = value ModuleType.__setattr__(self, name, value)
Example #21
Source File: test_descr.py From medicare-demo with Apache License 2.0 | 4 votes |
def overloading(): if verbose: print "Testing operator overloading..." class B(object): "Intermediate class because object doesn't have a __setattr__" class C(B): def __getattr__(self, name): if name == "foo": return ("getattr", name) else: raise AttributeError def __setattr__(self, name, value): if name == "foo": self.setattr = (name, value) else: return B.__setattr__(self, name, value) def __delattr__(self, name): if name == "foo": self.delattr = name else: return B.__delattr__(self, name) def __getitem__(self, key): return ("getitem", key) def __setitem__(self, key, value): self.setitem = (key, value) def __delitem__(self, key): self.delitem = key def __getslice__(self, i, j): return ("getslice", i, j) def __setslice__(self, i, j, value): self.setslice = (i, j, value) def __delslice__(self, i, j): self.delslice = (i, j) a = C() vereq(a.foo, ("getattr", "foo")) a.foo = 12 vereq(a.setattr, ("foo", 12)) del a.foo vereq(a.delattr, "foo") vereq(a[12], ("getitem", 12)) a[12] = 21 vereq(a.setitem, (12, 21)) del a[12] vereq(a.delitem, 12) vereq(a[0:10], ("getslice", 0, 10)) a[0:10] = "foo" vereq(a.setslice, (0, 10, "foo")) del a[0:10] vereq(a.delslice, (0, 10))
Example #22
Source File: __init__.py From lazy_import with GNU General Public License v3.0 | 4 votes |
def _lazy_module(modname, error_strings, lazy_mod_class): with _ImportLockContext(): fullmodname = modname fullsubmodname = None # ensure parent module/package is in sys.modules # and parent.modname=module, as soon as the parent is imported while modname: try: mod = sys.modules[modname] # We reached a (base) module that's already loaded. Let's stop # the cycle. Can't use 'break' because we still want to go # through the fullsubmodname check below. modname = '' except KeyError: err_s = error_strings.copy() err_s.setdefault('module', modname) class _LazyModule(lazy_mod_class): _lazy_import_error_msgs = {'msg': err_s.pop('msg')} try: _lazy_import_error_msgs['msg_callable'] = \ err_s.pop('msg_callable') except KeyError: pass _lazy_import_error_strings = err_s _lazy_import_callables = {} _lazy_import_submodules = {} def __repr__(self): return "Lazily-loaded module {}".format(self.__name__) # A bit of cosmetic, to make AttributeErrors read more natural _LazyModule.__name__ = 'module' # Actual module instantiation mod = sys.modules[modname] = _LazyModule(modname) # No need for __spec__. Maybe in the future. #if ModuleSpec: # ModuleType.__setattr__(mod, '__spec__', # ModuleSpec(modname, None)) if fullsubmodname: submod = sys.modules[fullsubmodname] ModuleType.__setattr__(mod, submodname, submod) _LazyModule._lazy_import_submodules[submodname] = submod fullsubmodname = modname modname, _, submodname = modname.rpartition('.') return sys.modules[fullmodname]
Example #23
Source File: test_descr.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 4 votes |
def overloading(): if verbose: print "Testing operator overloading..." class B(object): "Intermediate class because object doesn't have a __setattr__" class C(B): def __getattr__(self, name): if name == "foo": return ("getattr", name) else: raise AttributeError def __setattr__(self, name, value): if name == "foo": self.setattr = (name, value) else: return B.__setattr__(self, name, value) def __delattr__(self, name): if name == "foo": self.delattr = name else: return B.__delattr__(self, name) def __getitem__(self, key): return ("getitem", key) def __setitem__(self, key, value): self.setitem = (key, value) def __delitem__(self, key): self.delitem = key def __getslice__(self, i, j): return ("getslice", i, j) def __setslice__(self, i, j, value): self.setslice = (i, j, value) def __delslice__(self, i, j): self.delslice = (i, j) a = C() vereq(a.foo, ("getattr", "foo")) a.foo = 12 vereq(a.setattr, ("foo", 12)) del a.foo vereq(a.delattr, "foo") vereq(a[12], ("getitem", 12)) a[12] = 21 vereq(a.setitem, (12, 21)) del a[12] vereq(a.delitem, 12) vereq(a[0:10], ("getslice", 0, 10)) a[0:10] = "foo" vereq(a.setslice, (0, 10, "foo")) del a[0:10] vereq(a.delslice, (0, 10))
Example #24
Source File: test_descr.py From CTFCrackTools with GNU General Public License v3.0 | 4 votes |
def overloading(): if verbose: print "Testing operator overloading..." class B(object): "Intermediate class because object doesn't have a __setattr__" class C(B): def __getattr__(self, name): if name == "foo": return ("getattr", name) else: raise AttributeError def __setattr__(self, name, value): if name == "foo": self.setattr = (name, value) else: return B.__setattr__(self, name, value) def __delattr__(self, name): if name == "foo": self.delattr = name else: return B.__delattr__(self, name) def __getitem__(self, key): return ("getitem", key) def __setitem__(self, key, value): self.setitem = (key, value) def __delitem__(self, key): self.delitem = key def __getslice__(self, i, j): return ("getslice", i, j) def __setslice__(self, i, j, value): self.setslice = (i, j, value) def __delslice__(self, i, j): self.delslice = (i, j) a = C() vereq(a.foo, ("getattr", "foo")) a.foo = 12 vereq(a.setattr, ("foo", 12)) del a.foo vereq(a.delattr, "foo") vereq(a[12], ("getitem", 12)) a[12] = 21 vereq(a.setitem, (12, 21)) del a[12] vereq(a.delitem, 12) vereq(a[0:10], ("getslice", 0, 10)) a[0:10] = "foo" vereq(a.setslice, (0, 10, "foo")) del a[0:10] vereq(a.delslice, (0, 10))