相同的枚举项在SWIG>中包含不同类型的常量。 2.0.1 [英] The same enumeration items wraps in a constants of different types in SWIG > 2.0.1

查看:95
本文介绍了相同的枚举项在SWIG>中包含不同类型的常量。 2.0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SWIG 3.0.12中发现一个小bug。如果您对文件 examples / python / enum / example.h 进行了一些更改,只需在枚举项(g)中添加一个char常量:

 枚举颜色{RED,BLUE,GREEN ='g'}; 

然后进行换行,编译_example.so并运行 $ python runme .py ,您将获得:

 在此输入代码

* **颜色***
RED = 0
BLUE = 1
GREEN = g

*** Foo :: speed ***
Foo_IMPULSE = 0
Foo_WARP = 1
Foo_LUDICROUS = 2

测试使用带有功能的枚举

color = RED,speed = IMPULSE speed
color = BLUE,speed = WARP speed
追溯(最近的最后一次调用):
文件runme.py,第22行,< module>
example.enum_test(example.GREEN,example.Foo.LUDICROUS)
TypeError:在方法'enum_test'中,类型为'color'的参数1

这是奇怪的情况,不是吗?相同的枚举项包含不同类型的常量,可怜的小函数只等于一种类型的枚举常量(现在它等待int,但是GREEN常量类型是char)。如何在没有回滚SWIG版本的情况下绕过它?



此错误出现在SWIG 3.0.12,3.0.11中,但在2.0.1中所有的工作

解决方案

好的,这是一个旧的SWIG错误,出现在6年前,SWIG开发人员添加了不同类型枚举功能。可能他们忘记了PHP和Python等动态类型语言,但我不知道,但是对于我来说,这个问题是通过编译我自己的SWIG构建来解析源代码中的变化。



只需在 Source / CParse / parser.y 之后添加if条件:

  5620 5620 Swig_error(cparse_file,cparse_line,Type error。Expecting a integral type\\\
);
5621 5621}
5622 - if($$。type == T_CHAR)$$。type = T_INT;
5623 5622}
5624 5623;

然后用这个小小的变化构建你自己的SWIG,你将把所有的枚举项包装成整数常量。


I find a little bug in SWIG 3.0.12. If you make little changes in file examples/python/enum/example.h, just add a char constant to the one of enum item (g):

enum color { RED, BLUE, GREEN = 'g'};

And then make a wrap, compile _example.so and run $python runme.py, you will get:

enter code here

*** color ***
RED    = 0
BLUE   = 1
GREEN  = g

*** Foo::speed ***
Foo_IMPULSE   = 0
Foo_WARP      = 1
Foo_LUDICROUS = 2

Testing use of enums with functions

color = RED, speed = IMPULSE speed
color = BLUE, speed = WARP speed
Traceback (most recent call last):
  File "runme.py", line 22, in <module>
    example.enum_test(example.GREEN, example.Foo.LUDICROUS)
TypeError: in method 'enum_test', argument 1 of type 'color'

It is strange situation, isn't it? The same enumeration items wraps in a constants of different types and the poor little function waits just one type of enum constant (now it waits ints, but GREEN constant type is char). How it can be bypassed without rollback SWIG version, what do you think?

This bug appears in SWIG 3.0.12, 3.0.11, but in 2.0.1 all works fine.

解决方案

Ok, it's an old SWIG bug which appears 6 years earlier, when SWIG developers add Different-Type-Enums feature. May be they forgot about dynamically typed languages such as PHP and Python, I don't know, but for me this problem resolved by compile my own SWIG build with changes in the parser source code...

Just add the if-condition at Source/CParse/parser.y after:

5620 5620 Swig_error(cparse_file,cparse_line,"Type error. Expecting an integral type\n"); 
5621 5621 } 
5622 -    if ($$.type == T_CHAR) $$.type = T_INT; 
5623 5622 } 
5624 5623 ;

Then build your own SWIG with this little change and you will get wrapping all enums items to integer constants.

这篇关于相同的枚举项在SWIG&gt;中包含不同类型的常量。 2.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆