|
70 | 70 | This would typically be done in your init function. |
71 | 71 |
|
72 | 72 | $Log$ |
73 | | - Revision 2.2 1997/01/06 22:50:12 guido |
74 | | - Jim's latest version |
| 73 | + Revision 2.3 1997/04/09 17:34:28 guido |
| 74 | + Changed the way the C API was exported. Jim Fulton. |
| 75 | +
|
| 76 | + Revision 1.2 1997/01/27 14:13:05 jim |
| 77 | + Changed the way the C API was exported. |
75 | 78 |
|
76 | 79 | Revision 1.1 1997/01/02 15:18:36 chris |
77 | 80 | initial version |
|
81 | 84 |
|
82 | 85 | /* Basic fuctions to manipulate cStringIO objects from C */ |
83 | 86 |
|
84 | | -/* Read a string. If the last argument is -1, the remainder will be read. */ |
85 | | -static int(*PycStringIO_cread)(PyObject *, char **, int)=NULL; |
| 87 | +static struct PycStringIO_CAPI { |
| 88 | + |
| 89 | + /* Read a string. If the last argument is -1, the remainder will be read. */ |
| 90 | + int(*cread)(PyObject *, char **, int); |
86 | 91 |
|
87 | | -/* Read a line */ |
88 | | -static int(*PycStringIO_creadline)(PyObject *, char **)=NULL; |
| 92 | + /* Read a line */ |
| 93 | + int(*creadline)(PyObject *, char **); |
89 | 94 |
|
90 | | -/* Write a string */ |
91 | | -static int(*PycStringIO_cwrite)(PyObject *, char *, int)=NULL; |
| 95 | + /* Write a string */ |
| 96 | + int(*cwrite)(PyObject *, char *, int); |
92 | 97 |
|
93 | | -/* Get the cStringIO object as a Python string */ |
94 | | -static PyObject *(*PycStringIO_cgetvalue)(PyObject *)=NULL; |
| 98 | + /* Get the cStringIO object as a Python string */ |
| 99 | + PyObject *(*cgetvalue)(PyObject *); |
95 | 100 |
|
96 | | -/* Create a new output object */ |
97 | | -static PyObject *(*PycStringIO_NewOutput)(int)=NULL; |
| 101 | + /* Create a new output object */ |
| 102 | + PyObject *(*NewOutput)(int); |
98 | 103 |
|
99 | | -/* Create an input object from a Python string */ |
100 | | -static PyObject *(*PycStringIO_NewInput)(PyObject *)=NULL; |
| 104 | + /* Create an input object from a Python string */ |
| 105 | + PyObject *(*NewInput)(PyObject *); |
101 | 106 |
|
102 | | -/* The Python types for cStringIO input and output objects. |
103 | | - Note that you can do input on an output object. |
104 | | -*/ |
105 | | -static PyObject *PycStringIO_InputType=NULL, *PycStringIO_OutputType=NULL; |
| 107 | + /* The Python types for cStringIO input and output objects. |
| 108 | + Note that you can do input on an output object. |
| 109 | + */ |
| 110 | + PyTypeObject *InputType, *OutputType; |
| 111 | + |
| 112 | +} * PycStringIO = NULL; |
106 | 113 |
|
107 | 114 | /* These can be used to test if you have one */ |
108 | 115 | #define PycStringIO_InputCheck(O) \ |
109 | | - ((O)->ob_type==(PyTypeObject*)PycStringIO_InputType) |
| 116 | + ((O)->ob_type==PycStringIO->InputType) |
110 | 117 | #define PycStringIO_OutputCheck(O) \ |
111 | | - ((O)->ob_type==(PyTypeObject*)PycStringIO_OutputType) |
112 | | - |
113 | | -/* The following is used to implement PycString_IMPORT: */ |
114 | | -static PyObject *PycStringIO_Module=NULL, *PycStringIO_CObject=NULL; |
115 | | - |
116 | | -#define IMPORT_C_OBJECT(N) \ |
117 | | - if((PycStringIO_CObject=PyObject_GetAttrString(PycStringIO_Module, #N))) { \ |
118 | | - PycStringIO_ ## N = PyCObject_AsVoidPtr(PycStringIO_CObject); \ |
119 | | - Py_DECREF(PycStringIO_CObject); } |
| 118 | + ((O)->ob_type==PycStringIO->OutputType) |
| 119 | + |
| 120 | +static void * |
| 121 | +PyCObject_Import(char *module_name, char *name) |
| 122 | +{ |
| 123 | + PyObject *m, *c; |
| 124 | + void *r=NULL; |
| 125 | + |
| 126 | + if(m=PyImport_ImportModule(module_name)) |
| 127 | + { |
| 128 | + if(c=PyObject_GetAttrString(m,name)) |
| 129 | + { |
| 130 | + r=PyCObject_AsVoidPtr(c); |
| 131 | + Py_DECREF(c); |
| 132 | + } |
| 133 | + Py_DECREF(m); |
| 134 | + } |
| 135 | + |
| 136 | + return r; |
| 137 | +} |
120 | 138 |
|
121 | 139 | #define PycString_IMPORT \ |
122 | | - if((PycStringIO_Module=PyImport_ImportModule("cStringIO"))) { \ |
123 | | - PycStringIO_InputType=PyObject_GetAttrString(PycStringIO_Module, \ |
124 | | - "InputType"); \ |
125 | | - PycStringIO_OutputType=PyObject_GetAttrString(PycStringIO_Module, \ |
126 | | - "OutputType"); \ |
127 | | - IMPORT_C_OBJECT(cread); \ |
128 | | - IMPORT_C_OBJECT(creadline); \ |
129 | | - IMPORT_C_OBJECT(cwrite); \ |
130 | | - IMPORT_C_OBJECT(NewInput); \ |
131 | | - IMPORT_C_OBJECT(NewOutput); \ |
132 | | - IMPORT_C_OBJECT(cgetvalue); } |
| 140 | + PycStringIO=PyCObject_Import("cStringIO", "cStringIO_CAPI") |
133 | 141 |
|
134 | 142 | #endif /* CSTRINGIO_INCLUDED */ |
0 commit comments