Python bool转换为C ++(pybind11)

时间:2018-04-18 04:00:40

标签: c++ c++11 pybind11

我正在尝试将python bool转换为C ++ bool,以便它可以用于比较,但无法成功获取它。

这是我到目前为止的代码(注释掉以前的试验以供参考)。任何帮助将不胜感激。到目前为止,它应该打印“True”和“False”,但我无法在C ++ if语句中正确评估它。

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Scroll-x overflow test</title>
    <style>
        html, body{
          width:100%;
          height:100%;
          overflow:hidden;
          margin:0px;   

        }

        div.content{
          width:100%;
          height:90%;
          overflow:hidden;
          margin:0px;   

        } 

        .footer{
            background-color: #ffffcc;
            text-align: center;
        }

    </style>
</head>
<body>
    <div class="content">  

        <iframe name="main" src="scroll_table.html" width="100%" height="100%" marginheight="0" frameborder="0" border="0" scrolling="auto"></iframe>

    </div>
    <div class="footer">
        <p>Scroll bar test</p>
    </div>
</body>
</html>

编辑:

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("");
        }
    }

似乎有效,但已被弃用。当我尝试使用建议的“f.is(Py_True)”时,它会产生错误if(f == Py_True){

2 个答案:

答案 0 :(得分:0)

看来这可以通过以下对上面代码段的更改来解决

f.is(Py_True)

应该是

if(py::str(f).is(py::str(Py_True)))

希望这有助于其他可能有同样问题的人。重要的是要注意,虽然已弃用,但&#39; ==&#39;比较(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("");
        }
}

答案 1 :(得分:0)

检查此功能

int PyBool_Check(PyObject * o) 如果o的类型为PyBool_Type,则返回true。