Changeset 333
- Timestamp:
- 12/29/08 23:02:04 (4 years ago)
- Location:
- pyyaml/trunk
- Files:
-
- 2 added
- 10 edited
-
ext/_yaml.pyx (modified) (15 diffs)
-
lib/yaml/__init__.py (modified) (3 diffs)
-
lib/yaml/cyaml.py (modified) (3 diffs)
-
lib/yaml/emitter.py (modified) (1 diff)
-
tests/data/invalid-character.stream-error (modified) (1 diff)
-
tests/data/invalid-utf8-byte.loader-error (modified) (1 diff)
-
tests/data/invalid-utf8-byte.stream-error (modified) (1 diff)
-
tests/data/latin.unicode (added)
-
tests/data/odd-utf16.stream-error (modified) (previous)
-
tests/lib/test_input_output.py (added)
-
tests/lib/test_yaml.py (modified) (1 diff)
-
tests/lib/test_yaml_ext.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
pyyaml/trunk/ext/_yaml.pyx
r331 r333 250 250 cdef object current_event 251 251 cdef object anchors 252 cdef object stream_cache 253 cdef int stream_cache_len 254 cdef int stream_cache_pos 255 cdef int unicode_source 252 256 253 257 def __init__(self, stream): … … 261 265 except AttributeError: 262 266 is_readable = 0 267 self.unicode_source = 0 263 268 if is_readable: 264 269 self.stream = stream … … 267 272 except AttributeError: 268 273 self.stream_name = '<file>' 274 self.stream_cache = None 275 self.stream_cache_len = 0 276 self.stream_cache_pos = 0 269 277 yaml_parser_set_input(&self.parser, input_handler, <void *>self) 270 278 else: … … 272 280 stream = PyUnicode_AsUTF8String(stream) 273 281 self.stream_name = '<unicode string>' 282 self.unicode_source = 1 274 283 else: 275 self.stream_name = '< string>'284 self.stream_name = '<byte string>' 276 285 if PyString_CheckExact(stream) == 0: 277 286 raise TypeError("a string or stream input is required") … … 364 373 encoding = None 365 374 if token.data.stream_start.encoding == YAML_UTF8_ENCODING: 366 encoding = u"utf-8" 375 if self.unicode_source == 0: 376 encoding = u"utf-8" 367 377 elif token.data.stream_start.encoding == YAML_UTF16LE_ENCODING: 368 378 encoding = u"utf-16-le" … … 516 526 encoding = None 517 527 if event.data.stream_start.encoding == YAML_UTF8_ENCODING: 518 encoding = "utf-8" 528 if self.unicode_source == 0: 529 encoding = "utf-8" 519 530 elif event.data.stream_start.encoding == YAML_UTF16LE_ENCODING: 520 531 encoding = "utf-16-le" … … 878 889 cdef CParser parser 879 890 parser = <CParser>data 880 value = parser.stream.read(size) 881 if PyUnicode_CheckExact(value) != 0: 882 value = PyUnicode_AsUTF8String(value) 883 if PyString_CheckExact(value) == 0: 884 raise TypeError("a string value is expected") 885 if PyString_GET_SIZE(value) > size: 886 raise ValueError("a string value it too long") 887 memcpy(buffer, PyString_AS_STRING(value), PyString_GET_SIZE(value)) 888 read[0] = PyString_GET_SIZE(value) 891 if parser.stream_cache is None: 892 value = parser.stream.read(size) 893 if PyUnicode_CheckExact(value) != 0: 894 value = PyUnicode_AsUTF8String(value) 895 parser.unicode_source = 1 896 if PyString_CheckExact(value) == 0: 897 raise TypeError("a string value is expected") 898 parser.stream_cache = value 899 parser.stream_cache_pos = 0 900 parser.stream_cache_len = PyString_GET_SIZE(value) 901 if (parser.stream_cache_len - parser.stream_cache_pos) < size: 902 size = parser.stream_cache_len - parser.stream_cache_pos 903 if size > 0: 904 memcpy(buffer, PyString_AS_STRING(parser.stream_cache) 905 + parser.stream_cache_pos, size) 906 read[0] = size 907 parser.stream_cache_pos += size 908 if parser.stream_cache_pos == parser.stream_cache_len: 909 parser.stream_cache = None 889 910 return 1 890 911 … … 895 916 cdef object stream 896 917 897 cdef yaml_encoding_t use_encoding898 918 cdef int document_start_implicit 899 919 cdef int document_end_implicit … … 905 925 cdef int last_alias_id 906 926 cdef int closed 907 cdef int decode_output 927 cdef int dump_unicode 928 cdef object use_encoding 908 929 909 930 def __init__(self, stream, canonical=None, indent=None, width=None, … … 913 934 raise MemoryError 914 935 self.stream = stream 915 self.d ecode_output = 1936 self.dump_unicode = 0 916 937 try: 917 stream.encoding 938 if stream.encoding: 939 self.dump_unicode = 1 918 940 except AttributeError: 919 self.decode_output = 0 941 pass 942 self.use_encoding = encoding 920 943 yaml_emitter_set_output(&self.emitter, output_handler, <void *>self) 921 if canonical is not None:944 if canonical: 922 945 yaml_emitter_set_canonical(&self.emitter, 1) 923 946 if indent is not None: … … 925 948 if width is not None: 926 949 yaml_emitter_set_width(&self.emitter, width) 927 if allow_unicode is not None:950 if allow_unicode: 928 951 yaml_emitter_set_unicode(&self.emitter, 1) 929 952 if line_break is not None: … … 934 957 elif line_break == '\r\n': 935 958 yaml_emitter_set_break(&self.emitter, YAML_CRLN_BREAK) 936 if encoding == 'utf-16-le':937 self.use_encoding = YAML_UTF16LE_ENCODING938 elif encoding == 'utf-16-be':939 self.use_encoding = YAML_UTF16BE_ENCODING940 else:941 self.use_encoding = YAML_UTF8_ENCODING942 959 self.document_start_implicit = 1 943 960 if explicit_start: … … 987 1004 elif event_object.encoding == 'utf-16-be': 988 1005 encoding = YAML_UTF16BE_ENCODING 1006 if event_object.encoding is None: 1007 self.dump_unicode = 1 1008 if self.dump_unicode == 1: 1009 encoding = YAML_UTF8_ENCODING 989 1010 yaml_stream_start_event_initialize(event, encoding) 990 1011 elif event_class is StreamEndEvent: … … 1151 1172 def open(self): 1152 1173 cdef yaml_event_t event 1174 cdef yaml_encoding_t encoding 1153 1175 if self.closed == -1: 1154 yaml_stream_start_event_initialize(&event, self.use_encoding) 1176 if self.use_encoding == 'utf-16-le': 1177 encoding = YAML_UTF16LE_ENCODING 1178 elif self.use_encoding == 'utf-16-be': 1179 encoding = YAML_UTF16BE_ENCODING 1180 else: 1181 encoding = YAML_UTF8_ENCODING 1182 if self.use_encoding is None: 1183 self.dump_unicode = 1 1184 if self.dump_unicode == 1: 1185 encoding = YAML_UTF8_ENCODING 1186 yaml_stream_start_event_initialize(&event, encoding) 1155 1187 if yaml_emitter_emit(&self.emitter, &event) == 0: 1156 1188 error = self._emitter_error() … … 1374 1406 cdef CEmitter emitter 1375 1407 emitter = <CEmitter>data 1376 if emitter.d ecode_output== 0:1408 if emitter.dump_unicode == 0: 1377 1409 value = PyString_FromStringAndSize(buffer, size) 1378 1410 else: -
pyyaml/trunk/lib/yaml/__init__.py
r314 r333 92 92 getvalue = None 93 93 if stream is None: 94 try: 95 from cStringIO import StringIO 96 except ImportError: 97 from StringIO import StringIO 94 from StringIO import StringIO 98 95 stream = StringIO() 99 96 getvalue = stream.getvalue … … 116 113 getvalue = None 117 114 if stream is None: 118 try: 115 if encoding is None: 116 from StringIO import StringIO 117 else: 119 118 from cStringIO import StringIO 120 except ImportError:121 from StringIO import StringIO122 119 stream = StringIO() 123 120 getvalue = stream.getvalue … … 152 149 getvalue = None 153 150 if stream is None: 154 try: 151 if encoding is None: 152 from StringIO import StringIO 153 else: 155 154 from cStringIO import StringIO 156 except ImportError:157 from StringIO import StringIO158 155 stream = StringIO() 159 156 getvalue = stream.getvalue -
pyyaml/trunk/lib/yaml/cyaml.py
r223 r333 42 42 version=None, tags=None): 43 43 CEmitter.__init__(self, stream, canonical=canonical, 44 indent=indent, width=width, 44 indent=indent, width=width, encoding=encoding, 45 45 allow_unicode=allow_unicode, line_break=line_break, 46 46 explicit_start=explicit_start, explicit_end=explicit_end, … … 59 59 version=None, tags=None): 60 60 CEmitter.__init__(self, stream, canonical=canonical, 61 indent=indent, width=width, 61 indent=indent, width=width, encoding=encoding, 62 62 allow_unicode=allow_unicode, line_break=line_break, 63 63 explicit_start=explicit_start, explicit_end=explicit_end, … … 76 76 version=None, tags=None): 77 77 CEmitter.__init__(self, stream, canonical=canonical, 78 indent=indent, width=width, 78 indent=indent, width=width, encoding=encoding, 79 79 allow_unicode=allow_unicode, line_break=line_break, 80 80 explicit_start=explicit_start, explicit_end=explicit_end, -
pyyaml/trunk/lib/yaml/emitter.py
r328 r333 155 155 def expect_stream_start(self): 156 156 if isinstance(self.event, StreamStartEvent): 157 if self.event.encoding :157 if self.event.encoding and not getattr(self.stream, 'encoding', None): 158 158 self.encoding = self.event.encoding 159 159 self.write_stream_start() -
pyyaml/trunk/tests/data/invalid-character.stream-error
r322 r333 1 *************************************************************** 2 *************************************************************** 3 *************************************************************** 4 *************************************************************** 5 *************************************************************** 6 *************************************************************** 7 *************************************************************** 8 *************************************************************** 9 *************************************************************** 10 *************************************************************** 11 *************************************************************** 12 *************************************************************** 13 *************************************************************** 14 *************************************************************** 15 *************************************************************** 16 *************************************************************** 1 ############################################################### 2 ############################################################### 3 ############################################################### 4 ############################################################### 5 ############################################################### 6 ############################################################### 7 ############################################################### 8 ############################################################### 9 ############################################################### 10 ############################################################### 11 ############################################################### 12 ############################################################### 13 ############################################################### 14 ############################################################### 15 ############################################################### 16 ############################################################### 17 ############################################################### 18 ############################################################### 19 ############################################################### 20 ############################################################### 21 ############################################################### 22 ############################################################### 23 ############################################################### 24 ############################################################### 25 ############################################################### 26 ############################################################### 27 ############################################################### 28 ############################################################### 29 ############################################################### 30 ############################################################### 31 ############################################################### 32 ############################################################### 33 ############################################################### 34 ############################################################### 35 ############################################################### 36 ############################################################### 37 ############################################################### 38 ############################################################### 39 ############################################################### 40 ############################################################### 41 ############################################################### 42 ############################################################### 43 ############################################################### 44 ############################################################### 45 ############################################################### 46 ############################################################### 47 ############################################################### 48 ############################################################### 49 ############################################################### 50 ############################################################### 51 ############################################################### 52 ############################################################### 53 ############################################################### 54 ############################################################### 55 ############################################################### 56 ############################################################### 57 ############################################################### 58 ############################################################### 59 ############################################################### 60 ############################################################### 61 ############################################################### 62 ############################################################### 63 ############################################################### 64 ############################################################### 17 65 Control character ('\x0'): <-- 18 *************************************************************** 66 ############################################################### -
pyyaml/trunk/tests/data/invalid-utf8-byte.loader-error
r323 r333 1 *************************************************************** 2 *************************************************************** 3 *************************************************************** 4 *************************************************************** 5 *************************************************************** 6 *************************************************************** 7 *************************************************************** 8 *************************************************************** 9 *************************************************************** 10 *************************************************************** 11 *************************************************************** 12 *************************************************************** 13 *************************************************************** 14 *************************************************************** 15 *************************************************************** 16 *************************************************************** 1 ############################################################### 2 ############################################################### 3 ############################################################### 4 ############################################################### 5 ############################################################### 6 ############################################################### 7 ############################################################### 8 ############################################################### 9 ############################################################### 10 ############################################################### 11 ############################################################### 12 ############################################################### 13 ############################################################### 14 ############################################################### 15 ############################################################### 16 ############################################################### 17 ############################################################### 18 ############################################################### 19 ############################################################### 20 ############################################################### 21 ############################################################### 22 ############################################################### 23 ############################################################### 24 ############################################################### 25 ############################################################### 26 ############################################################### 27 ############################################################### 28 ############################################################### 29 ############################################################### 30 ############################################################### 31 ############################################################### 32 ############################################################### 33 ############################################################### 34 ############################################################### 35 ############################################################### 36 ############################################################### 37 ############################################################### 38 ############################################################### 39 ############################################################### 40 ############################################################### 41 ############################################################### 42 ############################################################### 43 ############################################################### 44 ############################################################### 45 ############################################################### 46 ############################################################### 47 ############################################################### 48 ############################################################### 49 ############################################################### 50 ############################################################### 51 ############################################################### 52 ############################################################### 53 ############################################################### 54 ############################################################### 55 ############################################################### 56 ############################################################### 57 ############################################################### 58 ############################################################### 59 ############################################################### 60 ############################################################### 61 ############################################################### 62 ############################################################### 63 ############################################################### 64 ############################################################### 17 65 Invalid byte ('\xFF'): ÿ <-- 18 *************************************************************** 66 ############################################################### -
pyyaml/trunk/tests/data/invalid-utf8-byte.stream-error
r322 r333 1 *************************************************************** 2 *************************************************************** 3 *************************************************************** 4 *************************************************************** 5 *************************************************************** 6 *************************************************************** 7 *************************************************************** 8 *************************************************************** 9 *************************************************************** 10 *************************************************************** 11 *************************************************************** 12 *************************************************************** 13 *************************************************************** 14 *************************************************************** 15 *************************************************************** 16 *************************************************************** 1 ############################################################### 2 ############################################################### 3 ############################################################### 4 ############################################################### 5 ############################################################### 6 ############################################################### 7 ############################################################### 8 ############################################################### 9 ############################################################### 10 ############################################################### 11 ############################################################### 12 ############################################################### 13 ############################################################### 14 ############################################################### 15 ############################################################### 16 ############################################################### 17 ############################################################### 18 ############################################################### 19 ############################################################### 20 ############################################################### 21 ############################################################### 22 ############################################################### 23 ############################################################### 24 ############################################################### 25 ############################################################### 26 ############################################################### 27 ############################################################### 28 ############################################################### 29 ############################################################### 30 ############################################################### 31 ############################################################### 32 ############################################################### 33 ############################################################### 34 ############################################################### 35 ############################################################### 36 ############################################################### 37 ############################################################### 38 ############################################################### 39 ############################################################### 40 ############################################################### 41 ############################################################### 42 ############################################################### 43 ############################################################### 44 ############################################################### 45 ############################################################### 46 ############################################################### 47 ############################################################### 48 ############################################################### 49 ############################################################### 50 ############################################################### 51 ############################################################### 52 ############################################################### 53 ############################################################### 54 ############################################################### 55 ############################################################### 56 ############################################################### 57 ############################################################### 58 ############################################################### 59 ############################################################### 60 ############################################################### 61 ############################################################### 62 ############################################################### 63 ############################################################### 64 ############################################################### 17 65 Invalid byte ('\xFF'): ÿ <-- 18 *************************************************************** 66 ############################################################### -
pyyaml/trunk/tests/lib/test_yaml.py
r330 r333 11 11 from test_representer import * 12 12 from test_recursive import * 13 from test_input_output import * 13 14 14 15 if __name__ == '__main__': -
pyyaml/trunk/tests/lib/test_yaml_ext.py
r330 r333 268 268 269 269 import test_tokens, test_structure, test_errors, test_resolver, test_constructor, \ 270 test_emitter, test_representer, test_recursive 270 test_emitter, test_representer, test_recursive, test_input_output 271 271 wrap_ext([test_tokens, test_structure, test_errors, test_resolver, test_constructor, 272 test_emitter, test_representer, test_recursive ])272 test_emitter, test_representer, test_recursive, test_input_output]) 273 273 274 274 if __name__ == '__main__':
Note: See TracChangeset
for help on using the changeset viewer.
