Override Atom.__setattr__() to make Atom instances immutable. Thanks to Brian
authorZac Medico <zmedico@gentoo.org>
Wed, 5 Nov 2008 23:55:17 +0000 (23:55 -0000)
committerZac Medico <zmedico@gentoo.org>
Wed, 5 Nov 2008 23:55:17 +0000 (23:55 -0000)
Harring for the suggestion.

svn path=/main/trunk/; revision=11812

pym/portage/dep.py

index fc6a8b10e66d13f9cbe2f490000ee56511f61110..bcfe63c459b2880b5395ca7a06b2b460dafde804 100644 (file)
@@ -529,29 +529,32 @@ class Atom(object):
        def __init__(self, s):
                if not isvalidatom(s, allow_blockers=True):
                        raise InvalidAtom(s)
+               obj_setattr = object.__setattr__
                for x in self._str_methods:
-                       setattr(self, x, getattr(s, x))
+                       obj_setattr(self, x, getattr(s, x))
 
                blocker = "!" == s[:1]
                if blocker:
-                       self.blocker = self._blocker(forbid_overlap=("!" == s[1:2]))
-                       if self.blocker.overlap.forbid:
+                       blocker = self._blocker(forbid_overlap=("!" == s[1:2]))
+                       if blocker.overlap.forbid:
                                s = s[2:]
                        else:
                                s = s[1:]
                else:
-                       self.blocker = False
-
-               self.cp = dep_getkey(s)
-               self.cpv = dep_getcpv(s)
-               self.slot = dep_getslot(s)
-               self.operator = get_operator(s)
-               #self.repo = self._get_repo(s)
-               self.use = dep_getusedeps(s)
-               if self.use:
-                       self.use = _use_dep(self.use)
+                       blocker = False
+               obj_setattr(self, "blocker", blocker)
+
+               obj_setattr(self, "cp", dep_getkey(s))
+               obj_setattr(self, "cpv", dep_getcpv(s))
+               obj_setattr(self, "slot", dep_getslot(s))
+               obj_setattr(self, "operator", get_operator(s))
+
+               use = dep_getusedeps(s)
+               if use:
+                       use = _use_dep(use)
                else:
-                       self.use = None
+                       use = None
+               obj_setattr(self, "use", use)
 
        def __cmp__(self, other):
                self_str = str(self)
@@ -562,6 +565,9 @@ class Atom(object):
                        return 1
                return -1
 
+       def __setattr__(self, name, value):
+               raise AttributeError("Atom instances are immutable")
+
 def get_operator(mydep):
        """
        Return the operator used in a depstring.