15.4 在C扩展模块中操作隐形指针

上传人:豆浆 文档编号:36798720 上传时间:2018-04-02 格式:PDF 页数:3 大小:58.18KB
返回 下载 相关 举报
15.4 在C扩展模块中操作隐形指针_第1页
第1页 / 共3页
15.4 在C扩展模块中操作隐形指针_第2页
第2页 / 共3页
15.4 在C扩展模块中操作隐形指针_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《15.4 在C扩展模块中操作隐形指针》由会员分享,可在线阅读,更多相关《15.4 在C扩展模块中操作隐形指针(3页珍藏版)》请在金锄头文库上搜索。

1、15.4 在C扩展模块中操作隐形指针问题You have an extension module that needs to handle a pointer to a C data structure, butyou dont want to expose any internal details of the structure to Python.解决案Opaque data structures are easily handled by wrapping them inside capsule objects.Consider this fragment of C code from

2、 our sample code:typedef struct Point double x,y; Point;extern double distance(Point *p1, Point *p2);Here is an example of extension code that wraps the Point structure and distance()function using capsules:/ Destructor function for points (#)/static void del_Point(PyObject *obj) free(PyCapsule_GetP

3、ointer(obj,”Point”);/ Utility functions (#)/static Point *PyPoint_AsPoint(PyObject *obj) return (Point *) PyCapsule_GetPointer(obj, “Point”);static PyObject *PyPoint_FromPoint(Point *p, int must_free) return PyCapsule_New(p, “Point”, must_free ? del_Point : NULL); / Create a new Point object (#)/sta

4、tic PyObject *py_Point(PyObject *self, PyObject *args) Point *p;double x,y;if (!PyArg_ParseTuple(args,”dd”,p = (Point *) malloc(sizeof(Point);p-x = x;p-y = y;return PyPoint_FromPoint(p, 1);static PyObject *py_distance(PyObject *self, PyObject *args) Point *p1, *p2;PyObject *py_p1, *py_p2;double resu

5、lt;if (!PyArg_ParseTuple(args,”OO”,if (!(p1 = PyPoint_AsPoint(py_p1) return NULL;if (!(p2 = PyPoint_AsPoint(py_p2) return NULL;result = distance(p1,p2);return Py_BuildValue(“d”, result);Using these functions from Python looks like this: import sample p1 = sample.Point(2,3) p2 = sample.Point(4,5) p1

6、p2 sample.distance(p1,p2) 2.8284271247461903 讨论Capsules are similar to a typed C pointer. Internally, they hold a generic pointer alongwith an identifying name and can be easily created using the PyCapsule_New() function.In addition, an optional destructor function can be attached to a capsule to re

7、lease theunderlying memory when the capsule object is garbage collected.To extract the pointer contained inside a capsule, use the PyCapsule_GetPointer()function and specify the name. If the supplied name doesnt match that of the capsuleor some other error occurs, an exception is raised and NULL is

8、returned.In this recipe, a pair of utility functionsPyPoint_FromPoint() and PyPoint_AsPoint()have been written to deal with the mechanics of creating and unwindingPoint instances from capsule objects. In any extension functions, well use these func-tions instead of working with capsules directly. Th

9、is design choice makes it easier todeal with possible changes to the wrapping of Point objects in the future. For example,if you decided to use something other than a capsule later, you would only have to changethese two functions.One tricky part about capsules concerns garbage collection and memory

10、 management.The PyPoint_FromPoint() function accepts a must_free argument that indicateswhether the underlying Point * structure is to be collected when the capsule is de-stroyed. When working with certain kinds of C code, ownership issues can be difficultto handle (e.g., perhaps a Point structure i

11、s embedded within a larger data structurethat is managed separately). Rather than making a unilateral decision to garbage collect,this extra argument gives control back to the programmer. It should be noted that thedestructor associated with an existing capsule can also be changed using the PyCapsul

12、e_SetDestructor() function.Capsules are a sensible solution to interfacing with certain kinds of C code involvingstructures. For instance, sometimes you just dont care about exposing the internals ofa structure or turning it into a full-fledged extension type.With a capsule, you can puta lightweight wrapper around it and easily pass it around to other extension functions.W3Cschool()最的技术知识分享与学习平台此篇内容来于站户上传并发布。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号