Mercurial > cpython-lru_cache-weakref
annotate Modules/cStringIO.c @ 10072:ecebcd84df37 legacy-trunk
put the preferences file in a folder called "Python" inside the prefs folder, just like the IDE does -- jvr
| author | Just van Rossum <just@lettererror.com> |
|---|---|
| date | Tue, 02 Feb 1999 15:49:03 +0000 |
| parents | 08379c7f5fa9 |
| children | a63c9c3e9ecc |
| rev | line source |
|---|---|
| 4598 | 1 /* |
| 9642 | 2 * $Id$ |
|
9521
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
3 * |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
4 * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA. |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
5 * All rights reserved. |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
6 * |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
7 * Redistribution and use in source and binary forms, with or without |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
8 * modification, are permitted provided that the following conditions are |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
9 * met: |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
10 * |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
11 * o Redistributions of source code must retain the above copyright |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
12 * notice, this list of conditions, and the disclaimer that follows. |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
13 * |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
14 * o Redistributions in binary form must reproduce the above copyright |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
15 * notice, this list of conditions, and the following disclaimer in |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
16 * the documentation and/or other materials provided with the |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
17 * distribution. |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
18 * |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
19 * o All advertising materials mentioning features or use of this |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
20 * software must display the following acknowledgement: |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
21 * |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
22 * This product includes software developed by Digital Creations |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
23 * and its contributors. |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
24 * |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
25 * o Neither the name of Digital Creations nor the names of its |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
26 * contributors may be used to endorse or promote products derived |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
27 * from this software without specific prior written permission. |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
28 * |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
29 * |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
30 * THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
31 * IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
33 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
34 * CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
36 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
37 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
38 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
39 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
40 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
41 * DAMAGE. |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
42 * |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
43 # |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
44 # If you have questions regarding this software, contact: |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
45 # |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
46 # Digital Creations, L.C. |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
47 # 910 Princess Ann Street |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
48 # Fredericksburge, Virginia 22401 |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
49 # |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
50 # info@digicool.com |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
51 # |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
52 # (540) 371-6909 |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
53 */ |
| 4598 | 54 static char cStringIO_module_documentation[] = |
| 55 "A simple fast partial StringIO replacement.\n" | |
| 56 "\n" | |
| 57 "This module provides a simple useful replacement for\n" | |
| 58 "the StringIO module that is written in C. It does not provide the\n" | |
| 59 "full generality if StringIO, but it provides anough for most\n" | |
| 60 "applications and is especially useful in conjuction with the\n" | |
| 61 "pickle module.\n" | |
| 62 "\n" | |
| 63 "Usage:\n" | |
| 64 "\n" | |
| 65 " from cStringIO import StringIO\n" | |
| 66 "\n" | |
| 67 " an_output_stream=StringIO()\n" | |
| 68 " an_output_stream.write(some_stuff)\n" | |
| 69 " ...\n" | |
| 70 " value=an_output_stream.getvalue() # str(an_output_stream) works too!\n" | |
| 71 "\n" | |
| 72 " an_input_stream=StringIO(a_string)\n" | |
| 73 " spam=an_input_stream.readline()\n" | |
| 74 " spam=an_input_stream.read(5)\n" | |
|
9521
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
75 " an_input_stream.seek(0) # OK, start over\n" |
| 4598 | 76 " spam=an_input_stream.read() # and read it all\n" |
| 77 " \n" | |
| 78 "If someone else wants to provide a more complete implementation,\n" | |
| 79 "go for it. :-) \n" | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
80 "\n" |
| 9642 | 81 "$Id$\n" |
| 4598 | 82 ; |
| 83 | |
| 84 #include "Python.h" | |
| 85 #include "import.h" | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
86 #include "cStringIO.h" |
| 4598 | 87 |
| 88 #define UNLESS(E) if(!(E)) | |
| 89 | |
| 90 /* Declarations for objects of type StringO */ | |
| 91 | |
| 92 typedef struct { | |
| 93 PyObject_HEAD | |
| 94 char *buf; | |
| 9642 | 95 int pos, string_size, buf_size, softspace; |
| 4598 | 96 } Oobject; |
| 97 | |
| 98 /* Declarations for objects of type StringI */ | |
| 99 | |
| 100 typedef struct { | |
| 101 PyObject_HEAD | |
| 102 char *buf; | |
| 9642 | 103 int pos, string_size; |
| 4598 | 104 PyObject *pbuf; |
| 105 } Iobject; | |
| 106 | |
| 107 static char O_reset__doc__[] = | |
| 108 "reset() -- Reset the file position to the beginning" | |
| 109 ; | |
| 110 | |
| 111 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
112 O_reset(Oobject *self, PyObject *args) { |
| 4598 | 113 self->pos = 0; |
| 114 | |
| 115 Py_INCREF(Py_None); | |
| 116 return Py_None; | |
| 117 } | |
| 118 | |
| 119 | |
| 120 static char O_tell__doc__[] = | |
| 121 "tell() -- get the current position."; | |
| 122 | |
| 123 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
124 O_tell(Oobject *self, PyObject *args) { |
| 4598 | 125 return PyInt_FromLong(self->pos); |
| 126 } | |
| 127 | |
| 128 | |
| 129 static char O_seek__doc__[] = | |
| 130 "seek(position) -- set the current position\n" | |
| 131 "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF"; | |
| 132 | |
| 133 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
134 O_seek(Oobject *self, PyObject *args) { |
| 4598 | 135 int position, mode = 0; |
| 136 | |
|
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
|
137 UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) { |
| 4598 | 138 return NULL; |
| 139 } | |
| 140 | |
|
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
|
141 if (mode == 2) { |
| 4598 | 142 position += self->string_size; |
| 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 else if (mode == 1) { |
| 4598 | 145 position += self->pos; |
| 146 } | |
| 147 | |
|
9521
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
148 if (position > self->buf_size) { |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
149 self->buf_size*=2; |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
150 if(self->buf_size <= position) self->buf_size=position+1; |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
151 UNLESS(self->buf=(char*)realloc(self->buf,self->buf_size*sizeof(char))) { |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
152 self->buf_size=self->pos=0; |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
153 return PyErr_NoMemory(); |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
154 } |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
155 } |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
156 else if(position < 0) position=0; |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
157 |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
158 self->pos=position; |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
159 |
|
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
160 while(--position >= self->string_size) self->buf[position]=0; |
| 4598 | 161 |
| 4906 | 162 Py_INCREF(Py_None); |
| 163 return Py_None; | |
| 4598 | 164 } |
| 165 | |
| 166 static char O_read__doc__[] = | |
| 167 "read([s]) -- Read s characters, or the rest of the string" | |
| 168 ; | |
| 169 | |
| 170 static int | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
171 O_cread(PyObject *self, char **output, int n) { |
| 4598 | 172 int l; |
| 173 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
174 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
|
175 if (n < 0 || n > l) { |
| 4598 | 176 n = l; |
| 9642 | 177 if (n < 0) n=0; |
| 4598 | 178 } |
| 179 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
180 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
181 ((Oobject*)self)->pos += n; |
| 4598 | 182 return n; |
| 183 } | |
| 184 | |
| 185 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
186 O_read(Oobject *self, PyObject *args) { |
| 4598 | 187 int n = -1; |
| 188 char *output; | |
| 189 | |
| 190 UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL; | |
| 191 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
192 n=O_cread((PyObject*)self,&output,n); |
| 4598 | 193 |
| 194 return PyString_FromStringAndSize(output, n); | |
| 195 } | |
| 196 | |
| 197 | |
| 198 static char O_readline__doc__[] = | |
| 199 "readline() -- Read one line" | |
| 200 ; | |
| 201 | |
| 202 static int | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
203 O_creadline(PyObject *self, char **output) { |
| 4598 | 204 char *n, *s; |
| 205 int l; | |
| 206 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
207 for (n = ((Oobject*)self)->buf + ((Oobject*)self)->pos, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
208 s = ((Oobject*)self)->buf + ((Oobject*)self)->string_size; |
| 4598 | 209 n < s && *n != '\n'; n++); |
| 210 if (n < s) n++; | |
| 211 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
212 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
213 l = n - ((Oobject*)self)->buf - ((Oobject*)self)->pos; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
214 ((Oobject*)self)->pos += l; |
| 4598 | 215 return l; |
| 216 } | |
| 217 | |
| 218 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
219 O_readline(Oobject *self, PyObject *args) { |
| 4598 | 220 int n; |
| 221 char *output; | |
| 222 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
223 n=O_creadline((PyObject*)self,&output); |
| 4598 | 224 return PyString_FromStringAndSize(output, n); |
| 225 } | |
| 226 | |
| 227 static char O_write__doc__[] = | |
| 228 "write(s) -- Write a string to the file" | |
| 229 "\n\nNote (hack:) writing None resets the buffer" | |
| 230 ; | |
| 231 | |
| 232 | |
| 233 static int | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
234 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
|
235 int newl; |
| 4906 | 236 |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
237 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
|
238 if(newl >= ((Oobject*)self)->buf_size) { |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
239 ((Oobject*)self)->buf_size*=2; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
240 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
|
241 UNLESS(((Oobject*)self)->buf= |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
242 (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
|
243 (((Oobject*)self)->buf_size) *sizeof(char))) { |
| 4598 | 244 PyErr_SetString(PyExc_MemoryError,"out of memory"); |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
245 ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0; |
| 4598 | 246 return -1; |
| 247 } | |
| 248 } | |
| 249 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
250 memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l); |
| 4598 | 251 |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
252 ((Oobject*)self)->pos += l; |
| 4598 | 253 |
|
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
|
254 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
|
255 ((Oobject*)self)->string_size = ((Oobject*)self)->pos; |
| 4598 | 256 } |
| 257 | |
| 258 return l; | |
| 259 } | |
| 260 | |
| 261 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
262 O_write(Oobject *self, PyObject *args) { |
| 4598 | 263 PyObject *s; |
|
4957
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
264 char *c; |
|
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
265 int l; |
| 4598 | 266 |
| 9642 | 267 UNLESS(PyArg_ParseTuple(args, "O", &s)) return NULL; |
| 4906 | 268 UNLESS(-1 != (l=PyString_Size(s))) return NULL; |
| 269 UNLESS(c=PyString_AsString(s)) return NULL; | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
270 UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL; |
| 4598 | 271 |
| 4906 | 272 Py_INCREF(Py_None); |
| 273 return Py_None; | |
| 4598 | 274 } |
| 275 | |
| 9642 | 276 static char O_getval__doc__[] = |
| 277 "getvalue([use_pos]) -- Get the string value." | |
| 278 "\n" | |
| 279 "If use_pos is specified and is a true value, then the string returned\n" | |
| 280 "will include only the text up to the current file position.\n" | |
| 281 ; | |
| 282 | |
| 4598 | 283 static PyObject * |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
284 O_getval(Oobject *self, PyObject *args) { |
| 9642 | 285 PyObject *use_pos=Py_None; |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
286 int s; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
287 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
288 use_pos=Py_None; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
289 UNLESS(PyArg_ParseTuple(args,"|O",&use_pos)) return NULL; |
| 9642 | 290 if(PyObject_IsTrue(use_pos)) { |
| 291 s=self->pos; | |
| 292 if (s > self->string_size) s=self->string_size; | |
| 293 } | |
| 294 else | |
| 295 s=self->string_size; | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
296 return PyString_FromStringAndSize(self->buf, s); |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
297 } |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
298 |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
299 static PyObject * |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
300 O_cgetval(PyObject *self) { |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
301 return PyString_FromStringAndSize(((Oobject*)self)->buf, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
302 ((Oobject*)self)->pos); |
| 4598 | 303 } |
| 304 | |
| 305 static char O_truncate__doc__[] = | |
| 306 "truncate(): truncate the file at the current position."; | |
| 307 | |
| 308 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
309 O_truncate(Oobject *self, PyObject *args) { |
| 9642 | 310 if (self->string_size > self->pos) |
| 311 self->string_size = self->pos; | |
| 4598 | 312 Py_INCREF(Py_None); |
| 313 return Py_None; | |
| 314 } | |
| 315 | |
| 316 static char O_isatty__doc__[] = "isatty(): always returns 0"; | |
| 317 | |
| 318 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
319 O_isatty(Oobject *self, PyObject *args) { |
| 4598 | 320 return PyInt_FromLong(0); |
| 321 } | |
| 322 | |
| 323 static char O_close__doc__[] = "close(): explicitly release resources held."; | |
| 324 | |
| 325 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
326 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
|
327 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
|
328 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
|
329 self->buf = NULL; |
| 4598 | 330 |
| 331 self->pos = self->string_size = self->buf_size = 0; | |
| 332 | |
| 333 Py_INCREF(Py_None); | |
| 334 return Py_None; | |
| 335 } | |
| 336 | |
| 337 static char O_flush__doc__[] = "flush(): does nothing."; | |
| 338 | |
| 339 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
340 O_flush(Oobject *self, PyObject *args) { |
| 4598 | 341 Py_INCREF(Py_None); |
| 342 return Py_None; | |
| 343 } | |
| 344 | |
| 345 | |
| 346 static char O_writelines__doc__[] = "blah"; | |
| 347 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
348 O_writelines(Oobject *self, PyObject *args) { |
| 4598 | 349 PyObject *string_module = 0; |
| 350 static PyObject *string_joinfields = 0; | |
| 351 | |
| 9642 | 352 UNLESS(PyArg_ParseTuple(args, "O", args)) { |
| 4598 | 353 return NULL; |
| 354 } | |
| 355 | |
|
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
|
356 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
|
357 UNLESS(string_module = PyImport_ImportModule("string")) { |
| 4598 | 358 return NULL; |
| 359 } | |
| 360 | |
| 361 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
|
362 PyObject_GetAttrString(string_module, "joinfields")) { |
| 4598 | 363 return NULL; |
| 364 } | |
| 365 | |
| 366 Py_DECREF(string_module); | |
| 367 } | |
| 368 | |
|
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
|
369 if (PyObject_Length(args) == -1) { |
| 4598 | 370 return NULL; |
| 371 } | |
| 372 | |
| 373 return O_write(self, | |
| 374 PyObject_CallFunction(string_joinfields, "Os", args, "")); | |
| 375 } | |
| 376 | |
| 377 static struct PyMethodDef O_methods[] = { | |
| 9642 | 378 {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__}, |
| 379 {"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__}, | |
| 380 {"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__}, | |
| 381 {"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__}, | |
| 382 {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__}, | |
| 383 {"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__}, | |
| 384 {"getvalue", (PyCFunction)O_getval, METH_VARARGS, O_getval__doc__}, | |
| 385 {"truncate", (PyCFunction)O_truncate, METH_VARARGS, O_truncate__doc__}, | |
| 386 {"isatty", (PyCFunction)O_isatty, METH_VARARGS, O_isatty__doc__}, | |
| 387 {"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__}, | |
| 388 {"flush", (PyCFunction)O_flush, METH_VARARGS, O_flush__doc__}, | |
| 389 {"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__}, | |
| 390 {NULL, NULL} /* sentinel */ | |
| 4598 | 391 }; |
| 392 | |
| 393 | |
| 394 static void | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
395 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
|
396 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
|
397 free(self->buf); |
| 4598 | 398 PyMem_DEL(self); |
| 399 } | |
| 400 | |
| 401 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
402 O_getattr(Oobject *self, char *name) { |
|
9521
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
403 if (strcmp(name, "softspace") == 0) { |
|
5315
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
404 return PyInt_FromLong(self->softspace); |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
405 } |
| 4598 | 406 return Py_FindMethod(O_methods, (PyObject *)self, name); |
| 407 } | |
| 408 | |
|
5315
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
409 static int |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
410 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
|
411 long x; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
412 if (strcmp(name, "softspace") != 0) { |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
413 PyErr_SetString(PyExc_AttributeError, name); |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
414 return -1; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
415 } |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
416 x = PyInt_AsLong(value); |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
417 if (x == -1 && PyErr_Occurred()) |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
418 return -1; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
419 self->softspace = x; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
420 return 0; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
421 } |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
422 |
| 4598 | 423 static char Otype__doc__[] = |
| 424 "Simple type for output to strings." | |
| 425 ; | |
| 426 | |
| 427 static PyTypeObject Otype = { | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
428 PyObject_HEAD_INIT(NULL) |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
429 0, /*ob_size*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
430 "StringO", /*tp_name*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
431 sizeof(Oobject), /*tp_basicsize*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
432 0, /*tp_itemsize*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
433 /* methods */ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
434 (destructor)O_dealloc, /*tp_dealloc*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
435 (printfunc)0, /*tp_print*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
436 (getattrfunc)O_getattr, /*tp_getattr*/ |
|
5315
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
437 (setattrfunc)O_setattr, /*tp_setattr*/ |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
438 (cmpfunc)0, /*tp_compare*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
439 (reprfunc)0, /*tp_repr*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
440 0, /*tp_as_number*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
441 0, /*tp_as_sequence*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
442 0, /*tp_as_mapping*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
443 (hashfunc)0, /*tp_hash*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
444 (ternaryfunc)0, /*tp_call*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
445 (reprfunc)0, /*tp_str*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
446 |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
447 /* Space for future expansion */ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
448 0L,0L,0L,0L, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
449 Otype__doc__ /* Documentation string */ |
| 4598 | 450 }; |
| 451 | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
452 static PyObject * |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
453 newOobject(int size) { |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
454 Oobject *self; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
455 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
456 self = PyObject_NEW(Oobject, &Otype); |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
457 if (self == NULL) |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
458 return NULL; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
459 self->pos=0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
460 self->string_size = 0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
461 self->softspace = 0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
462 |
|
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
|
463 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
|
464 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
|
465 self->buf_size = 0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
466 return NULL; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
467 } |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
468 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
469 self->buf_size=size; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
470 return (PyObject*)self; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
471 } |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
472 |
| 4598 | 473 /* End of code for StringO objects */ |
| 474 /* -------------------------------------------------------- */ | |
| 475 | |
| 476 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
477 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
|
478 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
|
479 self->pbuf = NULL; |
| 4598 | 480 |
| 481 self->pos = self->string_size = 0; | |
| 9642 | 482 |
| 483 Py_INCREF(Py_None); | |
| 484 return Py_None; | |
| 485 } | |
| 486 | |
| 487 static PyObject * | |
| 488 I_seek(Oobject *self, PyObject *args) { | |
| 489 int position, mode = 0; | |
| 490 | |
| 491 UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) { | |
| 492 return NULL; | |
| 493 } | |
| 494 | |
| 495 if (mode == 2) { | |
| 496 position += self->string_size; | |
| 497 } | |
| 498 else if (mode == 1) { | |
| 499 position += self->pos; | |
| 500 } | |
| 501 | |
| 502 if(position < 0) position=0; | |
| 503 | |
| 504 self->pos=position; | |
| 4598 | 505 |
| 506 Py_INCREF(Py_None); | |
| 507 return Py_None; | |
| 508 } | |
| 509 | |
| 510 static struct PyMethodDef I_methods[] = { | |
| 9642 | 511 {"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__}, |
| 512 {"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__}, | |
| 513 {"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__}, | |
| 514 {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__}, | |
| 515 {"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__}, | |
| 516 {"getvalue", (PyCFunction)O_getval, METH_VARARGS, O_getval__doc__}, | |
| 517 {"truncate", (PyCFunction)O_truncate, METH_VARARGS, O_truncate__doc__}, | |
| 518 {"isatty", (PyCFunction)O_isatty, METH_VARARGS, O_isatty__doc__}, | |
| 519 {"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__}, | |
| 520 {"flush", (PyCFunction)O_flush, METH_VARARGS, O_flush__doc__}, | |
| 521 {NULL, NULL} | |
| 4598 | 522 }; |
| 523 | |
| 524 static void | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
525 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
|
526 Py_XDECREF(self->pbuf); |
| 4598 | 527 PyMem_DEL(self); |
| 528 } | |
| 529 | |
| 530 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
531 I_getattr(Iobject *self, char *name) { |
| 4598 | 532 return Py_FindMethod(I_methods, (PyObject *)self, name); |
| 533 } | |
| 534 | |
| 535 static char Itype__doc__[] = | |
| 536 "Simple type for treating strings as input file streams" | |
| 537 ; | |
| 538 | |
| 539 static PyTypeObject Itype = { | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
540 PyObject_HEAD_INIT(NULL) |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
541 0, /*ob_size*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
542 "StringI", /*tp_name*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
543 sizeof(Iobject), /*tp_basicsize*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
544 0, /*tp_itemsize*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
545 /* methods */ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
546 (destructor)I_dealloc, /*tp_dealloc*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
547 (printfunc)0, /*tp_print*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
548 (getattrfunc)I_getattr, /*tp_getattr*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
549 (setattrfunc)0, /*tp_setattr*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
550 (cmpfunc)0, /*tp_compare*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
551 (reprfunc)0, /*tp_repr*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
552 0, /*tp_as_number*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
553 0, /*tp_as_sequence*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
554 0, /*tp_as_mapping*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
555 (hashfunc)0, /*tp_hash*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
556 (ternaryfunc)0, /*tp_call*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
557 (reprfunc)0, /*tp_str*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
558 |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
559 /* Space for future expansion */ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
560 0L,0L,0L,0L, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
561 Itype__doc__ /* Documentation string */ |
| 4598 | 562 }; |
| 563 | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
564 static PyObject * |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
565 newIobject(PyObject *s) { |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
566 Iobject *self; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
567 char *buf; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
568 int size; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
569 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
570 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
|
571 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
|
572 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
|
573 Py_INCREF(s); |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
574 self->buf=buf; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
575 self->string_size=size; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
576 self->pbuf=s; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
577 self->pos=0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
578 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
579 return (PyObject*)self; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
580 } |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
581 |
| 4598 | 582 /* End of code for StringI objects */ |
| 583 /* -------------------------------------------------------- */ | |
| 584 | |
| 585 | |
| 586 static char IO_StringIO__doc__[] = | |
| 587 "StringIO([s]) -- Return a StringIO-like stream for reading or writing" | |
| 588 ; | |
| 589 | |
| 590 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
591 IO_StringIO(PyObject *self, PyObject *args) { |
| 4598 | 592 PyObject *s=0; |
| 593 | |
|
9521
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
594 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
|
595 if(s) return newIobject(s); |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
596 return newOobject(128); |
| 4598 | 597 } |
| 598 | |
| 599 /* List of methods defined in the module */ | |
| 600 | |
| 601 static struct PyMethodDef IO_methods[] = { | |
| 4906 | 602 {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__}, |
| 603 {NULL, NULL} /* sentinel */ | |
| 4598 | 604 }; |
| 605 | |
| 606 | |
| 607 /* Initialization function for the module (*must* be called initcStringIO) */ | |
| 608 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
609 static struct PycStringIO_CAPI CAPI = { |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
610 O_cread, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
611 O_creadline, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
612 O_cwrite, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
613 O_cgetval, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
614 newOobject, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
615 newIobject, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
616 &Itype, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
617 &Otype, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
618 }; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
619 |
| 9642 | 620 #ifndef DL_EXPORT /* declarations for DLL import/export */ |
| 621 #define DL_EXPORT(RTYPE) RTYPE | |
| 622 #endif | |
|
9588
38023a425e48
Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents:
9521
diff
changeset
|
623 DL_EXPORT(void) |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
624 initcStringIO() { |
|
6227
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
625 PyObject *m, *d, *v; |
| 4598 | 626 |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
627 |
| 4598 | 628 /* Create the module and add the functions */ |
| 629 m = Py_InitModule4("cStringIO", IO_methods, | |
| 630 cStringIO_module_documentation, | |
| 631 (PyObject*)NULL,PYTHON_API_VERSION); | |
| 632 | |
| 633 /* Add some symbolic constants to the module */ | |
| 634 d = PyModule_GetDict(m); | |
| 635 | |
| 4906 | 636 /* Export C API */ |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
637 Itype.ob_type=&PyType_Type; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
638 Otype.ob_type=&PyType_Type; |
|
6227
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
639 PyDict_SetItemString(d,"cStringIO_CAPI", |
|
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
640 v = PyCObject_FromVoidPtr(&CAPI,NULL)); |
|
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
641 Py_XDECREF(v); |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
642 |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
643 /* Export Types */ |
| 4598 | 644 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); |
| 645 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
|
646 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
647 /* Maybe make certain warnings go away */ |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
648 if(0) PycString_IMPORT; |
| 4598 | 649 |
| 650 /* Check for errors */ | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
651 if (PyErr_Occurred()) Py_FatalError("can't initialize module cStringIO"); |
| 4598 | 652 } |
