Skip to content

Commit 3147fcc

Browse files
committed
atom: implement __str__() for Blocker and SlotOperator enums
1 parent 6a25db6 commit 3147fcc

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/pkgcraft/atom/base.pyx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import functools
2-
from enum import Enum
2+
from enum import IntEnum
33

44
cimport cython
55

@@ -196,7 +196,7 @@ def _cached_atom(cls, atom, eapi=None):
196196
return cls(atom, eapi)
197197

198198

199-
class Blocker(Enum):
199+
class Blocker(IntEnum):
200200
Strong = C.BLOCKER_STRONG
201201
Weak = C.BLOCKER_WEAK
202202

@@ -207,8 +207,14 @@ class Blocker(Enum):
207207
return Blocker(blocker)
208208
raise ValueError(f'invalid blocker: {s}')
209209

210+
def __str__(self):
211+
c_str = C.pkgcraft_atom_blocker_str(self)
212+
s = c_str.decode()
213+
C.pkgcraft_str_free(c_str)
214+
return s
215+
210216

211-
class SlotOperator(Enum):
217+
class SlotOperator(IntEnum):
212218
Equal = C.SLOT_OPERATOR_EQUAL
213219
Star = C.SLOT_OPERATOR_STAR
214220

@@ -219,6 +225,12 @@ class SlotOperator(Enum):
219225
return SlotOperator(slot_op)
220226
raise ValueError(f'invalid slot operator: {s}')
221227

228+
def __str__(self):
229+
c_str = C.pkgcraft_atom_slot_op_str(self)
230+
s = c_str.decode()
231+
C.pkgcraft_str_free(c_str)
232+
return s
233+
222234

223235
@cython.final
224236
cdef class Atom(Cpv):

src/pkgcraft/pkgcraft_c.pxd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ cdef extern from "pkgcraft.h":
149149
# The argument should be a UTF-8 string.
150150
int pkgcraft_atom_blocker_from_str(const char *s)
151151

152+
# Return the string for a Blocker.
153+
char *pkgcraft_atom_blocker_str(Blocker b)
154+
152155
# Return an atom's category, e.g. the atom "=cat/pkg-1-r2" has a category of "cat".
153156
#
154157
# # Safety
@@ -260,6 +263,9 @@ cdef extern from "pkgcraft.h":
260263
# The argument should be a UTF-8 string.
261264
int pkgcraft_atom_slot_op_from_str(const char *s)
262265

266+
# Return the string for a SlotOperator.
267+
char *pkgcraft_atom_slot_op_str(SlotOperator op)
268+
263269
# Return the string for an atom.
264270
#
265271
# # Safety

0 commit comments

Comments
 (0)