首页 > 代码库 > 项目记录 -- python调用回调函数

项目记录 -- python调用回调函数

C源文件:

 1 static int 2 get_callback(zpool_handle_t *zhp, void *data) 3 { 4     zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 5     char value[MAXNAMELEN]; 6     zprop_source_t srctype; 7     zprop_list_t *pl; 8  9     for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {10 11         /*12          * Skip the special fake placeholder. This will also skip13          * over the name property when ‘all‘ is specified.14          */15         if (pl->pl_prop == ZPOOL_PROP_NAME &&16             pl == cbp->cb_proplist)17             continue;18 19         if (pl->pl_prop == ZPROP_INVAL &&20             (zpool_prop_feature(pl->pl_user_prop) ||21             zpool_prop_unsupported(pl->pl_user_prop))) {22             srctype = ZPROP_SRC_LOCAL;23 24             if (zpool_prop_get_feature(zhp, pl->pl_user_prop,25                 value, sizeof (value)) == 0) {26                 zprop_print_one_property(zpool_get_name(zhp),27                     cbp, pl->pl_user_prop, value, srctype,28                     NULL, NULL);29             }30         } else {31             if (zpool_get_prop(zhp, pl->pl_prop, value,32                 sizeof (value), &srctype) != 0)33                 continue;34 35             zprop_print_one_property(zpool_get_name(zhp), cbp,36                 zpool_prop_to_name(pl->pl_prop), value, srctype,37                 NULL, NULL);38         }39     }40     return (0);41 }42 来源 -- illumos/usr/src/cmd/zpool/zpool_main.c

上面代码中:value定义的是数组char value[MAXNAMELEN];,我采用的方法如下:

1 value = http://www.mamicode.com/c_char * 256"color: #008080;">2 3 libzfs.zpool_get_prop(zhp, prop, value, 256, byref(srctype))

采用上述方法时,当运行到libzfs.zpool_get_prop时,直接显示为None

 

正确的方法为:

1 value = http://www.mamicode.com/create_string_buffer(256)2 3 ret = libzfs.zpool_get_prop(zhp, prop, value, 256, byref(srctype))

采用create_string_buffer才能正确显示

执行命令:print(type(value))
              print(dir(value))

显示如下:

<class ‘ctypes.c_char_Array_256‘>
[‘__class__‘, ‘__ctypes_from_outparam__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dict__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__module__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__setstate__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘__weakref__‘, ‘_b_base_‘, ‘_b_needsfree_‘, ‘_length_‘, ‘_objects‘, ‘_type_‘, ‘raw‘, ‘value‘]