Permalink
Browse files

update from 3.08 to 3.10 of PyYAML

  • Loading branch information...
1 parent 1f1778f commit da87d468642b5740c79bc604797a47fc6831f42b @ptarjan committed Jun 19, 2011
Showing with 72 additions and 80 deletions.
  1. +49 −22 yaml/__init__.py
  2. +3 −12 yaml/constructor.py
  3. +10 −2 yaml/emitter.py
  4. +5 −0 yaml/parser.py
  5. +3 −38 yaml/reader.py
  6. +0 −5 yaml/representer.py
  7. +2 −1 yaml/scanner.py
View
@@ -8,7 +8,7 @@
from loader import *
from dumper import *
-__version__ = '3.08'
+__version__ = '3.10'
try:
from cyaml import *
@@ -21,50 +21,68 @@ def scan(stream, Loader=Loader):
Scan a YAML stream and produce scanning tokens.
"""
loader = Loader(stream)
- while loader.check_token():
- yield loader.get_token()
+ try:
+ while loader.check_token():
+ yield loader.get_token()
+ finally:
+ loader.dispose()
def parse(stream, Loader=Loader):
"""
Parse a YAML stream and produce parsing events.
"""
loader = Loader(stream)
- while loader.check_event():
- yield loader.get_event()
+ try:
+ while loader.check_event():
+ yield loader.get_event()
+ finally:
+ loader.dispose()
def compose(stream, Loader=Loader):
"""
Parse the first YAML document in a stream
and produce the corresponding representation tree.
"""
loader = Loader(stream)
- return loader.get_single_node()
+ try:
+ return loader.get_single_node()
+ finally:
+ loader.dispose()
def compose_all(stream, Loader=Loader):
"""
Parse all YAML documents in a stream
- and produce corresponsing representation trees.
+ and produce corresponding representation trees.
"""
loader = Loader(stream)
- while loader.check_node():
- yield loader.get_node()
+ try:
+ while loader.check_node():
+ yield loader.get_node()
+ finally:
+ loader.dispose()
def load(stream, Loader=Loader):
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
"""
loader = Loader(stream)
- return loader.get_single_data()
+ try:
+ return loader.get_single_data()
+ finally:
+ loader.dispose()
def load_all(stream, Loader=Loader):
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
"""
loader = Loader(stream)
- while loader.check_data():
- yield loader.get_data()
+ try:
+ while loader.check_data():
+ yield loader.get_data()
+ finally:
+ loader.dispose()
def safe_load(stream):
"""
@@ -96,8 +114,11 @@ def emit(events, stream=None, Dumper=Dumper,
getvalue = stream.getvalue
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break)
- for event in events:
- dumper.emit(event)
+ try:
+ for event in events:
+ dumper.emit(event)
+ finally:
+ dumper.dispose()
if getvalue:
return getvalue()
@@ -122,10 +143,13 @@ def serialize_all(nodes, stream=None, Dumper=Dumper,
allow_unicode=allow_unicode, line_break=line_break,
encoding=encoding, version=version, tags=tags,
explicit_start=explicit_start, explicit_end=explicit_end)
- dumper.open()
- for node in nodes:
- dumper.serialize(node)
- dumper.close()
+ try:
+ dumper.open()
+ for node in nodes:
+ dumper.serialize(node)
+ dumper.close()
+ finally:
+ dumper.dispose()
if getvalue:
return getvalue()
@@ -160,10 +184,13 @@ def dump_all(documents, stream=None, Dumper=Dumper,
allow_unicode=allow_unicode, line_break=line_break,
encoding=encoding, version=version, tags=tags,
explicit_start=explicit_start, explicit_end=explicit_end)
- dumper.open()
- for data in documents:
- dumper.represent(data)
- dumper.close()
+ try:
+ dumper.open()
+ for data in documents:
+ dumper.represent(data)
+ dumper.close()
+ finally:
+ dumper.dispose()
if getvalue:
return getvalue()
View
@@ -7,11 +7,6 @@
import datetime
-try:
- set
-except NameError:
- from sets import Set as set
-
import binascii, re, sys, types
class ConstructorError(MarkedYAMLError):
@@ -58,11 +53,11 @@ def construct_document(self, node):
return data
def construct_object(self, node, deep=False):
+ if node in self.constructed_objects:
+ return self.constructed_objects[node]
if deep:
old_deep = self.deep_construct
self.deep_construct = True
- if node in self.constructed_objects:
- return self.constructed_objects[node]
if node in self.recursive_objects:
raise ConstructorError(None, None,
"found unconstructable recursive node", node.start_mark)
@@ -502,11 +497,7 @@ def find_python_name(self, name, mark):
raise ConstructorError("while constructing a Python object", mark,
"expected non-empty name appended to the tag", mark)
if u'.' in name:
- # Python 2.4 only
- #module_name, object_name = name.rsplit('.', 1)
- items = name.split('.')
- object_name = items.pop()
- module_name = '.'.join(items)
+ module_name, object_name = name.rsplit('.', 1)
else:
module_name = '__builtin__'
object_name = name
View
@@ -103,6 +103,11 @@ def __init__(self, stream, canonical=None, indent=None, width=None,
self.analysis = None
self.style = None
+ def dispose(self):
+ # Reset the state attributes (to clear self-references)
+ self.states = []
+ self.state = None
+
def emit(self, event):
self.events.append(event)
while not self.need_more_events():
@@ -581,7 +586,9 @@ def prepare_tag(self, tag):
return tag
handle = None
suffix = tag
- for prefix in self.tag_prefixes:
+ prefixes = self.tag_prefixes.keys()
+ prefixes.sort()
+ for prefix in prefixes:
if tag.startswith(prefix) \
and (prefix == u'!' or len(prefix) < len(tag)):
handle = self.tag_prefixes[prefix]
@@ -787,7 +794,7 @@ def flush_stream(self):
def write_stream_start(self):
# Write BOM if needed.
if self.encoding and self.encoding.startswith('utf-16'):
- self.stream.write(u'\xFF\xFE'.encode(self.encoding))
+ self.stream.write(u'\uFEFF'.encode(self.encoding))
def write_stream_end(self):
self.flush_stream()
@@ -1025,6 +1032,7 @@ def write_folded(self, text):
else:
if ch is None or ch in u' \n\x85\u2028\u2029':
data = text[start:end]
+ self.column += len(data)
if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
View
@@ -86,6 +86,11 @@ def __init__(self):
self.marks = []
self.state = self.parse_stream_start
+ def dispose(self):
+ # Reset the state attributes (to clear self-references)
+ self.states = []
+ self.state = None
+
def check_event(self, *choices):
# Check the type of the next event.
if self.current_event is None:
View
@@ -21,41 +21,6 @@
import codecs, re
-# Unfortunately, codec functions in Python 2.3 does not support the `finish`
-# arguments, so we have to write our own wrappers.
-
-try:
- codecs.utf_8_decode('', 'strict', False)
- from codecs import utf_8_decode, utf_16_le_decode, utf_16_be_decode
-
-except TypeError:
-
- def utf_16_le_decode(data, errors, finish=False):
- if not finish and len(data) % 2 == 1:
- data = data[:-1]
- return codecs.utf_16_le_decode(data, errors)
-
- def utf_16_be_decode(data, errors, finish=False):
- if not finish and len(data) % 2 == 1:
- data = data[:-1]
- return codecs.utf_16_be_decode(data, errors)
-
- def utf_8_decode(data, errors, finish=False):
- if not finish:
- # We are trying to remove a possible incomplete multibyte character
- # from the suffix of the data.
- # The first byte of a multi-byte sequence is in the range 0xc0 to 0xfd.
- # All further bytes are in the range 0x80 to 0xbf.
- # UTF-8 encoded UCS characters may be up to six bytes long.
- count = 0
- while count < 5 and count < len(data) \
- and '\x80' <= data[-count-1] <= '\xBF':
- count -= 1
- if count < 5 and count < len(data) \
- and '\xC0' <= data[-count-1] <= '\xFD':
- data = data[:-count-1]
- return codecs.utf_8_decode(data, errors)
-
class ReaderError(YAMLError):
def __init__(self, name, position, character, encoding, reason):
@@ -159,13 +124,13 @@ def determine_encoding(self):
self.update_raw()
if not isinstance(self.raw_buffer, unicode):
if self.raw_buffer.startswith(codecs.BOM_UTF16_LE):
- self.raw_decode = utf_16_le_decode
+ self.raw_decode = codecs.utf_16_le_decode
self.encoding = 'utf-16-le'
elif self.raw_buffer.startswith(codecs.BOM_UTF16_BE):
- self.raw_decode = utf_16_be_decode
+ self.raw_decode = codecs.utf_16_be_decode
self.encoding = 'utf-16-be'
else:
- self.raw_decode = utf_8_decode
+ self.raw_decode = codecs.utf_8_decode
self.encoding = 'utf-8'
self.update(1)
View
@@ -7,11 +7,6 @@
import datetime
-try:
- set
-except NameError:
- from sets import Set as set
-
import sys, copy_reg, types
class RepresenterError(YAMLError):
View
@@ -374,7 +374,8 @@ def fetch_stream_end(self):
# Set the current intendation to -1.
self.unwind_indent(-1)
- # Reset everything (not really needed).
+ # Reset simple keys.
+ self.remove_possible_simple_key()
self.allow_simple_key = False
self.possible_simple_keys = {}

0 comments on commit da87d46

Please sign in to comment.