|
5 | 5 | ----------
|
6 | 6 | 问题
|
7 | 7 | ----------
|
8 |
| -You have a C extension module that internally defines a variety of useful functions that |
9 |
| -you would like to export as a public C API for use elsewhere. You would like to use these |
10 |
| -functions inside other extension modules, but don’t know how to link them together, |
11 |
| -and doing it with the C compiler/linker seems excessively complicated (or impossible). |
| 8 | +你有一个C扩展模块,在内部定义了很多有用的函数,你想将它们导出为一个公共的C API供其他地方使用。 |
| 9 | +你想在其他扩展模块中使用这些函数,但是不知道怎样将它们链接起来, |
| 10 | +并且通过C编译器/链接器来做看上去特别复杂(或者不可能做到)。 |
12 | 11 |
|
13 | 12 | |
|
14 | 13 |
|
15 | 14 | ----------
|
16 | 15 | 解决方案
|
17 | 16 | ----------
|
18 |
| -This recipe focuses on the code written to handle Point objects, which were presented |
19 |
| -in Recipe 15.4. If you recall, that C code included some utility functions like this: |
| 17 | +本节聚焦在处理Point对象(在15.4小节已经讲过)。仔细回一下,在C代码中包含了如下这些工具函数: |
20 | 18 |
|
21 |
| -/* Destructor function for points */ |
22 |
| -static void del_Point(PyObject *obj) { |
| 19 | +:: |
23 | 20 |
|
24 |
| - free(PyCapsule_GetPointer(obj,"Point")); |
25 |
| -} |
| 21 | + /* Destructor function for points */ |
| 22 | + static void del_Point(PyObject *obj) { |
26 | 23 |
|
27 |
| -/* Utility functions */ |
28 |
| -static Point *PyPoint_AsPoint(PyObject *obj) { |
29 |
| - return (Point *) PyCapsule_GetPointer(obj, "Point"); |
30 |
| -} |
| 24 | + free(PyCapsule_GetPointer(obj,"Point")); |
| 25 | + } |
31 | 26 |
|
32 |
| -static PyObject *PyPoint_FromPoint(Point *p, int must_free) { |
33 |
| - return PyCapsule_New(p, "Point", must_free ? del_Point : NULL); |
34 |
| -} |
| 27 | + /* Utility functions */ |
| 28 | + static Point *PyPoint_AsPoint(PyObject *obj) { |
| 29 | + return (Point *) PyCapsule_GetPointer(obj, "Point"); |
| 30 | + } |
35 | 31 |
|
36 |
| -The problem now addressed is how to export the PyPoint_AsPoint() and Py |
37 |
| -Point_FromPoint() functions as an API that other extension modules could use and |
38 |
| -link to (e.g., if you have other extensions that also want to use the wrapped Point |
39 |
| -objects). |
40 |
| -To solve this problem, start by introducing a new header file for the “sample” extension |
41 |
| -called pysample.h. Put the following code in it: |
| 32 | + static PyObject *PyPoint_FromPoint(Point *p, int must_free) { |
| 33 | + return PyCapsule_New(p, "Point", must_free ? del_Point : NULL); |
| 34 | + } |
42 | 35 |
|
43 |
| -/* pysample.h */ |
44 |
| -#include "Python.h" |
45 |
| -#include "sample.h" |
46 |
| -#ifdef __cplusplus |
47 |
| -extern "C" { |
48 |
| -#endif |
49 |
| -
|
50 |
| -/* Public API Table */ |
51 |
| -typedef struct { |
52 |
| - Point *(*aspoint)(PyObject *); |
53 |
| - PyObject *(*frompoint)(Point *, int); |
54 |
| -} _PointAPIMethods; |
55 |
| -
|
56 |
| -#ifndef PYSAMPLE_MODULE |
57 |
| -/* Method table in external module */ |
58 |
| -static _PointAPIMethods *_point_api = 0; |
59 |
| -
|
60 |
| -/* Import the API table from sample */ |
61 |
| -static int import_sample(void) { |
62 |
| - _point_api = (_PointAPIMethods *) PyCapsule_Import("sample._point_api",0); |
63 |
| - return (_point_api != NULL) ? 1 : 0; |
64 |
| -} |
| 36 | +现在的问题是怎样将 ``PyPoint_AsPoint()`` 和 ``Point_FromPoint()`` 函数作为API导出, |
| 37 | +这样其他扩展模块能使用并链接它们,比如如果你有其他扩展也想使用包装的Point对象。 |
65 | 38 |
|
66 |
| -/* Macros to implement the programming interface */ |
67 |
| -#define PyPoint_AsPoint(obj) (_point_api->aspoint)(obj) |
68 |
| -#define PyPoint_FromPoint(obj) (_point_api->frompoint)(obj) |
69 |
| -#endif |
| 39 | +要解决这个问题,首先要为 ``sample`` 扩展写个新的头文件名叫 ``pysample.h`` ,如下: |
70 | 40 |
|
71 |
| -#ifdef __cplusplus |
72 |
| -} |
73 |
| -#endif |
| 41 | +:: |
| 42 | + |
| 43 | + /* pysample.h */ |
| 44 | + #include "Python.h" |
| 45 | + #include "sample.h" |
| 46 | + #ifdef __cplusplus |
| 47 | + extern "C" { |
| 48 | + #endif |
| 49 | + |
| 50 | + /* Public API Table */ |
| 51 | + typedef struct { |
| 52 | + Point *(*aspoint)(PyObject *); |
| 53 | + PyObject *(*frompoint)(Point *, int); |
| 54 | + } _PointAPIMethods; |
| 55 | + |
| 56 | + #ifndef PYSAMPLE_MODULE |
| 57 | + /* Method table in external module */ |
| 58 | + static _PointAPIMethods *_point_api = 0; |
| 59 | + |
| 60 | + /* Import the API table from sample */ |
| 61 | + static int import_sample(void) { |
| 62 | + _point_api = (_PointAPIMethods *) PyCapsule_Import("sample._point_api",0); |
| 63 | + return (_point_api != NULL) ? 1 : 0; |
| 64 | + } |
| 65 | + |
| 66 | + /* Macros to implement the programming interface */ |
| 67 | + #define PyPoint_AsPoint(obj) (_point_api->aspoint)(obj) |
| 68 | + #define PyPoint_FromPoint(obj) (_point_api->frompoint)(obj) |
| 69 | + #endif |
| 70 | + |
| 71 | + #ifdef __cplusplus |
| 72 | + } |
| 73 | + #endif |
74 | 74 |
|
75 | 75 | The most important feature here is the _PointAPIMethods table of function pointers. It
|
76 | 76 | will be initialized in the exporting module and found by importing modules.
|
|
0 commit comments