Debian Codegraph @

rpython/rlib/rStringIO.py

raw symbols SCIP
1 from rpython.rlib.rstring import StringBuilder
2 from rpython.rlib.objectmodel import we_are_translated
3
4 AT_END = -1
5
6
7 class RStringIO(object):
8 """RPython-level StringIO object.
9 The fastest path through this code is for the case of a bunch of write()
10 followed by getvalue().
11 """
12 def __init__(self):
13 self.init()
14
15 def init(self):
16 # The real content is the join of the following data:
17 # * the list of characters self.__bigbuffer;
18 # * each of the strings in self.__strings.
19 #
20 self.__closed = False
21 self.__strings = None
22 self.__bigbuffer = None
23 self.__pos = AT_END
24
25 def close(self):
26 self.__closed = True
27 self.__strings = None
28 self.__bigbuffer = None
29 self.__pos = AT_END
30
31 def is_closed(self):
32 return self.__closed
33
34 def __copy_into_bigbuffer(self):
35 """Copy all the data into the list of characters self.__bigbuffer."""
36 if self.__bigbuffer is None:
37 self.__bigbuffer = []
38 if self.__strings is not None:
39 self.__bigbuffer += self.__strings.build()
40 self.__strings = None
41
42 def getvalue(self):
43 """If self.__strings contains more than 1 string, join all the
44 strings together. Return the final single string."""
45 if self.__bigbuffer is not None:
46 self.__copy_into_bigbuffer()
47 return ''.join(self.__bigbuffer)
48 if self.__strings is not None:
49 return self.__strings.build()
50 return ''
51
52 def getsize(self):
53 result = 0
54 if self.__bigbuffer is not None:
55 result += len(self.__bigbuffer)
56 if self.__strings is not None:
57 result += self.__strings.getlength()
58 return result
59
60 def write(self, buffer):
61 # Idea: for the common case of a sequence of write() followed
62 # by only getvalue(), self.__bigbuffer remains empty. It is only
63 # used to handle the more complicated cases.
64 if self.__pos == AT_END:
65 self.__fast_write(buffer)
66 else:
67 self.__slow_write(buffer)
68
69 def __fast_write(self, buffer):
70 if self.__strings is None:
71 self.__strings = StringBuilder()
72 self.__strings.append(buffer)
73
74 def __slow_write(self, buffer):
75 assert buffer is not None # help annotator
76 p = self.__pos
77 assert p >= 0
78 endp = p + len(buffer)
79 if self.__bigbuffer is not None and len(self.__bigbuffer) >= endp:
80 # semi-fast path: the write is entirely inside self.__bigbuffer
81 for i in range(len(buffer)):
82 self.__bigbuffer[p + i] = buffer[i]
83 else:
84 # slow path: collect all data into self.__bigbuffer and
85 # handle the various cases
86 self.__copy_into_bigbuffer()
87 fitting = len(self.__bigbuffer) - p
88 if fitting > 0:
89 # the write starts before the end of the data
90 fitting = min(len(buffer), fitting)
91 for i in range(fitting):
92 self.__bigbuffer[p + i] = buffer[i]
93 if len(buffer) > fitting:
94 # the write extends beyond the end of the data
95 self.__bigbuffer += buffer[fitting:]
96 endp = AT_END
97 else:
98 # the write starts at or beyond the end of the data
99 self.__bigbuffer += '\x00' * (-fitting) + buffer
100 endp = AT_END
101 self.__pos = endp
102
103 def seek(self, position, mode=0):
104 if mode == 0:
105 if position == self.getsize():
106 self.__pos = AT_END
107 return
108 elif mode == 1:
109 if self.__pos == AT_END:
110 self.__pos = self.getsize()
111 position += self.__pos
112 elif mode == 2:
113 if position == 0:
114 self.__pos = AT_END
115 return
116 position += self.getsize()
117 if position < 0:
118 position = 0
119 self.__pos = position
120
121 def tell(self):
122 if self.__pos == AT_END:
123 result = self.getsize()
124 else:
125 result = self.__pos
126 assert result >= 0
127 return result
128
129 def read(self, size=-1):
130 p = self.__pos
131 if p == 0 and size < 0:
132 self.__pos = AT_END
133 return self.getvalue() # reading everything
134 if p == AT_END or size == 0:
135 return ''
136 assert p >= 0
137 self.__copy_into_bigbuffer()
138 mysize = len(self.__bigbuffer)
139 count = mysize - p
140 if size >= 0:
141 count = min(size, count)
142 if count <= 0:
143 return ''
144 if p == 0 and count == mysize:
145 self.__pos = AT_END
146 return ''.join(self.__bigbuffer)
147 else:
148 self.__pos = p + count
149 return ''.join(self.__bigbuffer[p:p+count])
150
151 def readline(self, size=-1):
152 p = self.__pos
153 if p == AT_END or size == 0:
154 return ''
155 assert p >= 0
156 self.__copy_into_bigbuffer()
157 end = len(self.__bigbuffer)
158 count = end - p
159 if size >= 0 and size < count:
160 end = p + size
161 if count <= 0:
162 return ''
163 i = p
164 while i < end:
165 finished = self.__bigbuffer[i] == '\n'
166 i += 1
167 if finished:
168 break
169 self.__pos = i
170 if not we_are_translated():
171 # assert that we read within the bounds!
172 bl = len(self.__bigbuffer)
173 assert p <= bl
174 assert i <= bl
175 return ''.join(self.__bigbuffer[p:i])
176
177 def truncate(self, size):
178 """Warning, this gets us slightly strange behavior from the
179 point of view of a traditional Unix file, but consistent with
180 Python 2.7's cStringIO module: it will not enlarge the file,
181 and it will always seek to the (new) end of the file."""
182 assert size >= 0
183 if size == 0:
184 self.__bigbuffer = None
185 self.__strings = None
186 else:
187 if self.__bigbuffer is None or size > len(self.__bigbuffer):
188 self.__copy_into_bigbuffer()
189 else:
190 # we can drop all extra strings
191 if self.__strings is not None:
192 self.__strings = None
193 if size < len(self.__bigbuffer):
194 del self.__bigbuffer[size:]
195 if len(self.__bigbuffer) == 0:
196 self.__bigbuffer = None
197 # it always has the effect of seeking at the new end
198 self.__pos = AT_END