| 1 |
From: "M.-A. Lemburg" <[email protected]> |
| 2 |
To: [email protected] |
| 3 |
Subject: Re: [Python-Dev] Buffer interface in abstract.c? |
| 4 |
Date: Tue, 03 Aug 1999 09:50:33 +0200 |
| 5 |
Message-ID: <[email protected]> |
| 6 |
In-Reply-To: <[email protected]> |
| 7 |
MIME-Version: 1.0 |
| 8 |
Content-Type: multipart/mixed; boundary="===============3450783038406365800==" |
| 9 |
|
| 10 |
--===============3450783038406365800== |
| 11 |
Content-Type: text/plain; charset="utf-8" |
| 12 |
Content-Transfer-Encoding: 7bit |
| 13 |
|
| 14 |
Greg Stein wrote: |
| 15 |
> |
| 16 |
> Mark Hammond wrote: |
| 17 |
> > ... |
| 18 |
> > Therefore, I would like to propose these functions to be added to |
| 19 |
> > abstract.c: |
| 20 |
> > |
| 21 |
> > int PyObject_GetBufferSize(); |
| 22 |
> > void *PyObject_GetReadWriteBuffer(); /* or "char *"? */ |
| 23 |
> > const void *PyObject_GetReadOnlyBuffer(); |
| 24 |
> > |
| 25 |
> > Although equivalent functions exist for the buffer object, I can't see the |
| 26 |
> > equivalent abstract implementations - ie, that work with any object |
| 27 |
> > supporting the protocol. |
| 28 |
> > |
| 29 |
> > Im willing to provide a patch if there is agreement a) the general idea is |
| 30 |
> > good, and b) my specific spelling of the idea is OK (less likely - |
| 31 |
> > PyBuffer_* seems better, but loses any implication of being abstract?). |
| 32 |
> |
| 33 |
> Marc-Andre proposed exactly the same thing back at the end of March (to |
| 34 |
> me and Guido). The two of us hashed out some of the stuff and M.A. came |
| 35 |
> up with a full patch for the stuff. Guido was relatively non-committal |
| 36 |
> at the point one way or another, but said they seemed fine. It appears |
| 37 |
> the stuff never made it into source control. |
| 38 |
> |
| 39 |
> If Marc-Andre can resurface the final proposal/patch, then we'd be set. |
| 40 |
|
| 41 |
Below is the code I currently use. I don't really remember if this |
| 42 |
is what Greg and I discussed a while back, but I'm sure he'll |
| 43 |
correct me ;-) Note that you the buffer length is implicitly |
| 44 |
returned by these APIs. |
| 45 |
|
| 46 |
/* Takes an arbitrary object which must support the character (single |
| 47 |
segment) buffer interface and returns a pointer to a read-only |
| 48 |
memory location useable as character based input for subsequent |
| 49 |
processing. |
| 50 |
|
| 51 |
buffer and buffer_len are only set in case no error |
| 52 |
occurrs. Otherwise, -1 is returned and an exception set. |
| 53 |
|
| 54 |
*/ |
| 55 |
|
| 56 |
static |
| 57 |
int PyObject_AsCharBuffer(PyObject *obj, |
| 58 |
const char **buffer, |
| 59 |
int *buffer_len) |
| 60 |
{ |
| 61 |
PyBufferProcs *pb = obj->ob_type->tp_as_buffer; |
| 62 |
const char *pp; |
| 63 |
int len; |
| 64 |
|
| 65 |
if ( pb == NULL || |
| 66 |
pb->bf_getcharbuffer == NULL || |
| 67 |
pb->bf_getsegcount == NULL ) { |
| 68 |
PyErr_SetString(PyExc_TypeError, |
| 69 |
"expected a character buffer object"); |
| 70 |
goto onError; |
| 71 |
} |
| 72 |
if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) { |
| 73 |
PyErr_SetString(PyExc_TypeError, |
| 74 |
"expected a single-segment buffer object"); |
| 75 |
goto onError; |
| 76 |
} |
| 77 |
len = (*pb->bf_getcharbuffer)(obj,0,&pp); |
| 78 |
if (len < 0) |
| 79 |
goto onError; |
| 80 |
*buffer = pp; |
| 81 |
*buffer_len = len; |
| 82 |
return 0; |
| 83 |
|
| 84 |
onError: |
| 85 |
return -1; |
| 86 |
} |
| 87 |
|
| 88 |
/* Same as PyObject_AsCharBuffer() except that this API expects a |
| 89 |
readable (single segment) buffer interface and returns a pointer |
| 90 |
to a read-only memory location which can contain arbitrary data. |
| 91 |
|
| 92 |
buffer and buffer_len are only set in case no error |
| 93 |
occurrs. Otherwise, -1 is returned and an exception set. |
| 94 |
|
| 95 |
*/ |
| 96 |
|
| 97 |
static |
| 98 |
int PyObject_AsReadBuffer(PyObject *obj, |
| 99 |
const void **buffer, |
| 100 |
int *buffer_len) |
| 101 |
{ |
| 102 |
PyBufferProcs *pb = obj->ob_type->tp_as_buffer; |
| 103 |
void *pp; |
| 104 |
int len; |
| 105 |
|
| 106 |
if ( pb == NULL || |
| 107 |
pb->bf_getreadbuffer == NULL || |
| 108 |
pb->bf_getsegcount == NULL ) { |
| 109 |
PyErr_SetString(PyExc_TypeError, |
| 110 |
"expected a readable buffer object"); |
| 111 |
goto onError; |
| 112 |
} |
| 113 |
if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) { |
| 114 |
PyErr_SetString(PyExc_TypeError, |
| 115 |
"expected a single-segment buffer object"); |
| 116 |
goto onError; |
| 117 |
} |
| 118 |
len = (*pb->bf_getreadbuffer)(obj,0,&pp); |
| 119 |
if (len < 0) |
| 120 |
goto onError; |
| 121 |
*buffer = pp; |
| 122 |
*buffer_len = len; |
| 123 |
return 0; |
| 124 |
|
| 125 |
onError: |
| 126 |
return -1; |
| 127 |
} |
| 128 |
|
| 129 |
/* Takes an arbitrary object which must support the writeable (single |
| 130 |
segment) buffer interface and returns a pointer to a writeable |
| 131 |
memory location in buffer of size buffer_len. |
| 132 |
|
| 133 |
buffer and buffer_len are only set in case no error |
| 134 |
occurrs. Otherwise, -1 is returned and an exception set. |
| 135 |
|
| 136 |
*/ |
| 137 |
|
| 138 |
static |
| 139 |
int PyObject_AsWriteBuffer(PyObject *obj, |
| 140 |
void **buffer, |
| 141 |
int *buffer_len) |
| 142 |
{ |
| 143 |
PyBufferProcs *pb = obj->ob_type->tp_as_buffer; |
| 144 |
void*pp; |
| 145 |
int len; |
| 146 |
|
| 147 |
if ( pb == NULL || |
| 148 |
pb->bf_getwritebuffer == NULL || |
| 149 |
pb->bf_getsegcount == NULL ) { |
| 150 |
PyErr_SetString(PyExc_TypeError, |
| 151 |
"expected a writeable buffer object"); |
| 152 |
goto onError; |
| 153 |
} |
| 154 |
if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) { |
| 155 |
PyErr_SetString(PyExc_TypeError, |
| 156 |
"expected a single-segment buffer object"); |
| 157 |
goto onError; |
| 158 |
} |
| 159 |
len = (*pb->bf_getwritebuffer)(obj,0,&pp); |
| 160 |
if (len < 0) |
| 161 |
goto onError; |
| 162 |
*buffer = pp; |
| 163 |
*buffer_len = len; |
| 164 |
return 0; |
| 165 |
|
| 166 |
onError: |
| 167 |
return -1; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
-- |
| 172 |
Marc-Andre Lemburg |
| 173 |
______________________________________________________________________ |
| 174 |
Y2000: 150 days left |
| 175 |
Business: http://www.lemburg.com/ |
| 176 |
Python Pages: http://www.lemburg.com/python/ |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
--===============3450783038406365800==-- |
| 182 |
|
| 183 |
|