[C++-sig] how t o specify and/or update global dictionary for bp::call call
Gennadiy Rozental
rogeeff at gmail.com
Thu Sep 4 18:38:05 CEST 2008
Stefan Seefeld <seefeld <at> sympatico.ca> writes:
> For avoidance of doubt: you are saying the import itself was successful,
> but there was no 'foo' symbol in the global namespace after that point ?
Yes. That's true
> I just modified the exec.cpp test (libs/python/tests/exec.cpp), and it
> all works like a charm.
> (For reference: go to libs/python/tests/exec.cpp and modify the
> 'exec_test' function to simulate what you want. Here is the modified
> script I use:
>
> python::object main = python::import("__main__");
> python::object global(main.attr("__dict__"));
> python::object result = python::exec(
> "from embedded_hello import * \n"
> "from sys import * \n"
> //<-- added just to show how to populate the global namespace of the script
> "class PythonDerived(Base): \n"
> " def hello(self): \n"
> " print platform \n"
> //<-- use it
> " return 'Hello from Python!' \n",
> global, global);
This only works because you used global dictionary as local. I can't do this for
various reasons, main of which is that I am trying to mimic namespacing, which
is unavailable directly in python. So the local dictionary is the dictionary of
the module I am trying to put this class in.
> python::object PythonDerived = global["PythonDerived"];
> python::object py_base = PythonDerived();
> Base& py = python::extract<Base&>(py_base);
> assert(py.hello() == "Hello from Python!");
So what I need to update global dictionary before hello call here even if it was
not used as local during exec call.
Gennadiy
More information about the Cplusplus-sig
mailing list