Mercurial > cpython-lru_cache-weakref
annotate Modules/cStringIO.c @ 7278:59d2761ba1cf legacy-trunk
Remove obsolete pagenumbering / pagestyle cruft.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Fri, 13 Feb 1998 07:08:56 +0000 |
| parents | 1853b5bc0548 |
| children | bbbd0b105e27 |
| rev | line source |
|---|---|
| 4598 | 1 /* |
| 2 | |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
3 cStringIO.c,v 1.23 1997/12/04 00:12:05 jim Exp |
| 4598 | 4 |
| 5 A simple fast partial StringIO replacement. | |
| 6 | |
| 7 | |
| 8 | |
| 9 Copyright | |
| 10 | |
| 11 Copyright 1996 Digital Creations, L.C., 910 Princess Anne | |
| 12 Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All | |
| 13 rights reserved. Copyright in this software is owned by DCLC, | |
| 14 unless otherwise indicated. Permission to use, copy and | |
| 15 distribute this software is hereby granted, provided that the | |
| 16 above copyright notice appear in all copies and that both that | |
| 17 copyright notice and this permission notice appear. Note that | |
| 18 any product, process or technology described in this software | |
| 19 may be the subject of other Intellectual Property rights | |
| 20 reserved by Digital Creations, L.C. and are not licensed | |
| 21 hereunder. | |
| 22 | |
| 23 Trademarks | |
| 24 | |
| 25 Digital Creations & DCLC, are trademarks of Digital Creations, L.C.. | |
| 26 All other trademarks are owned by their respective companies. | |
| 27 | |
| 28 No Warranty | |
| 29 | |
| 30 The software is provided "as is" without warranty of any kind, | |
| 31 either express or implied, including, but not limited to, the | |
| 32 implied warranties of merchantability, fitness for a particular | |
| 33 purpose, or non-infringement. This software could include | |
| 34 technical inaccuracies or typographical errors. Changes are | |
| 35 periodically made to the software; these changes will be | |
| 36 incorporated in new editions of the software. DCLC may make | |
| 37 improvements and/or changes in this software at any time | |
| 38 without notice. | |
| 39 | |
| 40 Limitation Of Liability | |
| 41 | |
| 42 In no event will DCLC be liable for direct, indirect, special, | |
| 43 incidental, economic, cover, or consequential damages arising | |
| 44 out of the use of or inability to use this software even if | |
| 45 advised of the possibility of such damages. Some states do not | |
| 46 allow the exclusion or limitation of implied warranties or | |
| 47 limitation of liability for incidental or consequential | |
| 48 damages, so the above limitation or exclusion may not apply to | |
| 49 you. | |
| 50 | |
| 51 If you have questions regarding this software, | |
| 52 contact: | |
| 53 | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
54 info@digicool.com |
| 4598 | 55 Digital Creations L.C. |
| 56 | |
| 57 (540) 371-6909 | |
| 58 | |
| 59 | |
| 60 */ | |
| 61 static char cStringIO_module_documentation[] = | |
| 62 "A simple fast partial StringIO replacement.\n" | |
| 63 "\n" | |
| 64 "This module provides a simple useful replacement for\n" | |
| 65 "the StringIO module that is written in C. It does not provide the\n" | |
| 66 "full generality if StringIO, but it provides anough for most\n" | |
| 67 "applications and is especially useful in conjuction with the\n" | |
| 68 "pickle module.\n" | |
| 69 "\n" | |
| 70 "Usage:\n" | |
| 71 "\n" | |
| 72 " from cStringIO import StringIO\n" | |
| 73 "\n" | |
| 74 " an_output_stream=StringIO()\n" | |
| 75 " an_output_stream.write(some_stuff)\n" | |
| 76 " ...\n" | |
| 77 " value=an_output_stream.getvalue() # str(an_output_stream) works too!\n" | |
| 78 "\n" | |
| 79 " an_input_stream=StringIO(a_string)\n" | |
| 80 " spam=an_input_stream.readline()\n" | |
| 81 " spam=an_input_stream.read(5)\n" | |
| 82 " an_input_stream.reset() # OK, start over, note no seek yet\n" | |
| 83 " spam=an_input_stream.read() # and read it all\n" | |
| 84 " \n" | |
| 85 "If someone else wants to provide a more complete implementation,\n" | |
| 86 "go for it. :-) \n" | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
87 "\n" |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
88 "cStringIO.c,v 1.23 1997/12/04 00:12:05 jim Exp\n" |
| 4598 | 89 ; |
| 90 | |
| 91 #include "Python.h" | |
| 92 #include "import.h" | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
93 #include "cStringIO.h" |
| 4598 | 94 |
| 95 #define UNLESS(E) if(!(E)) | |
| 96 | |
| 97 /* Declarations for objects of type StringO */ | |
| 98 | |
| 99 typedef struct { | |
| 100 PyObject_HEAD | |
| 101 char *buf; | |
|
5315
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
102 int pos, string_size, buf_size, closed, softspace; |
| 4598 | 103 } Oobject; |
| 104 | |
| 105 /* Declarations for objects of type StringI */ | |
| 106 | |
| 107 typedef struct { | |
| 108 PyObject_HEAD | |
| 109 char *buf; | |
| 110 int pos, string_size, closed; | |
| 111 PyObject *pbuf; | |
| 112 } Iobject; | |
| 113 | |
| 114 static char O_reset__doc__[] = | |
| 115 "reset() -- Reset the file position to the beginning" | |
| 116 ; | |
| 117 | |
| 118 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
119 O_reset(Oobject *self, PyObject *args) { |
| 4598 | 120 self->pos = 0; |
| 121 | |
| 122 Py_INCREF(Py_None); | |
| 123 return Py_None; | |
| 124 } | |
| 125 | |
| 126 | |
| 127 static char O_tell__doc__[] = | |
| 128 "tell() -- get the current position."; | |
| 129 | |
| 130 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
131 O_tell(Oobject *self, PyObject *args) { |
| 4598 | 132 return PyInt_FromLong(self->pos); |
| 133 } | |
| 134 | |
| 135 | |
| 136 static char O_seek__doc__[] = | |
| 137 "seek(position) -- set the current position\n" | |
| 138 "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF"; | |
| 139 | |
| 140 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
141 O_seek(Oobject *self, PyObject *args) { |
| 4598 | 142 int position, mode = 0; |
| 143 | |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
144 UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) { |
| 4598 | 145 return NULL; |
| 146 } | |
| 147 | |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
148 if (mode == 2) { |
| 4598 | 149 position += self->string_size; |
| 150 } | |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
151 else if (mode == 1) { |
| 4598 | 152 position += self->pos; |
| 153 } | |
| 154 | |
| 155 self->pos = (position > self->string_size ? self->string_size : | |
| 156 (position < 0 ? 0 : position)); | |
| 157 | |
| 4906 | 158 Py_INCREF(Py_None); |
| 159 return Py_None; | |
| 4598 | 160 } |
| 161 | |
| 162 static char O_read__doc__[] = | |
| 163 "read([s]) -- Read s characters, or the rest of the string" | |
| 164 ; | |
| 165 | |
| 166 static int | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
167 O_cread(PyObject *self, char **output, int n) { |
| 4598 | 168 int l; |
| 169 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
170 l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos; |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
171 if (n < 0 || n > l) { |
| 4598 | 172 n = l; |
| 173 } | |
| 174 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
175 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
176 ((Oobject*)self)->pos += n; |
| 4598 | 177 return n; |
| 178 } | |
| 179 | |
| 180 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
181 O_read(Oobject *self, PyObject *args) { |
| 4598 | 182 int n = -1; |
| 183 char *output; | |
| 184 | |
| 185 UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL; | |
| 186 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
187 n=O_cread((PyObject*)self,&output,n); |
| 4598 | 188 |
| 189 return PyString_FromStringAndSize(output, n); | |
| 190 } | |
| 191 | |
| 192 | |
| 193 static char O_readline__doc__[] = | |
| 194 "readline() -- Read one line" | |
| 195 ; | |
| 196 | |
| 197 static int | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
198 O_creadline(PyObject *self, char **output) { |
| 4598 | 199 char *n, *s; |
| 200 int l; | |
| 201 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
202 for (n = ((Oobject*)self)->buf + ((Oobject*)self)->pos, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
203 s = ((Oobject*)self)->buf + ((Oobject*)self)->string_size; |
| 4598 | 204 n < s && *n != '\n'; n++); |
| 205 if (n < s) n++; | |
| 206 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
207 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
208 l = n - ((Oobject*)self)->buf - ((Oobject*)self)->pos; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
209 ((Oobject*)self)->pos += l; |
| 4598 | 210 return l; |
| 211 } | |
| 212 | |
| 213 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
214 O_readline(Oobject *self, PyObject *args) { |
| 4598 | 215 int n; |
| 216 char *output; | |
| 217 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
218 n=O_creadline((PyObject*)self,&output); |
| 4598 | 219 return PyString_FromStringAndSize(output, n); |
| 220 } | |
| 221 | |
| 222 static char O_write__doc__[] = | |
| 223 "write(s) -- Write a string to the file" | |
| 224 "\n\nNote (hack:) writing None resets the buffer" | |
| 225 ; | |
| 226 | |
| 227 | |
| 228 static int | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
229 O_cwrite(PyObject *self, char *c, int l) { |
|
4957
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
230 int newl; |
| 4906 | 231 |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
232 newl=((Oobject*)self)->pos+l; |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
233 if(newl >= ((Oobject*)self)->buf_size) { |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
234 ((Oobject*)self)->buf_size*=2; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
235 if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
236 UNLESS(((Oobject*)self)->buf= |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
237 (char*)realloc(((Oobject*)self)->buf, |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
238 (((Oobject*)self)->buf_size) *sizeof(char))) { |
| 4598 | 239 PyErr_SetString(PyExc_MemoryError,"out of memory"); |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
240 ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0; |
| 4598 | 241 return -1; |
| 242 } | |
| 243 } | |
| 244 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
245 memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l); |
| 4598 | 246 |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
247 ((Oobject*)self)->pos += l; |
| 4598 | 248 |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
249 if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) { |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
250 ((Oobject*)self)->string_size = ((Oobject*)self)->pos; |
| 4598 | 251 } |
| 252 | |
| 253 return l; | |
| 254 } | |
| 255 | |
| 256 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
257 O_write(Oobject *self, PyObject *args) { |
| 4598 | 258 PyObject *s; |
|
4957
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
259 char *c; |
|
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
260 int l; |
| 4598 | 261 |
| 262 UNLESS(PyArg_Parse(args, "O", &s)) return NULL; | |
| 4906 | 263 UNLESS(-1 != (l=PyString_Size(s))) return NULL; |
| 264 UNLESS(c=PyString_AsString(s)) return NULL; | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
265 UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL; |
| 4598 | 266 |
| 4906 | 267 Py_INCREF(Py_None); |
| 268 return Py_None; | |
| 4598 | 269 } |
| 270 | |
| 271 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
272 O_getval(Oobject *self, PyObject *args) { |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
273 PyObject *use_pos; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
274 int s; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
275 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
276 use_pos=Py_None; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
277 UNLESS(PyArg_ParseTuple(args,"|O",&use_pos)) return NULL; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
278 if(PyObject_IsTrue(use_pos)) s=self->pos; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
279 else s=self->string_size; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
280 return PyString_FromStringAndSize(self->buf, s); |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
281 } |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
282 |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
283 static PyObject * |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
284 O_cgetval(PyObject *self) { |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
285 return PyString_FromStringAndSize(((Oobject*)self)->buf, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
286 ((Oobject*)self)->pos); |
| 4598 | 287 } |
| 288 | |
| 289 static char O_truncate__doc__[] = | |
| 290 "truncate(): truncate the file at the current position."; | |
| 291 | |
| 292 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
293 O_truncate(Oobject *self, PyObject *args) { |
| 4598 | 294 self->string_size = self->pos; |
| 295 Py_INCREF(Py_None); | |
| 296 return Py_None; | |
| 297 } | |
| 298 | |
| 299 static char O_isatty__doc__[] = "isatty(): always returns 0"; | |
| 300 | |
| 301 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
302 O_isatty(Oobject *self, PyObject *args) { |
| 4598 | 303 return PyInt_FromLong(0); |
| 304 } | |
| 305 | |
| 306 static char O_close__doc__[] = "close(): explicitly release resources held."; | |
| 307 | |
| 308 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
309 O_close(Oobject *self, PyObject *args) { |
|
6215
eb7a01b86f93
Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents:
5991
diff
changeset
|
310 if (self->buf != NULL) |
|
eb7a01b86f93
Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents:
5991
diff
changeset
|
311 free(self->buf); |
|
eb7a01b86f93
Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents:
5991
diff
changeset
|
312 self->buf = NULL; |
| 4598 | 313 |
| 314 self->pos = self->string_size = self->buf_size = 0; | |
| 315 self->closed = 1; | |
| 316 | |
| 317 Py_INCREF(Py_None); | |
| 318 return Py_None; | |
| 319 } | |
| 320 | |
| 321 static char O_flush__doc__[] = "flush(): does nothing."; | |
| 322 | |
| 323 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
324 O_flush(Oobject *self, PyObject *args) { |
| 4598 | 325 Py_INCREF(Py_None); |
| 326 return Py_None; | |
| 327 } | |
| 328 | |
| 329 | |
| 330 static char O_writelines__doc__[] = "blah"; | |
| 331 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
332 O_writelines(Oobject *self, PyObject *args) { |
| 4598 | 333 PyObject *string_module = 0; |
| 334 static PyObject *string_joinfields = 0; | |
| 335 | |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
336 UNLESS(PyArg_Parse(args, "O", args)) { |
| 4598 | 337 return NULL; |
| 338 } | |
| 339 | |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
340 if (!string_joinfields) { |
|
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
341 UNLESS(string_module = PyImport_ImportModule("string")) { |
| 4598 | 342 return NULL; |
| 343 } | |
| 344 | |
| 345 UNLESS(string_joinfields= | |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
346 PyObject_GetAttrString(string_module, "joinfields")) { |
| 4598 | 347 return NULL; |
| 348 } | |
| 349 | |
| 350 Py_DECREF(string_module); | |
| 351 } | |
| 352 | |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
353 if (PyObject_Length(args) == -1) { |
| 4598 | 354 return NULL; |
| 355 } | |
| 356 | |
| 357 return O_write(self, | |
| 358 PyObject_CallFunction(string_joinfields, "Os", args, "")); | |
| 359 } | |
| 360 | |
| 361 static struct PyMethodDef O_methods[] = { | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
362 {"write", (PyCFunction)O_write, 0, O_write__doc__}, |
| 4598 | 363 {"read", (PyCFunction)O_read, 1, O_read__doc__}, |
| 364 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, | |
| 365 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, | |
| 366 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, | |
| 367 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
368 {"getvalue", (PyCFunction)O_getval, 1, |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
369 "getvalue([use_pos]) -- Get the string value." |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
370 "\n" |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
371 "If use_pos is specified and is a true value, then the string returned\n" |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
372 "will include only the text up to the current file position.\n" |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
373 }, |
| 4598 | 374 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, |
| 375 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, | |
| 376 {"close", (PyCFunction)O_close, 0, O_close__doc__}, | |
| 377 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, | |
| 378 {"writelines", (PyCFunction)O_writelines, 0, O_writelines__doc__}, | |
| 379 {NULL, NULL} /* sentinel */ | |
| 380 }; | |
| 381 | |
| 382 | |
| 383 static void | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
384 O_dealloc(Oobject *self) { |
|
6215
eb7a01b86f93
Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents:
5991
diff
changeset
|
385 if (self->buf != NULL) |
|
eb7a01b86f93
Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents:
5991
diff
changeset
|
386 free(self->buf); |
| 4598 | 387 PyMem_DEL(self); |
| 388 } | |
| 389 | |
| 390 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
391 O_getattr(Oobject *self, char *name) { |
|
5315
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
392 if (strcmp(name, "softspace") == 0) { |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
393 return PyInt_FromLong(self->softspace); |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
394 } |
| 4598 | 395 return Py_FindMethod(O_methods, (PyObject *)self, name); |
| 396 } | |
| 397 | |
|
5315
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
398 static int |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
399 O_setattr(Oobject *self, char *name, PyObject *value) { |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
400 long x; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
401 if (strcmp(name, "softspace") != 0) { |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
402 PyErr_SetString(PyExc_AttributeError, name); |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
403 return -1; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
404 } |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
405 x = PyInt_AsLong(value); |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
406 if (x == -1 && PyErr_Occurred()) |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
407 return -1; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
408 self->softspace = x; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
409 return 0; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
410 } |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
411 |
| 4598 | 412 static char Otype__doc__[] = |
| 413 "Simple type for output to strings." | |
| 414 ; | |
| 415 | |
| 416 static PyTypeObject Otype = { | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
417 PyObject_HEAD_INIT(NULL) |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
418 0, /*ob_size*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
419 "StringO", /*tp_name*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
420 sizeof(Oobject), /*tp_basicsize*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
421 0, /*tp_itemsize*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
422 /* methods */ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
423 (destructor)O_dealloc, /*tp_dealloc*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
424 (printfunc)0, /*tp_print*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
425 (getattrfunc)O_getattr, /*tp_getattr*/ |
|
5315
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
426 (setattrfunc)O_setattr, /*tp_setattr*/ |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
427 (cmpfunc)0, /*tp_compare*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
428 (reprfunc)0, /*tp_repr*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
429 0, /*tp_as_number*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
430 0, /*tp_as_sequence*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
431 0, /*tp_as_mapping*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
432 (hashfunc)0, /*tp_hash*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
433 (ternaryfunc)0, /*tp_call*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
434 (reprfunc)0, /*tp_str*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
435 |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
436 /* Space for future expansion */ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
437 0L,0L,0L,0L, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
438 Otype__doc__ /* Documentation string */ |
| 4598 | 439 }; |
| 440 | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
441 static PyObject * |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
442 newOobject(int size) { |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
443 Oobject *self; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
444 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
445 self = PyObject_NEW(Oobject, &Otype); |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
446 if (self == NULL) |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
447 return NULL; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
448 self->pos=0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
449 self->closed = 0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
450 self->string_size = 0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
451 self->softspace = 0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
452 |
|
6740
1853b5bc0548
New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents:
6227
diff
changeset
|
453 UNLESS(self->buf=malloc(size*sizeof(char))) { |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
454 PyErr_SetString(PyExc_MemoryError,"out of memory"); |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
455 self->buf_size = 0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
456 return NULL; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
457 } |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
458 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
459 self->buf_size=size; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
460 return (PyObject*)self; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
461 } |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
462 |
| 4598 | 463 /* End of code for StringO objects */ |
| 464 /* -------------------------------------------------------- */ | |
| 465 | |
| 466 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
467 I_close(Iobject *self, PyObject *args) { |
|
6215
eb7a01b86f93
Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents:
5991
diff
changeset
|
468 Py_XDECREF(self->pbuf); |
|
eb7a01b86f93
Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents:
5991
diff
changeset
|
469 self->pbuf = NULL; |
| 4598 | 470 |
| 471 self->pos = self->string_size = 0; | |
| 472 self->closed = 1; | |
| 473 | |
| 474 Py_INCREF(Py_None); | |
| 475 return Py_None; | |
| 476 } | |
| 477 | |
| 478 static struct PyMethodDef I_methods[] = { | |
| 479 {"read", (PyCFunction)O_read, 1, O_read__doc__}, | |
| 480 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, | |
| 481 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, | |
| 482 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, | |
| 483 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, | |
| 484 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, | |
| 485 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, | |
| 486 {"close", (PyCFunction)I_close, 0, O_close__doc__}, | |
| 487 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, | |
| 488 {NULL, NULL} /* sentinel */ | |
| 489 }; | |
| 490 | |
| 491 static void | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
492 I_dealloc(Iobject *self) { |
|
6215
eb7a01b86f93
Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents:
5991
diff
changeset
|
493 Py_XDECREF(self->pbuf); |
| 4598 | 494 PyMem_DEL(self); |
| 495 } | |
| 496 | |
| 497 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
498 I_getattr(Iobject *self, char *name) { |
| 4598 | 499 return Py_FindMethod(I_methods, (PyObject *)self, name); |
| 500 } | |
| 501 | |
| 502 static char Itype__doc__[] = | |
| 503 "Simple type for treating strings as input file streams" | |
| 504 ; | |
| 505 | |
| 506 static PyTypeObject Itype = { | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
507 PyObject_HEAD_INIT(NULL) |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
508 0, /*ob_size*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
509 "StringI", /*tp_name*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
510 sizeof(Iobject), /*tp_basicsize*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
511 0, /*tp_itemsize*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
512 /* methods */ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
513 (destructor)I_dealloc, /*tp_dealloc*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
514 (printfunc)0, /*tp_print*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
515 (getattrfunc)I_getattr, /*tp_getattr*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
516 (setattrfunc)0, /*tp_setattr*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
517 (cmpfunc)0, /*tp_compare*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
518 (reprfunc)0, /*tp_repr*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
519 0, /*tp_as_number*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
520 0, /*tp_as_sequence*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
521 0, /*tp_as_mapping*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
522 (hashfunc)0, /*tp_hash*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
523 (ternaryfunc)0, /*tp_call*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
524 (reprfunc)0, /*tp_str*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
525 |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
526 /* Space for future expansion */ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
527 0L,0L,0L,0L, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
528 Itype__doc__ /* Documentation string */ |
| 4598 | 529 }; |
| 530 | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
531 static PyObject * |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
532 newIobject(PyObject *s) { |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
533 Iobject *self; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
534 char *buf; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
535 int size; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
536 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
537 UNLESS(buf=PyString_AsString(s)) return NULL; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
538 UNLESS(-1 != (size=PyString_Size(s))) return NULL; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
539 UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
540 Py_INCREF(s); |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
541 self->buf=buf; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
542 self->string_size=size; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
543 self->pbuf=s; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
544 self->pos=0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
545 self->closed = 0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
546 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
547 return (PyObject*)self; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
548 } |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
549 |
| 4598 | 550 /* End of code for StringI objects */ |
| 551 /* -------------------------------------------------------- */ | |
| 552 | |
| 553 | |
| 554 static char IO_StringIO__doc__[] = | |
| 555 "StringIO([s]) -- Return a StringIO-like stream for reading or writing" | |
| 556 ; | |
| 557 | |
| 558 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
559 IO_StringIO(PyObject *self, PyObject *args) { |
| 4598 | 560 PyObject *s=0; |
| 561 | |
| 562 UNLESS(PyArg_ParseTuple(args, "|O", &s)) return NULL; | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
563 if(s) return newIobject(s); |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
564 return newOobject(128); |
| 4598 | 565 } |
| 566 | |
| 567 /* List of methods defined in the module */ | |
| 568 | |
| 569 static struct PyMethodDef IO_methods[] = { | |
| 4906 | 570 {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__}, |
| 571 {NULL, NULL} /* sentinel */ | |
| 4598 | 572 }; |
| 573 | |
| 574 | |
| 575 /* Initialization function for the module (*must* be called initcStringIO) */ | |
| 576 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
577 static struct PycStringIO_CAPI CAPI = { |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
578 O_cread, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
579 O_creadline, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
580 O_cwrite, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
581 O_cgetval, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
582 newOobject, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
583 newIobject, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
584 &Itype, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
585 &Otype, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
586 }; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
587 |
| 4598 | 588 void |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
589 initcStringIO() { |
|
6227
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
590 PyObject *m, *d, *v; |
| 4598 | 591 |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
592 |
| 4598 | 593 /* Create the module and add the functions */ |
| 594 m = Py_InitModule4("cStringIO", IO_methods, | |
| 595 cStringIO_module_documentation, | |
| 596 (PyObject*)NULL,PYTHON_API_VERSION); | |
| 597 | |
| 598 /* Add some symbolic constants to the module */ | |
| 599 d = PyModule_GetDict(m); | |
| 600 | |
| 4906 | 601 /* Export C API */ |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
602 Itype.ob_type=&PyType_Type; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
603 Otype.ob_type=&PyType_Type; |
|
6227
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
604 PyDict_SetItemString(d,"cStringIO_CAPI", |
|
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
605 v = PyCObject_FromVoidPtr(&CAPI,NULL)); |
|
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
606 Py_XDECREF(v); |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
607 |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
608 /* Export Types */ |
| 4598 | 609 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); |
| 610 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype); | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
611 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
612 /* Maybe make certain warnings go away */ |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
613 if(0) PycString_IMPORT; |
| 4598 | 614 |
| 615 /* Check for errors */ | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
616 if (PyErr_Occurred()) Py_FatalError("can't initialize module cStringIO"); |
| 4598 | 617 } |
