From f9276a12e6225ace86ed9f32a2ebb601998ec2fe Mon Sep 17 00:00:00 2001 From: Chris Sullivan Date: Tue, 19 Apr 2016 18:32:12 -0400 Subject: [PATCH] Added a function to extract intrinsic typed variables from python into C++ --- example_external.cpp | 24 ++++++++++++++++++++++++ include/pylingual.hpp | 5 +++++ 2 files changed, 29 insertions(+) create mode 100644 example_external.cpp diff --git a/example_external.cpp b/example_external.cpp new file mode 100644 index 0000000..17ef400 --- /dev/null +++ b/example_external.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +int main(){ + + try{ + + pylingual py; + + py.call("x = 42"); + auto x = py.get("x"); + cout << x << endl; + + return 0; + + + }catch(boost::python::error_already_set const &){ + //Parse and output the exception + string perror_str = parse_python_exception(); + cout << "Error in Python: " << perror_str << endl; + } +} diff --git a/include/pylingual.hpp b/include/pylingual.hpp index dc932c2..2f2a4b9 100644 --- a/include/pylingual.hpp +++ b/include/pylingual.hpp @@ -53,6 +53,11 @@ class pylingual { return Vec; } + template + T get(const char* var_to_extract) { + return extract(main_module.attr(var_to_extract)); + } + template void call(const char* code_to_execute,char* var_to_extract,vector &output) { exec(code_to_execute,main_namespace);