Using SWIG to build Python extensions -- help!

Carl Bevil carl_bevil at yahoo.com
Fri Jan 24 18:57:23 EST 2003


I've gotten a bit further on this.  I've added an entry called "swigtest" into 
my _PyImport_Inittab table, and I've traced in and it appears as if my init 
function is getting called.

But now I get a message:

Traceback (most recent call last):
  File "<string>", line 1, in ?
SystemError: _PyImport_FixupExtension: module swigtest not loaded

Tracing into the interpreter I'm not sure exactly what is going on (not being 
familiar with how the interpreter works).  But it appears that the problem 
occurs here:

PyObject *
_PyImport_FixupExtension(char *name, char *filename)
{
	PyObject *modules, *mod, *dict, *copy;
	if (extensions == NULL) {
		extensions = PyDict_New();
		if (extensions == NULL)
			return NULL;
	}
	modules = PyImport_GetModuleDict();
	mod = PyDict_GetItemString(modules, name);
[...]

..at the last line above.  Both name and filename are set to the same address 
(which is the string "swigtest").  mod is NULL after this statement's execution.

I've been searching the web for information, but to me it looks like I'm doing 
everything correct.  Anyone out there have any ideas or suggestions?

Thanks!

Carl

On Fri, 24 Jan 2003 12:19:59 -0800 Carl Bevil <carl_bevil at yahoo.com> wrote:

> Hello all.  I'm experimenting with using SWIG to build a Python extension.  
I 
> have a really simple C++ file that I'd like to extend Python with as a test.
> 
> I'm building the interpreter as a library that I statically link into my 
main 
> application.  Dynamic linking is not an option for my target platform, so 
I've 
> downloaded the Python source code.  I'm using Python 2.1.3.
> 
> I create two files, swigtest.cpp and swigtest.h.  swigtest.h has a function 
> prototype, and swigtest.cpp has the function body.
> 
> Then I run SWIG:
> 
> swig -python -c++ -module swigtest swigtest.h
> 
> This builds two files: swigtest.py and swigtest_wrap.cxx.  I rename the 
latter 
> to swigtest.cpp, as the compiler I'm using (Metrowerks) doesn't like .cxx 
files.
> 
> Then I compile and link swigtest_wrap.cpp and swigtest.cpp into the python 
> interpreter, creating a python21_d.lib file (I'm on Windows).
> 
> Then I build my main application (a C++ app), and link it statically with 
the 
> python interpreter library that I just built.
> 
> When I run, I execute the following statement:
> 
> PyRun_SimpleString("import swigtest");
> 
> ... and I get an error that there is no module named swigtest.
> 
> What am I doing wrong here?  And do I need to do anything with the 
swigtest.py 
> file that SWIG creates? I've read the chapter in O'Reilly's Programming 
Python 
> on extending Python, but it makes no mention of SWIG generating a .py file.  
> And all of its examples are dynamically linked.
> 
> Thanks!
> 
> Carl
> 




More information about the Python-list mailing list