[IPython-dev] Different behaviour: python vs. ipython when using a C module
Stefan Reichör
stefan at xsteve.at
Mon Oct 16 03:05:30 CDT 2006
"Fernando Perez" <fperez.net at gmail.com> writes:
> On 10/16/06, Stefan Reichör <stefan at xsteve.at> wrote:
>> Hi!
>>
>> I use python in C/C++ Applications for scripting.
>>
>> I have implemented a C/C++ library that is used in that application.
>> To test that library, I use python (preferred ipython).
>> That works very well.
>>
>> Now I have added an embedded python interpreter to the C/C++ library.
>> That interpreter makes the library very easy customizable.
>>
>> But with this setup I discovered a different behaviour when I compare
>> python with ipython. The python version works like I expect. When I
>> use ipython, it does not work.
>>
>> Now I'd like to find out, if this is a bug in ipython, that can be
>> fixed, or if I do something wrong.
>
> Well, I'm not really sure: you are doing something a bit odd, and I
> honestly don't know if it's correct or if you're getting away with
> murder by accident.
Let me try to describe my problem and the solution I use at the moment
a bit mor in detail.
I extend the C library via an embedded python interpreter. This
interpreter can access different parts of the library. So I thought it
would be nice to provide this distinct parts as different modules. So
in my application there are several embedded modules.
After I have imported the embedmodule, I load a user configuration
script that has access to the embedmodule.
If there is a different way to achieve this goal, I will happily use
that.
> Your enclosing init function does this:
>
> void inittestmodule() {
> Py_Initialize();
> Py_InitModule("testmodule", testmoduleAccessMethods);
>
> Py_InitModule("embedmodule",embedmoduleAccessMethods);
> PyImport_ImportModule("embedmodule");
>
> printf("Before import embedmodule\n");
> PyRun_SimpleString("import embedmodule");
> printf("After import embedmodule\n");
>
> PyRun_SimpleString("embedmodule.func2()");
> }
>
> It initializes a second internal module, 'embedmodule', inside itself.
> I don't have the foggiest idea whether this trick of creating a
> module within another is kosher at the C-API level (I know it can be
> done in pure python using the 'new' module).
>
> I realize it works in pure python, so I'd be inclined to say it's an
> ipython bug: ipython should always let you do things that are valid
> python. But I honestly don't know what the solution is here, nor even
> if it's not a fluke that it works for the pure python case.
>
> I'll be glad to fix this one if we could understand better the
> problem, so if anyone knows what's going on, by all means pitch in.
I don't know anything about ther ipython internals. But the error
message suggests, that the import functionality is not available
for C extensions, when using ipython.
The following function does not work also:
void inittestmodule() {
Py_Initialize();
Py_InitModule("testmodule", testmoduleAccessMethods);
PyRun_SimpleString("import os");
}
Stefan.
More information about the IPython-dev
mailing list