Chinaunix首页 | 论坛 | 博客
  • 博客访问: 116589
  • 博文数量: 22
  • 博客积分: 835
  • 博客等级: 准尉
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-25 21:23
文章分类

全部博文(22)

文章存档

2011年(1)

2009年(21)

我的朋友

分类: LINUX

2009-12-11 16:28:23

5个最常用的Python/C API
1:PyImport_ImportModule
PyObject* PyImport_ImportModule (char *name)
Return value: New reference.
This is a simplified interface to PyImport_ImportModuleEx() below, leaving the globals and locals arguments set to NULL. When the name argument contains a dot (i.e., when it specifies a submodule of a package), the fromlist argument is set to the list ['*'] so that the return value is the named module rather than the top-level package containing it as would otherwise be the case. (Unfortunately, this has an additional side effect when name in fact specifies a subpackage instead of a submodule: the submodules specified in the package's __all__ variable are loaded.) Return a new reference to the imported module, or NULL with an exception set on failure (the module may still be created in this case -- examine sys.modules to find out).
2:PyObject_GetAttrString
PyObject* PyObject_GetAttrString (PyObject *o, char *attr_name)
Return value: New reference.
Retrieve an attribute named attr_name from object o. Returns the attribute value on success, or NULL on failure.  Thisis the equivalent of the Python expression "o.attr_name".
3:PyObject_CallFunction
PyObject* PyObject_CallFunction (PyObject *callable_object, char *format, ...)
Return value: New reference.
Call a callable Python object callable_object, with a variable number of C arguments. The C arguments are described using a Py_BuildValue() style format string. The format may be NULL, indicating that no arguments are provided. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression "apply(o, args)".
4:PyString_AsString
char* PyString_AsString (PyObject *string)
Returns a null-terminated representation of the contents of string. The pointer refers to the internal buffer of string, not a copy. The data must not be modified in any way. It must not be de-allocated.
 
四步曲:
1)一般都是通过(pmod = PyImport_ImportModule ("xuanwu.app_context")先来加载一个模块(py脚本),得到一个PyObject *pmod对象
2)然后通过pfunc_get_default_context= PyObject_GetAttrString(pmod, "get_default_context"),来得到这个py脚本里面的一个函数的指针

3)最后通过调用这个函数的指针就相当于调用py脚本了PyObject *pobj_string =PyObject_CallFunction (pfunc_get_default_context, NULL),返回值是一个PyObject对象,
4)通过PyString_AsString把PyObject对像转化为一个c++中的string,char *temp = PyString_AsString (pobj_string);

其他:
python类的支持:
5:PyObject_CallMethod
PyObject* PyObject_CallMethod (PyObject *o, char *m, char *format, ...)
Return value: New reference.
Call the method named m of object o with a variable number of C arguments. The C arguments are described by a Py_BuildValue() format string. The format may be NULL, indicating that no arguments are provided. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression "o.method(args)". Note that special method names, such as __add__(), __getitem__(), and so on are not supported. The specific abstract-object routines for these must be used.
 
阅读(2021) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~