本文介绍了如何在Swig的python中使用enum?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举声明,如下所示:

I have a enum declaration as follows:

typedef enum mail_ {
    Out = 0,
    Int = 1,
    Spam = 2
} mail;

功能:

mail status;
int fill_mail_data(int i, &status);

在上面的函数中,状态被填充

In the function above, status gets filled up and will send.

当我通过痛饮尝试时,我面临以下问题:

When I am trying this through swig I am facing the following issues:


  1. 它没有显示邮件的详细信息。当我尝试打印 mail .__ doc __ help(mail)时,抛出错误,说没有这样的消息属性,尽管我可以使用这些值(垃圾邮件 In Out )。

  2. 如上所示,Swig不知道什么是 main ,所以不是接受该邮件的任何函数参数。

  1. It is not showing details of mail. When I try to print mail.__doc__ or help(mail), it is throwing an error saying there is no such Attribute, though though i am able to use those values (Spam, In, and Out).
  2. As shown above, the Swig does not know what main is, so it is not accepting any function arguments for that mail.


推荐答案

对于SWIG,枚举只是一个整数。要将其用作示例中的输出参数,还可以将参数声明为输出参数,如下所示:

To SWIG, an enum is just an integer. To use it as an output parameter as in your example, you also can declare the parameter as an output parameter like so:

%module x

// Declare "mail* status" as an output parameter.
// It will be returned along with the return value of a function
// as a tuple if necessary, and will not be required as a function
// parameter.
%include <typemaps.i>
%apply int *OUTPUT {mail* status};

%inline %{

typedef enum mail_ {
    Out  = 0,
    Int  = 1,
    Spam = 2
} mail;

int fill_mail_data(int i, mail* status)
{
    *status = Spam;
    return i+1;
}

%}

使用:

>>> import x
>>> dir(x)    # Note no "mail" object, just Int, Out, Spam which are ints.
['Int', 'Out', 'Spam', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_newclass', '_object', '_swig_getattr', '_swig_property', '_swig_repr', '_swig_setattr', '_swig_setattr_nondynamic', '_x', 'fill_mail_data']
>>> x.fill_mail_data(5)
[6, 2]
>>> ret,mail = x.fill_mail_data(5)
>>> mail == x.Spam
True

这篇关于如何在Swig的python中使用enum?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 23:53