Commit 20736bda authored by Nicolas Delaby's avatar Nicolas Delaby

Add support of set type

parent d9034195
......@@ -57,6 +57,7 @@ class Marshaller(object):
self.tag_code = E.code
self.tag_none = E.none
self.tag_instance = E.object
self.tag_set = E.set
# The four basic functions that form the caller's interface
def dump(self, value, file):
......@@ -189,6 +190,16 @@ class Marshaller(object):
xml_tree.append(self._marshal(v, kw))
return xml_tree
def m_set(self, value, kw):
kw['id'] += 1
i = str(kw['id'])
kw[str(id(value))] = i
kw[i] = value
xml_tree = self.tag_set(id='i%s' % i)
for elem in value:
xml_tree.append(self._marshal(elem, kw))
return xml_tree
# Python 2.2 renames dictionary to dict.
def m_dict(self, value, kw):
return self.m_dictionary(value, kw)
......@@ -250,6 +261,7 @@ class Marshaller(object):
TUPLE = {}
LIST = {}
DICT = {}
SET = {}
class Unmarshaller(ElementTreeContentHandler):
# This dictionary maps element names to the names of starting and ending
......@@ -270,7 +282,8 @@ class Unmarshaller(ElementTreeContentHandler):
'reference': ('um_start_reference', None),
'code': ('um_start_code', 'um_end_code'),
'none': ('um_start_none', 'um_end_none'),
'object': ('um_start_instance', 'um_end_instance')
'object': ('um_start_instance', 'um_end_instance'),
'set': ('um_start_set', 'um_end_set'),
}
def __init__(self):
......@@ -476,6 +489,14 @@ class Unmarshaller(ElementTreeContentHandler):
self.kw[id] = L
self.data_stack.append(L)
def um_start_set(self, name, attrs):
self.data_stack.append(SET)
S = set()
if 'id' in attrs:
id = attrs['id']
self.kw[id] = S
self.data_stack.append(S)
def um_end_list(self, name):
ds = self.data_stack
for index in range(len(ds)-1, -1, -1):
......@@ -486,6 +507,16 @@ class Unmarshaller(ElementTreeContentHandler):
L[:] = ds[index + 2:len(ds)]
ds[index:] = [L]
def um_end_set(self, name):
ds = self.data_stack
for index in range(len(ds)-1, -1, -1):
if ds[index] is SET:
break
assert index != -1
S = ds[index + 1]
[S.add(item) for item in ds[index + 2:len(ds)]]
ds[index:] = [S]
def um_start_tuple(self, name, attrs):
self.data_stack.append(TUPLE)
......@@ -611,6 +642,7 @@ def runtests(namespace_uri=None):
['alpha', 'beta', 'gamma'],
{'key': 'value', 1: 2},
'éàù^ç'.decode('utf-8'),
set(('a', 1,)),
]
if namespace_uri:
test(load, loads, dump_ns, dumps_ns, L)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment