Initial Commit
This commit is contained in:
6
src/CMakeLists.txt
Normal file
6
src/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
include_directories(${Python3_INCLUDE_DIRS})
|
||||
|
||||
add_executable(main main.cc PyWrap.cc)
|
||||
|
||||
target_link_libraries(main ${Python3_LIBRARIES})
|
32
src/PyWrap.cc
Normal file
32
src/PyWrap.cc
Normal file
@@ -0,0 +1,32 @@
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#include <stdexcept>
|
||||
#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;
|
||||
}
|
||||
}
|
17
src/PyWrap.h
Normal file
17
src/PyWrap.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef PYWRAP_H
|
||||
#define PYWRAP_H
|
||||
#include <Python.h>
|
||||
#include <string>
|
||||
|
||||
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 */
|
8
src/main.cc
Normal file
8
src/main.cc
Normal file
@@ -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;
|
||||
}
|
Reference in New Issue
Block a user