5

问题:我使用 SWIG 在 python 中包装了一些 c++ 代码。在 python 方面,我想采用一个包装的 c++ 指针并将其向下转换为指向子类的指针。我在 SWIG .i 文件中添加了一个新的 c++ 函数来执行此向下转换,但是当我从 python 调用它时,我得到一个 TypeError。

以下是详细信息:

我有两个 C++ 类,Base 和 Derived。Derived 是 Base 的子类。我有第三个类,Container,它包含一个 Derived,并提供了一个访问器。访问器将 Derived 作为 const Base& 返回,如下所示:

class Container {
  public:
    const Base& GetBase() const {
      return derived_;
    }

  private:
    Derived derived_;
};

我已经使用 SWIG 将这些类包装在 python 中。在我的 python 代码中,我想将 Base 引用向下转换为 Derived。为此,我在 swig .i 文件中写入了一个使用 c++ 进行向下转换的辅助函数:

%inline %{
  Derived* CastToDerived(Base* base) {
    return static_cast<Derived*>(base);
  }
%}

在我的 python 代码中,我称之为向下转换函数:

base = container.GetBase()
derived = CastToDerived(base)

当我这样做时,我收到以下错误:

TypeError: in method 'CastToDerived', argument 1 of type 'Base *'

为什么会发生这种情况?

作为参考,这里是 SWIG 生成的 .cxx 文件的相关位;即原始函数及其python接口化的doppelganger:

  Derived* CastToDerived(Base* base) {
    return static_cast<Derived*>(base);
  }

//  (lots of other generated code omitted)

SWIGINTERN PyObject *_wrap_CastToDerived(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  PyObject *resultobj = 0;
  Base *arg1 = (Base *) 0 ;
  void *argp1 = 0 ;
  int res1 = 0 ;
  PyObject * obj0 = 0 ;
  Derived *result = 0 ;

  if (!PyArg_ParseTuple(args,(char *)"O:CastToDerived",&obj0)) SWIG_fail;
  res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Base, 0 |  0 );
  if (!SWIG_IsOK(res1)) {
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "CastToDerived" "', argument " "1"" of type '" "Base *""'"); 
  }
  arg1 = reinterpret_cast< Base * >(argp1);
  result = (Derived *)CastToDerived(arg1);
  resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Derived, 0 |  0 );
  return resultobj;
fail:
  return NULL;
}

任何帮助将不胜感激。

——马特

4

3 回答 3

3

正如我上面评论的那样,这似乎适用于 swig 1.3.40。

这是我的文件:

通道:

#include <iostream>
class Base {};
class Derived : public Base
{
    public:
        void f() const { std::cout << "In Derived::f()" << std::endl; }
};
class Container {
  public:
    const Base& GetBase() const {
      return derived_;
    }
  private:
    Derived derived_;
};

%module c

%{
#define SWIG_FILE_WITH_INIT
#include "c.h"
%}

%inline %{
  Derived* CastToDerived(Base* base) {
    return static_cast<Derived*>(base);
  }
%}
class Base
{
};

class Derived : public Base
{
    public:
        void f() const;
};

class Container {
  public:
    const Base& GetBase() const;
};

ctest.py

import c

container = c.Container()
b = container.GetBase()
d = c.CastToDerived(b)
d.f()
print "ok"

运行:

$ swig -c++ -python c.i
$ g++ -fPIC -I/usr/include/python2.6 -c -g c_wrap.cxx
$ g++ -shared -o _c.so c_wrap.o
$ python ctest.py 
In Derived::f()
ok
于 2010-10-13T12:56:15.530 回答
0

您是否有可能多次定义 Base 类?我在 ctypes 上遇到过类似的问题,我无意中在两个不同的模块中定义了相同的结构类。我在纯 Python 中也发生过类似的事情,我使用 imp.load_module 加载插件类,创建该类的对象,然后重新加载模块 - 噗!创建的对象将不再通过类的 isinstance 测试,因为重新加载的类,即使它具有相同的名称,也是具有不同 ID 的不同类。(此博客条目中有更完整的描述。)

于 2010-10-13T09:51:14.717 回答
0

我在您的代码中注意到的两件事第一个 GetBase 返回对 const 的引用,第二个 CastToDerived 需要一个指向非 const Base 的指针。

即使在 C++ 中,你也很难完成这项工作。我不知道还有什么问题,但我会先尝试解决这个问题。

于 2010-10-13T06:45:50.187 回答