~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/protocol.py

  • Committer: Martin Pool
  • Date: 2008-04-24 07:22:53 UTC
  • mto: This revision was merged to the branch mainline in revision 3415.
  • Revision ID: mbp@sourcefrog.net-20080424072253-opmjij7xfy38w27f
Remove every assert statement from bzrlib!

Depending on the context they are:

 * turned into an explicit if/raise of either AssertionError 
   or something more specific -- particularly where they protect
   programming interfaces, complex invariants, or data file integrity
 * removed, if they're redundant with a later check, not protecting
   a meaningful invariant
 * turned into a selftest method on tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
89
89
        
90
90
        :param bytes: must be a byte string
91
91
        """
92
 
        assert isinstance(bytes, str)
 
92
        if not isinstance(bytes, str):
 
93
            raise ValueError(bytes)
93
94
        self.in_buffer += bytes
94
95
        if not self.has_dispatched:
95
96
            if '\n' not in self.in_buffer:
133
134
            self.request.accept_body(body_data)
134
135
            if self._body_decoder.finished_reading:
135
136
                self.request.end_of_body()
136
 
                assert self.request.finished_reading, \
137
 
                    "no more body, request not finished"
 
137
                if not self.request.finished_reading:
 
138
                    raise AssertionError("no more body, request not finished")
138
139
            if self.request.response is not None:
139
140
                self._send_response(self.request.response)
140
141
                self.excess_buffer = self.in_buffer
141
142
                self.in_buffer = ''
142
143
            else:
143
 
                assert not self.request.finished_reading, \
144
 
                    "no response and we have finished reading."
 
144
                if self.request.finished_reading:
 
145
                    raise AssertionError(
 
146
                        "no response and we have finished reading.")
145
147
 
146
148
    def _send_response(self, response):
147
149
        """Send a smart server response down the output stream."""
148
 
        assert not self._finished, 'response already sent'
 
150
        if self._finished:
 
151
            raise AssertionError('response already sent')
149
152
        args = response.args
150
153
        body = response.body
151
154
        self._finished = True
153
156
        self._write_success_or_failure_prefix(response)
154
157
        self._write_func(_encode_tuple(args))
155
158
        if body is not None:
156
 
            assert isinstance(body, str), 'body must be a str'
 
159
            if not isinstance(body, str):
 
160
                raise ValueError(body)
157
161
            bytes = self._encode_bulk_data(body)
158
162
            self._write_func(bytes)
159
163
 
202
206
 
203
207
    def _send_response(self, response):
204
208
        """Send a smart server response down the output stream."""
205
 
        assert not self._finished, 'response already sent'
 
209
        if (self._finished):
 
210
            raise AssertionError('response already sent')
206
211
        self._finished = True
207
212
        self._write_protocol_version()
208
213
        self._write_success_or_failure_prefix(response)
209
214
        self._write_func(_encode_tuple(response.args))
210
215
        if response.body is not None:
211
 
            assert isinstance(response.body, str), 'body must be a str'
212
 
            assert response.body_stream is None, (
213
 
                'body_stream and body cannot both be set')
 
216
            if not isinstance(response.body, str):
 
217
                raise AssertionError('body must be a str')
 
218
            if not (response.body_stream is None):
 
219
                raise AssertionError(
 
220
                    'body_stream and body cannot both be set')
214
221
            bytes = self._encode_bulk_data(response.body)
215
222
            self._write_func(bytes)
216
223
        elif response.body_stream is not None: