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:
Hi Faheem,
Hi Chad,
Thanks for replying.
I ran into that too. In short, the line with "handle<>( PyRun_String" isn't being understood how you want it too... You can surround that expression with parenthesis, and it should work:
(handle<>( PyRun_String("hello = file('hello.txt', 'w')\n" "hello.write('Hello world!')\n" "hello.close()", Py_file_input, main_namespace.get(), main_namespace.get()) ));
Thanks. Indeed it does. I hope the tutorial can be changed appropriately, unless it already has been, of course. Any idea why this is necessary? It isn't in the other two places <handle> is used.
I think it's something to do with C++ thinking you're trying to declare a function or function pointer or something. That's the best I could come up with... Any insight from others would be appreciated. :)
I can load my module, but when I try to run the function, I get
NameError: name 'file' is not defined
Any idea about this?
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.
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.
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.
Suppose I have three functions, say f1, f2, f3.
Then I have f1 calling f2 calling f3.
f1 -> f2 -> f3
Suppose f3 calls some python function (say `foo') from an external module or something.
Then if I rewrite f3 using Boost.Python, I still need a wrapper, say f1', to pass `foo' as an argument. Then I need
f1 -> f2 -> f3' -> f3
I suppose I don't need `foo' passed down from f1 because if I import foo into global namespace into the same module where I define f1, f2, f3', then Python's scoping rules will ensure that if I use `foo' as an argument to f3' then it will be picked up from the global namespace.
Furthermore, if I decide to rewrite f2 into Boost.Python, then I would have to create another Python wrapper f2' for f2, and pass f3' as an argument to f2' (ugh). So I'd get the following
f1 -> f2' -> f2 -> f3' -> f3
I'd rather have
f1 -> f2 -> f3
where f2 and f3 only exist as C++ functions. Much cleaner.
So I thought that perhaps I could imbed the interpreter inside (say) f3, and persuade it to make `foo' available to me inside C++. I don't know if this is even possible. I have seen no examples of this. In fact, I have seen no examples of using the python interpeter inside extension modules. In fact, I've been rather unsuccessful so far in getting anything related to embedding to work.
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.
Am I missing something? Thanks in advance for any information.
Hope that helps, -- Chad Austin http://aegisknight.org/