Python included v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 13 Dec 2018 19:12:38 +0100
branchv_0
changeset 10 2c8f5e05c7b7
parent 9 ed7ab0a06849
child 11 5e0b317f4100
Python included
asan-ignore.txt
nbproject/configurations.xml
src/CMakeLists.txt
src/PythonHandler.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/asan-ignore.txt	Thu Dec 13 19:12:38 2018 +0100
@@ -0,0 +1,12 @@
+# There migth be memory leaks ouside our program in shared libraries.
+# We want to ignore them and focus on leaks in our code.
+
+# To use this ignore list, run with:
+# LSAN_OPTIONS=suppressions=asan-ignore.txt
+# In Netbeans it should be set in the project properties / Run / Environment
+
+# Would ignore all memory leaks:
+# leak:.*
+
+# Ignore particular libraries:
+leak:/usr/lib/x86_64-linux-gnu/libpython3.6m.so
--- a/nbproject/configurations.xml	Thu Dec 13 18:17:36 2018 +0100
+++ b/nbproject/configurations.xml	Thu Dec 13 19:12:38 2018 +0100
@@ -51,7 +51,6 @@
                    kind="IMPORTANT_FILES_FOLDER">
       <itemPath>CMakeLists.txt</itemPath>
       <itemPath>build/Debug/Makefile</itemPath>
-      <itemPath>build/Release/Makefile</itemPath>
     </logicalFolder>
   </logicalFolder>
   <sourceFolderFilter>^(nbproject|build)$</sourceFolderFilter>
@@ -82,6 +81,8 @@
               <pElem>../relpipe-lib-reader.cpp/include</pElem>
               <pElem>../relpipe-lib-writer.cpp/include</pElem>
               <pElem>../relpipe-lib-cli.cpp/include</pElem>
+              <pElem>/usr/include/python3.6m</pElem>
+              <pElem>/usr/include/x86_64-linux-gnu/python3.6m</pElem>
               <pElem>build/Debug/src</pElem>
             </incDir>
           </ccTool>
--- a/src/CMakeLists.txt	Thu Dec 13 18:17:36 2018 +0100
+++ b/src/CMakeLists.txt	Thu Dec 13 19:12:38 2018 +0100
@@ -18,7 +18,7 @@
 
 # Relpipe libraries:
 INCLUDE(FindPkgConfig)
-pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-writer.cpp relpipe-lib-cli.cpp)
+pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-writer.cpp relpipe-lib-cli.cpp python-3.6)
 include_directories(${RELPIPE_LIBS_INCLUDE_DIRS})
 link_directories(${RELPIPE_LIBS_LIBRARY_DIRS})
 
--- a/src/PythonHandler.h	Thu Dec 13 18:17:36 2018 +0100
+++ b/src/PythonHandler.h	Thu Dec 13 19:12:38 2018 +0100
@@ -26,6 +26,8 @@
 #include <codecvt>
 #include <regex>
 
+#include <Python.h>
+
 #include <relpipe/reader/typedefs.h>
 #include <relpipe/reader/TypeId.h>
 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
@@ -48,6 +50,8 @@
 private:
 	shared_ptr<writer::RelationalWriter> relationalWriter;
 
+	wchar_t* pythonProgramName;
+
 	wregex relationNameRegEx;
 	wregex attributeNameRegEx;
 	wregex searchRegEx;
@@ -63,15 +67,27 @@
 	GrepHandler(ostream& output, const vector<string_t>& arguments) {
 		relationalWriter.reset(writer::Factory::create(output));
 
+		pythonProgramName = Py_DecodeLocale("relpipe-tr-python", NULL);
+		Py_SetProgramName(pythonProgramName);
+		Py_Initialize();
+		
+		//PyRun_SimpleString("print('Hello from Python!')");
+
 		if (arguments.size() == 3) {
 			relationNameRegEx = wregex(arguments[0]);
 			attributeNameRegEx = wregex(arguments[1]);
 			searchRegEx = wregex(arguments[2]);
 		} else {
+			PyMem_RawFree(pythonProgramName);
 			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-python <relationNameRegExp> <attributeNameRegExp> <searchRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
 		}
 	}
 
+	virtual ~GrepHandler() {
+		Py_FinalizeEx();
+		PyMem_RawFree(pythonProgramName);
+	}
+
 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
 		vector<writer::AttributeMetadata> writerMetadata;