classification
Title: Embedding python into shared library crash on AIX
Type: crash Stage:
Components: Interpreter Core Versions: Python 2.6
process
Status: languishing Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, damahay123, loewis, sable
Priority: normal Keywords:

Created on 2009-08-20 13:32 by damahay123, last changed 2014-03-23 03:12 by BreamoreBoy.

Messages (5)
msg91773 - (view) Author: damahay123 (damahay123) Date: 2009-08-20 13:32
Hi there,
I'm trying to embedding my python code into a .so on AIX and load it
with my main application. Since there is no libpython2.6.so available on
AIX, I have to link my .so with libpython2.6.a. I have to make some
twist to make it compile. And so far so good until I run my main. My
embedding python .so give me error like the following
Fatal Python error: Interpreter not initialized (version mismatch?)
I check the initialization status by calling Py_IsInitialized and it
said yes. So I'm wondering if this embedding python into .so ever work
on AIX.
I have no problem to do the same thing on linux, solaris since python
has libpython2.6.so on both system. But our system needs the supoort on
AIX. My embedding python is very simple like the following

#include <Python.h>
#include <iostream>

extern "C"
void allocate()
{
    std::cout << " am i ok = " << Py_IsInitialized() << std::endl;
    
    Py_InitializeEx(0);
    std::cout << " am i ok 1 = " << Py_IsInitialized() << std::endl;
    
    PyRun_SimpleString("from time import time, datetime, ctime\n"
                       "print 'Today is',ctime(time())\n");
    Py_Finalize();
}

my main application is also very simple
#include <iostream>
#include <iomanip>
#include <dlfcn.h>
//#include <link.h>
#include <Python.h>

typedef  void (*ALLOCATE)();

int main (int argc, char ** argv)
{
    // parse params to load shared object
    if (argc < 2)
    {
	std::cerr << "Usage: " << argv[0] << " sharedObject(s)" << std::endl;
	return 0;
    }

    //    Py_Initialize();
    
    for (int i = 1; i < argc; ++i)
    {
	void * handle = ::dlopen(argv[i], RTLD_LAZY);
	if (!handle)
	{
	    std::cerr << dlerror() << argv[i] << std::endl;
	
	    return 0;
	}
	
	// Use that handle to locate the symbol.  The symbol must be 
	// demangled so it has to be compiled with extern "C".
	ALLOCATE dlmAllocate = (ALLOCATE) ::dlsym(handle, "allocate");
	if (!dlmAllocate)
	{
	    std::cerr << dlerror() << std::endl;
	    return 0;
	}
    
	dlmAllocate();
    }


    return 0;
}

here is my makefile

CXX := g++

default: DLOpenTest mypython.so

DLOpenTest: DLOpen.C
	$(CXX) -o DLOpenTest DLOpen.C -ldl
-Wl,-bE:/mnts/cdstools/Python-2.6.2/aix-ppc-5.3/lib/python2.6/config/python.exp
-lld -I/mnts/cdstools/Python-2.6.2/aix-ppc-5.3/include/python2.6
-L/mnts/cdstools/Python-2.6.2/aix-ppc-5.3/lib/ -lpython2.6 -lpthread

mypython.so: mypython.C
	$(CXX) -shared -nostartfiles
-I/mnts/cdstools/Python-2.6.2/aix-ppc-5.3/include/python2.6
-L/mnts/cdstools/Python-2.6.2/aix-ppc-5.3/lib/ -lpython2.6 -lpthread -o
mypython.so mypython.C


clean:
	rm *.o DLOpenTest mypython.so

Can someone help me out? Or has anyone even tried same thing on AIX?

NOTE, the issue is not like issue 4434 or 1332869. Please don't reply
this issue with those two number. I've seen them already. It's not helpful

Thanks a log
msg91789 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2009-08-20 22:13
If you get differing results from Py_IsInitialized, my guess is that you
managed to link two different copies of the Python VM into your
application, each with its own set of global variables.

An AIX expert would be required to diagnose this in more detail;
unfortunately, we are not aware of any such expert.
msg116025 - (view) Author: Sébastien Sablé (sable) Date: 2010-09-10 16:04
You may want to take a look at issue 941346 in order to compile libpython2.6.so on AIX.

I also embed python in my AIX application, and I had no problem once python was compiled as share thanks to the patch provided in the other issue.
msg192851 - (view) Author: Sébastien Sablé (sable) Date: 2013-07-11 08:53
This issue has been fixed as part of issue 941346 and should be closed in my opinion.
msg214541 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-03-23 03:12
msg192851 states that this should be closed.  If somebody agrees with that statement would they please do the honours.
History
Date User Action Args
2014-03-23 03:12:47BreamoreBoysetnosy: + BreamoreBoy
messages: + msg214541
2013-07-11 08:53:43sablesetmessages: + msg192851
2013-07-10 09:59:06christian.heimessetstatus: open -> languishing
2010-09-10 16:04:20sablesetnosy: + sable
messages: + msg116025
2009-08-20 22:13:33loewissetnosy: + loewis
messages: + msg91789
2009-08-20 13:32:01damahay123create