【发布时间】:2009-03-23 17:52:17
【问题描述】:
我正在尝试包装 Patricia Tries(Perl 的 NET::Patricia)以在 python 中公开。我在其中一门课上遇到困难。
因此,从 python 中查看的 patricia 节点(下图)实例具有“数据”属性。读起来没问题,但写起来就坏了。
typedef struct _patricia_node_t {
u_int bit; /* flag if this node used */
prefix_t *prefix; /* who we are in patricia tree */
struct _patricia_node_t *l, *r; /* left and right children */
struct _patricia_node_t *parent;/* may be used */
void *data; /* pointer to data */
void *user1; /* pointer to usr data (ex. route flap info) */
} patricia_node_t;
具体来说:
>>> N = patricia.patricia_node_t()
>>> assert N.data == None
>>> N.data = 1
TypeError: in method 'patricia_node_t_data_set', argument 2 of type 'void *'
现在我的 C 很弱。根据我在 SWIG 书中读到的内容,我认为这意味着我需要向它传递一个指向数据的指针。根据the book:
另外,如果你需要将原始指针值传递给一些外部 python 库,你可以通过将指针对象转换为整数来实现...但是,逆运算是不可能的,即你不能从原始整数值构建一个 Swig 指针对象。
问题:
- 我理解正确吗?
- 我该如何解决这个问题? % 扩展了吗?打字机?细节会很有帮助。
注意事项:
- 我无法更改 C 源代码,但可以在其他 .h 文件或接口 .i 文件中对其进行扩展。
- 据我了解,“数据”字段应该能够包含“任何东西”,以获得我不知道的“任何东西”的一些合理值。
【问题讨论】: