/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 bzrlib/smart/message.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
try:
18
 
    from collections.abc import deque
19
 
except ImportError:  # python < 3.7
20
 
    from collections import deque
21
 
 
22
 
from io import (
23
 
    BytesIO,
24
 
    )
25
 
 
26
 
from ... import (
 
17
from __future__ import absolute_import
 
18
 
 
19
import collections
 
20
from cStringIO import StringIO
 
21
 
 
22
from bzrlib import (
27
23
    debug,
28
24
    errors,
29
25
    )
30
 
from ...trace import mutter
 
26
from bzrlib.trace import mutter
31
27
 
32
28
 
33
29
class MessageHandler(object):
115
111
        self.responder.send_error(exception)
116
112
 
117
113
    def byte_part_received(self, byte):
118
 
        if not isinstance(byte, bytes):
119
 
            raise TypeError(byte)
120
114
        if self.expecting == 'body':
121
 
            if byte == b'S':
 
115
            if byte == 'S':
122
116
                # Success.  Nothing more to come except the end of message.
123
117
                self.expecting = 'end'
124
 
            elif byte == b'E':
 
118
            elif byte == 'E':
125
119
                # Error.  Expect an error structure.
126
120
                self.expecting = 'error'
127
121
            else:
220
214
        MessageHandler.__init__(self)
221
215
        self.status = None
222
216
        self.args = None
223
 
        self._bytes_parts = deque()
 
217
        self._bytes_parts = collections.deque()
224
218
        self._body_started = False
225
219
        self._body_stream_status = None
226
220
        self._body = None
232
226
        self._medium_request = medium_request
233
227
 
234
228
    def byte_part_received(self, byte):
235
 
        if not isinstance(byte, bytes):
236
 
            raise TypeError(byte)
237
 
        if byte not in [b'E', b'S']:
 
229
        if byte not in ['E', 'S']:
238
230
            raise errors.SmartProtocolError(
239
231
                'Unknown response status: %r' % (byte,))
240
232
        if self._body_started:
253
245
        self._bytes_parts.append(bytes)
254
246
 
255
247
    def structure_part_received(self, structure):
256
 
        if not isinstance(structure, tuple):
 
248
        if type(structure) is not tuple:
257
249
            raise errors.SmartProtocolError(
258
250
                'Args structure is not a sequence: %r' % (structure,))
259
251
        if not self._body_started:
263
255
                    % (structure, self.args))
264
256
            self.args = structure
265
257
        else:
266
 
            if self._body_stream_status != b'E':
 
258
            if self._body_stream_status != 'E':
267
259
                raise errors.SmartProtocolError(
268
260
                    'Unexpected structure received after body: %r'
269
261
                    % (structure,))
284
276
            self.finished_reading = True
285
277
            self._medium_request.finished_reading()
286
278
            return
287
 
        data = self._medium_request.read_bytes(next_read_size)
288
 
        if data == b'':
 
279
        bytes = self._medium_request.read_bytes(next_read_size)
 
280
        if bytes == '':
289
281
            # end of file encountered reading from server
290
282
            if 'hpss' in debug.debug_flags:
291
283
                mutter(
296
288
                "Unexpected end of message. "
297
289
                "Please check connectivity and permissions, and report a bug "
298
290
                "if problems persist.")
299
 
        self._protocol_decoder.accept_bytes(data)
 
291
        self._protocol_decoder.accept_bytes(bytes)
300
292
 
301
293
    def protocol_error(self, exception):
302
294
        # Whatever the error is, we're done with this request.
311
303
            self._wait_for_response_end()
312
304
        if 'hpss' in debug.debug_flags:
313
305
            mutter('   result:   %r', self.args)
314
 
        if self.status == b'E':
 
306
        if self.status == 'E':
315
307
            self._wait_for_response_end()
316
308
            _raise_smart_server_error(self.args)
317
309
        return tuple(self.args)
329
321
        # != -1.  (2008/04/30, Andrew Bennetts)
330
322
        if self._body is None:
331
323
            self._wait_for_response_end()
332
 
            body_bytes = b''.join(self._bytes_parts)
 
324
            body_bytes = ''.join(self._bytes_parts)
333
325
            if 'hpss' in debug.debug_flags:
334
326
                mutter('              %d body bytes read', len(body_bytes))
335
 
            self._body = BytesIO(body_bytes)
 
327
            self._body = StringIO(body_bytes)
336
328
            self._bytes_parts = None
337
329
        return self._body.read(count)
338
330
 
344
336
                    mutter('              %d byte part read', len(bytes_part))
345
337
                yield bytes_part
346
338
            self._read_more()
347
 
        if self._body_stream_status == b'E':
 
339
        if self._body_stream_status == 'E':
348
340
            _raise_smart_server_error(self._body_error_args)
349
341
 
350
342
    def cancel_read_body(self):
354
346
def _raise_smart_server_error(error_tuple):
355
347
    """Raise exception based on tuple received from smart server
356
348
 
357
 
    Specific error translation is handled by breezy.bzr.remote._translate_error
 
349
    Specific error translation is handled by bzrlib.remote._translate_error
358
350
    """
359
 
    if error_tuple[0] == b'UnknownMethod':
 
351
    if error_tuple[0] == 'UnknownMethod':
360
352
        raise errors.UnknownSmartMethod(error_tuple[1])
361
353
    raise errors.ErrorFromSmartServer(error_tuple)