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

David Abrahams dave at boost-consulting.com
Fri Jan 3 21:34:56 CET 2003


"Bjorn Pettersen" <BPettersen at NAREX.com> writes:

>> From: Dirk Gerrits [mailto:dirk at gerrits.homeip.net] 
>> 
>> Nicodemus wrote:
> [...]
>> > I think two separate functions would be better.
>> > 
>> > void interpreter::exec( const std::string& code, dict& globals,
> dict& locals );
>> > - Executes the given code in the context of the two given
> dictionaries.
>> > 
>> > object interpreter::eval( const std::string& expr, dict& globals,
> dict& locals );
>> > - Evaluates the Python expression given, in the context of the two 
>> > dictionaries, and returns the resulting object.
>> > 
>> > They're equivalent to the "exec" statement and to the "eval" builtin
>> > function in python.
>> 
>> I like that. Let's hear what others think about it.
>
> 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
> convenient/flexible.
>
> 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).

Looks to me like that int should be an exception instead.


-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list