/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: 2020-02-07 02:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 7492.
  • Revision ID: jelmer@jelmer.uk-20200207021430-m49iq3x4x8xlib6x
Drop python2 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
    def read_n(self, length):
31
31
        """
32
 
        >>> IterableFileBase(['This ', 'is ', 'a ', 'test.']).read_n(8)
33
 
        'This is '
 
32
        >>> IterableFileBase([b'This ', b'is ', b'a ', b'test.']).read_n(8)
 
33
        b'This is '
34
34
        """
35
35
        def test_length(result):
36
36
            if len(result) >= length:
41
41
 
42
42
    def read_to(self, sequence, length=None):
43
43
        """
44
 
        >>> f = IterableFileBase(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
45
 
        >>> f.read_to('\\n')
46
 
        'Th\\n'
47
 
        >>> f.read_to('\\n')
48
 
        'is is \\n'
 
44
        >>> f = IterableFileBase([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
 
45
        >>> f.read_to(b'\\n')
 
46
        b'Th\\n'
 
47
        >>> f.read_to(b'\\n')
 
48
        b'is is \\n'
49
49
        """
50
50
        def test_contents(result):
51
51
            if length is not None:
79
79
 
80
80
    def read_all(self):
81
81
        """
82
 
        >>> IterableFileBase(['This ', 'is ', 'a ', 'test.']).read_all()
83
 
        'This is a test.'
 
82
        >>> IterableFileBase([b'This ', b'is ', b'a ', b'test.']).read_all()
 
83
        b'This is a test.'
84
84
        """
85
85
        def no_stop(result):
86
86
            return None
88
88
 
89
89
    def push_back(self, contents):
90
90
        """
91
 
        >>> f = IterableFileBase(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
92
 
        >>> f.read_to('\\n')
93
 
        'Th\\n'
94
 
        >>> f.push_back("Sh")
 
91
        >>> f = IterableFileBase([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
 
92
        >>> f.read_to(b'\\n')
 
93
        b'Th\\n'
 
94
        >>> f.push_back(b"Sh")
95
95
        >>> f.read_all()
96
 
        'Shis is \\na te\\nst.'
 
96
        b'Shis is \\na te\\nst.'
97
97
        """
98
98
        self._buffer = contents + self._buffer
99
99
 
121
121
 
122
122
    def close(self):
123
123
        """
124
 
        >>> f = IterableFile(['This ', 'is ', 'a ', 'test.'])
 
124
        >>> f = IterableFile([b'This ', b'is ', b'a ', b'test.'])
125
125
        >>> f.closed
126
126
        False
127
127
        >>> f.close()
158
158
    def __next__(self):
159
159
        """Implementation of the iterator protocol's next()
160
160
 
161
 
        >>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.'])
 
161
        >>> f = IterableFile([b'This \\n', b'is ', b'a ', b'test.'])
162
162
        >>> next(f)
163
 
        'This \\n'
 
163
        b'This \\n'
164
164
        >>> f.close()
165
165
        >>> next(f)
166
166
        Traceback (most recent call last):
167
167
        ValueError: File is closed.
168
 
        >>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.\\n'])
169
 
        >>> next(f)
170
 
        'This \\n'
171
 
        >>> next(f)
172
 
        'is a test.\\n'
 
168
        >>> f = IterableFile([b'This \\n', b'is ', b'a ', b'test.\\n'])
 
169
        >>> next(f)
 
170
        b'This \\n'
 
171
        >>> next(f)
 
172
        b'is a test.\\n'
173
173
        >>> next(f)
174
174
        Traceback (most recent call last):
175
175
        StopIteration
181
181
 
182
182
    def __iter__(self):
183
183
        """
184
 
        >>> list(IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.']))
185
 
        ['Th\\n', 'is is \\n', 'a te\\n', 'st.']
186
 
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
184
        >>> list(IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.']))
 
185
        [b'Th\\n', b'is is \\n', b'a te\\n', b'st.']
 
186
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
187
187
        >>> f.close()
188
188
        >>> list(f)
189
189
        Traceback (most recent call last):
193
193
 
194
194
    def read(self, length=None):
195
195
        """
196
 
        >>> IterableFile(['This ', 'is ', 'a ', 'test.']).read()
197
 
        'This is a test.'
198
 
        >>> f = IterableFile(['This ', 'is ', 'a ', 'test.'])
 
196
        >>> IterableFile([b'This ', b'is ', b'a ', b'test.']).read()
 
197
        b'This is a test.'
 
198
        >>> f = IterableFile([b'This ', b'is ', b'a ', b'test.'])
199
199
        >>> f.read(10)
200
 
        'This is a '
201
 
        >>> f = IterableFile(['This ', 'is ', 'a ', 'test.'])
 
200
        b'This is a '
 
201
        >>> f = IterableFile([b'This ', b'is ', b'a ', b'test.'])
202
202
        >>> f.close()
203
203
        >>> f.read(10)
204
204
        Traceback (most recent call last):
215
215
        Read characters until a sequence is found, with optional max size.
216
216
        The specified sequence, if found, will be included in the result
217
217
 
218
 
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
219
 
        >>> f.read_to('i')
220
 
        'Th\\ni'
221
 
        >>> f.read_to('i')
222
 
        's i'
 
218
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
 
219
        >>> f.read_to(b'i')
 
220
        b'Th\\ni'
 
221
        >>> f.read_to(b'i')
 
222
        b's i'
223
223
        >>> f.close()
224
 
        >>> f.read_to('i')
 
224
        >>> f.read_to(b'i')
225
225
        Traceback (most recent call last):
226
226
        ValueError: File is closed.
227
227
        """
230
230
 
231
231
    def readline(self, size=None):
232
232
        """
233
 
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
233
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
234
234
        >>> f.readline()
235
 
        'Th\\n'
 
235
        b'Th\\n'
236
236
        >>> f.readline(4)
237
 
        'is i'
 
237
        b'is i'
238
238
        >>> f.close()
239
239
        >>> f.readline()
240
240
        Traceback (most recent call last):
244
244
 
245
245
    def readlines(self, sizehint=None):
246
246
        """
247
 
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
247
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
248
248
        >>> f.readlines()
249
 
        ['Th\\n', 'is is \\n', 'a te\\n', 'st.']
250
 
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
249
        [b'Th\\n', b'is is \\n', b'a te\\n', b'st.']
 
250
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
251
251
        >>> f.close()
252
252
        >>> f.readlines()
253
253
        Traceback (most recent call last):