embeded python progam into visual C++ application crash

2007-05-29 Thread fabien.lyon
hello,
The C++ application uses a python module which wraps commands set for CVS
management:
checkout, checkin and tag.
We used python2.5.1 and Visual C++ 6.0

The problem we get is:
After a good import and definition of python functions we have a random
unhandled exception (from python25.dll) when calling python interface
function several times.
All the python module has been tested using the python IDLE.


This the C++ sequence code we used:

Py_initialize()
Py_Import("moduleName")
cvs_init()   // cvs view initialisation handled by
python script  init()
cvs_set_tag()// cvs commit and tag handled by python
script settag()
   // the exception occured here


This is the python script part:

def init(pathBench=None):
global CVSROOT
global BENCH_DIR
ret = 0
str = 'CVS initialization: init() OK'

if os.path.isdir(pathBench):
try :
cvsapi.checkout(CVSROOT, 'bench', 
destination=pathBench, recursive=False)
except cvsapi.CheckoutError, error:
ret = -1
str = "CVS initialisation %s: %s, init() KO" % 
(pathBench,error)
else:
ret = -1
str = "CVS initialization: pathBench %s is not a directory, 
init() KO" %
pathBench

return ret,str


def settag(tagName, pathBench):
ret = 0
str = 'CVS commit and tag: Warning no bench, settag()'

try:
pathBench = pathBench.rstrip('\\')
pathTest = pathTest.rstrip('\\')
pathStorage = pathStorage.rstrip('\\')

if not os.path.isdir(pathBench) :
ret = -1
return ret, str

 ret, str = SettagView(tagName, pathBench)

except cvsapi.PathError,error:
ret = -1
str = "settag(): path error %s" % error
except cvsapi.AddError,error:
ret = -1
str = "settag(): cvs add error %s" % error
except:
ret = -1
str = "settag() unknown error"

return ret,str



Any information will be welcome as the scripts are running under IDLE and
not in embeded C++

thanks for your help

Fabien LYON

Service Informatique ISIS
*  [EMAIL PROTECTED]
*   (+33) 05-61-43-59-04
*   (poste interne) 5904
Fax (+33) 05-61-43-58-60
*   6 rue des Frères Boude
ZI Thibaud, BP 70439
31104 Toulouse Cedex 1



-- 
http://mail.python.org/mailman/listinfo/python-list


TR: embeded python progam into visual C++ application crash

2007-05-31 Thread fabien.lyon


-Message d'origine-
De : fabien.lyon [mailto:[EMAIL PROTECTED]
Envoyé : mercredi 30 mai 2007 20:16
À : 'python-list@python.org'
Objet : RE: embeded python progam into visual C++ application crash


> hello,
> The C++ application uses a python module which wraps commands set for CVS
> management:
> checkout, checkin and tag.
> We used python2.5.1 and Visual C++ 6.0

2.5.1 is compiled with Visual Studio 2005 - I hope you had no problems
with VC++ 6.0?

> The problem we get is:
> After a good import and definition of python functions we have a random
> unhandled exception (from python25.dll) when calling python interface
> function several times.
> All the python module has been tested using the python IDLE.
>
>
> This the C++ sequence code we used:
>
> Py_initialize()
> Py_Import("moduleName")
> cvs_init()   // cvs view initialisation handled
> by
> python script  init()
> cvs_set_tag()// cvs commit and tag handled by python
> script settag()
>// the exception occured here

>> Neither Py_initialize nor Py_Import functions exist - so please post
>> actual code.

>> --
>> Gabriel Genellina

Ho sorry you are right (this pb disturbs me too much).
Investigation and efforts has been helpfull, so we have found the problem.
This is the following description :

void InterfaceTestConfigPython_C::InitCVS(CString path_vue_bench_p, CString
path_vue_test_p)
{
PyObject *pArgs= NULL, *pValue= NULL;
PyObject *pPathVueBench= NULL, *pPathVueTest= NULL ;

//python objects for each python function argument
pPathVueBench = PyString_FromString((LPCTSTR)path_vue_bench_p);
pPathVueTest = PyString_FromString((LPCTSTR)path_vue_test_p);

//python object to collect all arguments
pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, pPathVueBench);
PyTuple_SetItem(pArgs, 1, pPathVueTest);

// python function call
pValue = PyObject_CallObject(pFuncInit_m, pArgs);

// clean up memory
Py_DECREF(pArgs);

// process return value
if (pValue != NULL)
{

// clean up memory
Py_DECREF(pValue);
}

// clean up memory
Py_DECREF(pPathVueBench );
Py_DECREF(pPathVueTest);
}

The problem we get come from a wrong code. The last two lines, calling
Py_DECREF, are not mandatory and make the python interpreter in a bad day.
When the InitCVS() function call Py_DECREF(pArgs) the memory allocated for
pPathVueBench and pPathVueTest is free (i guess) because the
PyTuple_SetItem(pArgs, 0, pPathVueBench) steals the reference of the python
object you add into the tuple pArgs like the python documentation tells. So
we have suppressed the last clean up memory
in the InitCVS() function and now the program is running as expected.

I join the C++ object code if this can help someone.





InterfaceTestConfigPython_C.cpp
Description: Binary data
-- 
http://mail.python.org/mailman/listinfo/python-list