~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/pack.py

  • Committer: Martin Pool
  • Date: 2009-07-01 07:22:00 UTC
  • mto: This revision was merged to the branch mainline in revision 4502.
  • Revision ID: mbp@sourcefrog.net-20090701072200-hh21x94g1g11dll7
ReadVFile copes if readv result isn't an iter; also better errors

Show diffs side-by-side

added added

removed removed

Lines of Context:
169
169
    # gradually consume the readv result.
170
170
 
171
171
    def __init__(self, readv_result):
 
172
        """Construct a new ReadVFile wrapper.
 
173
 
 
174
        :seealso: make_readv_reader
 
175
 
 
176
        :param readv_result: the most recent readv result - list or generator
 
177
        """
 
178
        # we rely on its state as a generator to keep track of how much has
 
179
        # been used.
 
180
        if not getattr(readv_result, 'next'):
 
181
            readv_result = iter(readv_result)
172
182
        self.readv_result = readv_result
173
 
        # the most recent readv result block
174
183
        self._string = None
175
184
 
176
185
    def _next(self):
184
193
        self._next()
185
194
        result = self._string.read(length)
186
195
        if len(result) < length:
187
 
            raise errors.BzrError('request for too much data from a readv hunk.')
 
196
            raise errors.BzrError('wanted %d bytes but next '
 
197
                'hunk only contains %d: %r...' %
 
198
                (length, len(result), result[:20]))
188
199
        return result
189
200
 
190
201
    def readline(self):
192
203
        self._next()
193
204
        result = self._string.readline()
194
205
        if self._string.tell() == self._string_length and result[-1] != '\n':
195
 
            raise errors.BzrError('short readline in the readvfile hunk.')
 
206
            raise errors.BzrError('short readline in the readvfile hunk: %r'
 
207
                % (readline, ))
196
208
        return result
197
209
 
198
210