[MERGE] Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch).
Andrew Bennetts
andrew at canonical.com
Fri Apr 13 09:10:39 BST 2007
This bundle has yet more changes from the hpss branch: some new tests (and small
fixes for some), and also adds bzrlib/smart/client.py.
-Andrew.
-------------- next part --------------
# Bazaar revision bundle v0.9
#
# message:
# Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch).
# committer: Andrew Bennetts <andrew.bennetts at canonical.com>
# date: Fri 2007-04-13 17:59:17.549000025 +1000
=== added file bzrlib/smart/client.py // file-id:client.py-20061116014825-2k6ad
... a6xgulslami-1
--- /dev/null
+++ bzrlib/smart/client.py
@@ -0,0 +1,57 @@
+# Copyright (C) 2006 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+from urlparse import urlparse
+
+from bzrlib.smart import protocol
+from bzrlib.urlutils import unescape
+
+
+class SmartClient(object):
+
+ def __init__(self, medium):
+ self._medium = medium
+
+ def call(self, method, *args):
+ """Call a method on the remote server."""
+ result, protocol = self.call2(method, *args)
+ protocol.cancel_read_body()
+ return result
+
+ def call2(self, method, *args):
+ """Call a method and return the result and the protocol object."""
+ request = self._medium.get_request()
+ smart_protocol = protocol.SmartClientRequestProtocolOne(request)
+ smart_protocol.call(method, *args)
+ return smart_protocol.read_response_tuple(expect_body=True), smart_protocol
+
+ def call_with_body_bytes(self, method, args, body):
+ """Call a method on the remote server with body bytes."""
+ if type(method) is not str:
+ raise TypeError('method must be a byte string, not %r' % (method,))
+ for arg in args:
+ if type(arg) is not str:
+ raise TypeError('args must be byte strings, not %r' % (args,))
+ if type(body) is not str:
+ raise TypeError('body must be byte string, not %r' % (body,))
+ request = self._medium.get_request()
+ smart_protocol = protocol.SmartClientRequestProtocolOne(request)
+ smart_protocol.call_with_body_bytes((method, ) + args, body)
+ return smart_protocol.read_response_tuple()
+
+ def remote_path_from_transport(self, transport):
+ """Convert transport into a path suitable for using in a request."""
+ return unescape(urlparse(transport.base)[2]).encode('utf8')
=== modified file bzrlib/tests/test_smart_transport.py
--- bzrlib/tests/test_smart_transport.py
+++ bzrlib/tests/test_smart_transport.py
@@ -31,6 +31,7 @@
urlutils,
)
from bzrlib.smart import (
+ client,
medium,
protocol,
request,
@@ -1451,6 +1452,42 @@
errors.ReadingCompleted, smart_protocol.read_body_bytes)
+class TestSmartClientUnicode(tests.TestCase):
+ """SmartClient tests for unicode arguments.
+
+ Unicode arguments to call_with_body_bytes are not correct (remote method
+ names, arguments, and bodies must all be expressed as byte strings), but
+ SmartClient should gracefully reject them, rather than getting into a broken
+ state that prevents future correct calls from working. That is, it should
+ be possible to issue more requests on the medium afterwards, rather than
+ allowing one bad call to call_with_body_bytes to cause later calls to
+ mysteriously fail with TooManyConcurrentRequests.
+ """
+
+ def assertCallDoesNotBreakMedium(self, method, args, body):
+ """Call a medium with the given method, args and body, then assert that
+ the medium is left in a sane state, i.e. is capable of allowing further
+ requests.
+ """
+ input = StringIO("\n")
+ output = StringIO()
+ client_medium = medium.SmartSimplePipesClientMedium(input, output)
+ smart_client = client.SmartClient(client_medium)
+ self.assertRaises(TypeError,
+ smart_client.call_with_body_bytes, method, args, body)
+ self.assertEqual("", output.getvalue())
+ self.assertEqual(None, client_medium._current_request)
+
+ def test_call_with_body_bytes_unicode_method(self):
+ self.assertCallDoesNotBreakMedium(u'method', ('args',), 'body')
+
+ def test_call_with_body_bytes_unicode_args(self):
+ self.assertCallDoesNotBreakMedium('method', (u'args',), 'body')
+
+ def test_call_with_body_bytes_unicode_body(self):
+ self.assertCallDoesNotBreakMedium('method', ('args',), u'body')
+
+
class LengthPrefixedBodyDecoder(tests.TestCase):
# XXX: TODO: make accept_reading_trailer invoke translate_response or
=== modified file bzrlib/tests/test_transport_implementations.py
--- bzrlib/tests/test_transport_implementations.py
+++ bzrlib/tests/test_transport_implementations.py
@@ -22,6 +22,7 @@
import os
from cStringIO import StringIO
+from StringIO import StringIO as pyStringIO
import stat
import sys
@@ -35,10 +36,10 @@
TransportNotPossible, ConnectionError,
InvalidURL)
from bzrlib.osutils import getcwd
+from bzrlib.smart import medium
from bzrlib.symbol_versioning import zero_eleven
from bzrlib.tests import TestCaseInTempDir, TestSkipped
from bzrlib.tests.test_transport import TestTransportImplementation
-from bzrlib.smart import medium
from bzrlib.transport import memory, remote
import bzrlib.transport
@@ -369,6 +370,33 @@
dir_mode=0777, create_parent_dir=True)
self.assertTransportMode(t, 'dir777', 0777)
+ def test_put_bytes_unicode(self):
+ # Expect put_bytes to raise AssertionError or UnicodeEncodeError if
+ # given unicode "bytes". UnicodeEncodeError doesn't really make sense
+ # (we don't want to encode unicode here at all, callers should be
+ # strictly passing bytes to put_bytes), but we allow it for backwards
+ # compatibility. At some point we should use a specific exception.
+ t = self.get_transport()
+ if t.is_readonly():
+ return
+ unicode_string = u'\u1234'
+ self.assertRaises(
+ (AssertionError, UnicodeEncodeError),
+ t.put_bytes, 'foo', unicode_string)
+
+ def test_put_file_unicode(self):
+ # Like put_bytes, except with a StringIO.StringIO of a unicode string.
+ # This situation can happen (and has) if code is careless about the type
+ # of "string" they initialise/write to a StringIO with. We cannot use
+ # cStringIO, because it never returns unicode from read.
+ # Like put_bytes, UnicodeEncodeError isn't quite the right exception to
+ # raise, but we raise it for hysterical raisins.
+ t = self.get_transport()
+ if t.is_readonly():
+ return
+ unicode_file = pyStringIO(u'\u1234')
+ self.assertRaises(UnicodeEncodeError, t.put_file, 'foo', unicode_file)
+
def test_put_multi(self):
t = self.get_transport()
=== modified file bzrlib/transport/__init__.py
--- bzrlib/transport/__init__.py
+++ bzrlib/transport/__init__.py
@@ -1243,8 +1243,6 @@
register_lazy_transport('ftp://', 'bzrlib.transport.ftp', 'FtpTransport')
register_lazy_transport('aftp://', 'bzrlib.transport.ftp', 'FtpTransport')
register_lazy_transport('memory://', 'bzrlib.transport.memory', 'MemoryTransport')
-register_lazy_transport('chroot+', 'bzrlib.transport.chroot',
- 'ChrootTransportDecorator')
register_lazy_transport('readonly+', 'bzrlib.transport.readonly', 'ReadonlyTransportDecorator')
register_lazy_transport('fakenfs+', 'bzrlib.transport.fakenfs', 'FakeNFSTransportDecorator')
register_lazy_transport('vfat+',
=== modified file bzrlib/transport/http/__init__.py
--- bzrlib/transport/http/__init__.py
+++ bzrlib/transport/http/__init__.py
@@ -508,8 +508,8 @@
class SmartClientHTTPMediumRequest(medium.SmartClientMediumRequest):
"""A SmartClientMediumRequest that works with an HTTP medium."""
- def __init__(self, smart_medium):
- medium.SmartClientMediumRequest.__init__(self, smart_medium)
+ def __init__(self, client_medium):
+ medium.SmartClientMediumRequest.__init__(self, client_medium)
self._buffer = ''
def _accept_bytes(self, bytes):
=== modified file bzrlib/transport/memory.py
--- bzrlib/transport/memory.py
+++ bzrlib/transport/memory.py
@@ -128,7 +128,14 @@
"""See Transport.put_file()."""
_abspath = self._abspath(relpath)
self._check_parent(_abspath)
- self._files[_abspath] = (f.read(), mode)
+ bytes = f.read()
+ if type(bytes) is not str:
+ # Although not strictly correct, we raise UnicodeEncodeError to be
+ # compatible with other transports.
+ raise UnicodeEncodeError(
+ 'undefined', bytes, 0, 1,
+ 'put_file must be given a file of bytes, not unicode.')
+ self._files[_abspath] = (bytes, mode)
def mkdir(self, relpath, mode=None):
"""See Transport.mkdir()."""
=== modified directory // last-changed:andrew.bennetts at canonical.com-200704130
... 75917-jlvhfi0s607smiu6
# revision id: andrew.bennetts at canonical.com-20070413075917-jlvhfi0s607smiu6
# sha1: cf5da26ec8041ebcbfbca280cef4978402b208cd
# inventory sha1: 972ccbeb922d01151c36678572402bdf565396f1
# parent ids:
# pqm at pqm.ubuntu.com-20070413050623-10v4wozs1tu04kcu
# base id: pqm at pqm.ubuntu.com-20070413050623-10v4wozs1tu04kcu
# properties:
# branch-nick: hpss-miscellany-2
More information about the bazaar
mailing list