void symbol_delete(SYMBOL* symbol) { int i; free(symbol->id); switch (symbol->type) { case t_symbol_var: free_type_decl(symbol->data.var_data.type); break; case t_symbol_class: break; case t_symbol_loop: break; case t_symbol_func: free_type_decl(symbol->data.func_data.type); for (i = 0; i < symbol->data.func_data.nArgs; i++) free_func_def_arg(symbol->data.func_data.args[i]); free(symbol->data.func_data.args); break; case t_symbol_switch: free_type_object(symbol->data.switch_data.type); break; } free(symbol); }
int main(int argc, char *argv[]) { std::cout << "C++: This is process: " << getpid() << std::endl; PyObject *pName, *pModule, *pFunc; //, *pDict, ; PyObject *pValue; // *pArgs, // int i; std::string module("python6"); std::string function("run_now"); // Initilize Python Py_Initialize(); /* NameObjectType.tp_new = PyType_GenericNew; if (PyType_Ready(&NameObjectType) < 0) { PyErr_Print(); fprintf(stderr, "Type not ready - PyType_Ready\n"); return 1; } */ PyTypeObject* type_object = create_type_object(); if (PyType_Ready(type_object) < 0) { PyErr_Print(); fprintf(stderr, "Type not ready - PyType_Ready\n"); free_type_object(type_object); return 1; } PyObject* m = Py_InitModule3("name", NULL, "XXX"); if (!m) { PyErr_Print(); fprintf(stderr, "Failed to load initilize module - Py_InitModule3\n"); free_type_object(type_object); return 1; } /*Py_INCREF(&NameObjectType); PyModule_AddObject(m, "Name", (PyObject *)&NameObjectType);*/ Py_INCREF(type_object); PyModule_AddObject(m, "Name", (PyObject*)type_object); // Load module python2.py pName = PyString_FromString(module.c_str()); // Error checking of pName left out pModule = PyImport_Import(pName); Py_DECREF(pName); if (!pModule) { PyErr_Print(); fprintf(stderr, "Failed to load \"%s\"\n", argv[1]); free_type_object(type_object); return 1; } // Get the python fuction to call pFunc = PyObject_GetAttrString(pModule, function.c_str()); // pFunc is a new reference if (!pFunc || !PyCallable_Check(pFunc)) { if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]); Py_XDECREF(pFunc); Py_DECREF(pModule); free_type_object(type_object); return 1; } // Make the call to the python function pValue = PyObject_CallObject(pFunc, NULL); if (!pValue) { PyErr_Print(); fprintf(stderr,"Call failed\n"); Py_DECREF(pFunc); Py_DECREF(pModule); free_type_object(type_object); return 1; } // Show the result of the python call printf("Result of call: %ld\n", PyInt_AsLong(pValue)); // Decrease counters for python garbage collection Py_DECREF(pValue); Py_XDECREF(pFunc); Py_DECREF(pModule); std::cout << "Before Py_Finalize()" << std::endl; // Clean up python Py_Finalize(); free_type_object(type_object); std::cout << "Freeing the allocated memory" << std::endl; return 0; }