diff options
author | Rafael H. Schloming <rhs@apache.org> | 2009-03-06 15:03:00 +0000 |
---|---|---|
committer | Rafael H. Schloming <rhs@apache.org> | 2009-03-06 15:03:00 +0000 |
commit | a7bb158ab88b8d23337444cd77015d8e602d3d30 (patch) | |
tree | 9d658edc28a54d387d74b3c1de16c9b671f5e223 | |
parent | 5cd0c3d4e5bc8a7468476a45a2e542500944f000 (diff) | |
download | qpid-python-a7bb158ab88b8d23337444cd77015d8e602d3d30.tar.gz |
codec and unicode tests and fixes
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@750934 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | qpid/python/qpid/codec010.py | 19 | ||||
-rw-r--r-- | qpid/python/qpid/datatypes.py | 3 | ||||
-rw-r--r-- | qpid/python/qpid/spec010.py | 40 | ||||
-rw-r--r-- | qpid/python/tests/codec010.py | 16 |
4 files changed, 56 insertions, 22 deletions
diff --git a/qpid/python/qpid/codec010.py b/qpid/python/qpid/codec010.py index f34025ef17..62b0f6cf2a 100644 --- a/qpid/python/qpid/codec010.py +++ b/qpid/python/qpid/codec010.py @@ -19,7 +19,7 @@ import datetime from packer import Packer -from datatypes import serial, timestamp, RangedSet, Struct +from datatypes import serial, timestamp, RangedSet, Struct, UUID class CodecException(Exception): pass @@ -131,6 +131,11 @@ class Codec(Packer): def write_str16(self, s): self.write_vbin16(s.encode("utf8")) + def read_str16_latin(self): + return self.read_vbin16().decode("iso-8859-15") + def write_str16_latin(self, s): + self.write_vbin16(s.encode("iso-8859-15")) + def read_vbin16(self): return self.read(self.read_uint16()) @@ -166,7 +171,7 @@ class Codec(Packer): if m is not None: sc.write_uint32(len(m)) for k, v in m.items(): - type = self.spec.encoding(v.__class__) + type = self.spec.encoding(v) if type == None: raise CodecException("no encoding for %s" % v.__class__) sc.write_str8(k) @@ -191,9 +196,9 @@ class Codec(Packer): sc = StringCodec(self.spec) if a is not None: if len(a) > 0: - type = self.spec.encoding(a[0].__class__) + type = self.spec.encoding(a[0]) else: - type = self.spec.encoding(None.__class__) + type = self.spec.encoding(None) sc.write_uint8(type.code) sc.write_uint32(len(a)) for o in a: @@ -216,7 +221,7 @@ class Codec(Packer): if l is not None: sc.write_uint32(len(l)) for o in l: - type = self.spec.encoding(o.__class__) + type = self.spec.encoding(o) sc.write_uint8(type.code) type.encode(sc, o) self.write_vbin32(sc.encoded) @@ -273,9 +278,11 @@ class Codec(Packer): getattr(self, attr)(n) def read_uuid(self): - return self.unpack("16s") + return UUID(self.unpack("16s")) def write_uuid(self, s): + if isinstance(s, UUID): + s = s.bytes self.pack("16s", s) def read_bin128(self): diff --git a/qpid/python/qpid/datatypes.py b/qpid/python/qpid/datatypes.py index eb1f86b0b0..532995e051 100644 --- a/qpid/python/qpid/datatypes.py +++ b/qpid/python/qpid/datatypes.py @@ -289,7 +289,8 @@ class UUID: def __cmp__(self, other): if isinstance(other, UUID): return cmp(self.bytes, other.bytes) - raise NotImplemented() + else: + return -1 def __str__(self): return "%08x-%04x-%04x-%04x-%04x%08x" % struct.unpack("!LHHHHL", self.bytes) diff --git a/qpid/python/qpid/spec010.py b/qpid/python/qpid/spec010.py index cbc85a5e8b..9a88b88169 100644 --- a/qpid/python/qpid/spec010.py +++ b/qpid/python/qpid/spec010.py @@ -467,19 +467,30 @@ class Exception(Named, Node): node.exceptions.append(self) Node.register(self) +def direct(t): + return lambda x: t + +def map_str(s): + for c in s: + if ord(c) >= 0x80: + return "vbin16" + return "str16_latin" + class Spec(Node): ENCODINGS = { - basestring: "vbin16", - int: "int64", - long: "int64", - float: "float", - None.__class__: "void", - list: "list", - tuple: "list", - dict: "map", - datatypes.timestamp: "datetime", - datetime.datetime: "datetime" + unicode: direct("str16"), + str: map_str, + int: direct("int64"), + long: direct("int64"), + float: direct("float"), + None.__class__: direct("void"), + list: direct("list"), + tuple: direct("list"), + dict: direct("map"), + datatypes.timestamp: direct("datetime"), + datetime.datetime: direct("datetime"), + datatypes.UUID: direct("uuid") } def __init__(self, major, minor, port, children): @@ -500,11 +511,14 @@ class Spec(Node): self.structs_by_name = {} self.enums = {} - def encoding(self, klass): + def encoding(self, obj): + return self._encoding(obj.__class__, obj) + + def _encoding(self, klass, obj): if Spec.ENCODINGS.has_key(klass): - return self.named[Spec.ENCODINGS[klass]] + return self.named[Spec.ENCODINGS[klass](obj)] for base in klass.__bases__: - result = self.encoding(base) + result = self._encoding(base, obj) if result != None: return result diff --git a/qpid/python/tests/codec010.py b/qpid/python/tests/codec010.py index 1912eac591..099734a040 100644 --- a/qpid/python/tests/codec010.py +++ b/qpid/python/tests/codec010.py @@ -23,7 +23,7 @@ from unittest import TestCase from qpid.spec010 import load from qpid.codec010 import StringCodec from qpid.testlib import testrunner -from qpid.datatypes import timestamp +from qpid.datatypes import timestamp, uuid4 class CodecTest(TestCase): @@ -42,6 +42,12 @@ class CodecTest(TestCase): def testMapString(self): self.check("map", {"string": "this is a test"}) + def testMapUnicode(self): + self.check("map", {"unicode": u"this is a unicode test"}) + + def testMapBinary(self): + self.check("map", {"binary": "\x7f\xb4R^\xe5\xf0:\x89\x96E1\xf6\xfe\xb9\x1b\xf5"}) + def testMapInt(self): self.check("map", {"int": 3}) @@ -68,14 +74,20 @@ class CodecTest(TestCase): def testMapList(self): self.check("map", {"list": [1, "two", 3.0, -4]}) + def testMapUUID(self): + self.check("map", {"uuid": uuid4()}) + def testMapAll(self): decoded = self.check("map", {"string": "this is a test", + "unicode": u"this is a unicode test", + "binary": "\x7f\xb4R^\xe5\xf0:\x89\x96E1\xf6\xfe\xb9\x1b\xf5", "int": 3, "long": 2**32, "timestamp": timestamp(0), "none": None, "map": {"string": "nested map"}, - "list": [1, "two", 3.0, -4]}) + "list": [1, "two", 3.0, -4], + "uuid": uuid4()}) assert isinstance(decoded["timestamp"], timestamp) def testMapEmpty(self): |