Changeset 20
- Timestamp:
- 08/12/05 16:15:19 (8 years ago)
- Location:
- trunk
- Files:
-
- 2 added
- 7 edited
-
ext/_syckmodule.c (modified) (4 diffs)
-
lib/syck/dumpers.py (modified) (4 diffs)
-
sandbox/syck-parser/syck-parser.c (modified) (2 diffs)
-
sandbox/syck-parser/test_aliases.yml (added)
-
sandbox/syck-parser/test_aliases2.yml (added)
-
tests/test_dumper.py (modified) (3 diffs)
-
tests/test_loader.py (modified) (2 diffs)
-
tests/test_parser.py (modified) (2 diffs)
-
tests/test_syck.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ext/_syckmodule.c
r17 r20 1059 1059 if (!object) goto error; 1060 1060 for (k = 0; k < node->data.list->idx; k++) { 1061 index = syck_seq_read(node, k) ;1061 index = syck_seq_read(node, k)-1; 1062 1062 value = PyList_GetItem(self->symbols, index); 1063 1063 if (!value) goto error; … … 1073 1073 for (k = 0; k < node->data.pairs->idx; k++) 1074 1074 { 1075 index = syck_map_read(node, map_key, k) ;1075 index = syck_map_read(node, map_key, k)-1; 1076 1076 key = PyList_GetItem(self->symbols, index); 1077 1077 if (!key) goto error; 1078 index = syck_map_read(node, map_value, k) ;1078 index = syck_map_read(node, map_value, k)-1; 1079 1079 value = PyList_GetItem(self->symbols, index); 1080 1080 if (!value) goto error; … … 1098 1098 goto error; 1099 1099 1100 index = PyList_GET_SIZE(self->symbols) -1;1100 index = PyList_GET_SIZE(self->symbols); 1101 1101 return index; 1102 1102 … … 1268 1268 1269 1269 self->parsing = 1; 1270 index = syck_parse(self->parser) ;1270 index = syck_parse(self->parser)-1; 1271 1271 self->parsing = 0; 1272 1272 -
trunk/lib/syck/dumpers.py
r19 r20 10 10 'emit', 'dump', 'emit_documents', 'dump_documents'] 11 11 12 INF = 1e300000 13 NEGINF = -INF 14 NAN = INF/INF 15 16 12 17 class GenericDumper(_syck.Emitter): 13 18 … … 16 21 17 22 def _convert(self, object, object_to_node): 18 if id(object) in object_to_node :23 if id(object) in object_to_node and self.allow_aliases(object): 19 24 return object_to_node[id(object)] 20 25 node = self.represent(object) … … 29 34 node.value[self._convert(key, object_to_node)] = \ 30 35 self._convert(value, object_to_node) 36 # # Workaround against a Syck bug: 37 # if node.kind == 'scalar' and node.style not in ['1quote', '2quote'] \ 38 # and node.value and node.value[-1] in [' ', '\t']: 39 # node.style = '2quote' 31 40 return node 32 41 … … 39 48 return _syck.Scalar(str(object), tag="tag:yaml.org,2002:str") 40 49 50 def allow_aliases(self, object): 51 return True 52 41 53 class Dumper(GenericDumper): 42 54 55 def __init__(self, *args, **kwds): 56 super(Dumper, self).__init__(*args, **kwds) 57 43 58 def represent(self, object): 59 for object_type in type(object).__mro__: 60 if object_type.__module__ == '__builtin__': 61 name = object_type.__name__ 62 else: 63 name = '%s.%s' % (object_type.__module__, object_type.__name__) 64 method = 'represent_%s' % name.replace('.', '_') 65 if hasattr(self, method): 66 return getattr(self, method)(object) 44 67 return super(Dumper, self).represent(object) 68 69 def represent_object(self, object): 70 return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:str") 71 72 def represent_NoneType(self, object): 73 return _syck.Scalar('~', tag="tag:yaml.org,2002:null") 74 75 def represent_bool(self, object): 76 return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:bool") 77 78 def represent_str(self, object): 79 return _syck.Scalar(str(object), tag="tag:yaml.org,2002:str") 80 81 def represent_list(self, object): 82 return _syck.Seq(object[:], tag="tag:yaml.org,2002:seq") 83 84 def represent_dict(self, object): 85 return _syck.Map(object.copy(), tag="tag:yaml.org,2002:map") 86 87 def represent_int(self, object): 88 return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:int") 89 90 def represent_int(self, object): 91 return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:int") 92 93 def represent_float(self, object): 94 value = repr(object) 95 if value == repr(INF): 96 value = '.inf' 97 elif value == repr(NEGINF): 98 value = '-.inf' 99 elif value == repr(NAN): 100 value = '.nan' 101 return _syck.Scalar(value, tag="tag:yaml.org,2002:float") 102 103 def represent_sets_Set(self, object): 104 return _syck.Seq(list(object), tag="tag:yaml.org,2002:set") 105 106 def represent_datetime_datetime(self, object): 107 return _syck.Scalar(object.isoformat(), tag="tag:yaml.org,2002:timestamp") 108 109 def allow_aliases(self, object): 110 if object is None or type(object) in [int, bool, float]: 111 return False 112 if type(object) is str and (not object or object.isalnum()): 113 return False 114 return True 45 115 46 116 def emit(node, output=None, **parameters): -
trunk/sandbox/syck-parser/syck-parser.c
r4 r20 123 123 switch (n->kind) { 124 124 case syck_str_kind: 125 output(indent, " Node Scalar (%s):", n->type_id);125 output(indent, "[%d] Node Scalar (%s):", id, n->type_id); 126 126 output(indent+1, "Value: '%s'", n->data.str->ptr); 127 127 break; 128 128 case syck_seq_kind: 129 output(indent, " Node Sequence (%s):", n->type_id);129 output(indent, "[%d] Node Sequence (%s):", id, n->type_id); 130 130 for (i = 0; i < syck_seq_count(n); i++) { 131 131 output(indent+1, "Item #%d:", i+1); … … 134 134 break; 135 135 case syck_map_kind: 136 output(indent, " Node Mapping (%s):", n->type_id);136 output(indent, "[%d] Node Mapping (%s):", id, n->type_id); 137 137 for (i = 0; i < syck_map_count(n); i++) { 138 138 output(indent+1, "Key #%d:", i+1); -
trunk/tests/test_dumper.py
r19 r20 2 2 import unittest 3 3 import syck 4 import StringIO 4 import StringIO, datetime, sets 5 5 import test_emitter 6 6 … … 15 15 'Sammy Sosa', 16 16 'Ken Griffey', 17 ] 18 19 ALIAS_SCALAR = ["foo, bar, and baz"] 20 ALIAS_SCALAR.append(ALIAS_SCALAR[0]) 21 ALIAS_SCALAR.append(ALIAS_SCALAR[0]) 22 ALIAS_SCALAR.append(ALIAS_SCALAR[0]) 23 24 ALIAS_SEQ = [['foo', ['bar']]] 25 ALIAS_SEQ.append(ALIAS_SEQ[0]) 26 ALIAS_SEQ.append(ALIAS_SEQ[0]) 27 28 ALIAS_MAP = [{'foo': 'bar'}] 29 ALIAS_MAP.append(ALIAS_MAP[0]) 30 31 ODD_ALIASES = [ 32 [None]*2, 33 [0]*3, 34 [1]*4, 35 [100]*5, 36 ['']*6, 37 ['foo']*7, 38 ] 39 40 INF = 1e300000 41 NAN = INF/INF 42 43 SCALARS = [ 44 None, 45 True, False, 46 0, 123, -4567, 47 123.4e-5, INF, NAN, 48 'foo, bar, baz', 49 datetime.datetime(2001, 12, 15, 2, 59, 43, 100000), 50 ] 51 52 COLLECTIONS = [ 53 sets.Set(range(10)), 17 54 ] 18 55 … … 73 110 map(test_emitter.strip, nodes)) 74 111 112 class TestAliases(unittest.TestCase): 113 114 def testAliases(self): 115 self._testAlias(ALIAS_SCALAR) 116 self._testAlias(ALIAS_SEQ) 117 self._testAlias(ALIAS_MAP) 118 119 def _testAlias(self, objects): 120 objects = syck.load(syck.dump(objects)) 121 for object in objects: 122 self.assert_(object is objects[0]) 123 124 class TestNoOddAliases(unittest.TestCase): 125 126 def testOddAliases(self): 127 document = syck.parse(syck.dump(ODD_ALIASES)) 128 for group in document.value: 129 for item in group.value[1:]: 130 self.assert_(item is not group.value[0]) 131 132 class TestScalarTypes(unittest.TestCase): 133 134 def testScalarTypes(self): 135 scalars = syck.load(syck.dump(SCALARS)) 136 for a, b in zip(scalars, SCALARS): 137 self.assertEqual(type(a), type(b)) 138 self.assertEqual(a, b) 139 140 class TestCollectionTypes(unittest.TestCase): 141 142 def testCollectionTypes(self): 143 collections = syck.load(syck.dump(COLLECTIONS)) 144 for a, b in zip(collections, COLLECTIONS): 145 self.assertEqual(type(a), type(b)) 146 self.assertEqual(a, b) 147 148 -
trunk/tests/test_loader.py
r18 r20 130 130 'baseball teams': sets.Set(['Boston Red Sox', 'Detroit Tigers', 'New York Yankees']), 131 131 } 132 133 ALIASES = """ 134 foo: &bar baz 135 bar: *bar 136 """ 132 137 133 138 class TestDocuments(test_parser.TestDocuments): … … 258 263 self.assertEqual(syck.load(SET[0]), SET[1]) 259 264 265 class TestAliasesParsingAndLoading(unittest.TestCase): 266 267 def testAliasesParsing(self): 268 node = syck.parse(ALIASES) 269 values = node.value.values() 270 print values 271 print id(values[0]) 272 print id(values[1]) 273 self.assert_(values[0] is values[1]) 274 275 def testAliasesLoading(self): 276 document = syck.load(ALIASES) 277 self.assert_(document['foo'] is document['bar']) 278 -
trunk/tests/test_parser.py
r17 r20 169 169 """ 170 170 171 ALIASES = """ 172 - &alias foo 173 - *alias 174 """ 175 171 176 class TestAttributes(unittest.TestCase): 172 177 … … 359 364 self.assertRaises(TypeError, lambda: parser.parse()) 360 365 366 class TestParsingAliases(unittest.TestCase): 367 368 def testAliases(self): 369 parser = _syck.Parser(ALIASES) 370 node = parser.parse() 371 self.assert_(node.value[0] is node.value[1]) 372 -
trunk/tests/test_syck.py
r19 r20 3 3 4 4 #from test_node import * 5 #from test_parser import *6 #from test_loader import *7 #from test_emitter import *5 from test_parser import * 6 from test_loader import * 7 from test_emitter import * 8 8 from test_dumper import * 9 9
Note: See TracChangeset
for help on using the changeset viewer.
