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

  • Committer: Jelmer Vernooij
  • Date: 2017-06-10 16:40:42 UTC
  • mfrom: (6653.6.7 rename-controldir)
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170610164042-zrxqgy2htyduvke2
MergeĀ rename-controldirĀ branch.

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
from __future__ import absolute_import
 
18
 
17
19
import collections
18
 
from cStringIO import StringIO
19
20
 
20
 
from bzrlib import (
 
21
from .. import (
21
22
    debug,
22
23
    errors,
23
24
    )
24
 
from bzrlib.trace import mutter
 
25
from ..sixish import (
 
26
    BytesIO,
 
27
    )
 
28
from ..trace import mutter
25
29
 
26
30
 
27
31
class MessageHandler(object):
243
247
        self._bytes_parts.append(bytes)
244
248
 
245
249
    def structure_part_received(self, structure):
246
 
        if type(structure) is not tuple:
 
250
        if not isinstance(structure, tuple):
247
251
            raise errors.SmartProtocolError(
248
252
                'Args structure is not a sequence: %r' % (structure,))
249
253
        if not self._body_started:
303
307
            mutter('   result:   %r', self.args)
304
308
        if self.status == 'E':
305
309
            self._wait_for_response_end()
306
 
            _translate_error(self.args)
 
310
            _raise_smart_server_error(self.args)
307
311
        return tuple(self.args)
308
312
 
309
313
    def read_body_bytes(self, count=-1):
322
326
            body_bytes = ''.join(self._bytes_parts)
323
327
            if 'hpss' in debug.debug_flags:
324
328
                mutter('              %d body bytes read', len(body_bytes))
325
 
            self._body = StringIO(body_bytes)
 
329
            self._body = BytesIO(body_bytes)
326
330
            self._bytes_parts = None
327
331
        return self._body.read(count)
328
332
 
335
339
                yield bytes_part
336
340
            self._read_more()
337
341
        if self._body_stream_status == 'E':
338
 
            _translate_error(self._body_error_args)
 
342
            _raise_smart_server_error(self._body_error_args)
339
343
 
340
344
    def cancel_read_body(self):
341
345
        self._wait_for_response_end()
342
346
 
343
347
 
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)
 
348
def _raise_smart_server_error(error_tuple):
 
349
    """Raise exception based on tuple received from smart server
 
350
 
 
351
    Specific error translation is handled by breezy.remote._translate_error
 
352
    """
 
353
    if error_tuple[0] == 'UnknownMethod':
 
354
        raise errors.UnknownSmartMethod(error_tuple[1])
 
355
    raise errors.ErrorFromSmartServer(error_tuple)