On Mon, 28 Jun 2004 15:56:13 -0500, Chad Austin <caustin@gmail.com> wrote:
On Mon, 28 Jun 2004 17:48:59 +0000 (UTC), Faheem Mitha <faheem@email.unc.edu> wrote:
On Sun, 27 Jun 2004 23:46:06 -0500, Chad Austin <caustin@gmail.com> wrote:
Nope, sorry... I had a problem similar to that in that __builtins__ wasn't getting set in my modules, but I was doing some weird stuff. You may want to at least check that __builtins__ is set in your script.
How should I do that? I thought that handle<> main_module(borrowed( PyImport_AddModule("__main__") )); handle<> main_namespace(borrowed( PyModule_GetDict(main_module.get()))); should do the trick, though I don't really understand what these lines do. I wonder if the fact that I am calling the interpreter inside an extension module is causing any problems.
If you know of other places the tutorial needs to be changed, I would appreciate being informed of them.
I think it might be useful if I said explicitly what it is I am trying to do here, and ask for suggestions.
I'm writing an extension module, and want to call a python function, namely array from the numarray.strings module. This is part of the Python Numarray library. The question is how to do this.
Something like:
object m = object(handle<>( PyImport_Import("numarray.strings") )); object result = m.attr("array")(arguments);
should work for you.
Ok. Thanks.
I have tried doing this with callbacks and it seems to work fine. The trouble with callbacks is that it means I have to have a python wrapper with an argument slot for every function I want to import. This is clunky.
[stuff deleted...]
I'm totally confused by everything you just said about callbacks, but you should be able to easily call back into Python from C++ using Boost.Python's object (and related subclasses) or even the native Python/C API. I don't see a reason importing another native module from a native module would cause a problem, as long as you write your C++ code in the same way you'd write the Python.
Sorry I was unclear. I'd be willing to try to clarify if that would be useful. I can certainly make callbacks work. The point is I have to pass the relevant functions from Python, which imposes some overhead, which I'd like to get rid of. Faheem.