Код: Выделить всё
bool isComplex(py::object src)
{
// put src into a buffer accessible from both C++ and python (i is an index in this buffer)
int i = pyosetsrc(src);
// Build a python function to do the "is it complex" test
std::string fn;
fn = "isinstance(mycode.pyogetsrc("; // pyogetsrc(i) gets src back out of the shared buffer
fn += std::to_string(i);
fn += "), complex)";
// Wrap this in an eval statement
std::string evalfn;
evalfn = "eval('";
evalfn += fn;
evalfn += "')";
// Run the eval statement
py::object builtins = py::module_::import("builtins");
py::object eval = builtins.attr("eval");
py::object res = eval(evalfn);
// Process the result
return py::isinstance
(res) && py::cast(res);
}
< /code>
и для обработки функций есть аналогичный взлом, на этот раз с: < /p>
fn = "callable(mycode.pyogetsrc(";
fn += std::to_string(i);
fn += "))";
Подробнее здесь: https://stackoverflow.com/questions/796 ... a-callable
Мобильная версия