Package: python-mysqldb
Version: 1.2.2-7

On Debian GNU/Linux 5.0 (lenny). Python 2.5.2-3.

When python is embedded in a shared library (in my case a plugin), the
_mysql.so component of MySQLdb fails to load, due to this error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: /var/lib/python-support/python2.5/_mysql.so: undefined
symbol: PyExc_ImportError

Discovered while trying to use the ida2sql.py script within IDAPython.

A simple example program is attached demonstrating the problem clearly,
this sample program also demonstrates a work-around, by setting the
RTLD_GLOBAL flag when loading the shared library, the necessary python
symbols are exported for _mysql.so and it loads correctly. Unfortunately
this isn't possible in my situation since IDA Pro is not open source,
and in general I think it's unwise for an application to export all the
symbols from any plugins it may load as there is a high probability of a
symbol collision.

I have also been able to work around this issue by re-linking _mysql.so
and including a direct dependancy on libpython2.5.so.1.0. This may not
be the right thing to do when using MySQLdb from the standard python
interpreter however?

#include <stdio.h>
#include <dlfcn.h>

int main(int argc) {
	int flags = (argc > 1) ? RTLD_LAZY | RTLD_GLOBAL : RTLD_LAZY;
	void *h = dlopen("./pysql.so", flags);
	if (h) {
		void *s = dlsym(h, "pysql");
		if (s) {
			((void (*)())s)();
		} else {
			fprintf(stderr, "Unresolved: pysql\n");
		}
	} else {
		perror("loading pysql.so");
	}
	return 0;
}
#include <Python.h>

void pysql() {
	Py_Initialize();
	PyRun_SimpleString("import _mysql\nprint 'Got here'");
	Py_Finalize();
}
pymain: pymain.c pysql.so
	gcc -o pymain pymain.c -ldl

pysql.so: pysql.c
	gcc -shared -o pysql.so -I/usr/include/python2.5 pysql.c -lpython2.5

Reply via email to