From a241a6424a0aeaf3728954f18d4d9f9a09af9821 Mon Sep 17 00:00:00 2001 From: litlighilit Date: Sun, 19 Jan 2025 11:20:39 +0800 Subject: [PATCH] fixup,break(has/getattr): allow PyStr argtype to fix "compile failed" --- src/pylib/builtins/attr.nim | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/pylib/builtins/attr.nim b/src/pylib/builtins/attr.nim index 42d28d59c..f6ee1b368 100644 --- a/src/pylib/builtins/attr.nim +++ b/src/pylib/builtins/attr.nim @@ -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