Problem with embedded python
frederic cabaud
fcabaud at free.fr
Tue Jan 7 05:12:53 EST 2003
Hello,
I am working with embedded python from C++Builder and with the following
code I "break the memory" (read at FFFFFFFF adress) when, I suppose, the
embedded python module use too much memory.
-> WITH A SMALL PYTHON MODULE: IT WOKS.
Are there some (unknown) limitations ?
This code is called from C++. A python module must (and it does : I checked
it) return a tupple composed of 1 string and 4 lists.
Normally I use correctly Py_DECREF().
For example, after each PyTuple_GetItem(rslt, Cpt);
I call Py_DECREF(ItemTuple);
// Freeing item tuple
I tried with python22 and also with the last one 23.
Regards
AnsiString __fastcall TDB_Tree::PythonExecute(
// Data to access with python
AnsiString Module, AnsiString Method, AnsiString Argument,
TTreeNode *Node,
// Pointer for data : not at the same place in display
// Can be pTableFile or pTableDir
TTable *pParamTable)
{
AnsiString SmartScapeReturn = "OK";
char *Ptr;
int Len;
int TupleSize, ListSize;
unsigned int Cpt, Cpt2;
long answer;
PyObject *modname, *mod, *mdict, *func, *stringarg, *args, *rslt;
PyObject *ItemTuple, *ItemList;
TTreeNode *NewNode;
try
{
modname = PyString_FromString(Module.c_str());
mod = PyImport_Import(modname);
if (mod) {
mdict = PyModule_GetDict(mod);
func = PyDict_GetItemString(mdict, Method.c_str()); /* borrowed
reference */
if (func)
{
if (PyCallable_Check(func)) {
if (strlen (Argument.c_str()) == 0) // For example : identification
{
rslt = PyObject_CallObject(func, NULL);
}
else
{
stringarg = PyString_FromString(Argument.c_str());
args = PyTuple_New(1);
PyTuple_SetItem(args, 0, stringarg);
rslt = PyObject_CallObject(func, args);
}
if (rslt) {
//answer = PyInt_AsLong(rslt);
//PyString_AsStringAndSize(rslt, &Ptr, &Len);
if (PyTuple_Check(rslt) == true)
{
TupleSize = PyTuple_Size(rslt);
if (TupleSize != 5)
{
Cpt = 65535;// Break the loop
SmartScapeReturn = "Bad Tuple size";
}
else
{
for (Cpt = 0 ; Cpt < TupleSize ; Cpt++)
{
ItemTuple = PyTuple_GetItem(rslt, Cpt);
switch (Cpt)
{
// R E S U L T
case SSCAPE_RESULT:
{
// String
PyString_AsStringAndSize(ItemTuple, &Ptr, &Len);
SmartScapeReturn = AnsiString(Ptr);
if (SmartScapeReturn == "OK")
{
if (strcmp(Method.c_str(), "identification") == 0)
{
TTreeNode *pFirst;
pFirst =
TreeView1->Items->AddFirst(pRoot,CurrentModule);
// This child is only created for the node apearance
('+' picture )
TreeView1->Items->AddChild(pFirst,"DummyForTreeCreation");
Node = pFirst; // For the dir management a few lines
furhter
// way to avoid to go to "Expanding"
ManagesExpand = false;
Node->Expand(false);
ManagesExpand = true;
}
}
else
{
Cpt = 65535;// Break the loop
}
}
break;
// D A T A
case SSCAPE_DATA_NAMES:
case SSCAPE_DATA_VALUES:
{
ListSize = PyList_Size(ItemTuple);
if (ListSize < 0)
{
Cpt = 65535;// Break the loop
SmartScapeReturn = "Negative size for list Size of dir";
}
else
{
if (ListSize != 0)
{
// pParamTable Can be pTableFile or pTableDir
// Empty for the first time
if (Cpt == SSCAPE_DATA_NAMES)
{
pParamTable->Active=false;
try
{
pParamTable->EmptyTable();
}
catch(EDatabaseError &e)
{
}
pParamTable->Active=true;
for (Cpt2 = 0 ; Cpt2 < ListSize ; Cpt2++)
{
// Create the data
pParamTable->Insert();
pParamTable->FieldByName("Name")->AsString = "Create
new line";
pParamTable->Post();
}
}
// Go to the begin of the database
pParamTable->First();
// List
// name and values
for (Cpt2 = 0 ; Cpt2 < ListSize ; Cpt2++)
{
// Get from python
ItemList = PyList_GetItem(ItemTuple, Cpt2); /* Can't
fail */
PyString_AsStringAndSize(ItemList, &Ptr, &Len);
if (ItemList == NULL)
{
Cpt = Cpt2 = 65535;// Break the loop
SmartScapeReturn = "Problem in FILES List
extraction";
}
else
{
pParamTable->Edit();
// Add in display
if (Cpt == SSCAPE_DATA_NAMES)
{
pParamTable->FieldByName("Name")->AsString =
AnsiString(Ptr);
}
else
{
pParamTable->FieldByName("Data")->AsString =
AnsiString(Ptr);
}
pParamTable->Post();
pParamTable->Next();
}
}
// Go to the begin of the database (for display)
pParamTable->First();
}
}
}
break;
// F I L E S
case SSCAPE_FILES:
{
// List
// Name, modules, method, parameter
ListSize = PyList_Size(ItemTuple);
if (ListSize < 0)
{
Cpt = 65535;// Break the loop
SmartScapeReturn = "Negative size for list Size of dir";
}
else
{
if (ListSize > 0)
{
ListBox1->Items->Clear();
}
for (Cpt2 = 0 ; Cpt2 < ListSize ; Cpt2++)
{
// Get from python
ItemList = PyList_GetItem(ItemTuple, Cpt2); /* Can't
fail */
if (ItemList == NULL)
{
Cpt = Cpt2 = 65535;// Break the loop
SmartScapeReturn = "Problem in FILES List
extraction";
}
else
{
PyString_AsStringAndSize(ItemList, &Ptr, &Len);
// Add in display
ListBox1->Items->Add(AnsiString(Ptr));
}
}// for()
}
}
break;
// D I R
case SSCAPE_DIR:
{
// List
// Name, modules, method parameter
ListSize = PyList_Size(ItemTuple);
if (ListSize < 0)
{
Cpt = 65535;// Break the loop
SmartScapeReturn = "Negative size for list Size of dir";
}
else
{
if (ListSize > 0)
{
Node->Item[0]->Delete(); // DummyForTreeCreation is
deleted
}
for (Cpt2 = 0 ; Cpt2 < ListSize ; Cpt2++)
{
// Get from python
ItemList = PyList_GetItem(ItemTuple, Cpt2); /* Can't
fail */
if (ItemList == NULL)
{
Cpt = Cpt2 = 65535;// Break the loop
SmartScapeReturn = "Problem in DIR List extraction";
}
else
{
PyString_AsStringAndSize(ItemList, &Ptr, &Len);
// Add in display
NewNode =
TreeView1->Items->AddChild(Node,AnsiString(Ptr));
// This child is only created for the node apearance
('+' picture )
TreeView1->Items->AddChild(NewNode,"DummyForTreeCreation");
}
}// for()
}
}
break;
}
Py_DECREF(ItemTuple); // Freeing item tuple
}// for()
}
}
else
{
SmartScapeReturn = "Bad check of the tupple";
}
Py_XDECREF(rslt);
}
else
{
SmartScapeReturn = "Bad func call in module " +Module + " with
method " + Method +" with arguments " + Argument;
}
if (Argument != "")
{
Py_XDECREF(stringarg);
Py_XDECREF(args);
}
}
else
{
SmartScapeReturn = "Bad func call in module " +Module + " with
method " + Method;
}
}
else
{
SmartScapeReturn = "Bad function in module "+Module;
}
Py_XDECREF(mod);
}
else
{
SmartScapeReturn = "Bad module "+ Module;
}
Py_XDECREF(modname);
}
catch(...)
{
SmartScapeReturn = "Problem with python access";
}
return(SmartScapeReturn);
}
More information about the Python-list
mailing list