Skip to content

Commit 84e22d2

Browse files
committed
dep: merge Dependency and DependencySet __init__() and from_ptr()
1 parent 2a0397f commit 84e22d2

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

src/pkgcraft/dep/base.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ cdef class Dependency:
77
cdef readonly object set
88

99
@staticmethod
10-
cdef Dependency from_ptr(C.Dependency *)
10+
cdef Dependency from_ptr(C.Dependency *, Dependency inst=*)
1111

1212

1313
cdef class DependencySet:
1414
cdef C.DependencySet *ptr
1515
cdef readonly object set
1616

1717
@staticmethod
18-
cdef DependencySet from_ptr(C.DependencySet *)
18+
cdef DependencySet from_ptr(C.DependencySet *, DependencySet inst=*)
1919

2020
cdef clone(self)
2121
cdef create(self, C.DependencySet *)
@@ -27,4 +27,4 @@ cdef class DependencySet:
2727
cdef class MutableDependencySet(DependencySet):
2828

2929
@staticmethod
30-
cdef MutableDependencySet from_ptr(C.DependencySet *)
30+
cdef DependencySet from_ptr(C.DependencySet *, DependencySet inst=*)

src/pkgcraft/dep/base.pyx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ cdef class Dependency:
6464
if ptr is NULL:
6565
raise PkgcraftError
6666

67-
self.set = DependencySetKind(ptr.set)
68-
self.kind = DependencyKind(ptr.kind)
69-
self.ptr = ptr
67+
Dependency.from_ptr(ptr, self)
7068

7169
@classmethod
7270
def package(cls, s: str = None, eapi: Eapi | str = None):
@@ -99,9 +97,10 @@ cdef class Dependency:
9997
return cls(s, set=DependencySetKind.SrcUri)
10098

10199
@staticmethod
102-
cdef Dependency from_ptr(C.Dependency *ptr):
100+
cdef Dependency from_ptr(C.Dependency *ptr, Dependency inst = None):
103101
"""Create a Dependency from a pointer and type."""
104-
inst = <Dependency>Dependency.__new__(Dependency)
102+
if inst is None:
103+
inst = <Dependency>Dependency.__new__(Dependency)
105104
inst.set = DependencySetKind(ptr.set)
106105
inst.kind = DependencyKind(ptr.kind)
107106
inst.ptr = ptr
@@ -229,8 +228,7 @@ cdef class DependencySet:
229228
if ptr is NULL:
230229
raise PkgcraftError
231230

232-
self.set = DependencySetKind(ptr.set)
233-
self.ptr = ptr
231+
DependencySet.from_ptr(ptr, self)
234232

235233
@classmethod
236234
def package(cls, s: str = None, eapi: Eapi | str = None):
@@ -263,9 +261,10 @@ cdef class DependencySet:
263261
return cls(s, set=DependencySetKind.SrcUri)
264262

265263
@staticmethod
266-
cdef DependencySet from_ptr(C.DependencySet *ptr):
264+
cdef DependencySet from_ptr(C.DependencySet *ptr, DependencySet inst = None):
267265
"""Create a DependencySet from a pointer."""
268-
inst = <DependencySet>DependencySet.__new__(DependencySet)
266+
if inst is None:
267+
inst = <DependencySet>DependencySet.__new__(DependencySet)
269268
inst.set = DependencySetKind(ptr.set)
270269
inst.ptr = ptr
271270
return inst
@@ -527,12 +526,11 @@ cdef class MutableDependencySet(DependencySet):
527526
"""Mutable set of dependency objects."""
528527

529528
@staticmethod
530-
cdef MutableDependencySet from_ptr(C.DependencySet *ptr):
529+
cdef MutableDependencySet from_ptr(C.DependencySet *ptr, DependencySet inst = None):
531530
"""Create a MutableDependencySet from a pointer."""
532-
inst = <MutableDependencySet>MutableDependencySet.__new__(MutableDependencySet)
533-
inst.set = DependencySetKind(ptr.set)
534-
inst.ptr = ptr
535-
return inst
531+
if inst is None:
532+
inst = <MutableDependencySet>MutableDependencySet.__new__(MutableDependencySet)
533+
return DependencySet.from_ptr(ptr, inst)
536534

537535
def sort(self):
538536
"""Recursively sort a DependencySet.

0 commit comments

Comments
 (0)