Skip to content

Commit 54740fa

Browse files
committed
fix(py): Enum(value) now may raise ValueError
1 parent 758ccf6 commit 54740fa

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

src/pylib/Lib/enum_impl/enumType.nim

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
import std/[hashes, tables, strformat]
33
import ../../pyerrors/simperr
44

5-
template GenPyEnumMeth*(Self, Value: typedesc; genObjMeth = true, nameError = NameError, Str = string){.dirty.} =
5+
template GenPyEnumInit*(Self, Value: typedesc; sym){.dirty.} =
6+
bind `[]`
7+
proc sym*(value: Self): Self = value
8+
proc sym*(value: Value): Self =
9+
`Self.names`.withValue value, name:
10+
return `Self.member_map`[name[]]
11+
raise newException(ValueError, repr(value) & " is not a valid " & $Self)
12+
13+
template GenPyEnumMeth*(Self; Value: typedesc; genObjMeth = true, genInit = false, nameError = NameError, Str = string){.dirty.} =
614
## XXX: NIM-BUG: as `std/tables` is not working with `bind` once required at compile time,
715
## `import std/tables` is a must before using this template.
816
bind contains, `[]`
917
bind hash, Table, Hash, withValue, `[]=`, `$`, items, len, fmt, formatValue
10-
11-
var `Self.names`{.genSym, compileTime.}: Table[Value, string] ## self._name_
12-
var `Self.member_map`{.genSym, compileTime.}: Table[Str, Self] ## cls._member_map_
18+
var `Self.names`{.compileTime.}: Table[Value, string] ## self._name_
19+
var `Self.member_map`{.compileTime.}: Table[Str, Self] ## cls._member_map_
20+
bind GenPyEnumInit
1321

1422
using self: Self
1523
using cls: typedesc[Self]
@@ -38,6 +46,8 @@ template GenPyEnumMeth*(Self, Value: typedesc; genObjMeth = true, nameError = Na
3846
# XXX: Python here also need to handle property and class attribute
3947
`Self.member_map`[name] = self
4048
proc add_alias*(self; name: Str) = Self.add_member(name, self)
49+
when genInit:
50+
GenPyEnumInit(Self, Value, Self)
4151

4252

4353
# _simple_enum's convert_class's Enum / IntEnum / StrEnum branch

src/pylib/Lib/enum_impl/intEnum.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import ./enumType
44

55

66

7-
template GenIntEnumMeth*(Self: typedesc; Int = int){.dirty.} =
7+
template GenIntEnumMeth*(Self: typedesc; Int = int, genObjMeth = true, genInit = false){.dirty.} =
88
bind GenPyEnumMeth
9-
GenPyEnumMeth(Self, Int)
9+
GenPyEnumMeth(Self, Int, genObjMeth, genInit)
1010
converter toInt*(self: Self): Int = self.Int
1111

12-
template DeclIntEnumMeth*(Self; Int = int){.dirty.} =
12+
template DeclIntEnumMeth*(Self; Int = int, genObjMeth = true, genInit = false){.dirty.} =
1313
bind GenIntEnumMeth
1414
type Self = distinct Int
15-
GenIntEnumMeth(Self, Int)
15+
GenIntEnumMeth(Self, Int, genObjMeth, genInit)

src/pylib/Lib/signal_impl/enums.nim

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import std/tables
33
import ./pylifecycle except SigInfo
44
import ./pynsig
5-
import ../enum_impl/intEnum
5+
import ../enum_impl/[intEnum, enumType]
6+
import ./enums_decl
67

78
# ITIMER* (not enum)
89
template Exp(sym) =
@@ -13,17 +14,20 @@ Exp ITIMER_REAL
1314
Exp ITIMER_VIRTUAL
1415
Exp ITIMER_PROF
1516

17+
template genEnum(name) =
18+
GenIntEnumMeth name
19+
GenPyEnumInit(name, int, name)
20+
1621
# SIG*
17-
DeclIntEnumMeth Signals
18-
export Signals
22+
genEnum Signals
1923

20-
template add_enum[E](sym, val) =
21-
let sym* = E.add_member(astToStr(sym), val)
24+
template add_enum(E, sym, val) =
25+
let sym* = enums_decl.E.add_member(astToStr(sym), val)
2226

2327
template add_sig(sym) =
2428
when declared(sym):
2529
when sym != DEF_SIG:
26-
add_enum[Signals](sym, sym)
30+
add_enum(Signals, sym, sym)
2731

2832
when true: # just convenient for code folding
2933
add_sig CTRL_C_EVENT
@@ -71,26 +75,26 @@ when true: # just convenient for code folding
7175

7276

7377
# SIG_*
74-
DeclIntEnumMeth Handlers
75-
export Handlers
78+
genEnum Handlers
7679

7780
template add_handler(sym) =
7881
when declared(sym):
79-
add_enum[Handlers](sym, cast[int](sym))
82+
add_enum(Handlers, sym, cast[int](sym))
8083

8184
add_handler SIG_DFL
8285
add_handler SIG_IGN
8386

8487

8588
# Sigmasks
86-
DeclIntEnumMeth Sigmasks
87-
export Sigmasks
89+
genEnum Sigmasks
8890

8991
template add_sigmask(sym) =
9092
when declared(sym):
91-
add_enum[Sigmasks](sym, sym)
93+
add_enum(Sigmasks, sym, sym)
9294

9395
when not defined(windows):
9496
add_sigmask SIG_BLOCK
9597
add_sigmask SIG_UNBLOCK
9698
add_sigmask SIG_SETMASK
99+
100+
export Signals, Handlers, Sigmasks
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
template decl(name) =
3+
type name* = distinct int
4+
5+
decl Signals
6+
decl Handlers
7+
decl Sigmasks

0 commit comments

Comments
 (0)