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

  • Committer: Jelmer Vernooij
  • Date: 2019-10-13 22:53:02 UTC
  • mfrom: (7290.1.35 work)
  • mto: This revision was merged to the branch mainline in revision 7405.
  • Revision ID: jelmer@jelmer.uk-20191013225302-vg88ztajzq05hkas
Merge lp:brz/3.0.

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
 
import collections
18
 
from cStringIO import StringIO
19
 
 
20
 
from bzrlib import (
 
17
from __future__ import absolute_import
 
18
 
 
19
try:
 
20
    from collections.abc import deque
 
21
except ImportError:  # python < 3.7
 
22
    from collections import deque
 
23
 
 
24
from ... import (
21
25
    debug,
22
26
    errors,
23
27
    )
24
 
from bzrlib.trace import mutter
 
28
from ...sixish import (
 
29
    BytesIO,
 
30
    )
 
31
from ...trace import mutter
25
32
 
26
33
 
27
34
class MessageHandler(object):
109
116
        self.responder.send_error(exception)
110
117
 
111
118
    def byte_part_received(self, byte):
 
119
        if not isinstance(byte, bytes):
 
120
            raise TypeError(byte)
112
121
        if self.expecting == 'body':
113
 
            if byte == 'S':
 
122
            if byte == b'S':
114
123
                # Success.  Nothing more to come except the end of message.
115
124
                self.expecting = 'end'
116
 
            elif byte == 'E':
 
125
            elif byte == b'E':
117
126
                # Error.  Expect an error structure.
118
127
                self.expecting = 'error'
119
128
            else:
212
221
        MessageHandler.__init__(self)
213
222
        self.status = None
214
223
        self.args = None
215
 
        self._bytes_parts = collections.deque()
 
224
        self._bytes_parts = deque()
216
225
        self._body_started = False
217
226
        self._body_stream_status = None
218
227
        self._body = None
224
233
        self._medium_request = medium_request
225
234
 
226
235
    def byte_part_received(self, byte):
227
 
        if byte not in ['E', 'S']:
 
236
        if not isinstance(byte, bytes):
 
237
            raise TypeError(byte)
 
238
        if byte not in [b'E', b'S']:
228
239
            raise errors.SmartProtocolError(
229
240
                'Unknown response status: %r' % (byte,))
230
241
        if self._body_started:
243
254
        self._bytes_parts.append(bytes)
244
255
 
245
256
    def structure_part_received(self, structure):
246
 
        if type(structure) is not tuple:
 
257
        if not isinstance(structure, tuple):
247
258
            raise errors.SmartProtocolError(
248
259
                'Args structure is not a sequence: %r' % (structure,))
249
260
        if not self._body_started:
253
264
                    % (structure, self.args))
254
265
            self.args = structure
255
266
        else:
256
 
            if self._body_stream_status != 'E':
 
267
            if self._body_stream_status != b'E':
257
268
                raise errors.SmartProtocolError(
258
269
                    'Unexpected structure received after body: %r'
259
270
                    % (structure,))
274
285
            self.finished_reading = True
275
286
            self._medium_request.finished_reading()
276
287
            return
277
 
        bytes = self._medium_request.read_bytes(next_read_size)
278
 
        if bytes == '':
 
288
        data = self._medium_request.read_bytes(next_read_size)
 
289
        if data == b'':
279
290
            # end of file encountered reading from server
280
291
            if 'hpss' in debug.debug_flags:
281
292
                mutter(
286
297
                "Unexpected end of message. "
287
298
                "Please check connectivity and permissions, and report a bug "
288
299
                "if problems persist.")
289
 
        self._protocol_decoder.accept_bytes(bytes)
 
300
        self._protocol_decoder.accept_bytes(data)
290
301
 
291
302
    def protocol_error(self, exception):
292
303
        # Whatever the error is, we're done with this request.
301
312
            self._wait_for_response_end()
302
313
        if 'hpss' in debug.debug_flags:
303
314
            mutter('   result:   %r', self.args)
304
 
        if self.status == 'E':
 
315
        if self.status == b'E':
305
316
            self._wait_for_response_end()
306
 
            _translate_error(self.args)
 
317
            _raise_smart_server_error(self.args)
307
318
        return tuple(self.args)
308
319
 
309
320
    def read_body_bytes(self, count=-1):
319
330
        # != -1.  (2008/04/30, Andrew Bennetts)
320
331
        if self._body is None:
321
332
            self._wait_for_response_end()
322
 
            body_bytes = ''.join(self._bytes_parts)
 
333
            body_bytes = b''.join(self._bytes_parts)
323
334
            if 'hpss' in debug.debug_flags:
324
335
                mutter('              %d body bytes read', len(body_bytes))
325
 
            self._body = StringIO(body_bytes)
 
336
            self._body = BytesIO(body_bytes)
326
337
            self._bytes_parts = None
327
338
        return self._body.read(count)
328
339
 
334
345
                    mutter('              %d byte part read', len(bytes_part))
335
346
                yield bytes_part
336
347
            self._read_more()
337
 
        if self._body_stream_status == 'E':
338
 
            _translate_error(self._body_error_args)
 
348
        if self._body_stream_status == b'E':
 
349
            _raise_smart_server_error(self._body_error_args)
339
350
 
340
351
    def cancel_read_body(self):
341
352
        self._wait_for_response_end()
342
353
 
343
354
 
344
 
def _translate_error(error_tuple):
345
 
    # Many exceptions need some state from the requestor to be properly
346
 
    # translated (e.g. they need a branch object).  So this only translates a
347
 
    # few errors, and the rest are turned into a generic ErrorFromSmartServer.
348
 
    error_name = error_tuple[0]
349
 
    error_args = error_tuple[1:]
350
 
    if error_name == 'UnknownMethod':
351
 
        raise errors.UnknownSmartMethod(error_args[0])
352
 
    if error_name == 'LockContention':
353
 
        raise errors.LockContention('(remote lock)')
354
 
    elif error_name == 'LockFailed':
355
 
        raise errors.LockFailed(*error_args[:2])
356
 
    elif error_name == 'FileExists':
357
 
        raise errors.FileExists(error_args[0])
358
 
    elif error_name == 'NoSuchFile':
359
 
        raise errors.NoSuchFile(error_args[0])
360
 
    else:
361
 
        raise errors.ErrorFromSmartServer(error_tuple)
 
355
def _raise_smart_server_error(error_tuple):
 
356
    """Raise exception based on tuple received from smart server
 
357
 
 
358
    Specific error translation is handled by breezy.bzr.remote._translate_error
 
359
    """
 
360
    if error_tuple[0] == b'UnknownMethod':
 
361
        raise errors.UnknownSmartMethod(error_tuple[1])
 
362
    raise errors.ErrorFromSmartServer(error_tuple)