From 6ab8de5125c63381a06e5d207ca4289c7962acc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Sat, 28 Oct 2023 17:42:18 +0200 Subject: [PATCH] Initial Commit --- .gitignore | 39 +++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 15 +++++++++++++++ src/CMakeLists.txt | 6 ++++++ src/PyWrap.cc | 32 ++++++++++++++++++++++++++++++++ src/PyWrap.h | 17 +++++++++++++++++ src/main.cc | 8 ++++++++ 6 files changed, 117 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 src/CMakeLists.txt create mode 100644 src/PyWrap.cc create mode 100644 src/PyWrap.h create mode 100644 src/main.cc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c07ee27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app +**/build +**/lcoverage +.idea +cmake-* +.vscode +**/CMakeFiles +**/gcovr-report \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ed98400 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.5) +project(testcpy) + +set( CMAKE_CXX_STANDARD 17) +set( CMAKE_CXX_STANDARD_REQUIRED ON ) + +find_package(Python3 3.11...3.11.9 COMPONENTS Interpreter Development REQUIRED) + +message("Python_FOUND:${Python3_FOUND}") +message("Python_VERSION:${Python3_VERSION}") +message("Python_Development_FOUND:${Python3_Development_FOUND}") +message("Python_LIBRARIES:${Python3_LIBRARIES}") +message("Python_INCLUDE_DIRS ${Python3_INCLUDE_DIRS}") + +add_subdirectory(src) \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..44420fe --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,6 @@ + +include_directories(${Python3_INCLUDE_DIRS}) + +add_executable(main main.cc PyWrap.cc) + +target_link_libraries(main ${Python3_LIBRARIES}) diff --git a/src/PyWrap.cc b/src/PyWrap.cc new file mode 100644 index 0000000..e0104dc --- /dev/null +++ b/src/PyWrap.cc @@ -0,0 +1,32 @@ +#define PY_SSIZE_T_CLEAN +#include +#include "PyWrap.h" + +namespace pywrap { + void PyWrap::doCommand(const std::string& command) + { + auto status = initPython(command); + if (PyStatus_Exception(status)) { + throw std::runtime_error("Error initializing Python"); + } + Py_RunMain(); + } + + PyStatus PyWrap::initPython(const std::string& command) + { + PyStatus status; + + PyConfig config; + PyConfig_InitPythonConfig(&config); + config.isolated = 0; + wchar_t* commandCoded = Py_DecodeLocale(command.c_str(), NULL); + config.run_command = commandCoded; + status = PyConfig_Read(&config); + if (PyStatus_Exception(status)) { + throw std::runtime_error("Error reading config"); + } + status = Py_InitializeFromConfig(&config); + PyConfig_Clear(&config); + return status; + } +} \ No newline at end of file diff --git a/src/PyWrap.h b/src/PyWrap.h new file mode 100644 index 0000000..bcb346e --- /dev/null +++ b/src/PyWrap.h @@ -0,0 +1,17 @@ +#ifndef PYWRAP_H +#define PYWRAP_H +#include +#include + +namespace pywrap { + class PyWrap { + public: + PyWrap() = default; + ~PyWrap() = default; + void doCommand(const std::string& command); + private: + PyStatus initPython(const std::string& command); + + }; +} /* namespace python */ +#endif /* PYWRAP_H */ \ No newline at end of file diff --git a/src/main.cc b/src/main.cc new file mode 100644 index 0000000..e310ed6 --- /dev/null +++ b/src/main.cc @@ -0,0 +1,8 @@ +#include "PyWrap.h" + +int main(int argc, char* argv[]) +{ + auto wrap = pywrap::PyWrap(); + wrap.doCommand("from stree import Stree; print(Stree().version())"); + return 0; +} \ No newline at end of file