Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

A Possible Bug in Call Policies

未关闭
#310 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
cpp, python
领域
api, backend

调研方向

从链接的 pytest_isinstance_issue 复现开始,先分别运行两个 pytest 文件,然后运行完整的测试套件。阅读 callpolicies.cpp、overloading.cpp、test_callpolicies.py 以及对应的 overloading 测试,以比较导出的 Example 类和 factory 结果。确定是 Boost.Python、pytest 还是 Python 导致返回了错误的类,并记录或修正该行为,即视为完成。

由索引模型根据 Issue 内容生成。

描述

Hi, I believe I just encountered a possible bug in boost-python...

First of all, the back story, I'm putting together a python-boost based example for my cmaketools project, which makes a cmake-based python package build as straight-forward as possible. I've taken the examples by TNG (who created his examples based off of the official documentation) and turned them into a package with modules with pytest tests. The modules based on his "09-Overloading" example is failing "08-CallPolicies" example in pytest only when they are run together.

Description

When there is a class with the same name but under different a module (in a same package?), return_value_policy<manage_new_object>() call (possibly?) picks a wrong class to instantiate under pytest running 2 test py files each testing one of the classes.

What is peculiar is that it works when only one test file is tested but fails when both are tested together. So, it could be a pytest issue (or Python's for that matter)...

Versions

Boost: 1.71 (official pre-built binary)
Boost Python DLL: boost_python37-vc142-mt-x64-1_71.dll
Python: 3.7.6 (conda)
Compiler: MSVC 2019 (16.6.1)
OS: Window 10

(Near) Minimal Example

Please find the entire example project at https://github.com/hokiedsp/pytest_isinstance_issue. Clone and run the following to reproduce (hopefully)

pip -e .
pytest tests/test_callpolicies.py # passes
pytest tests/test_nor.py # passes
pytest

Last pytest call fails with the assertion error:

    def test_callpolicies():
        f = policies.Example.factory()
>       assert isinstance(f, policies.Example) and str(f) == "factory"
E       AssertionError: assert (False)
E        +  where False = isinstance(<boost_python_example.overloading.Example object at 0x00000202D58B0300>, <class 'boost_python_example.callpolicies.Example'>)
E        +    where <class 'boost_python_example.callpolicies.Example'> = policies.Example

I'm anticipating f to be boost_python_example.callpolicies.Example but instead boost_python_example.overloading.Example.

Side Note: If CMake cannot find Boost, you need to modify Line 14 of setup.py

configure_opts = ['-DBOOST_ROOT=C:\\Users\\tikum\\AppData\\Local\\Programs\\boost_1_71_0']

code snippets

Here are the sections of most relevant:

callpolicies.cpp

#include <sstream>
#include <string>

class Example {
    Example(std::string n) : mName(n) {}
    std::string mName;
public:
    std::string name() const { return mName; }
    static Example* factory()
    {
        return new Example("factory");
    }
// ... snip...
};

#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(callpolicies)
{
    class_<Example>("Example", no_init)
        .def("__str__", &Example::name)
        .def("factory", &Example::factory,
            return_value_policy<manage_new_object>())
        .staticmethod("factory")
    ;
}	

overloading.cpp

#include <sstream>
#include <string>

class Example {
public:
    Example()
    {}
    // ... snip...
};

#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(makeIt_overloads, makeIt, 1, 3)
BOOST_PYTHON_MODULE(overloading)
{
    void        (Example::*d1)()             = &Example::doit;
    std::string (Example::*d2)(unsigned int) = &Example::doit;
    void        (Example::*d3)(std::string)  = &Example::doit;

    class_<Example>("Example")
        .def("__str__", &Example::print)
// ...snip...
    ; 
}	

test_callpolicies.py

#!/usr/bin/env python
import pytest
from boost_python_example import callpolicies as policies

def test_callpolicies():
    # e = callpolicies.Example() # won't work, constructore not available from python
    f = policies.Example.factory()
    assert isinstance(f, policies.Example) and str(f) == "factory"
主要语言
C++
星标
537
派生
223
平均合并
11 小时 22 分钟
30 天内合并 PR
2

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

boostorg/python 的其他 Issue

查看 boostorg/python 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。