[IPython-dev] Different behaviour: python vs. ipython when using a C module

Stefan Reichör stefan at xsteve.at
Mon Oct 16 03:13:59 EDT 2006


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.

I have stripped down my project to a minimal example that fails.

I create a C extension called testmodule. This extension initializes
an additional module: the embedmodule. Importing embedmodule does work
in python, but it does not work in iypthon!

PyObject *test_func1(PyObject *self, PyObject *args)
{
  printf("testmodule.func1 called :-)\n");
  return Py_None;
}

static PyMethodDef testmoduleAccessMethods[] = {
  {"func1",           test_func1, METH_VARARGS},
  {NULL,              NULL}        /* Sentinel */
};


PyObject *embed_func2(PyObject *self, PyObject *args)
{
  printf("embedmodule.func2 called :-)\n");
  return Py_None;
}

static PyMethodDef embedmoduleAccessMethods[] = {
  {"func2",           embed_func2, METH_VARARGS},
  {NULL,              NULL}        /* Sentinel */
};

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()");
}

When I run the example via python, I can do the following:
% python -i test-embed.py                                                                                                                                            9:05
Before import embedmodule
After import embedmodule
embedmodule.func2 called :-)
>>> dir()
['__builtins__', '__doc__', '__file__', '__name__', 'embedmodule', 'testmodule']
>>> dir(embedmodule)
['__doc__', '__name__', 'func2']
>>> dir(testmodule)
['__doc__', '__file__', '__name__', 'func1']



The following happens, when I use ipython:
% ipython test-embed.py                                                                                                                                              9:09
Before import embedmodule
---------------------------------------------------------------------------
exceptions.ImportError                               Traceback (most recent call last)

/home/srei/work/embed-python-test/<string> 

ImportError: __import__ not found
After import embedmodule
---------------------------------------------------------------------------
exceptions.NameError                                 Traceback (most recent call last)

/home/srei/work/embed-python-test/<string> 

NameError: name 'embedmodule' is not defined
Python 2.4.1 (#1, Apr  1 2005, 11:41:56) 
Type "copyright", "credits" or "license" for more information.

IPython 0.7.3.svn -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: dir()
Out[1]: 
['In',
 'Out',
 '_',
 '__',
 '__IP',
 '___',
 '__builtins__',
 '__file__',
 '__name__',
 '__nonzero__',
 '_dh',
 '_i',
 '_i1',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 'help',
 'testmodule']


Any ideas, why the call to PyRun_SimpleString("import embedmodule");
does not work?

Stefan.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: testmodule.c
Type: application/octet-stream
Size: 1021 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20061016/70a37cee/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SConstruct
Type: application/octet-stream
Size: 382 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20061016/70a37cee/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SConscript
Type: application/octet-stream
Size: 120 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20061016/70a37cee/attachment-0002.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test-embed.py
Type: application/octet-stream
Size: 91 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20061016/70a37cee/attachment-0003.obj>


More information about the IPython-dev mailing list