Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7ed0e3

Browse files
committed
Accept Finn Bock's patch #102208 to hardcode EINVAL to 22 when errno
can't be imported. This makes StringIO.py work with Jython. Also, get rid of the string module by converting to string methods. Shorten some lines by using augmented assignment where appropriate.
1 parent 132dce2 commit c7ed0e3

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

Lib/StringIO.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929
- There's a simple test set (see end of this file).
3030
"""
3131

32-
import errno
33-
import string
32+
try:
33+
from errno import EINVAL
34+
except ImportError:
35+
EINVAL = 22
36+
37+
EMPTYSTRING = ''
3438

3539
class StringIO:
3640
def __init__(self, buf = ''):
@@ -52,12 +56,12 @@ def seek(self, pos, mode = 0):
5256
if self.closed:
5357
raise ValueError, "I/O operation on closed file"
5458
if self.buflist:
55-
self.buf = self.buf + string.joinfields(self.buflist, '')
59+
self.buf += EMPTYSTRING.join(self.buflist)
5660
self.buflist = []
5761
if mode == 1:
58-
pos = pos + self.pos
62+
pos += self.pos
5963
elif mode == 2:
60-
pos = pos + self.len
64+
pos += self.len
6165
self.pos = max(0, pos)
6266
def tell(self):
6367
if self.closed:
@@ -67,7 +71,7 @@ def read(self, n = -1):
6771
if self.closed:
6872
raise ValueError, "I/O operation on closed file"
6973
if self.buflist:
70-
self.buf = self.buf + string.joinfields(self.buflist, '')
74+
self.buf += EMPTYSTRING.join(self.buflist)
7175
self.buflist = []
7276
if n < 0:
7377
newpos = self.len
@@ -80,9 +84,9 @@ def readline(self, length=None):
8084
if self.closed:
8185
raise ValueError, "I/O operation on closed file"
8286
if self.buflist:
83-
self.buf = self.buf + string.joinfields(self.buflist, '')
87+
self.buf += EMPTYSTRING.join(self.buflist)
8488
self.buflist = []
85-
i = string.find(self.buf, '\n', self.pos)
89+
i = self.buf.find('\n', self.pos)
8690
if i < 0:
8791
newpos = self.len
8892
else:
@@ -110,8 +114,7 @@ def truncate(self, size=None):
110114
if size is None:
111115
size = self.pos
112116
elif size < 0:
113-
raise IOError(errno.EINVAL,
114-
"Negative size not allowed")
117+
raise IOError(EINVAL, "Negative size not allowed")
115118
elif size < self.pos:
116119
self.pos = size
117120
self.buf = self.getvalue()[:size]
@@ -125,7 +128,7 @@ def write(self, s):
125128
newpos = self.pos + len(s)
126129
if self.pos < self.len:
127130
if self.buflist:
128-
self.buf = self.buf + string.joinfields(self.buflist, '')
131+
self.buf += EMPTYSTRING.join(self.buflist)
129132
self.buflist = []
130133
self.buflist = [self.buf[:self.pos], s, self.buf[newpos:]]
131134
self.buf = ''
@@ -136,13 +139,13 @@ def write(self, s):
136139
self.len = newpos
137140
self.pos = newpos
138141
def writelines(self, list):
139-
self.write(string.joinfields(list, ''))
142+
self.write(EMPTYSTRING.join(list))
140143
def flush(self):
141144
if self.closed:
142145
raise ValueError, "I/O operation on closed file"
143146
def getvalue(self):
144147
if self.buflist:
145-
self.buf = self.buf + string.joinfields(self.buflist, '')
148+
self.buf += EMPTYSTRING.join(self.buflist)
146149
self.buflist = []
147150
return self.buf
148151

0 commit comments

Comments
 (0)