/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/iterablefile.py

  • Committer: Jelmer Vernooij
  • Date: 2018-03-30 14:47:14 UTC
  • mto: This revision was merged to the branch mainline in revision 6930.
  • Revision ID: jelmer@jelmer.uk-20180330144714-sncrnhqh8y4nmpbr
Add basic tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
 
18
from __future__ import absolute_import
18
19
 
19
20
class IterableFileBase(object):
20
21
    """Create a file-like object from any iterable"""
22
23
    def __init__(self, iterable):
23
24
        object.__init__(self)
24
25
        self._iter = iterable.__iter__()
25
 
        self._buffer = ""
 
26
        self._buffer = b""
26
27
        self.done = False
27
28
 
28
29
    def read_n(self, length):
66
67
        result = self._buffer
67
68
        while result_length(result) is None:
68
69
            try:
69
 
                result += self._iter.next()
 
70
                result += next(self._iter)
70
71
            except StopIteration:
71
72
                self.done = True
72
 
                self._buffer = ""
 
73
                self._buffer = b""
73
74
                return result
74
75
        output_length = result_length(result)
75
76
        self._buffer = result[output_length:]
109
110
    def _make_iterator(self):
110
111
        while not self._file_base.done:
111
112
            self._check_closed()
112
 
            result = self._file_base.read_to('\n')
113
 
            if result != '':
 
113
            result = self._file_base.read_to(b'\n')
 
114
            if result != b'':
114
115
                yield result
115
116
 
116
117
    def _check_closed(self):
131
132
 
132
133
    closed = property(lambda x: x._closed)
133
134
 
 
135
    def __enter__(self):
 
136
        return self
 
137
 
 
138
    def __exit__(self, exc_type, exc_val, exc_tb):
 
139
        # If there was an error raised, prefer the original one
 
140
        try:
 
141
            self.close()
 
142
        except:
 
143
            if exc_type is None:
 
144
                raise
 
145
        return False
 
146
 
134
147
    def flush(self):
135
148
        """No-op for standard compliance.
136
149
        >>> f = IterableFile([])
141
154
        """
142
155
        self._check_closed()
143
156
 
144
 
    def next(self):
 
157
    def __next__(self):
145
158
        """Implementation of the iterator protocol's next()
146
159
 
147
160
        >>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.'])
148
 
        >>> f.next()
 
161
        >>> next(f)
149
162
        'This \\n'
150
163
        >>> f.close()
151
 
        >>> f.next()
 
164
        >>> next(f)
152
165
        Traceback (most recent call last):
153
166
        ValueError: File is closed.
154
167
        >>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.\\n'])
155
 
        >>> f.next()
 
168
        >>> next(f)
156
169
        'This \\n'
157
 
        >>> f.next()
 
170
        >>> next(f)
158
171
        'is a test.\\n'
159
 
        >>> f.next()
 
172
        >>> next(f)
160
173
        Traceback (most recent call last):
161
174
        StopIteration
162
175
        """
163
176
        self._check_closed()
164
 
        return self._iter.next()
 
177
        return next(self._iter)
 
178
 
 
179
    next = __next__
165
180
 
166
181
    def __iter__(self):
167
182
        """
224
239
        Traceback (most recent call last):
225
240
        ValueError: File is closed.
226
241
        """
227
 
        return self.read_to('\n', size)
 
242
        return self.read_to(b'\n', size)
228
243
 
229
244
    def readlines(self, sizehint=None):
230
245
        """
240
255
        lines = []
241
256
        while True:
242
257
            line = self.readline()
243
 
            if line == "":
 
258
            if line == b"":
244
259
                return lines
245
260
            if sizehint is None:
246
261
                lines.append(line)