Author: rhs
Date: Tue Apr 29 13:24:48 2008
New Revision: 652086
URL: http://svn.apache.org/viewvc?rev=652086&view=rev
Log:
QPID-979: added backwards compatible uuid to qpid.datatypes
Modified:
incubator/qpid/trunk/qpid/python/qpid/datatypes.py
incubator/qpid/trunk/qpid/python/tests/datatypes.py
Modified: incubator/qpid/trunk/qpid/python/qpid/datatypes.py
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/datatypes.py?rev=652086&r1=652085&r2=652086&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/datatypes.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/datatypes.py Tue Apr 29 13:24:48 2008
@@ -17,7 +17,7 @@
# under the License.
#
-import threading
+import threading, struct
class Struct:
@@ -172,3 +172,40 @@
def is_set(self):
return self._set.isSet()
+
+try:
+ import uuid
+ def random_uuid():
+ return uuid.uuid4().get_bytes()
+except ImportError:
+ import random
+ def random_uuid():
+ bytes = [random.randint(0, 255) for i in xrange(16)]
+
+ # From RFC4122, the version bits are set to 0100
+ bytes[7] &= 0x0F
+ bytes[7] |= 0x40
+
+ # From RFC4122, the top two bits of byte 8 get set to 01
+ bytes[8] &= 0x3F
+ bytes[8] |= 0x80
+ return "".join(map(chr, bytes))
+
+def uuid4():
+ return UUID(random_uuid())
+
+class UUID:
+
+ def __init__(self, bytes):
+ self.bytes = bytes
+
+ def __cmp__(self, other):
+ if isinstance(other, UUID):
+ return cmp(self.bytes, other.bytes)
+ raise NotImplemented()
+
+ def __str__(self):
+ return "%08x-%04x-%04x-%04x-%04x%08x" % struct.unpack("!LHHHHL",
self.bytes)
+
+ def __repr__(self):
+ return "UUID(%r)" % str(self)
Modified: incubator/qpid/trunk/qpid/python/tests/datatypes.py
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/tests/datatypes.py?rev=652086&r1=652085&r2=652086&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/tests/datatypes.py (original)
+++ incubator/qpid/trunk/qpid/python/tests/datatypes.py Tue Apr 29 13:24:48 2008
@@ -100,3 +100,12 @@
range = a.ranges[0]
assert range.lower == 0
assert range.upper == 8
+
+class UUIDTest(TestCase):
+
+ def test(self):
+ # this test is kind of lame, but it does excercise the basic
+ # functionality of the class
+ u = uuid4()
+ for i in xrange(1024):
+ assert u != uuid4()