Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calling variable binded functions #259

Open
untoreh opened this issue Mar 15, 2022 · 0 comments
Open

calling variable binded functions #259

untoreh opened this issue Mar 15, 2022 · 0 comments

Comments

@untoreh
Copy link

untoreh commented Mar 15, 2022

python

class Some:
    def thing(what):
        print(what)

nim

when isMainModule:
    let
        m = pyimport("test")
        oname = "Some"
        fname = "thing"
        o = m.getattr(oname)
        f = o.getattr(fname)

    f(what="nice")

Modified dotCall macro to make it work:

macro fnCall(o: untyped, args: varargs[untyped]): untyped =
  let plainArgs = newTree(nnkBracket)
  let kwArgs = newTree(nnkBracket)

  for arg in args:
    # Skip the bogus [] `args` when no argument is passed
    if arg.kind == nnkHiddenStdConv and arg[0].kind == nnkEmpty:
      continue
    elif arg.kind != nnkExprEqExpr:
      plainArgs.add(newCall("toPyObjectArgument", arg))
    else:
      expectKind(arg[0], nnkIdent)
      kwArgs.add(newTree(nnkPar,
        newCall("cstring", newLit($arg[0])),
        newCall("toPyObjectArgument", arg[1])))

  result = newCall(bindSym"newPyObjectConsumingRef",
    newCall(bindSym"callObjectAux", newcall("privateRawPyObj", o), plainArgs, kwArgs))

template `()`*(o: PyObject, args: varargs[untyped]): PyObject =
    fnCall(o, args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant