summaryrefslogtreecommitdiff
path: root/qpid/python
diff options
context:
space:
mode:
authorRafael H. Schloming <rhs@apache.org>2010-09-10 12:29:24 +0000
committerRafael H. Schloming <rhs@apache.org>2010-09-10 12:29:24 +0000
commitf54240840f359f99a39b828fb6713d4847278da2 (patch)
tree83b8bf61c200a0194e4385c286b649b0a652e712 /qpid/python
parent560aa7bce96f66e7295436a3509a2103f11f16cf (diff)
downloadqpid-python-f54240840f359f99a39b828fb6713d4847278da2.tar.gz
made qpid.datatypes module use builtin UUID when available
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@995770 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/python')
-rw-r--r--qpid/python/qpid/codec010.py2
-rw-r--r--qpid/python/qpid/datatypes.py58
2 files changed, 32 insertions, 28 deletions
diff --git a/qpid/python/qpid/codec010.py b/qpid/python/qpid/codec010.py
index d65f8c36f5..5ad1ef14c1 100644
--- a/qpid/python/qpid/codec010.py
+++ b/qpid/python/qpid/codec010.py
@@ -357,7 +357,7 @@ class Codec(Packer):
getattr(self, attr)(n)
def read_uuid(self):
- return UUID(self.unpack("16s"))
+ return UUID(bytes=self.unpack("16s"))
def write_uuid(self, s):
if isinstance(s, UUID):
s = s.bytes
diff --git a/qpid/python/qpid/datatypes.py b/qpid/python/qpid/datatypes.py
index c37929394c..ca1466c261 100644
--- a/qpid/python/qpid/datatypes.py
+++ b/qpid/python/qpid/datatypes.py
@@ -286,10 +286,35 @@ class Future:
return self._set.isSet()
try:
- import uuid
- def random_uuid():
- return uuid.uuid4().get_bytes()
+ from uuid import uuid4
+ from uuid import UUID
except ImportError:
+ class UUID:
+ def __init__(self, hex=None, bytes=None):
+ if [hex, bytes].count(None) != 1:
+ raise TypeErrror("need one of hex or bytes")
+ if bytes is not None:
+ self.bytes = bytes
+ elif hex is not None:
+ fields=hex.split("-")
+ fields[4:5] = [fields[4][:4], fields[4][4:]]
+ self.bytes = struct.pack("!LHHHHL", *[int(x,16) for x in fields])
+
+ def __cmp__(self, other):
+ if isinstance(other, UUID):
+ return cmp(self.bytes, other.bytes)
+ else:
+ return -1
+
+ def __str__(self):
+ return "%08x-%04x-%04x-%04x-%04x%08x" % struct.unpack("!LHHHHL", self.bytes)
+
+ def __repr__(self):
+ return "UUID(%r)" % str(self)
+
+ def __hash__(self):
+ return self.bytes.__hash__()
+
import os, random, socket, time
rand = random.Random()
rand.seed((os.getpid(), time.time(), socket.gethostname()))
@@ -305,32 +330,11 @@ except ImportError:
bytes[8] |= 0x80
return "".join(map(chr, bytes))
-def uuid4():
- return UUID(random_uuid())
+ def uuid4():
+ return UUID(bytes=random_uuid())
def parseUUID(str):
- fields=str.split("-")
- fields[4:5] = [fields[4][:4], fields[4][4:]]
- return UUID(struct.pack("!LHHHHL", *[int(x,16) for x in fields]))
-
-class UUID:
- def __init__(self, bytes):
- self.bytes = bytes
-
- def __cmp__(self, other):
- if isinstance(other, UUID):
- return cmp(self.bytes, other.bytes)
- else:
- return -1
-
- def __str__(self):
- return "%08x-%04x-%04x-%04x-%04x%08x" % struct.unpack("!LHHHHL", self.bytes)
-
- def __repr__(self):
- return "UUID(%r)" % str(self)
-
- def __hash__(self):
- return self.bytes.__hash__()
+ return UUID(hex=str)
class timestamp(float):