Skip to content

Commit 8a5aca2

Browse files
committed
dep: support __contains__(str) for dependency types
1 parent 6022af6 commit 8a5aca2

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/pkgcraft/C.pxd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,12 @@ cdef extern from "pkgcraft.h":
815815
# The arguments must be valid Dependency pointers.
816816
bool pkgcraft_dependency_contains_dependency(Dependency *d1, Dependency *d2)
817817

818+
# Determine if a Dependency contains a given raw string.
819+
#
820+
# # Safety
821+
# The arguments must be valid pointers.
822+
bool pkgcraft_dependency_contains_str(Dependency *d, const char *s)
823+
818824
# Evaluate a Dependency.
819825
#
820826
# # Safety
@@ -908,6 +914,12 @@ cdef extern from "pkgcraft.h":
908914
# The arguments must be valid DependencySet and Dependency pointers.
909915
bool pkgcraft_dependency_set_contains_dependency(DependencySet *s, Dependency *d)
910916

917+
# Determine if a DependencySet contains a given raw string.
918+
#
919+
# # Safety
920+
# The arguments must be valid pointers.
921+
bool pkgcraft_dependency_set_contains_str(DependencySet *d, const char *s)
922+
911923
# Determine if two DependencySets are equal.
912924
#
913925
# # Safety

src/pkgcraft/dep/base.pyx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ cdef class Dependency:
145145
def __contains__(self, obj):
146146
if isinstance(obj, Dependency):
147147
return C.pkgcraft_dependency_contains_dependency(self.ptr, (<Dependency>obj).ptr)
148+
elif isinstance(obj, str):
149+
return C.pkgcraft_dependency_contains_str(self.ptr, obj.encode())
148150
return False
149151

150152
def __iter__(self):
@@ -395,15 +397,10 @@ cdef class DependencySet:
395397
return depset
396398

397399
def __contains__(self, obj):
398-
cdef Dependency dep = None
399-
400400
if isinstance(obj, Dependency):
401-
dep = obj
401+
return C.pkgcraft_dependency_set_contains_dependency(self.ptr, (<Dependency>obj).ptr)
402402
elif isinstance(obj, str):
403-
dep = Dependency(obj, set=self.set)
404-
405-
if dep is not None:
406-
return C.pkgcraft_dependency_set_contains_dependency(self.ptr, dep.ptr)
403+
return C.pkgcraft_dependency_set_contains_str(self.ptr, obj.encode())
407404
return False
408405

409406
def __iter__(self):

tests/dep/test_base.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ def test_contains(self):
159159
assert Dependency.required_use("u2? ( b )") in d
160160
assert Dependency.required_use("b") not in d
161161

162+
# substrings
163+
assert "u2?" in d
164+
assert "b ) )" in d
165+
162166
# non-Dependency objects return False
163167
assert None not in d
164168

@@ -416,10 +420,14 @@ def test_contains(self):
416420
assert Dependency("a/b") in self.cls("a/b")
417421
assert Dependency("a/b") not in self.cls("u? ( a/b )")
418422

419-
# valid Dependency strings work
423+
# valid Dependency objects
420424
assert "a/b" in self.cls("a/b")
421425
assert "u? ( c/d )" in self.cls("a/b u? ( c/d )")
422426

427+
# substrings
428+
assert "u?" in self.cls("a/b u? ( c/d )")
429+
assert " ( " in self.cls("a/b u? ( c/d )")
430+
423431
# all other object types return False
424432
assert None not in self.cls("a/b")
425433

0 commit comments

Comments
 (0)