Base class of non exposed derived class in Python side?

オープン
#415 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
cpp
領域
api

調査の方向性

示されている Base、Derived、DerivedTwo、factory、factoryTwo の定義を使用して、例を再現します。まず Boost.Python の class_ 登録、bases 宣言、shared_ptr の変換動作を読み、DerivedTwo を登録した場合と登録しない場合で、返される Python 型を比較します。型の選択動作が説明され、fix の想定される扱いまたは対象範囲が文書化されていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Hello, I have a use case where a polymorphism factory can instantiate object of a non exposed class. I have a multi-level inheritance scheme and the last level is not exposed to Python. What I try to understand is why the Python object of the last level takes the type of the top-most class in the inheritance hierarchy and not the one just above?

Here is a minimal example to reproduce the behavior:

C++ side

class Base
{
public:
    Base() {}
    virtual ~Base() = default;
};

class Derived: public Base
{
public:
    Derived(int value) { m_value = value; }
    ~Derived() = default;

    int getValue() { return m_value; }

private:
    int m_value;
};

class DerivedTwo: public Derived
{
public:
    DerivedTwo(int value): Derived(value) {}
    ~DerivedTwo() = default;
};

std::shared_ptr<Base> factory()
{
    return std::make_shared<Derived>(9);
}

std::shared_ptr<Base> factoryTwo()
{
    return std::make_shared<DerivedTwo>(99);
}

BOOST_PYTHON_MODULE(mymodule)
{
    using namespace boost::python;

    class_<Base, std::shared_ptr<Base>, boost::noncopyable>("Base", init<>());

    class_<Derived, bases<Base>, std::shared_ptr<Derived>, boost::noncopyable>("Derived", init<int>())
        .def("get_value", &Derived::getValue)
    ;

    def("factory", &factory);
    def("factory_two", &factoryTwo);
}

Python side

import mymodule

obj = mymodule.factory()
print(type(obj))

obj2 = mymodule.factory_two()
print(type(obj2))

Output

<class 'mymodule.Derived'>
<class 'mymodule.Base'> <-- WHY? 

Obviously, if I expose DerivedTwo class, all is OK.
Thanks for your help.

主要言語
C++
スター
537
フォーク
223
平均マージ
11時間 22分
マージ済み PR(30日)
2

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

boostorg/python のほかの issue

boostorg/python の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。