Rev 3349: Better infrastructure for dealing with 'bad request' responses from a in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Wed Apr 9 08:36:53 BST 2008


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 3349
revision-id:pqm at pqm.ubuntu.com-20080409073641-pvhyvdyt42fph5xf
parent: pqm at pqm.ubuntu.com-20080409055704-gkzxh5nkpxai1h6x
parent: andrew.bennetts at canonical.com-20080409041913-a9kwi6v72b6s0vsg
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Wed 2008-04-09 08:36:41 +0100
message:
  Better infrastructure for dealing with 'bad request' responses from a
  	smart server. (Andrew Bennetts)
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
  bzrlib/remote.py               remote.py-20060720103555-yeeg2x51vn0rbtdp-1
  bzrlib/smart/client.py         client.py-20061116014825-2k6ada6xgulslami-1
  bzrlib/smart/protocol.py       protocol.py-20061108035435-ot0lstk2590yqhzr-1
  bzrlib/tests/test_remote.py    test_remote.py-20060720103555-yeeg2x51vn0rbtdp-2
  bzrlib/tests/test_smart_transport.py test_ssh_transport.py-20060608202016-c25gvf1ob7ypbus6-2
  bzrlib/transport/remote.py     ssh.py-20060608202016-c25gvf1ob7ypbus6-1
    ------------------------------------------------------------
    revno: 3297.3.7
    revision-id:andrew.bennetts at canonical.com-20080409041913-a9kwi6v72b6s0vsg
    parent: andrew.bennetts at canonical.com-20080409041528-e33iv9mob44u0o3h
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: unknown-response
    timestamp: Wed 2008-04-09 14:19:13 +1000
    message:
      Add NEWS entry.
    modified:
      NEWS                           NEWS-20050323055033-4e00b5db738777ff
    ------------------------------------------------------------
    revno: 3297.3.6
    revision-id:andrew.bennetts at canonical.com-20080409041528-e33iv9mob44u0o3h
    parent: andrew.bennetts at canonical.com-20080407104900-qbxxs53xvmwzzo7k
    parent: pqm at pqm.ubuntu.com-20080408192311-eoozb2m0s383xt35
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: unknown-response
    timestamp: Wed 2008-04-09 14:15:28 +1000
    message:
      Merge bzr.dev.
    added:
      doc/developers/integration.txt integration.txt-20080404022341-2lorxocp1in07zij-1
    modified:
      NEWS                           NEWS-20050323055033-4e00b5db738777ff
      bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
      doc/developers/index.txt       index.txt-20070508041241-qznziunkg0nffhiw-1
    ------------------------------------------------------------
    revno: 3297.3.5
    revision-id:andrew.bennetts at canonical.com-20080407104900-qbxxs53xvmwzzo7k
    parent: andrew.bennetts at canonical.com-20080407103457-ro4t95pd3imwt0zw
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: Unknown response refactor
    timestamp: Mon 2008-04-07 20:49:00 +1000
    message:
      Suppress a deprecation warning.
    modified:
      bzrlib/tests/test_remote.py    test_remote.py-20060720103555-yeeg2x51vn0rbtdp-2
    ------------------------------------------------------------
    revno: 3297.3.4
    revision-id:andrew.bennetts at canonical.com-20080407103457-ro4t95pd3imwt0zw
    parent: andrew.bennetts at canonical.com-20080407081738-yx4c53pvqfif313d
    parent: pqm at pqm.ubuntu.com-20080407100447-bsfwxwxhe0mcbp7q
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: Unknown response refactor
    timestamp: Mon 2008-04-07 20:34:57 +1000
    message:
      Merge from bzr.dev.
    added:
      bzrlib/tests/test_mutabletree.py test_mutabletree.py-20080405014429-2v0cdi3re320p8db-1
      bzrlib/tests/test_uncommit.py  test_uncommit.py-20080316104338-y3gxu67g5m2qih10-1
      bzrlib/transport/nosmart.py    nosmart.py-20080402095843-6ib17idympwy1zkr-1
      doc/developers/plugin-api.txt  pluginapi.txt-20080229110225-q2j5y4agqhlkjn0s-1
    renamed:
      bzrlib/tests/test_revisionnamespaces.py => bzrlib/tests/test_revisionspec.py testrevisionnamespaces.py-20050711050225-8b4af89e6b1efe84
    modified:
      NEWS                           NEWS-20050323055033-4e00b5db738777ff
      bzr                            bzr.py-20050313053754-5485f144c7006fa6
      bzrlib/_knit_load_data_c.pyx   knit_c.pyx-20070509143944-u42gy8w387a10m0j-1
      bzrlib/_knit_load_data_py.py   _knit_load_data_py.p-20070629000948-9a0nh4s118bi5y8n-1
      bzrlib/branch.py               branch.py-20050309040759-e4baf4e0d046576e
      bzrlib/bugtracker.py           bugtracker.py-20070410073305-vu1vu1qosjurg8kb-1
      bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
      bzrlib/bundle/__init__.py      changeset.py-20050513021216-b02ab57fb9738913
      bzrlib/bundle/serializer/v4.py v10.py-20070611062757-5ggj7k18s9dej0fr-1
      bzrlib/bzrdir.py               bzrdir.py-20060131065624-156dfea39c4387cb
      bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
      bzrlib/debug.py                debug.py-20061102062349-vdhrw9qdpck8cl35-1
      bzrlib/diff.py                 diff.py-20050309040759-26944fbbf2ebbf36
      bzrlib/doc/api/__init__.py     __init__.py-20051224020744-7b87d590843855bc
      bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
      bzrlib/fetch.py                fetch.py-20050818234941-26fea6105696365d
      bzrlib/help_topics/__init__.py help_topics.py-20060920210027-rnim90q9e0bwxvy4-1
      bzrlib/knit.py                 knit.py-20051212171256-f056ac8f0fbe1bd9
      bzrlib/log.py                  log.py-20050505065812-c40ce11702fe5fb1
      bzrlib/mail_client.py          mail_client.py-20070809192806-vuxt3t19srtpjpdn-1
      bzrlib/merge.py                merge.py-20050513021216-953b65a438527106
      bzrlib/multiparent.py          __init__.py-20070410133617-n1jdhcc1n1mibarp-1
      bzrlib/mutabletree.py          mutabletree.py-20060906023413-4wlkalbdpsxi2r4y-2
      bzrlib/plugin.py               plugin.py-20050622060424-829b654519533d69
      bzrlib/plugins/launchpad/lp_directory.py lp_indirect.py-20070126012204-de5rugwlt22c7u7e-1
      bzrlib/reconcile.py            reweave_inventory.py-20051108164726-1e5e0934febac06e
      bzrlib/remote.py               remote.py-20060720103555-yeeg2x51vn0rbtdp-1
      bzrlib/repofmt/knitrepo.py     knitrepo.py-20070206081537-pyy4a00xdas0j4pf-1
      bzrlib/repofmt/pack_repo.py    pack_repo.py-20070813041115-gjv5ma7ktfqwsjgn-1
      bzrlib/repofmt/weaverepo.py    presplitout.py-20070125045333-wfav3tsh73oxu3zk-1
      bzrlib/repository.py           rev_storage.py-20051111201905-119e9401e46257e3
      bzrlib/revision.py             revision.py-20050309040759-e77802c08f3999d5
      bzrlib/revisionspec.py         revisionspec.py-20050907152633-17567659fd5c0ddb
      bzrlib/smart/branch.py         branch.py-20061124031907-mzh3pla28r83r97f-1
      bzrlib/smart/bzrdir.py         bzrdir.py-20061122024551-ol0l0o0oofsu9b3t-1
      bzrlib/smart/client.py         client.py-20061116014825-2k6ada6xgulslami-1
      bzrlib/smart/medium.py         medium.py-20061103051856-rgu2huy59fkz902q-1
      bzrlib/smart/protocol.py       protocol.py-20061108035435-ot0lstk2590yqhzr-1
      bzrlib/smart/repository.py     repository.py-20061128022038-vr5wy5bubyb8xttk-1
      bzrlib/smart/request.py        request.py-20061108095550-gunadhxmzkdjfeek-1
      bzrlib/smart/server.py         server.py-20061110062051-chzu10y32vx8gvur-1
      bzrlib/smart/vfs.py            vfs.py-20061108095550-gunadhxmzkdjfeek-2
      bzrlib/status.py               status.py-20050505062338-431bfa63ec9b19e6
      bzrlib/symbol_versioning.py    symbol_versioning.py-20060105104851-9ecf8af605d15a80
      bzrlib/tests/TestUtil.py       TestUtil.py-20050824080200-5f70140a2d938694
      bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
      bzrlib/tests/blackbox/test_pull.py test_pull.py-20051201144907-64959364f629947f
      bzrlib/tests/blackbox/test_serve.py test_serve.py-20060913064329-8t2pvmsikl4s3xhl-1
      bzrlib/tests/blackbox/test_switch.py test_switch.py-20071122111948-0c5en6uz92bwl76h-1
      bzrlib/tests/blackbox/test_uncommit.py test_uncommit.py-20051027212835-84944b63adae51be
      bzrlib/tests/blackbox/test_version.py test_version.py-20070312060045-ol7th9z035r3im3d-1
      bzrlib/tests/branch_implementations/test_branch.py testbranch.py-20050711070244-121d632bc37d7253
      bzrlib/tests/interrepository_implementations/__init__.py __init__.py-20060220054744-baf49a1f88f17b1a
      bzrlib/tests/interrepository_implementations/test_interrepository.py test_interrepository.py-20060220061411-1ec13fa99e5e3eee
      bzrlib/tests/interversionedfile_implementations/test_join.py test_join.py-20060302012326-9b5e9b0f0a03fedc
      bzrlib/tests/repository_implementations/test_check_reconcile.py test_broken.py-20070928125406-62236394w0jpbpd6-2
      bzrlib/tests/repository_implementations/test_iter_reverse_revision_history.py test_iter_reverse_re-20070217015036-spu7j5ggch7pbpyd-1
      bzrlib/tests/repository_implementations/test_reconcile.py test_reconcile.py-20060223022332-572ef70a3288e369
      bzrlib/tests/repository_implementations/test_repository.py test_repository.py-20060131092128-ad07f494f5c9d26c
      bzrlib/tests/test_bugtracker.py test_bugtracker.py-20070410073305-vu1vu1qosjurg8kb-2
      bzrlib/tests/test_bundle.py    test.py-20050630184834-092aa401ab9f039c
      bzrlib/tests/test_diff.py      testdiff.py-20050727164403-d1a3496ebb12e339
      bzrlib/tests/test_errors.py    test_errors.py-20060210110251-41aba2deddf936a8
      bzrlib/tests/test_fetch.py     testfetch.py-20050825090644-f73e07e7dfb1765a
      bzrlib/tests/test_knit.py      test_knit.py-20051212171302-95d4c00dd5f11f2b
      bzrlib/tests/test_lazy_import.py test_lazy_import.py-20060910203832-f77c54gf3n232za0-2
      bzrlib/tests/test_log.py       testlog.py-20050728115707-1a514809d7d49309
      bzrlib/tests/test_mail_client.py test_mail_client.py-20070809192806-vuxt3t19srtpjpdn-2
      bzrlib/tests/test_merge.py     testmerge.py-20050905070950-c1b5aa49ff911024
      bzrlib/tests/test_osutils.py   test_osutils.py-20051201224856-e48ee24c12182989
      bzrlib/tests/test_plugins.py   plugins.py-20050622075746-32002b55e5e943e9
      bzrlib/tests/test_read_bundle.py test_read_bundle.py-20060615211421-ud8cwr1ulgd914zf-1
      bzrlib/tests/test_remote.py    test_remote.py-20060720103555-yeeg2x51vn0rbtdp-2
      bzrlib/tests/test_selftest.py  test_selftest.py-20051202044319-c110a115d8c0456a
      bzrlib/tests/test_smart.py     test_smart.py-20061122024551-ol0l0o0oofsu9b3t-2
      bzrlib/tests/test_smart_transport.py test_ssh_transport.py-20060608202016-c25gvf1ob7ypbus6-2
      bzrlib/tests/test_status.py    test_status.py-20060516190614-fbf6432e4a6e8aa5
      bzrlib/tests/test_transport_implementations.py test_transport_implementations.py-20051227111451-f97c5c7d5c49fce7
      bzrlib/tests/test_versionedfile.py test_versionedfile.py-20060222045249-db45c9ed14a1c2e5
      bzrlib/tests/test_wsgi.py      test_wsgi.py-20061005091552-rz8pva0olkxv0sd8-1
      bzrlib/tests/workingtree_implementations/test_commit.py test_commit.py-20060421013633-1610ec2331c8190f
      bzrlib/transform.py            transform.py-20060105172343-dd99e54394d91687
      bzrlib/transport/__init__.py   transport.py-20050711165921-4978aa7ce1285ad5
      bzrlib/transport/chroot.py     chroot.py-20061011104729-0us9mgm97z378vnt-1
      bzrlib/transport/ftp.py        ftp.py-20051116161804-58dc9506548c2a53
      bzrlib/transport/http/__init__.py http_transport.py-20050711212304-506c5fd1059ace96
      bzrlib/transport/http/wsgi.py  wsgi.py-20061005091552-rz8pva0olkxv0sd8-2
      bzrlib/transport/remote.py     ssh.py-20060608202016-c25gvf1ob7ypbus6-1
      bzrlib/uncommit.py             uncommit.py-20050626215513-5ec509fa425b305c
      bzrlib/versionedfile.py        versionedfile.py-20060222045106-5039c71ee3b65490
      bzrlib/weave.py                knit.py-20050627021749-759c29984154256b
      bzrlib/workingtree.py          workingtree.py-20050511021032-29b6ec0a681e02e3
      doc/developers/index.txt       index.txt-20070508041241-qznziunkg0nffhiw-1
      doc/developers/repository.txt  repository.txt-20070709152006-xkhlek456eclha4u-1
      doc/en/user-guide/annotating_changes.txt annotating_changes.t-20071122141511-0knao2lklsdsvb1q-1
      doc/en/user-guide/bazaar_workflows.txt bazaar_workflows.txt-20071114035000-q36a9h57ps06uvnl-1
      doc/en/user-guide/core_concepts.txt core_concepts.txt-20071114035000-q36a9h57ps06uvnl-2
      doc/en/user-guide/http_smart_server.txt fastcgi.txt-20061005091552-rz8pva0olkxv0sd8-3
      doc/en/user-guide/installing_bazaar.txt installing_bazaar.tx-20071114035000-q36a9h57ps06uvnl-4
      doc/en/user-guide/introducing_bazaar.txt introducing_bazaar.t-20071114035000-q36a9h57ps06uvnl-5
      doc/en/user-guide/partner_intro.txt partner_workflow.txt-20071122141511-0knao2lklsdsvb1q-4
      doc/en/user-guide/undoing_mistakes.txt undoing_mistakes.txt-20071121092300-8fyacngt1w98e5mp-1
      bzrlib/tests/test_revisionspec.py testrevisionnamespaces.py-20050711050225-8b4af89e6b1efe84
    ------------------------------------------------------------
    revno: 3297.3.3
    revision-id:andrew.bennetts at canonical.com-20080407081738-yx4c53pvqfif313d
    parent: andrew.bennetts at canonical.com-20080320204105-78ovhvc1iptbvdcx
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: Unknown response refactor
    timestamp: Mon 2008-04-07 18:17:38 +1000
    message:
      SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod.  Callers no longer need to do their own ad hoc unknown smart method error detection.
    modified:
      bzrlib/remote.py               remote.py-20060720103555-yeeg2x51vn0rbtdp-1
      bzrlib/tests/test_remote.py    test_remote.py-20060720103555-yeeg2x51vn0rbtdp-2
      bzrlib/tests/test_smart_transport.py test_ssh_transport.py-20060608202016-c25gvf1ob7ypbus6-2
      bzrlib/transport/remote.py     ssh.py-20060608202016-c25gvf1ob7ypbus6-1
    ------------------------------------------------------------
    revno: 3297.3.2
    revision-id:andrew.bennetts at canonical.com-20080320204105-78ovhvc1iptbvdcx
    parent: andrew.bennetts at canonical.com-20080320181421-91fif114b127aq05
    parent: andrew.bennetts at canonical.com-20080320204011-cxdpnod5fg1bjquh
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: Unknown response refactor
    timestamp: Thu 2008-03-20 15:41:05 -0500
    message:
      Merge trivial fix.
    modified:
      bzrlib/remote.py               remote.py-20060720103555-yeeg2x51vn0rbtdp-1
    ------------------------------------------------------------
    revno: 3297.3.1
    revision-id:andrew.bennetts at canonical.com-20080320181421-91fif114b127aq05
    parent: pqm at pqm.ubuntu.com-20080319032243-u1uwskj36dp0i0hs
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: Unknown response refactor
    timestamp: Thu 2008-03-20 13:14:21 -0500
    message:
      Raise UnknownSmartMethod automatically from read_response_tuple.
    modified:
      bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
      bzrlib/smart/client.py         client.py-20061116014825-2k6ada6xgulslami-1
      bzrlib/smart/protocol.py       protocol.py-20061108035435-ot0lstk2590yqhzr-1
      bzrlib/tests/test_smart_transport.py test_ssh_transport.py-20060608202016-c25gvf1ob7ypbus6-2
=== modified file 'NEWS'
--- a/NEWS	2008-04-09 03:00:55 +0000
+++ b/NEWS	2008-04-09 07:36:41 +0000
@@ -191,6 +191,11 @@
 
   INTERNALS:
 
+    * The ``read_response_tuple`` method of ``SmartClientRequestProtocol*``
+      classes will now raise ``UnknownSmartMethod`` when appropriate, so that
+      callers don't need to try distinguish unknown request errors from other
+      errors.  (Andrew Bennetts)
+      
     * ``VersionedFile`` now has a new method ``get_parent_map`` which, like
       ``Graph.get_parent_map`` returns a dict of key:parents. (Robert Collins)
 

=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2008-04-04 05:59:43 +0000
+++ b/bzrlib/errors.py	2008-04-07 10:34:57 +0000
@@ -1476,6 +1476,14 @@
         self.details = details
 
 
+class UnknownSmartMethod(InternalBzrError):
+
+    _fmt = "The server does not recognise the '%(verb)s' request."
+
+    def __init__(self, verb):
+        self.verb = verb
+
+
 # A set of semi-meaningful errors which can be thrown
 class TransportNotPossible(TransportError):
 

=== modified file 'bzrlib/remote.py'
--- a/bzrlib/remote.py	2008-04-07 08:20:13 +0000
+++ b/bzrlib/remote.py	2008-04-07 10:34:57 +0000
@@ -149,11 +149,9 @@
     def open_repository(self):
         path = self._path_for_remote_call(self._client)
         verb = 'BzrDir.find_repositoryV2'
-        response = self._client.call(verb, path)
-        if (response == ('error', "Generic bzr smart protocol error: "
-                "bad request '%s'" % verb) or
-              response == ('error', "Generic bzr smart protocol error: "
-                "bad request u'%s'" % verb)):
+        try:
+            response = self._client.call(verb, path)
+        except errors.UnknownSmartMethod:
             verb = 'BzrDir.find_repository'
             response = self._client.call(verb, path)
         assert response[0] in ('ok', 'norepository'), \
@@ -634,8 +632,12 @@
         """
         import tempfile
         path = self.bzrdir._path_for_remote_call(self._client)
-        response, protocol = self._client.call_expecting_body(
-            'Repository.tarball', path, compression)
+        try:
+            response, protocol = self._client.call_expecting_body(
+                'Repository.tarball', path, compression)
+        except errors.UnknownSmartMethod:
+            protocol.cancel_read_body()
+            return None
         if response[0] == 'ok':
             # Extract the tarball and return it
             t = tempfile.NamedTemporaryFile()
@@ -643,12 +645,6 @@
             t.write(protocol.read_body_bytes())
             t.seek(0)
             return t
-        if (response == ('error', "Generic bzr smart protocol error: "
-                "bad request 'Repository.tarball'") or
-              response == ('error', "Generic bzr smart protocol error: "
-                "bad request u'Repository.tarball'")):
-            protocol.cancel_read_body()
-            return None
         raise errors.UnexpectedSmartServerResponse(response)
 
     def sprout(self, to_bzrdir, revision_id=None):
@@ -815,24 +811,6 @@
                 100.0 * len(self._requested_parents) / len(ancestry))
         return dict((k, ancestry[k]) for k in present_keys)
 
-    def _response_is_unknown_method(self, response, verb):
-        """Return True if response is an unknonwn method response to verb.
-        
-        :param response: The response from a smart client call_expecting_body
-            call.
-        :param verb: The verb used in that call.
-        :return: True if an unknown method was encountered.
-        """
-        # This might live better on
-        # bzrlib.smart.protocol.SmartClientRequestProtocolOne
-        if (response[0] == ('error', "Generic bzr smart protocol error: "
-                "bad request '%s'" % verb) or
-              response[0] == ('error', "Generic bzr smart protocol error: "
-                "bad request u'%s'" % verb)):
-           response[1].cancel_read_body()
-           return True
-        return False
-
     def _get_parent_map(self, keys):
         """Helper for get_parent_map that performs the RPC."""
         medium = self._client._medium
@@ -883,9 +861,10 @@
             assert type(key) is str
         verb = 'Repository.get_parent_map'
         args = (path,) + tuple(keys)
-        response = self._client.call_with_body_bytes_expecting_body(
-            verb, args, self._serialise_search_recipe(recipe))
-        if self._response_is_unknown_method(response, verb):
+        try:
+            response = self._client.call_with_body_bytes_expecting_body(
+                verb, args, self._serialise_search_recipe(recipe))
+        except errors.UnknownSmartMethod:
             # Server does not support this method, so get the whole graph.
             # Worse, we have to force a disconnection, because the server now
             # doesn't realise it has a body on the wire to consume, so the
@@ -897,8 +876,8 @@
             # To avoid having to disconnect repeatedly, we keep track of the
             # fact the server doesn't understand remote methods added in 1.2.
             medium._remote_is_at_least_1_2 = False
-            return self._get_revision_graph(None)
-        elif response[0][0] not in ['ok']:
+            return self.get_revision_graph(None)
+        if response[0][0] not in ['ok']:
             response[1].cancel_read_body()
             raise errors.UnexpectedSmartServerResponse(response[0])
         if response[0][0] == 'ok':
@@ -1061,10 +1040,11 @@
         REQUEST_NAME = 'Repository.stream_revisions_chunked'
         path = self.bzrdir._path_for_remote_call(self._client)
         body = self._serialise_search_recipe(search.get_recipe())
-        response, protocol = self._client.call_with_body_bytes_expecting_body(
-            REQUEST_NAME, (path,), body)
-
-        if self._response_is_unknown_method((response, protocol), REQUEST_NAME):
+        try:
+            result = self._client.call_with_body_bytes_expecting_body(
+                REQUEST_NAME, (path,), body)
+            response, protocol = result
+        except errors.UnknownSmartMethod:
             # Server does not support this method, so fall back to VFS.
             # Worse, we have to force a disconnection, because the server now
             # doesn't realise it has a body on the wire to consume, so the

=== modified file 'bzrlib/smart/client.py'
--- a/bzrlib/smart/client.py	2008-03-28 09:07:01 +0000
+++ b/bzrlib/smart/client.py	2008-04-07 10:34:57 +0000
@@ -18,7 +18,10 @@
 from urlparse import urlparse
 
 from bzrlib.smart import protocol
-from bzrlib import urlutils
+from bzrlib import (
+    errors,
+    urlutils,
+    )
 
 
 class _SmartClient(object):
@@ -95,3 +98,4 @@
             
         rel_url = urlutils.relative_url(medium_base, transport.base)
         return urllib.unquote(rel_url)
+

=== modified file 'bzrlib/smart/protocol.py'
--- a/bzrlib/smart/protocol.py	2008-03-27 06:10:18 +0000
+++ b/bzrlib/smart/protocol.py	2008-04-07 10:34:57 +0000
@@ -469,6 +469,7 @@
         self._request = request
         self._body_buffer = None
         self._request_start_time = None
+        self._last_verb = None
 
     def call(self, *args):
         if 'hpss' in debug.debug_flags:
@@ -478,6 +479,7 @@
             self._request_start_time = time.time()
         self._write_args(args)
         self._request.finished_writing()
+        self._last_verb = args[0]
 
     def call_with_body_bytes(self, args, body):
         """Make a remote call of args with body bytes 'body'.
@@ -496,6 +498,7 @@
         bytes = self._encode_bulk_data(body)
         self._request.accept_bytes(bytes)
         self._request.finished_writing()
+        self._last_verb = args[0]
 
     def call_with_body_readv_array(self, args, body):
         """Make a remote call with a readv array.
@@ -515,6 +518,7 @@
         self._request.finished_writing()
         if 'hpss' in debug.debug_flags:
             mutter('              %d bytes in readv request', len(readv_bytes))
+        self._last_verb = args[0]
 
     def cancel_read_body(self):
         """After expecting a body, a response code may indicate one otherwise.
@@ -539,10 +543,28 @@
                 self._request_start_time = None
             else:
                 mutter('   result:   %s', repr(result)[1:-1])
+        self._response_is_unknown_method(result)
         if not expect_body:
             self._request.finished_reading()
         return result
 
+    def _response_is_unknown_method(self, result_tuple):
+        """Raise UnexpectedSmartServerResponse if the response is an 'unknonwn
+        method' response to the request.
+        
+        :param response: The response from a smart client call_expecting_body
+            call.
+        :param verb: The verb used in that call.
+        :raises: UnexpectedSmartServerResponse
+        """
+        if (result_tuple == ('error', "Generic bzr smart protocol error: "
+                "bad request '%s'" % self._last_verb) or
+              result_tuple == ('error', "Generic bzr smart protocol error: "
+                "bad request u'%s'" % self._last_verb)):
+            # The response will have no body, so we've finished reading.
+            self._request.finished_reading()
+            raise errors.UnknownSmartMethod(self._last_verb)
+        
     def read_body_bytes(self, count=-1):
         """Read bytes from the body, decoding into a byte stream.
         

=== modified file 'bzrlib/tests/test_remote.py'
--- a/bzrlib/tests/test_remote.py	2008-04-07 08:27:21 +0000
+++ b/bzrlib/tests/test_remote.py	2008-04-07 10:49:00 +0000
@@ -136,27 +136,36 @@
         """Create a FakeClient.
 
         :param responses: A list of response-tuple, body-data pairs to be sent
-            back to callers.
+            back to callers.  A special case is if the response-tuple is
+            'unknown verb', then a UnknownSmartMethod will be raised for that
+            call, using the second element of the tuple as the verb in the
+            exception.
         """
         self.responses = responses
         self._calls = []
         self.expecting_body = False
         _SmartClient.__init__(self, FakeMedium(self._calls), fake_medium_base)
 
+    def _get_next_response(self):
+        response_tuple = self.responses.pop(0)
+        if response_tuple[0][0] == 'unknown verb':
+            raise errors.UnknownSmartMethod(response_tuple[0][1])
+        return response_tuple
+
     def call(self, method, *args):
         self._calls.append(('call', method, args))
-        return self.responses.pop(0)[0]
+        return self._get_next_response()[0]
 
     def call_expecting_body(self, method, *args):
         self._calls.append(('call_expecting_body', method, args))
-        result = self.responses.pop(0)
+        result = self._get_next_response()
         self.expecting_body = True
         return result[0], FakeProtocol(result[1], self)
 
     def call_with_body_bytes_expecting_body(self, method, args, body):
         self._calls.append(('call_with_body_bytes_expecting_body', method,
             args, body))
-        result = self.responses.pop(0)
+        result = self._get_next_response()
         self.expecting_body = True
         return result[0], FakeProtocol(result[1], self)
 
@@ -315,6 +324,24 @@
             RemoteBzrDirFormat.probe_transport, OldServerTransport())
 
 
+class TestBzrDirOpenRepository(tests.TestCase):
+
+    def test_backwards_compat_1_2(self):
+        transport = MemoryTransport()
+        transport.mkdir('quack')
+        transport = transport.clone('quack')
+        client = FakeClient([
+            (('unknown verb', 'RemoteRepository.find_repositoryV2'), ''),
+            (('ok', '', 'no', 'no'), ''),],
+            transport.base)
+        bzrdir = RemoteBzrDir(transport, _client=client)
+        repo = bzrdir.open_repository()
+        self.assertEqual(
+            [('call', 'BzrDir.find_repositoryV2', ('quack/',)),
+             ('call', 'BzrDir.find_repository', ('quack/',))],
+            client._calls)
+
+
 class OldSmartClient(object):
     """A fake smart client for test_old_version that just returns a version one
     response to the 'hello' (query version) command.
@@ -536,23 +563,7 @@
         advisory anyway (a transport could be read-write, but then the
         underlying filesystem could be readonly anyway).
         """
-        client = FakeClient([(
-            ('error', "Generic bzr smart protocol error: "
-                      "bad request 'Transport.is_readonly'"), '')])
-        transport = RemoteTransport('bzr://example.com/', medium=False,
-                                    _client=client)
-        self.assertEqual(False, transport.is_readonly())
-        self.assertEqual(
-            [('call', 'Transport.is_readonly', ())],
-            client._calls)
-
-    def test_error_from_old_0_11_server(self):
-        """Same as test_error_from_old_server, but with the slightly different
-        error message from bzr 0.11 servers.
-        """
-        client = FakeClient([(
-            ('error', "Generic bzr smart protocol error: "
-                      "bad request u'Transport.is_readonly'"), '')])
+        client = FakeClient([(('unknown verb', 'Transport.is_readonly'), '')])
         transport = RemoteTransport('bzr://example.com/', medium=False,
                                     _client=client)
         self.assertEqual(False, transport.is_readonly())
@@ -704,17 +715,18 @@
         repo.unlock()
 
     def test_get_parent_map_reconnects_if_unknown_method(self):
-        error_msg = (
-            "Generic bzr smart protocol error: "
-            "bad request 'Repository.get_parent_map'")
         responses = [
-            (('error', error_msg), ''),
+            (('unknown verb', 'Repository.get_parent_map'), ''),
             (('ok',), '')]
         transport_path = 'quack'
         repo, client = self.setup_fake_client_and_repository(
             responses, transport_path)
         rev_id = 'revision-id'
-        parents = repo.get_parent_map([rev_id])
+        expected_deprecations = [
+            'bzrlib.remote.RemoteRepository.get_revision_graph was deprecated '
+            'in version 1.4.']
+        parents = self.callDeprecated(
+            expected_deprecations, repo.get_parent_map, [rev_id])
         self.assertEqual(
             [('call_with_body_bytes_expecting_body',
               'Repository.get_parent_map', ('quack/', rev_id), '\n\n0'),
@@ -996,11 +1008,8 @@
     
     def test_backwards_compatibility(self):
         """If the server doesn't recognise this request, fallback to VFS."""
-        error_msg = (
-            "Generic bzr smart protocol error: "
-            "bad request 'Repository.stream_revisions_chunked'")
         responses = [
-            (('error', error_msg), '')]
+            (('unknown verb', 'Repository.stream_revisions_chunked'), '')]
         repo, client = self.setup_fake_client_and_repository(
             responses, 'path')
         self.mock_called = False

=== modified file 'bzrlib/tests/test_smart_transport.py'
--- a/bzrlib/tests/test_smart_transport.py	2008-04-01 02:16:14 +0000
+++ b/bzrlib/tests/test_smart_transport.py	2008-04-07 10:34:57 +0000
@@ -1603,6 +1603,43 @@
         smart_protocol.call_with_body_readv_array(('foo', ), [(1,2),(5,6)])
         self.assertEqual(expected_bytes, output.getvalue())
 
+    def _test_client_read_response_tuple_raises_UnknownSmartMethod(self,
+            server_bytes):
+        server_bytes = (
+            "error\x01Generic bzr smart protocol error: bad request 'foo'\n")
+        input = StringIO(server_bytes)
+        output = StringIO()
+        client_medium = medium.SmartSimplePipesClientMedium(input, output)
+        request = client_medium.get_request()
+        smart_protocol = protocol.SmartClientRequestProtocolOne(request)
+        smart_protocol.call('foo')
+        self.assertRaises(
+            errors.UnknownSmartMethod, smart_protocol.read_response_tuple)
+        # The request has been finished.  There is no body to read, and
+        # attempts to read one will fail.
+        self.assertRaises(
+            errors.ReadingCompleted, smart_protocol.read_body_bytes)
+
+    def test_client_read_response_tuple_raises_UnknownSmartMethod(self):
+        """read_response_tuple raises UnknownSmartMethod if the response says
+        the server did not recognise the request.
+        """
+        server_bytes = (
+            "error\x01Generic bzr smart protocol error: bad request 'foo'\n")
+        self._test_client_read_response_tuple_raises_UnknownSmartMethod(
+            server_bytes)
+
+    def test_client_read_response_tuple_raises_UnknownSmartMethod_0_11(self):
+        """read_response_tuple also raises UnknownSmartMethod if the response
+        from a bzr 0.11 says the server did not recognise the request.
+
+        (bzr 0.11 sends a slightly different error message to later versions.)
+        """
+        server_bytes = (
+            "error\x01Generic bzr smart protocol error: bad request u'foo'\n")
+        self._test_client_read_response_tuple_raises_UnknownSmartMethod(
+            server_bytes)
+
     def test_client_read_body_bytes_all(self):
         # read_body_bytes should decode the body bytes from the wire into
         # a response.
@@ -1932,6 +1969,28 @@
         smart_protocol.read_response_tuple(False)
         self.assertEqual(True, smart_protocol.response_status)
 
+    def test_client_read_response_tuple_raises_UnknownSmartMethod(self):
+        """read_response_tuple raises UnknownSmartMethod if the response is
+        says the server did not recognise the request.
+        """
+        server_bytes = (
+            protocol.RESPONSE_VERSION_TWO +
+            "failed\n" +
+            "error\x01Generic bzr smart protocol error: bad request 'foo'\n")
+        input = StringIO(server_bytes)
+        output = StringIO()
+        client_medium = medium.SmartSimplePipesClientMedium(input, output)
+        request = client_medium.get_request()
+        smart_protocol = protocol.SmartClientRequestProtocolTwo(request)
+        smart_protocol.call('foo')
+        self.assertRaises(
+            errors.UnknownSmartMethod, smart_protocol.read_response_tuple)
+        self.assertEqual(False, smart_protocol.response_status)
+        # The request has been finished.  There is no body to read, and
+        # attempts to read one will fail.
+        self.assertRaises(
+            errors.ReadingCompleted, smart_protocol.read_body_bytes)
+
     def test_client_read_body_bytes_all(self):
         # read_body_bytes should decode the body bytes from the wire into
         # a response.

=== modified file 'bzrlib/transport/remote.py'
--- a/bzrlib/transport/remote.py	2008-03-28 08:58:43 +0000
+++ b/bzrlib/transport/remote.py	2008-04-07 10:34:57 +0000
@@ -127,19 +127,17 @@
 
     def is_readonly(self):
         """Smart server transport can do read/write file operations."""
-        resp = self._call2('Transport.is_readonly')
-        if resp == ('yes', ):
-            return True
-        elif resp == ('no', ):
-            return False
-        elif (resp == ('error', "Generic bzr smart protocol error: "
-                                "bad request 'Transport.is_readonly'") or
-              resp == ('error', "Generic bzr smart protocol error: "
-                                "bad request u'Transport.is_readonly'")):
+        try:
+            resp = self._call2('Transport.is_readonly')
+        except errors.UnknownSmartMethod:
             # XXX: nasty hack: servers before 0.16 don't have a
             # 'Transport.is_readonly' verb, so we do what clients before 0.16
             # did: assume False.
             return False
+        if resp == ('yes', ):
+            return True
+        elif resp == ('no', ):
+            return False
         else:
             self._translate_error(resp)
         raise errors.UnexpectedSmartServerResponse(resp)




More information about the bazaar-commits mailing list