Changeset 328 for pyyaml/trunk/tests3/canonical.py
- Timestamp:
- 12/29/08 12:24:05 (4 years ago)
- Location:
- pyyaml/trunk/tests3
- Files:
-
- 1 edited
- 1 copied
-
. (copied) (copied from pyyaml/trunk/tests)
-
canonical.py (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pyyaml/trunk/tests3/canonical.py
r322 r328 8 8 9 9 def __init__(self, data): 10 try: 11 self.data = unicode(data, 'utf-8')+u'\0' 12 except UnicodeDecodeError: 13 raise CanonicalError("utf-8 stream is expected") 10 if isinstance(data, bytes): 11 try: 12 data = data.decode('utf-8') 13 except UnicodeDecodeError: 14 raise CanonicalError("utf-8 stream is expected") 15 self.data = data+'\0' 14 16 self.index = 0 15 17 self.tokens = [] … … 50 52 self.find_token() 51 53 ch = self.data[self.index] 52 if ch == u'\0':54 if ch == '\0': 53 55 self.tokens.append(yaml.StreamEndToken(None, None)) 54 56 break 55 elif ch == u'%':57 elif ch == '%': 56 58 self.tokens.append(self.scan_directive()) 57 elif ch == u'-' and self.data[self.index:self.index+3] == u'---':59 elif ch == '-' and self.data[self.index:self.index+3] == '---': 58 60 self.index += 3 59 61 self.tokens.append(yaml.DocumentStartToken(None, None)) 60 elif ch == u'[':62 elif ch == '[': 61 63 self.index += 1 62 64 self.tokens.append(yaml.FlowSequenceStartToken(None, None)) 63 elif ch == u'{':65 elif ch == '{': 64 66 self.index += 1 65 67 self.tokens.append(yaml.FlowMappingStartToken(None, None)) 66 elif ch == u']':68 elif ch == ']': 67 69 self.index += 1 68 70 self.tokens.append(yaml.FlowSequenceEndToken(None, None)) 69 elif ch == u'}':71 elif ch == '}': 70 72 self.index += 1 71 73 self.tokens.append(yaml.FlowMappingEndToken(None, None)) 72 elif ch == u'?':74 elif ch == '?': 73 75 self.index += 1 74 76 self.tokens.append(yaml.KeyToken(None, None)) 75 elif ch == u':':77 elif ch == ':': 76 78 self.index += 1 77 79 self.tokens.append(yaml.ValueToken(None, None)) 78 elif ch == u',':80 elif ch == ',': 79 81 self.index += 1 80 82 self.tokens.append(yaml.FlowEntryToken(None, None)) 81 elif ch == u'*' or ch == u'&':83 elif ch == '*' or ch == '&': 82 84 self.tokens.append(self.scan_alias()) 83 elif ch == u'!':85 elif ch == '!': 84 86 self.tokens.append(self.scan_tag()) 85 elif ch == u'"':87 elif ch == '"': 86 88 self.tokens.append(self.scan_scalar()) 87 89 else: … … 89 91 self.scanned = True 90 92 91 DIRECTIVE = u'%YAML 1.1'93 DIRECTIVE = '%YAML 1.1' 92 94 93 95 def scan_directive(self): 94 96 if self.data[self.index:self.index+len(self.DIRECTIVE)] == self.DIRECTIVE and \ 95 self.data[self.index+len(self.DIRECTIVE)] in u' \n\0':97 self.data[self.index+len(self.DIRECTIVE)] in ' \n\0': 96 98 self.index += len(self.DIRECTIVE) 97 99 return yaml.DirectiveToken('YAML', (1, 1), None, None) … … 100 102 101 103 def scan_alias(self): 102 if self.data[self.index] == u'*':104 if self.data[self.index] == '*': 103 105 TokenClass = yaml.AliasToken 104 106 else: … … 106 108 self.index += 1 107 109 start = self.index 108 while self.data[self.index] not in u', \n\0':110 while self.data[self.index] not in ', \n\0': 109 111 self.index += 1 110 112 value = self.data[start:self.index] … … 114 116 self.index += 1 115 117 start = self.index 116 while self.data[self.index] not in u' \n\0':118 while self.data[self.index] not in ' \n\0': 117 119 self.index += 1 118 120 value = self.data[start:self.index] 119 121 if not value: 120 value = u'!'121 elif value[0] == u'!':122 value = '!' 123 elif value[0] == '!': 122 124 value = 'tag:yaml.org,2002:'+value[1:] 123 elif value[0] == u'<' and value[-1] == u'>':125 elif value[0] == '<' and value[-1] == '>': 124 126 value = value[1:-1] 125 127 else: 126 value = u'!'+value128 value = '!'+value 127 129 return yaml.TagToken(value, None, None) 128 130 … … 134 136 135 137 QUOTE_REPLACES = { 136 u'\\': u'\\', 137 u'\"': u'\"', 138 u' ': u' ', 139 u'a': u'\x07', 140 u'b': u'\x08', 141 u'e': u'\x1B', 142 u'f': u'\x0C', 143 u'n': u'\x0A', 144 u'r': u'\x0D', 145 u't': u'\x09', 146 u'v': u'\x0B', 147 u'N': u'\u0085', 148 u'L': u'\u2028', 149 u'P': u'\u2029', 150 u'_': u'_', 151 u'0': u'\x00', 152 138 '\\': '\\', 139 '\"': '\"', 140 ' ': ' ', 141 'a': '\x07', 142 'b': '\x08', 143 'e': '\x1B', 144 'f': '\x0C', 145 'n': '\x0A', 146 'r': '\x0D', 147 't': '\x09', 148 'v': '\x0B', 149 'N': '\u0085', 150 'L': '\u2028', 151 'P': '\u2029', 152 '_': '_', 153 '0': '\x00', 153 154 } 154 155 … … 158 159 start = self.index 159 160 ignore_spaces = False 160 while self.data[self.index] != u'"':161 if self.data[self.index] == u'\\':161 while self.data[self.index] != '"': 162 if self.data[self.index] == '\\': 162 163 ignore_spaces = False 163 164 chunks.append(self.data[start:self.index]) … … 165 166 ch = self.data[self.index] 166 167 self.index += 1 167 if ch == u'\n':168 if ch == '\n': 168 169 ignore_spaces = True 169 170 elif ch in self.QUOTE_CODES: 170 171 length = self.QUOTE_CODES[ch] 171 172 code = int(self.data[self.index:self.index+length], 16) 172 chunks.append( unichr(code))173 chunks.append(chr(code)) 173 174 self.index += length 174 175 else: … … 177 178 chunks.append(self.QUOTE_REPLACES[ch]) 178 179 start = self.index 179 elif self.data[self.index] == u'\n':180 elif self.data[self.index] == '\n': 180 181 chunks.append(self.data[start:self.index]) 181 chunks.append( u' ')182 chunks.append(' ') 182 183 self.index += 1 183 184 start = self.index 184 185 ignore_spaces = True 185 elif ignore_spaces and self.data[self.index] == u' ':186 elif ignore_spaces and self.data[self.index] == ' ': 186 187 self.index += 1 187 188 start = self.index … … 191 192 chunks.append(self.data[start:self.index]) 192 193 self.index += 1 193 return yaml.ScalarToken( u''.join(chunks), False, None, None)194 return yaml.ScalarToken(''.join(chunks), False, None, None) 194 195 195 196 def find_token(self): 196 197 found = False 197 198 while not found: 198 while self.data[self.index] in u' \t':199 self.index += 1 200 if self.data[self.index] == u'#':201 while self.data[self.index] != u'\n':199 while self.data[self.index] in ' \t': 200 self.index += 1 201 if self.data[self.index] == '#': 202 while self.data[self.index] != '\n': 202 203 self.index += 1 203 if self.data[self.index] == u'\n':204 if self.data[self.index] == '\n': 204 205 self.index += 1 205 206 else:
Note: See TracChangeset
for help on using the changeset viewer.
