You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by rh...@apache.org on 2009/03/06 16:03:01 UTC
svn commit: r750934 - in /qpid/trunk/qpid/python: qpid/codec010.py
qpid/datatypes.py qpid/spec010.py tests/codec010.py
Author: rhs
Date: Fri Mar 6 15:03:00 2009
New Revision: 750934
URL: http://svn.apache.org/viewvc?rev=750934&view=rev
Log:
codec and unicode tests and fixes
Modified:
qpid/trunk/qpid/python/qpid/codec010.py
qpid/trunk/qpid/python/qpid/datatypes.py
qpid/trunk/qpid/python/qpid/spec010.py
qpid/trunk/qpid/python/tests/codec010.py
Modified: qpid/trunk/qpid/python/qpid/codec010.py
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/python/qpid/codec010.py?rev=750934&r1=750933&r2=750934&view=diff
==============================================================================
--- qpid/trunk/qpid/python/qpid/codec010.py (original)
+++ qpid/trunk/qpid/python/qpid/codec010.py Fri Mar 6 15:03:00 2009
@@ -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 @@
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 @@
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 @@
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 @@
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 @@
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):
Modified: qpid/trunk/qpid/python/qpid/datatypes.py
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/python/qpid/datatypes.py?rev=750934&r1=750933&r2=750934&view=diff
==============================================================================
--- qpid/trunk/qpid/python/qpid/datatypes.py (original)
+++ qpid/trunk/qpid/python/qpid/datatypes.py Fri Mar 6 15:03:00 2009
@@ -289,7 +289,8 @@
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)
Modified: qpid/trunk/qpid/python/qpid/spec010.py
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/python/qpid/spec010.py?rev=750934&r1=750933&r2=750934&view=diff
==============================================================================
--- qpid/trunk/qpid/python/qpid/spec010.py (original)
+++ qpid/trunk/qpid/python/qpid/spec010.py Fri Mar 6 15:03:00 2009
@@ -467,19 +467,30 @@
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 @@
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
Modified: qpid/trunk/qpid/python/tests/codec010.py
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/python/tests/codec010.py?rev=750934&r1=750933&r2=750934&view=diff
==============================================================================
--- qpid/trunk/qpid/python/tests/codec010.py (original)
+++ qpid/trunk/qpid/python/tests/codec010.py Fri Mar 6 15:03:00 2009
@@ -23,7 +23,7 @@
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 @@
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 @@
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):
---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project: http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org