Python 布尔转换为 C++ (pybind11)
Python bool conversion to C++ (pybind11)
我正在尝试将 python 布尔值转换为 C++ 布尔值,以便将其用于比较,但未能成功。
这是我目前的代码(注释掉以前的试验留作参考)。任何帮助将不胜感激。到目前为止它应该打印 "True" 和 "False",但我无法在 C++ if 语句中正确评估它。
py::object mwparser = py::module::import("mwparserfromhell");
py::object code = mwparser.attr("parse")(py::str(text));
py::object filtered = code.attr("filter_templates")();
for(auto temp : filtered) {
//auto type = template_figure_type(temp.attr("name"));
auto type = template_figure_type(py::str(temp.attr("name")));
// py::print("G ");
//py::print(type);
// auto type = "";
if(type != ""){
try {
//list.append(type);
//return list;
//py::print(py::str(type));
py::object f = temp.attr("has")("link");
py::print(py::str(f)); //displays "True" and "False" when it should
//PyObject *t = Py_True;
// int tr = PyObject_IsTrue(f);
// if(py::str(f) == py::str("False")) {
if(py::str(f).is(py::str("True"))) {
//if(temp.attr("has")("link")) {
//if(py::str(f) == py::str("True")){
// if(0){
temp.attr("remove")("link");
bContent_changed = true;
list.append(temp);
}
else {
py::print("NO");
}
} catch (std::domain_error) {
throw std::domain_error("");
}
}
编辑:
if(f == Py_True){
似乎有效,但已弃用。当我尝试使用建议的 "f.is(Py_True)" 时,它会产生错误 error: reference to type 'const
pybind11::detail::object_api<pybind11::handle>' could not bind to an
rvalue of type 'PyObject *' (aka '_object *')
if(f.is(Py_True)){
^~~~~~~
似乎可以通过对上述代码片段进行以下更改来解决此问题
f.is(Py_True)
应该是
if(py::str(f).is(py::str(Py_True)))
希望这对可能有相同问题的其他人有所帮助。重要的是要注意,虽然已弃用,但“==”比较 (f == Py_True) 仍然有效(暂时)。
对于那些好奇的(功能)更新代码如下:
py::object mwparser = py::module::import("mwparserfromhell");
py::object code = mwparser.attr("parse")(py::str(text));
py::object filtered = code.attr("filter_templates")();
for(auto temp : filtered) {
auto type = template_figure_type(py::str(temp.attr("name")));
if(type != ""){
try {
py::object f = temp.attr("has")("link");
py::print(py::str(f)); //displays "True" and "False" when it should
if(py::str(f).is(py::str(Py_True))){
temp.attr("remove")("link");
bContent_changed = true;
list.append(temp);
}
else {
py::print("NO");
}
} catch (std::domain_error) {
throw std::domain_error("");
}
}
勾选这个功能
int PyBool_Check(PyObject *o)
Return 如果 o 的类型为 PyBool_Type,则为真。
我正在尝试将 python 布尔值转换为 C++ 布尔值,以便将其用于比较,但未能成功。
这是我目前的代码(注释掉以前的试验留作参考)。任何帮助将不胜感激。到目前为止它应该打印 "True" 和 "False",但我无法在 C++ if 语句中正确评估它。
py::object mwparser = py::module::import("mwparserfromhell");
py::object code = mwparser.attr("parse")(py::str(text));
py::object filtered = code.attr("filter_templates")();
for(auto temp : filtered) {
//auto type = template_figure_type(temp.attr("name"));
auto type = template_figure_type(py::str(temp.attr("name")));
// py::print("G ");
//py::print(type);
// auto type = "";
if(type != ""){
try {
//list.append(type);
//return list;
//py::print(py::str(type));
py::object f = temp.attr("has")("link");
py::print(py::str(f)); //displays "True" and "False" when it should
//PyObject *t = Py_True;
// int tr = PyObject_IsTrue(f);
// if(py::str(f) == py::str("False")) {
if(py::str(f).is(py::str("True"))) {
//if(temp.attr("has")("link")) {
//if(py::str(f) == py::str("True")){
// if(0){
temp.attr("remove")("link");
bContent_changed = true;
list.append(temp);
}
else {
py::print("NO");
}
} catch (std::domain_error) {
throw std::domain_error("");
}
}
编辑:
if(f == Py_True){
似乎有效,但已弃用。当我尝试使用建议的 "f.is(Py_True)" 时,它会产生错误 error: reference to type 'const
pybind11::detail::object_api<pybind11::handle>' could not bind to an
rvalue of type 'PyObject *' (aka '_object *')
if(f.is(Py_True)){
^~~~~~~
似乎可以通过对上述代码片段进行以下更改来解决此问题
f.is(Py_True)
应该是
if(py::str(f).is(py::str(Py_True)))
希望这对可能有相同问题的其他人有所帮助。重要的是要注意,虽然已弃用,但“==”比较 (f == Py_True) 仍然有效(暂时)。
对于那些好奇的(功能)更新代码如下:
py::object mwparser = py::module::import("mwparserfromhell");
py::object code = mwparser.attr("parse")(py::str(text));
py::object filtered = code.attr("filter_templates")();
for(auto temp : filtered) {
auto type = template_figure_type(py::str(temp.attr("name")));
if(type != ""){
try {
py::object f = temp.attr("has")("link");
py::print(py::str(f)); //displays "True" and "False" when it should
if(py::str(f).is(py::str(Py_True))){
temp.attr("remove")("link");
bContent_changed = true;
list.append(temp);
}
else {
py::print("NO");
}
} catch (std::domain_error) {
throw std::domain_error("");
}
}
勾选这个功能
int PyBool_Check(PyObject *o) Return 如果 o 的类型为 PyBool_Type,则为真。