diff --git a/README.md b/README.md index e097a67..c1ec2aa 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # PyWrap -Python Wrapper to use Python classifiers from C++ \ No newline at end of file +Python Wrapper to use Python classifiers from C++ + +Explain issues with conda and libstdc++ and how to fix them +(moving conda libstdc++ to a different folder) diff --git a/c-api.pdf b/c-api.pdf new file mode 100644 index 0000000..216c6dd Binary files /dev/null and b/c-api.pdf differ diff --git a/src/PyWrap.cc b/src/PyWrap.cc index 2d815b3..5783fb9 100644 --- a/src/PyWrap.cc +++ b/src/PyWrap.cc @@ -1,88 +1,139 @@ #define PY_SSIZE_T_CLEAN #include #include "PyWrap.h" +#include +#include +#include +using namespace std; namespace pywrap { - void PyWrap::doCommand(const std::string& command) + PyWrap::PyWrap(const std::string& moduleName, const std::string& className) { - auto status = initPython(command); + PyStatus status = initPython(); if (PyStatus_Exception(status)) { throw std::runtime_error("Error initializing Python"); } - PyObject* module = PyImport_ImportModule("stree.Stree"); + module = PyImport_ImportModule(moduleName.c_str()); + if (PyErr_Occurred()) { + errorAbort("Could't import module " + moduleName); + } + classObject = PyObject_GetAttrString(module, className.c_str()); + if (PyErr_Occurred()) { + errorAbort("Couldn't find class " + className); + } + instance = PyObject_CallObject(classObject, NULL); + if (PyErr_Occurred()) { + errorAbort("Couldn't create instance of class " + className); + } + } + PyWrap::~PyWrap() + { + Py_DECREF(instance); + Py_DECREF(classObject); + Py_DECREF(module); + Py_Finalize(); + } + void PyWrap::errorAbort(const string& message) + { + cerr << message << endl; + PyErr_Print(); + exit(1); + } + void PyWrap::callMethod(const string& method) + { + PyObject* result; + if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL))) + errorAbort("Couldn't call method " + method); + cout << "Result: " << PyUnicode_AsUTF8(result) << endl; + Py_DECREF(result); + } + void PyWrap::doCommand2() + { + PyObject* list = Py_BuildValue("[s]", "Stree"); + // PyObject* module = PyImport_ImportModuleEx("stree", NULL, NULL, list); + PyObject* module = PyImport_ImportModule("stree"); + if (PyErr_Occurred()) { + PyErr_Print(); + cout << "Fails to obtain the module.\n"; + return; + } + cout << "Antes de empezar" << endl; if (module != nullptr) { - // // dict is a borrowed reference. - // auto pdict = PyModule_GetDict(module); - // if (pdict == nullptr) { - // cout << "Fails to get the dictionary.\n"; + cout << "Lo consiguió!!!" << endl; + // dict is a borrowed reference. + auto pdict = PyModule_GetDict(module); + if (pdict == nullptr) { + cout << "Fails to get the dictionary.\n"; + return; + } + Py_DECREF(module); + PyObject* pKeys = PyDict_Keys(pdict); + PyObject* pValues = PyDict_Values(pdict); + map my_map; + cout << "size: " << PyDict_Size(pdict) << endl; + char* cstr_key = new char[100]; + char* cstr_value = new char[500]; + for (Py_ssize_t i = 0; i < PyDict_Size(pdict); ++i) { + PyArg_Parse(PyList_GetItem(pKeys, i), "s", &cstr_key); + PyArg_Parse(PyList_GetItem(pValues, i), "s", &cstr_value); + //cout << cstr<< " "<< cstr2 < my_map; - // //cout << "size: " << PyDict_Size(pdict)<