~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/pack.py

  • Committer: Aaron Bentley
  • Date: 2007-06-28 17:13:06 UTC
  • mto: (2520.4.116 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2572.
  • Revision ID: abentley@panoramicfeedback.com-20070628171306-scpsxn9g89cchzz8
Use file-like objects as container input, not callables

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
 
97
97
class BaseReader(object):
98
98
 
99
 
    def __init__(self, reader_func):
 
99
    def __init__(self, source_file):
100
100
        """Constructor.
101
101
 
102
102
        :param reader_func: a callable that takes one optional argument,
104
104
            returns less than the requested number of bytes, then the end of the
105
105
            file/stream has been reached.
106
106
        """
107
 
        self.reader_func = reader_func
 
107
        self._source = source_file
 
108
 
 
109
    def reader_func(self, length=None):
 
110
        return self._source.read(length)
108
111
 
109
112
    def _read_line(self):
110
 
        """Read a line from the input stream.
111
 
 
112
 
        This is a simple but inefficient implementation that just reads one byte
113
 
        at a time.  Lines should not be very long, so this is probably
114
 
        tolerable.
115
 
 
116
 
        :returns: a line, without the trailing newline
117
 
        """
118
 
        # XXX: Have a maximum line length, to prevent malicious input from
119
 
        # consuming an unreasonable amount of resources?
120
 
        #   -- Andrew Bennetts, 2007-05-07.
121
 
        line = ''
122
 
        while not line.endswith('\n'):
123
 
            byte = self.reader_func(1)
124
 
            if byte == '':
125
 
                raise errors.UnexpectedEndOfContainerError()
126
 
            line += byte
127
 
        return line[:-1]
 
113
        line = self._source.readline()
 
114
        if not line.endswith('\n'):
 
115
            raise errors.UnexpectedEndOfContainerError()
 
116
        return line.rstrip('\n')
128
117
 
129
118
 
130
119
class ContainerReader(BaseReader):
180
169
            record_kind = self.reader_func(1)
181
170
            if record_kind == 'B':
182
171
                # Bytes record.
183
 
                reader = BytesRecordReader(self.reader_func)
 
172
                reader = BytesRecordReader(self._source)
184
173
                yield reader
185
174
            elif record_kind == 'E':
186
175
                # End marker.  There are no more records.