Skip to content

Commit

Permalink
fixup,break(has/getattr): allow PyStr argtype to fix "compile failed"
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed Jan 19, 2025
1 parent 9823443 commit a241a64
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/pylib/builtins/attr.nim
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@

import std/macros
import ../pystring/strimpl

proc newDotExpr(x: NimNode, attr: string): NimNode =
newDotExpr(x, ident attr)

macro hasattr*(x; attr: static[string]): bool =
let dot = newDotExpr(x, attr)
newCall("compiles", dot)

macro getattr*(x; attr: static[string]): untyped =
newDotExpr(x, attr)

macro getattr*(x; attr: static[string], default): untyped =
let val = newDotExpr(x, attr)
let attrS = newLit attr
result = quote do:
when not hasattr(`x`, `attrS`): `default`
else: `val`

macro setattr*(x; attr: static[string], val) =
newAssignment(newDotExpr(x, attr), val)
template gen(S){.dirty.} =
macro hasattr*(x; attr: static[S]): bool =
let dot = newDotExpr(x, attr)
newCall("compiles", dot)

macro getattr*(x; attr: static[S]): untyped =
newDotExpr(x, attr)

macro getattr*(x; attr: static[S], default): untyped =
let val = newDotExpr(x, attr)
let attrS = newLit attr
result = quote do:
when not hasattr(`x`, `attrS`): `default`
else: `val`

macro setattr*(x; attr: static[S], val) =
newAssignment(newDotExpr(x, attr), val)

gen string
gen PyStr

0 comments on commit a241a64

Please sign in to comment.