[C++-sig] Re: [BPL] python::interpreter proposal

Nicodemus nicodemus at globalite.com.br
Fri Jan 3 01:37:45 CET 2003

Bjorn Pettersen wrote:

>>Nicodemus wrote:
>>They're equivalent to the "exec" statement and to the "eval" builtin
>>function in python.
>That's essentially what I ended up with, although I have the
>global/local dict be persistent with the interpreter (i.e. you don't
>pass them in). Making them optional would probably be most

Agreed. You have working code for this already?

>I ended up making exec return an int so that we could do:
>  int foo(int x) {
>      static int ok = py::exec(
>          "def foo(y):\n"
>          "    return y**y\n");
>      return py::extract<int>(py::fn("foo")(x));
>  }
>(i.e. only calling exec once).

You mean exec returning an int to describe success or failure? I would 
prefer an exception in case of an error, and returning void.

>In addition to exec/eval I also have an "execute block" function that
>takes a series of statements, executes them, and returns the value of
>the last statement. It's convenient because we normally want to execute
>a "script" to get a result and the script normally consists of multiple
>lines. I implemented that by extracting the last statement, passing
>everything prior to it to exec, and returning the result of passing the
>last statement to eval. Of course, extracting the last statement is
>non-trivial and my implementation is wrong, although it does work for
>most cases <wink>.

Sorry, but I didn't quite understand, how the script returns this value? 
Could you post a simple example of such a script? Just curious. 8)


More information about the Cplusplus-sig mailing list