Skip to content

Commit 35cdb9c

Browse files
committed
chore: convert restrictions to meta.Immutable
Signed-off-by: Brian Harring <[email protected]>
1 parent 5307834 commit 35cdb9c

File tree

8 files changed

+86
-120
lines changed

8 files changed

+86
-120
lines changed

src/pkgcore/ebuild/atom.py

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
from ..restrictions.packages import AndRestriction as PkgAndRestriction
1717
from ..restrictions.packages import Conditional
1818
from ..restrictions.values import ContainmentMatch
19-
from . import cpv
19+
from . import cpv, errors, restricts
2020
from . import eapi as eapi_mod
21-
from . import errors, restricts
2221

2322
# namespace compatibility...
2423
MalformedAtom = errors.MalformedAtom
@@ -95,7 +94,6 @@ def __init__(self, atom: str, negate_vers: bool = False, eapi: str = "-1"):
9594
if not atom:
9695
raise errors.MalformedAtom(atom)
9796

98-
sf = object.__setattr__
9997
orig_atom = atom
10098
override_kls = False
10199
use_start = atom.find("[")
@@ -116,7 +114,7 @@ def __init__(self, atom: str, negate_vers: bool = False, eapi: str = "-1"):
116114
raise errors.MalformedAtom(orig_atom, "use restriction isn't completed")
117115
elif use_end != len(atom) - 1:
118116
raise errors.MalformedAtom(orig_atom, "trailing garbage after use dep")
119-
sf(self, "use", tuple(sorted(atom[use_start + 1 : use_end].split(","))))
117+
self.use = tuple(sorted(atom[use_start + 1 : use_end].split(",")))
120118
for x in self.use:
121119
# stripped purely for validation reasons
122120
try:
@@ -152,10 +150,10 @@ def __init__(self, atom: str, negate_vers: bool = False, eapi: str = "-1"):
152150
except IndexError:
153151
raise errors.MalformedAtom(orig_atom, "empty use dep detected")
154152
if override_kls:
155-
sf(self, "__class__", transitive_use_atom)
153+
self.__class__ = transitive_use_atom
156154
atom = atom[0:use_start] + atom[use_end + 1 :]
157155
else:
158-
sf(self, "use", None)
156+
self.use = None
159157
if slot_start != -1:
160158
i2 = atom.find("::", slot_start)
161159
if i2 != -1:
@@ -173,9 +171,9 @@ def __init__(self, atom: str, negate_vers: bool = False, eapi: str = "-1"):
173171
f"repo_id may contain only [a-Z0-9_-/], found {repo_id!r}",
174172
)
175173
atom = atom[:i2]
176-
sf(self, "repo_id", repo_id)
174+
self.repo_id = repo_id
177175
else:
178-
sf(self, "repo_id", None)
176+
self.repo_id = None
179177
# slot dep.
180178
slot = atom[slot_start + 1 :]
181179
slot_operator = subslot = None
@@ -230,17 +228,12 @@ def __init__(self, atom: str, negate_vers: bool = False, eapi: str = "-1"):
230228
if len(slots) == 2:
231229
slot, subslot = slots
232230

233-
sf(self, "slot_operator", slot_operator)
234-
sf(self, "slot", slot)
235-
sf(self, "subslot", subslot)
231+
self.slot_operator, self.slot, self.subslot = slot_operator, slot, subslot
236232
atom = atom[:slot_start]
237233
else:
238-
sf(self, "slot_operator", None)
239-
sf(self, "slot", None)
240-
sf(self, "subslot", None)
241-
sf(self, "repo_id", None)
234+
self.slot_operator = self.slot = self.subslot = self.repo_id = None
242235

243-
sf(self, "blocks", atom[0] == "!")
236+
self.blocks = atom[0] == "!"
244237
if self.blocks:
245238
atom = atom[1:]
246239
# hackish/slow, but lstrip doesn't take a 'prune this many' arg
@@ -251,32 +244,32 @@ def __init__(self, atom: str, negate_vers: bool = False, eapi: str = "-1"):
251244
f"strong blockers are not supported in EAPI {eapi}"
252245
)
253246
atom = atom[1:]
254-
sf(self, "blocks_strongly", True)
247+
self.blocks_strongly = True
255248
else:
256-
sf(self, "blocks_strongly", False)
249+
self.blocks_strongly = False
257250
else:
258-
sf(self, "blocks_strongly", False)
251+
self.blocks_strongly = False
259252

260253
if atom[0] in ("<", ">"):
261254
if atom[1] == "=":
262-
sf(self, "op", atom[:2])
255+
self.op = atom[:2]
263256
atom = atom[2:]
264257
else:
265-
sf(self, "op", atom[0])
258+
self.op = atom[0]
266259
atom = atom[1:]
267260
elif atom[0] == "=":
268261
if atom[-1] == "*":
269-
sf(self, "op", "=*")
262+
self.op = "=*"
270263
atom = atom[1:-1]
271264
else:
272265
atom = atom[1:]
273-
sf(self, "op", "=")
266+
self.op = "="
274267
elif atom[0] == "~":
275-
sf(self, "op", "~")
268+
self.op = "~"
276269
atom = atom[1:]
277270
else:
278-
sf(self, "op", "")
279-
sf(self, "cpvstr", atom)
271+
self.op = ""
272+
self.cpvstr = atom
280273

281274
if self.slot is not None and not eapi_obj.options.has_slot_deps:
282275
raise errors.MalformedAtom(
@@ -295,7 +288,7 @@ def __init__(self, atom: str, negate_vers: bool = False, eapi: str = "-1"):
295288
orig_atom, f"repo_id atoms aren't supported for EAPI {eapi}"
296289
)
297290
try:
298-
sf(self, "_cpv", cpv.CPV(self.cpvstr, versioned=bool(self.op)))
291+
self._cpv = cpv.CPV(self.cpvstr, versioned=bool(self.op))
299292
except errors.InvalidCPV as e:
300293
raise errors.MalformedAtom(orig_atom) from e
301294

@@ -308,8 +301,9 @@ def __init__(self, atom: str, negate_vers: bool = False, eapi: str = "-1"):
308301
)
309302
elif self.version is not None:
310303
raise errors.MalformedAtom(orig_atom, "versioned atom requires an operator")
311-
sf(self, "_hash", hash(orig_atom))
312-
sf(self, "negate_vers", negate_vers)
304+
305+
self._hash = hash(orig_atom)
306+
self.negate_vers = negate_vers
313307

314308
__getattr__ = klass.GetAttrProxy("_cpv")
315309
__dir__ = klass.DirProxy("_cpv")
@@ -362,7 +356,7 @@ def cnf_solutions(self, full_solution_expansion=False):
362356
return [[self]]
363357

364358
@property
365-
def is_simple(self):
359+
def is_simple(self) -> bool:
366360
return len(self.restrictions) == 2
367361

368362
@klass.jit_attr
@@ -694,7 +688,7 @@ class transitive_use_atom(atom):
694688
__inst_caching__ = True
695689
_nontransitive_use_atom = atom
696690

697-
is_simple = False
691+
is_simple = False # type: ignore
698692

699693
def _stripped_use(self):
700694
return str(self).split("[", 1)[0]

src/pkgcore/ebuild/conditionals.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ def __init__(
3434
node_conds=True,
3535
known_conditionals=None,
3636
):
37-
sf = object.__setattr__
38-
sf(self, "_known_conditionals", known_conditionals)
39-
sf(self, "element_class", element_class)
40-
sf(self, "restrictions", restrictions)
41-
sf(self, "_node_conds", node_conds)
42-
sf(self, "type", restriction.package_type)
43-
sf(self, "negate", False)
37+
self._known_conditionals = known_conditionals
38+
self.element_class = element_class
39+
self.restrictions = restrictions
40+
self._node_conds = node_conds
41+
self.type, self.negate = restriction.package_type, False
4442

4543
@classmethod
4644
def parse(

src/pkgcore/restrictions/boolean.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from itertools import islice
1111

12-
from snakeoil.klass import cached_hash, generic_equality
12+
from snakeoil.klass import cached_hash, generic_equality, immutable
1313

1414
from . import restriction
1515

@@ -42,12 +42,10 @@ def __init__(self, *restrictions, **kwds):
4242
:keyword negate: should the logic be negated?
4343
"""
4444

45-
sf = object.__setattr__
46-
4745
node_type = kwds.pop("node_type", None)
4846

49-
sf(self, "type", node_type)
50-
sf(self, "negate", kwds.pop("negate", False))
47+
self.type = node_type
48+
self.negate = kwds.pop("negate", False)
5149

5250
if node_type is not None:
5351
try:
@@ -62,12 +60,9 @@ def __init__(self, *restrictions, **kwds):
6260
)
6361

6462
if kwds.pop("finalize", True):
65-
if not isinstance(restrictions, tuple):
66-
sf(self, "restrictions", tuple(restrictions))
67-
else:
68-
sf(self, "restrictions", restrictions)
63+
self.restrictions = tuple(restrictions)
6964
else:
70-
sf(self, "restrictions", list(restrictions))
65+
self.restrictions = list(restrictions)
7166

7267
if kwds:
7368
kwds.pop("disable_inst_caching", None)
@@ -123,9 +118,10 @@ def add_restriction(self, *new_restrictions):
123118
except AttributeError:
124119
raise TypeError("%r is finalized" % self)
125120

121+
@immutable.Simple.__allow_mutation_wrapper__
126122
def finalize(self):
127123
"""finalize the restriction instance, disallowing adding restrictions."""
128-
object.__setattr__(self, "restrictions", tuple(self.restrictions))
124+
self.restrictions = tuple(self.restrictions)
129125

130126
def __repr__(self):
131127
return "<%s negate=%r type=%r finalized=%r restrictions=%r @%#8x>" % (

src/pkgcore/restrictions/delegated.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ def __init__(self, transform_func, negate=False):
3030
if not callable(transform_func):
3131
raise TypeError(transform_func)
3232

33-
object.__setattr__(self, "negate", negate)
34-
object.__setattr__(self, "_transform", transform_func)
33+
self.negate, self._transform = negate, transform_func
3534

3635
def match(self, pkginst):
3736
return self._transform(pkginst, "match") != self.negate

src/pkgcore/restrictions/packages.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,14 @@ def __init__(self, attr, childrestriction, negate=False, ignore_missing=True):
4545
"""
4646
if not childrestriction.type == self.subtype:
4747
raise TypeError("restriction must be of type %r" % (self.subtype,))
48-
sf = object.__setattr__
49-
sf(self, "negate", negate)
48+
self.negate = negate
5049
self._parse_attr(attr)
51-
sf(self, "restriction", childrestriction)
52-
sf(self, "ignore_missing", ignore_missing)
50+
self.restriction = childrestriction
51+
self.ignore_missing = ignore_missing
5352

5453
def _parse_attr(self, attr):
55-
object.__setattr__(self, "_pull_attr_func", static_attrgetter(attr))
56-
object.__setattr__(self, "_attr_split", attr.split("."))
54+
self._pull_attr_func = static_attrgetter(attr)
55+
self._attr_split = attr.split(".")
5756

5857
def _pull_attr(self, pkg):
5958
try:
@@ -171,10 +170,8 @@ def attrs(self):
171170
return tuple(".".join(x) for x in self._attr_split)
172171

173172
def _parse_attr(self, attrs):
174-
object.__setattr__(
175-
self, "_pull_attr_func", tuple(map(static_attrgetter, attrs))
176-
)
177-
object.__setattr__(self, "_attr_split", tuple(x.split(".") for x in attrs))
173+
self._pull_attr_func = tuple(map(static_attrgetter, attrs))
174+
self._attr_split = tuple(x.split(".") for x in attrs)
178175

179176
def _pull_attr(self, pkg):
180177
val = []
@@ -216,7 +213,7 @@ def __init__(self, attr, childrestriction, payload, **kwds):
216213
:param kwds: additional args to pass to :obj:`PackageRestriction`
217214
"""
218215
PackageRestriction.__init__(self, attr, childrestriction, **kwds)
219-
object.__setattr__(self, "payload", tuple(payload))
216+
self.payload = tuple(payload)
220217

221218
def __str__(self):
222219
s = PackageRestriction.__str__(self)
@@ -287,8 +284,8 @@ def __init__(self, *a, **kwds):
287284
key = kwds.pop("key", None)
288285
tag = kwds.pop("tag", None)
289286
boolean.AndRestriction.__init__(self, *a, **kwds)
290-
object.__setattr__(self, "key", key)
291-
object.__setattr__(self, "tag", tag)
287+
self.key = key
288+
self.tag = tag
292289

293290
def __str__(self):
294291
boolean_str = boolean.AndRestriction.__str__(self)

src/pkgcore/restrictions/restriction.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66

77
from snakeoil import caching, klass
88
from snakeoil.currying import pretty_docs
9+
from snakeoil.klass import immutable
910

1011

11-
class base(klass.SlotsPicklingMixin, metaclass=caching.WeakInstMeta):
12+
class base(
13+
klass.SlotsPicklingMixin,
14+
immutable.Simple,
15+
metaclass=caching.WeakInstMeta,
16+
):
1217
"""base restriction matching object.
1318
1419
all derivatives *should* be __slots__ based (lot of instances may
@@ -21,8 +26,6 @@ class base(klass.SlotsPicklingMixin, metaclass=caching.WeakInstMeta):
2126
__slots__ = ()
2227
package_matching = False
2328

24-
klass.inject_immutable_instance(locals())
25-
2629
def match(self, *arg, **kwargs):
2730
raise NotImplementedError
2831

@@ -75,9 +78,7 @@ def __getstate__(self):
7578
return self.negate, self.type
7679

7780
def __setstate__(self, state):
78-
negate, node_type = state
79-
object.__setattr__(self, "negate", negate)
80-
object.__setattr__(self, "type", node_type)
81+
self.negate, self.type = state
8182

8283

8384
class Negate(base):

0 commit comments

Comments
 (0)