Home > other >  [for] C call python script function abnormal
[for] C call python script function abnormal

Time:05-27

To implement a function, according to the input parameters, each call to the python script execution function, can appear find PyObject_CallObject or Py_Finalize coredump,
 
Int search (PyObject * pDict, char * line)
{
Int I=0, j=0, l_size=0;
char *str;
PRetVal pArgs PyObject * pFunc, * and * and * item;

Search_str pFunc=PyDict_GetItemString (pDict, "");
if ( ! PFunc | |! PyCallable_Check (pFunc)) {
Printf (" Call python function failed \ n ");
return -1;
}
PArgs=PyTuple_New (2);
PyTuple_SetItem (pArgs, 0, Py_BuildValue (" s ", "name"));
PyTuple_SetItem (pArgs, 1, Py_BuildValue (" s ", line));
PRetVal=PyObject_CallObject (pFunc pArgs);
If (PyList_Check (pRetVal)) {
L_size=PyList_Size (pRetVal);
For (j=0; j The item=PyList_GetItem (pRetVal, j);
PyArg_Parse (item, "s", & amp; STR);
Py_XDECREF (item);
}
}
Py_XDECREF (pRetVal);
Py_XDECREF (pArgs);
Py_XDECREF (pFunc);
return 0;
}

Int main (int arg c, char * * argv)
{
int i;
PModule PyObject * and * pDict;

Py_Initialize ();
if ( ! Py_IsInitialized ()) {
Printf (" Init python failed. \ n ");
return -1;
}
PyRun_SimpleString (" import sys ");
PyRun_SimpleString (" sys. Path. Append ('/') ");
PModule=PyImport_Import (PyUnicode_FromString (" search "));/* import search. Py */
if ( ! PModule) {
Printf (" Can 't find the search. Py. \ n ");
return -1;
}
PDict=PyModule_GetDict (pModule);
if ( ! PDict) {
Printf (" Getdict form python failed. \ n ");
return -1;
}

For (I=1; i Search (pDict, argv [I]);
}
Py_XDECREF (pDict);
Py_XDECREF (pModule);
Py_Finalize ();
return 0;
}


Python scripts (search. Py)
 
#! The/usr/bin/python3
The from influxdb_client import InfluxDBClient, Point
The from influxdb_client. Client. Write_api import SYNCHRONOUS

The bucket="XXX"
Token="XXX"
Org="XXX"

Client=InfluxDBClient [url="http://localhost:8086", token=token, org=org)
Write_api=client. Write_api (write_options=SYNCHRONOUS)
Query_api=client. Query_api ()

Def search (db, dest_ip) :
Result_list=[]
Result_list. Append (" str1 ")
Result_list. Append (" str2 ")
Return result_list


Program is a single thread to run, no multithreading implementation;
Can run normal for the first time, after the second string in the input, coredump happened, the stack is as follows:
[Thread was debugging using libthread_db enabled]
Using the host libthread_db library "- the gnu/Linux/lib/x86_64 - libthread_db. So. 1".
The Core was generated by `./log_analytics'.
The Program terminated with signal SIGSEGV, Segmentation fault.
# 0 0 x000055f8d71ac050 in search (x7fffd2c0e6d0 pDict=0 x7fb70bfb1480, line=0 "2.2.2.2") at log_analytics. C: 31
31 if (PyList_Check (pRetVal)) {
(GDB) p pRetVal
$1=0 x0 (PyObject *)
(GDB)

What reason is this? Code looks as if he didn't see any problem which release Object appear problem is suspected,
If you comment out the search function in the last few Py_XDECREF, following
Py_XDECREF (pRetVal);
Py_XDECREF (pArgs);
Py_XDECREF (pFunc);
Program is invoked after the exit, Py_Finalize () can also occur when coredump, stack is as follows:
(GDB) bt full
# 0 0 x0000000000000002 in?? ()
No symbol table info available.
# 1 0 x00007f121e81ebff in?? () the from/usr/lib/x86_64 - - the gnu/Linux libpython3.6 Margaret spellings o. 1.0
No symbol table info available.
# 2 0 x00007f121e91f899 in?? () the from/usr/lib/x86_64 - - the gnu/Linux libpython3.6 Margaret spellings o. 1.0
No symbol table info available.
# 3 0 x00007f121e8220b5 in?? () the from/usr/lib/x86_64 - - the gnu/Linux libpython3.6 Margaret spellings o. 1.0
No symbol table info available.
# 4 0 x00007f121e82306d PyGC_Collect in () the from/usr/lib/x86_64 - - the gnu/Linux libpython3.6 Margaret spellings o. 1.0
No symbol table info available.
# 5 0 x00007f121e84b763 Py_FinalizeEx in () the from/usr/lib/x86_64 - - the gnu/Linux libpython3.6 Margaret spellings o. 1.0
No symbol table info available.
# 6 0 x0000556a6a0b7341 in the main (arg c=7, argv=0 x7ffc41e00588) at log_analytics. C: 116
I=7
PModule=0 x7f121ca5fe58
PDict=0 x7f121cad9480
(GDB)

Here also don't see why coredump?
Py_XDECREF this is minus object references and release, if there is no call there will be a memory leak? (see the python website shows there are object references) required to reduce
If you don't call, how will be the core out Py_Finalize finally liberated the python interpreter?

Help you look great god, is there any wrong place, trouble told me,
  • Related