| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Jython
Revision: 5231
Author: pjenvey
Date: 21 Aug 2008 15:44:29
Changes:make cStringIO thread safe as unfortunately the CPython GIL guarantees it, as
does the pure python version. add cStringIO.In/OutputType
| ... | ...@@ -16,6 +16,7 @@ | |
| 16 | 16 | import org.python.core.PyList; |
| 17 | 17 | import org.python.core.PyObject; |
| 18 | 18 | import org.python.core.PyString; |
| 19 | import org.python.core.PyType; | |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | 22 | * This module implements a file-like class, StringIO, that reads and |
| ... | ...@@ -37,6 +38,9 @@ | |
| 37 | 38 | public static final int SEEK_CUR = 1; |
| 38 | 39 | public static final int SEEK_END = 2; |
| 39 | 40 | } |
| 41 | ||
| 42 | public static PyType InputType = PyType.fromClass(StringIO.class); | |
| 43 | public static PyType OutputType = PyType.fromClass(StringIO.class); | |
| 40 | 44 | |
| 41 | 45 | public static StringIO StringIO() { |
| 42 | 46 | return new StringIO(); |
| ... | ...@@ -133,7 +137,7 @@ | |
| 133 | 137 | * @param pos the position in the file. |
| 134 | 138 | * @param mode; 0=from the start, 1=relative, 2=from the end. |
| 135 | 139 | */ |
| 136 | public void seek(long pos, int mode) { | |
| 140 | public synchronized void seek(long pos, int mode) { | |
| 137 | 141 | _complain_ifclosed(); |
| 138 | 142 | switch (mode) { |
| 139 | 143 | case os.SEEK_CUR: |
| ... | ...@@ -152,7 +156,7 @@ | |
| 152 | 156 | /** |
| 153 | 157 | * Reset the file position to the beginning of the file. |
| 154 | 158 | */ |
| 155 | public void reset() { | |
| 159 | public synchronized void reset() { | |
| 156 | 160 | pos = 0; |
| 157 | 161 | } |
| 158 | 162 | |
| ... | ...@@ -160,7 +164,7 @@ | |
| 160 | 164 | * Return the file position. |
| 161 | 165 | * @returns the position in the file. |
| 162 | 166 | */ |
| 163 | public int tell() { | |
| 167 | public synchronized int tell() { | |
| 164 | 168 | _complain_ifclosed(); |
| 165 | 169 | return pos; |
| 166 | 170 | } |
| ... | ...@@ -186,7 +190,7 @@ | |
| 186 | 190 | * @returns A string containing the data read. |
| 187 | 191 | */ |
| 188 | 192 | |
| 189 | public String read(long size) { | |
| 193 | public synchronized String read(long size) { | |
| 190 | 194 | _complain_ifclosed(); |
| 191 | 195 | int size_int = _convert_to_int(size); |
| 192 | 196 | int len = buf.length(); |
| ... | ...@@ -225,7 +229,7 @@ | |
| 225 | 229 | * returned. |
| 226 | 230 | * @returns data from the file up to and including the newline. |
| 227 | 231 | */ |
| 228 | public String readline(long size) { | |
| 232 | public synchronized String readline(long size) { | |
| 229 | 233 | _complain_ifclosed(); |
| 230 | 234 | int size_int = _convert_to_int(size); |
| 231 | 235 | int len = buf.length(); |
| ... | ...@@ -247,7 +251,7 @@ | |
| 247 | 251 | * Read and return a line without the trailing newline. |
| 248 | 252 | * Usind by cPickle as an optimization. |
| 249 | 253 | */ |
| 250 | public String readlineNoNl() { | |
| 254 | public synchronized String readlineNoNl() { | |
| 251 | 255 | _complain_ifclosed(); |
| 252 | 256 | int len = buf.length(); |
| 253 | 257 | int i = buf.indexOf("\n", pos); |
| ... | ...@@ -303,7 +307,7 @@ | |
| 303 | 307 | /** |
| 304 | 308 | * truncate the file at the position pos. |
| 305 | 309 | */ |
| 306 | public void truncate(long pos) { | |
| 310 | public synchronized void truncate(long pos) { | |
| 307 | 311 | int pos_int = _convert_to_int(pos); |
| 308 | 312 | if (pos_int < 0) |
| 309 | 313 | pos_int = this.pos; |
| ... | ...@@ -320,7 +324,7 @@ | |
| 320 | 324 | write(obj.toString()); |
| 321 | 325 | } |
| 322 | 326 | |
| 323 | public void write(String s) { | |
| 327 | public synchronized void write(String s) { | |
| 324 | 328 | _complain_ifclosed(); |
| 325 | 329 | buf.setLength(pos); |
| 326 | 330 | int newpos = pos + s.length(); |
| ... | ...@@ -332,7 +336,7 @@ | |
| 332 | 336 | * Write a char to the file. Used by cPickle as an optimization. |
| 333 | 337 | * @param ch The data to write. |
| 334 | 338 | */ |
| 335 | public void writeChar(char ch) { | |
| 339 | public synchronized void writeChar(char ch) { | |
| 336 | 340 | int len = buf.length(); |
| 337 | 341 | if (len <= pos) |
| 338 | 342 | buf.setLength(pos + 1); |
| ... | ...@@ -363,7 +367,7 @@ | |
| 363 | 367 | * before the StringIO object's close() method is called. |
| 364 | 368 | * @return the contents of the StringIO. |
| 365 | 369 | */ |
| 366 | public String getvalue() { | |
| 370 | public synchronized String getvalue() { | |
| 367 | 371 | return buf.toString(); |
| 368 | 372 | } |
| 369 | 373 |