summaryrefslogtreecommitdiff
path: root/_test/lib
diff options
context:
space:
mode:
Diffstat (limited to '_test/lib')
-rw-r--r--_test/lib/canonical.py112
-rw-r--r--_test/lib/test_appliance.py22
-rw-r--r--_test/lib/test_canonical.py3
-rw-r--r--_test/lib/test_constructor.py14
-rw-r--r--_test/lib/test_errors.py15
-rw-r--r--_test/lib/test_input_output.py296
-rw-r--r--_test/lib/test_mark.py7
-rw-r--r--_test/lib/test_reader.py13
-rw-r--r--_test/lib/test_recursive.py15
-rw-r--r--_test/lib/test_representer.py5
-rw-r--r--_test/lib/test_resolver.py15
-rw-r--r--_test/lib/test_structure.py72
-rw-r--r--_test/lib/test_tokens.py5
-rw-r--r--_test/lib/test_yaml_ext.py27
14 files changed, 232 insertions, 389 deletions
diff --git a/_test/lib/canonical.py b/_test/lib/canonical.py
index af2c3cf..1cab1ff 100644
--- a/_test/lib/canonical.py
+++ b/_test/lib/canonical.py
@@ -3,7 +3,6 @@ import ruamel.yaml
from ruamel.yaml.composer import Composer
from ruamel.yaml.constructor import Constructor
from ruamel.yaml.resolver import Resolver
-from ruamel.yaml.compat import unichr, PY3
class CanonicalError(ruamel.yaml.YAMLError):
@@ -13,14 +12,11 @@ class CanonicalError(ruamel.yaml.YAMLError):
class CanonicalScanner:
def __init__(self, data):
try:
- if PY3:
- if isinstance(data, bytes):
- data = data.decode('utf-8')
- else:
- data = unicode(data, 'utf-8') # NOQA
+ if isinstance(data, bytes):
+ data = data.decode('utf-8')
except UnicodeDecodeError:
raise CanonicalError('utf-8 stream is expected')
- self.data = data + u'\0'
+ self.data = data + '\0'
self.index = 0
self.tokens = []
self.scanned = False
@@ -59,51 +55,51 @@ class CanonicalScanner:
while True:
self.find_token()
ch = self.data[self.index]
- if ch == u'\0':
+ if ch == '\0':
self.tokens.append(ruamel.yaml.StreamEndToken(None, None))
break
- elif ch == u'%':
+ elif ch == '%':
self.tokens.append(self.scan_directive())
- elif ch == u'-' and self.data[self.index : self.index + 3] == u'---':
+ elif ch == '-' and self.data[self.index : self.index + 3] == '---':
self.index += 3
self.tokens.append(ruamel.yaml.DocumentStartToken(None, None))
- elif ch == u'[':
+ elif ch == '[':
self.index += 1
self.tokens.append(ruamel.yaml.FlowSequenceStartToken(None, None))
- elif ch == u'{':
+ elif ch == '{':
self.index += 1
self.tokens.append(ruamel.yaml.FlowMappingStartToken(None, None))
- elif ch == u']':
+ elif ch == ']':
self.index += 1
self.tokens.append(ruamel.yaml.FlowSequenceEndToken(None, None))
- elif ch == u'}':
+ elif ch == '}':
self.index += 1
self.tokens.append(ruamel.yaml.FlowMappingEndToken(None, None))
- elif ch == u'?':
+ elif ch == '?':
self.index += 1
self.tokens.append(ruamel.yaml.KeyToken(None, None))
- elif ch == u':':
+ elif ch == ':':
self.index += 1
self.tokens.append(ruamel.yaml.ValueToken(None, None))
- elif ch == u',':
+ elif ch == ',':
self.index += 1
self.tokens.append(ruamel.yaml.FlowEntryToken(None, None))
- elif ch == u'*' or ch == u'&':
+ elif ch == '*' or ch == '&':
self.tokens.append(self.scan_alias())
- elif ch == u'!':
+ elif ch == '!':
self.tokens.append(self.scan_tag())
- elif ch == u'"':
+ elif ch == '"':
self.tokens.append(self.scan_scalar())
else:
raise CanonicalError('invalid token')
self.scanned = True
- DIRECTIVE = u'%YAML 1.1'
+ DIRECTIVE = '%YAML 1.1'
def scan_directive(self):
if (
self.data[self.index : self.index + len(self.DIRECTIVE)] == self.DIRECTIVE
- and self.data[self.index + len(self.DIRECTIVE)] in u' \n\0'
+ and self.data[self.index + len(self.DIRECTIVE)] in ' \n\0'
):
self.index += len(self.DIRECTIVE)
return ruamel.yaml.DirectiveToken('YAML', (1, 1), None, None)
@@ -111,13 +107,13 @@ class CanonicalScanner:
raise CanonicalError('invalid directive')
def scan_alias(self):
- if self.data[self.index] == u'*':
+ if self.data[self.index] == '*':
TokenClass = ruamel.yaml.AliasToken
else:
TokenClass = ruamel.yaml.AnchorToken
self.index += 1
start = self.index
- while self.data[self.index] not in u', \n\0':
+ while self.data[self.index] not in ', \n\0':
self.index += 1
value = self.data[start : self.index]
return TokenClass(value, None, None)
@@ -125,38 +121,38 @@ class CanonicalScanner:
def scan_tag(self):
self.index += 1
start = self.index
- while self.data[self.index] not in u' \n\0':
+ while self.data[self.index] not in ' \n\0':
self.index += 1
value = self.data[start : self.index]
if not value:
- value = u'!'
- elif value[0] == u'!':
+ value = '!'
+ elif value[0] == '!':
value = 'tag:yaml.org,2002:' + value[1:]
- elif value[0] == u'<' and value[-1] == u'>':
+ elif value[0] == '<' and value[-1] == '>':
value = value[1:-1]
else:
- value = u'!' + value
+ value = '!' + value
return ruamel.yaml.TagToken(value, None, None)
QUOTE_CODES = {'x': 2, 'u': 4, 'U': 8}
QUOTE_REPLACES = {
- u'\\': u'\\',
- u'"': u'"',
- u' ': u' ',
- u'a': u'\x07',
- u'b': u'\x08',
- u'e': u'\x1B',
- u'f': u'\x0C',
- u'n': u'\x0A',
- u'r': u'\x0D',
- u't': u'\x09',
- u'v': u'\x0B',
- u'N': u'\u0085',
- u'L': u'\u2028',
- u'P': u'\u2029',
- u'_': u'_',
- u'0': u'\x00',
+ '\\': '\\',
+ '"': '"',
+ ' ': ' ',
+ 'a': '\x07',
+ 'b': '\x08',
+ 'e': '\x1B',
+ 'f': '\x0C',
+ 'n': '\x0A',
+ 'r': '\x0D',
+ 't': '\x09',
+ 'v': '\x0B',
+ 'N': '\u0085',
+ 'L': '\u2028',
+ 'P': '\u2029',
+ '_': '_',
+ '0': '\x00',
}
def scan_scalar(self):
@@ -164,32 +160,32 @@ class CanonicalScanner:
chunks = []
start = self.index
ignore_spaces = False
- while self.data[self.index] != u'"':
- if self.data[self.index] == u'\\':
+ while self.data[self.index] != '"':
+ if self.data[self.index] == '\\':
ignore_spaces = False
chunks.append(self.data[start : self.index])
self.index += 1
ch = self.data[self.index]
self.index += 1
- if ch == u'\n':
+ if ch == '\n':
ignore_spaces = True
elif ch in self.QUOTE_CODES:
length = self.QUOTE_CODES[ch]
code = int(self.data[self.index : self.index + length], 16)
- chunks.append(unichr(code))
+ chunks.append(chr(code))
self.index += length
else:
if ch not in self.QUOTE_REPLACES:
raise CanonicalError('invalid escape code')
chunks.append(self.QUOTE_REPLACES[ch])
start = self.index
- elif self.data[self.index] == u'\n':
+ elif self.data[self.index] == '\n':
chunks.append(self.data[start : self.index])
- chunks.append(u' ')
+ chunks.append(' ')
self.index += 1
start = self.index
ignore_spaces = True
- elif ignore_spaces and self.data[self.index] == u' ':
+ elif ignore_spaces and self.data[self.index] == ' ':
self.index += 1
start = self.index
else:
@@ -202,12 +198,12 @@ class CanonicalScanner:
def find_token(self):
found = False
while not found:
- while self.data[self.index] in u' \t':
+ while self.data[self.index] in ' \t':
self.index += 1
- if self.data[self.index] == u'#':
- while self.data[self.index] != u'\n':
+ if self.data[self.index] == '#':
+ while self.data[self.index] != '\n':
self.index += 1
- if self.data[self.index] == u'\n':
+ if self.data[self.index] == '\n':
self.index += 1
else:
found = True
@@ -378,7 +374,9 @@ ruamel.yaml.canonical_load = canonical_load
def canonical_load_all(stream):
- return ruamel.yaml.load_all(stream, Loader=CanonicalLoader)
+ yaml = ruamel.yaml.YAML(typ='safe', pure=True)
+ yaml.Loader = CanonicalLoader
+ return yaml.load_all(stream)
ruamel.yaml.canonical_load_all = canonical_load_all
diff --git a/_test/lib/test_appliance.py b/_test/lib/test_appliance.py
index 137c271..d624ebe 100644
--- a/_test/lib/test_appliance.py
+++ b/_test/lib/test_appliance.py
@@ -1,13 +1,10 @@
-from __future__ import print_function
-
import sys
import os
import types
import traceback
import pprint
import argparse
-from ruamel.yaml.compat import PY3
# DATA = 'tests/data'
# determine the position of data dynamically relative to program
@@ -35,7 +32,8 @@ def find_test_filenames(directory):
for filename in os.listdir(directory):
if os.path.isfile(os.path.join(directory, filename)):
base, ext = os.path.splitext(filename)
- if base.endswith('-py2' if PY3 else '-py3'):
+ # ToDo: remove
+ if base.endswith('-py2'):
continue
filenames.setdefault(base, []).append(ext)
filenames = sorted(filenames.items())
@@ -105,13 +103,7 @@ def parse_arguments(args):
def execute(function, filenames, verbose):
- if PY3:
- name = function.__name__
- else:
- if hasattr(function, 'unittest_name'):
- name = function.unittest_name
- else:
- name = function.func_name
+ name = function.__name__
if verbose:
sys.stdout.write('=' * 75 + '\n')
sys.stdout.write('%s(%s)...\n' % (name, ', '.join(filenames)))
@@ -164,12 +156,8 @@ def display(results, verbose):
for filename in filenames:
sys.stdout.write('-' * 75 + '\n')
sys.stdout.write('%s:\n' % filename)
- if PY3:
- with open(filename, 'r', errors='replace') as fp:
- data = fp.read()
- else:
- with open(filename, 'rb') as fp:
- data = fp.read()
+ with open(filename, 'r', errors='replace') as fp:
+ data = fp.read()
sys.stdout.write(data)
if data and data[-1] != '\n':
sys.stdout.write('\n')
diff --git a/_test/lib/test_canonical.py b/_test/lib/test_canonical.py
index 48a1764..b5cd14d 100644
--- a/_test/lib/test_canonical.py
+++ b/_test/lib/test_canonical.py
@@ -1,5 +1,4 @@
-# from __future__ import absolute_import
-from __future__ import print_function
+
import ruamel.yaml
import canonical # NOQA
diff --git a/_test/lib/test_constructor.py b/_test/lib/test_constructor.py
index a66ff1a..738aaec 100644
--- a/_test/lib/test_constructor.py
+++ b/_test/lib/test_constructor.py
@@ -1,9 +1,6 @@
-from __future__ import absolute_import
-from __future__ import print_function
import ruamel.yaml
import pprint
-from ruamel.yaml.compat import PY2
import datetime
@@ -211,7 +208,6 @@ def _make_objects():
def __setstate__(self, state):
self.baz = state
- # if PY3 or PY2:
InitArgs = NewArgs
InitArgsWithState = NewArgsWithState
@@ -291,8 +287,6 @@ def _serialize_value(data):
return '{%s}' % ', '.join(items)
elif isinstance(data, datetime.datetime):
return repr(data.utctimetuple())
- elif PY2 and isinstance(data, unicode): # NOQA
- return data.encode('utf-8')
elif isinstance(data, float) and data != data:
return '?'
else:
@@ -303,9 +297,11 @@ def test_constructor_types(data_filename, code_filename, verbose=False):
_make_objects()
native1 = None
native2 = None
+ yaml = ruamel.yaml.YAML(typ='safe', pure=True)
+ yaml.loader = MyLoader
try:
with open(data_filename, 'rb') as fp0:
- native1 = list(ruamel.yaml.load_all(fp0, Loader=MyLoader))
+ native1 = list(yaml.load_all(fp0))
if len(native1) == 1:
native1 = native1[0]
with open(code_filename, 'rb') as fp0:
@@ -337,7 +333,9 @@ def test_roundtrip_data(code_filename, roundtrip_filename, verbose=False):
_make_objects()
with open(code_filename, 'rb') as fp0:
value1 = fp0.read()
- native2 = list(ruamel.yaml.load_all(value1, Loader=MyLoader))
+ yaml = YAML(typ='safe', pure=True)
+ yaml.Loader = MyLoader
+ native2 = list(yaml.load_all(value1))
if len(native2) == 1:
native2 = native2[0]
try:
diff --git a/_test/lib/test_errors.py b/_test/lib/test_errors.py
index b43540c..c0fd3df 100644
--- a/_test/lib/test_errors.py
+++ b/_test/lib/test_errors.py
@@ -1,14 +1,15 @@
-from __future__ import absolute_import
-from __future__ import print_function
-import ruamel.yaml as yaml
+import ruamel.yaml
+YAML = ruamel.yaml.YAML
+
import test_emitter
import warnings
-warnings.simplefilter('ignore', yaml.error.UnsafeLoaderWarning)
+warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning)
def test_loader_error(error_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
try:
with open(error_filename, 'rb') as fp0:
list(yaml.load_all(fp0))
@@ -23,6 +24,7 @@ test_loader_error.unittest = ['.loader-error']
def test_loader_error_string(error_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
try:
with open(error_filename, 'rb') as fp0:
list(yaml.load_all(fp0.read()))
@@ -37,6 +39,7 @@ test_loader_error_string.unittest = ['.loader-error']
def test_loader_error_single(error_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
try:
with open(error_filename, 'rb') as fp0:
yaml.load(fp0.read())
@@ -51,10 +54,11 @@ test_loader_error_single.unittest = ['.single-loader-error']
def test_emitter_error(error_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
with open(error_filename, 'rb') as fp0:
events = list(yaml.load(fp0, Loader=test_emitter.EventsLoader))
try:
- yaml.emit(events)
+ ruamel.yaml.emit(events)
except yaml.YAMLError as exc:
if verbose:
print('%s:' % exc.__class__.__name__, exc)
@@ -66,6 +70,7 @@ test_emitter_error.unittest = ['.emitter-error']
def test_dumper_error(error_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
with open(error_filename, 'rb') as fp0:
code = fp0.read()
try:
diff --git a/_test/lib/test_input_output.py b/_test/lib/test_input_output.py
index c36477f..37bda3d 100644
--- a/_test/lib/test_input_output.py
+++ b/_test/lib/test_input_output.py
@@ -1,79 +1,39 @@
-from __future__ import absolute_import
-from __future__ import print_function
-import ruamel.yaml as yaml
+from ruamel.yaml import YAML
import codecs
import tempfile
import os
import os.path
-from ruamel.yaml.compat import PY2, PY3, StringIO, BytesIO
+from ruamel.yaml.compat import StringIO, BytesIO
-if PY2:
-
- def _unicode_open(file, encoding, errors='strict'):
- info = codecs.lookup(encoding)
- if isinstance(info, tuple):
- reader = info[2]
- writer = info[3]
- else:
- reader = info.streamreader
- writer = info.streamwriter
- srw = codecs.StreamReaderWriter(file, reader, writer, errors)
- srw.encoding = encoding
- return srw
-
-
-if PY3:
-
- def test_unicode_input(unicode_filename, verbose=False):
- with open(unicode_filename, 'rb') as fp:
- data = fp.read().decode('utf-8')
- value = ' '.join(data.split())
- output = yaml.load(data)
- assert output == value, (output, value)
- output = yaml.load(StringIO(data))
+def test_unicode_input(unicode_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
+ with open(unicode_filename, 'rb') as fp:
+ data = fp.read().decode('utf-8')
+ value = ' '.join(data.split())
+ output = yaml.load(data)
+ assert output == value, (output, value)
+ output = yaml.load(StringIO(data))
+ assert output == value, (output, value)
+ for input in [
+ data.encode('utf-8'),
+ codecs.BOM_UTF8 + data.encode('utf-8'),
+ codecs.BOM_UTF16_BE + data.encode('utf-16-be'),
+ codecs.BOM_UTF16_LE + data.encode('utf-16-le'),
+ ]:
+ if verbose:
+ print('INPUT:', repr(input[:10]), '...')
+ output = yaml.load(input)
assert output == value, (output, value)
- for input in [
- data.encode('utf-8'),
- codecs.BOM_UTF8 + data.encode('utf-8'),
- codecs.BOM_UTF16_BE + data.encode('utf-16-be'),
- codecs.BOM_UTF16_LE + data.encode('utf-16-le'),
- ]:
- if verbose:
- print('INPUT:', repr(input[:10]), '...')
- output = yaml.load(input)
- assert output == value, (output, value)
- output = yaml.load(BytesIO(input))
- assert output == value, (output, value)
-
-
-else:
-
- def test_unicode_input(unicode_filename, verbose=False):
- with open(unicode_filename, 'rb') as fp:
- data = fp.read().decode('utf-8')
- value = ' '.join(data.split())
- output = yaml.load(_unicode_open(StringIO(data.encode('utf-8')), 'utf-8'))
+ output = yaml.load(BytesIO(input))
assert output == value, (output, value)
- for input in [
- data,
- data.encode('utf-8'),
- codecs.BOM_UTF8 + data.encode('utf-8'),
- codecs.BOM_UTF16_BE + data.encode('utf-16-be'),
- codecs.BOM_UTF16_LE + data.encode('utf-16-le'),
- ]:
- if verbose:
- print('INPUT:', repr(input[:10]), '...')
- output = yaml.load(input)
- assert output == value, (output, value)
- output = yaml.load(StringIO(input))
- assert output == value, (output, value)
test_unicode_input.unittest = ['.unicode']
def test_unicode_input_errors(unicode_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
with open(unicode_filename, 'rb') as fp:
data = fp.read().decode('utf-8')
for input in [
@@ -92,7 +52,7 @@ def test_unicode_input_errors(unicode_filename, verbose=False):
else:
raise AssertionError('expected an exception')
try:
- yaml.load(BytesIO(input) if PY3 else StringIO(input))
+ yaml.load(BytesIO(input))
except yaml.YAMLError as exc:
if verbose:
print(exc)
@@ -102,108 +62,63 @@ def test_unicode_input_errors(unicode_filename, verbose=False):
test_unicode_input_errors.unittest = ['.unicode']
-if PY3:
- def test_unicode_output(unicode_filename, verbose=False):
- with open(unicode_filename, 'rb') as fp:
- data = fp.read().decode('utf-8')
- value = ' '.join(data.split())
- for allow_unicode in [False, True]:
- data1 = yaml.dump(value, allow_unicode=allow_unicode)
- for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']:
- stream = StringIO()
+def test_unicode_output(unicode_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
+ with open(unicode_filename, 'rb') as fp:
+ data = fp.read().decode('utf-8')
+ value = ' '.join(data.split())
+ for allow_unicode in [False, True]:
+ data1 = yaml.dump(value, allow_unicode=allow_unicode)
+ for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']:
+ stream = StringIO()
+ yaml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode)
+ data2 = stream.getvalue()
+ data3 = yaml.dump(value, encoding=encoding, allow_unicode=allow_unicode)
+ if encoding is not None:
+ assert isinstance(data3, bytes)
+ data3 = data3.decode(encoding)
+ stream = BytesIO()
+ if encoding is None:
+ try:
+ yaml.dump(
+ value, stream, encoding=encoding, allow_unicode=allow_unicode
+ )
+ except TypeError as exc:
+ if verbose:
+ print(exc)
+ data4 = None
+ else:
+ raise AssertionError('expected an exception')
+ else:
yaml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode)
- data2 = stream.getvalue()
- data3 = yaml.dump(value, encoding=encoding, allow_unicode=allow_unicode)
- if encoding is not None:
- assert isinstance(data3, bytes)
- data3 = data3.decode(encoding)
- stream = BytesIO()
- if encoding is None:
+ data4 = stream.getvalue()
+ if verbose:
+ print('BYTES:', data4[:50])
+ data4 = data4.decode(encoding)
+ for copy in [data1, data2, data3, data4]:
+ if copy is None:
+ continue
+ assert isinstance(copy, str)
+ if allow_unicode:
try:
- yaml.dump(
- value, stream, encoding=encoding, allow_unicode=allow_unicode
- )
- except TypeError as exc:
+ copy[4:].encode('ascii')
+ except UnicodeEncodeError as exc:
if verbose:
print(exc)
- data4 = None
else:
raise AssertionError('expected an exception')
else:
- yaml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode)
- data4 = stream.getvalue()
- if verbose:
- print('BYTES:', data4[:50])
- data4 = data4.decode(encoding)
- for copy in [data1, data2, data3, data4]:
- if copy is None:
- continue
- assert isinstance(copy, str)
- if allow_unicode:
- try:
- copy[4:].encode('ascii')
- except UnicodeEncodeError as exc:
- if verbose:
- print(exc)
- else:
- raise AssertionError('expected an exception')
- else:
- copy[4:].encode('ascii')
- assert isinstance(data1, str), (type(data1), encoding)
- assert isinstance(data2, str), (type(data2), encoding)
-
-
-else:
-
- def test_unicode_output(unicode_filename, verbose=False):
- with open(unicode_filename, 'rb') as fp:
- data = fp.read().decode('utf-8')
- value = ' '.join(data.split())
- for allow_unicode in [False, True]:
- data1 = yaml.dump(value, allow_unicode=allow_unicode)
- for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']:
- stream = StringIO()
- yaml.dump(
- value,
- _unicode_open(stream, 'utf-8'),
- encoding=encoding,
- allow_unicode=allow_unicode,
- )
- data2 = stream.getvalue()
- data3 = yaml.dump(value, encoding=encoding, allow_unicode=allow_unicode)
- stream = StringIO()
- yaml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode)
- data4 = stream.getvalue()
- for copy in [data1, data2, data3, data4]:
- if allow_unicode:
- try:
- copy[4:].encode('ascii')
- except (UnicodeDecodeError, UnicodeEncodeError) as exc:
- if verbose:
- print(exc)
- else:
- raise AssertionError('expected an exception')
- else:
- copy[4:].encode('ascii')
- assert isinstance(data1, str), (type(data1), encoding)
- data1.decode('utf-8')
- assert isinstance(data2, str), (type(data2), encoding)
- data2.decode('utf-8')
- if encoding is None:
- assert isinstance(data3, unicode), (type(data3), encoding) # NOQA
- assert isinstance(data4, unicode), (type(data4), encoding) # NOQA
- else:
- assert isinstance(data3, str), (type(data3), encoding)
- data3.decode(encoding)
- assert isinstance(data4, str), (type(data4), encoding)
- data4.decode(encoding)
+ copy[4:].encode('ascii')
+ assert isinstance(data1, str), (type(data1), encoding)
+ assert isinstance(data2, str), (type(data2), encoding)
test_unicode_output.unittest = ['.unicode']
def test_file_output(unicode_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
with open(unicode_filename, 'rb') as fp:
data = fp.read().decode('utf-8')
handle, filename = tempfile.mkstemp()
@@ -212,32 +127,17 @@ def test_file_output(unicode_filename, verbose=False):
stream = StringIO()
yaml.dump(data, stream, allow_unicode=True)
data1 = stream.getvalue()
- if PY3:
- stream = BytesIO()
- yaml.dump(data, stream, encoding='utf-16-le', allow_unicode=True)
- data2 = stream.getvalue().decode('utf-16-le')[1:]
- with open(filename, 'w', encoding='utf-16-le') as stream:
- yaml.dump(data, stream, allow_unicode=True)
- with open(filename, 'r', encoding='utf-16-le') as fp0:
- data3 = fp0.read()
- with open(filename, 'wb') as stream:
- yaml.dump(data, stream, encoding='utf-8', allow_unicode=True)
- with open(filename, 'r', encoding='utf-8') as fp0:
- data4 = fp0.read()
- else:
- with open(filename, 'wb') as stream:
- yaml.dump(data, stream, allow_unicode=True)
- with open(filename, 'rb') as fp0:
- data2 = fp0.read()
- with open(filename, 'wb') as stream:
- yaml.dump(data, stream, encoding='utf-16-le', allow_unicode=True)
- with open(filename, 'rb') as fp0:
- data3 = fp0.read().decode('utf-16-le')[1:].encode('utf-8')
- stream = _unicode_open(open(filename, 'wb'), 'utf-8')
+ stream = BytesIO()
+ yaml.dump(data, stream, encoding='utf-16-le', allow_unicode=True)
+ data2 = stream.getvalue().decode('utf-16-le')[1:]
+ with open(filename, 'w', encoding='utf-16-le') as stream:
yaml.dump(data, stream, allow_unicode=True)
- stream.close()
- with open(filename, 'rb') as fp0:
- data4 = fp0.read()
+ with open(filename, 'r', encoding='utf-16-le') as fp0:
+ data3 = fp0.read()
+ with open(filename, 'wb') as stream:
+ yaml.dump(data, stream, encoding='utf-8', allow_unicode=True)
+ with open(filename, 'r', encoding='utf-8') as fp0:
+ data4 = fp0.read()
assert data1 == data2, (data1, data2)
assert data1 == data3, (data1, data3)
assert data1 == data4, (data1, data4)
@@ -250,40 +150,26 @@ test_file_output.unittest = ['.unicode']
def test_unicode_transfer(unicode_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
with open(unicode_filename, 'rb') as fp:
data = fp.read().decode('utf-8')
for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']:
input = data
- if PY3:
- if encoding is not None:
- input = ('\ufeff' + input).encode(encoding)
- output1 = yaml.emit(yaml.parse(input), allow_unicode=True)
- if encoding is None:
- stream = StringIO()
- else:
- stream = BytesIO()
- yaml.emit(yaml.parse(input), stream, allow_unicode=True)
- output2 = stream.getvalue()
- assert isinstance(output1, str), (type(output1), encoding)
- if encoding is None:
- assert isinstance(output2, str), (type(output1), encoding)
- else:
- assert isinstance(output2, bytes), (type(output1), encoding)
- output2.decode(encoding)
- else:
- if encoding is not None:
- input = (u'\ufeff' + input).encode(encoding)
- output1 = yaml.emit(yaml.parse(input), allow_unicode=True)
+ if encoding is not None:
+ input = ('\ufeff' + input).encode(encoding)
+ output1 = yaml.emit(yaml.parse(input), allow_unicode=True)
+ if encoding is None:
stream = StringIO()
- yaml.emit(yaml.parse(input), _unicode_open(stream, 'utf-8'), allow_unicode=True)
- output2 = stream.getvalue()
- if encoding is None:
- assert isinstance(output1, unicode), (type(output1), encoding) # NOQA
- else:
- assert isinstance(output1, str), (type(output1), encoding)
- output1.decode(encoding)
- assert isinstance(output2, str), (type(output2), encoding)
- output2.decode('utf-8')
+ else:
+ stream = BytesIO()
+ yaml.emit(yaml.parse(input), stream, allow_unicode=True)
+ output2 = stream.getvalue()
+ assert isinstance(output1, str), (type(output1), encoding)
+ if encoding is None:
+ assert isinstance(output2, str), (type(output1), encoding)
+ else:
+ assert isinstance(output2, bytes), (type(output1), encoding)
+ output2.decode(encoding)
test_unicode_transfer.unittest = ['.unicode']
diff --git a/_test/lib/test_mark.py b/_test/lib/test_mark.py
index 0ff2789..2644a79 100644
--- a/_test/lib/test_mark.py
+++ b/_test/lib/test_mark.py
@@ -1,12 +1,9 @@
-from __future__ import absolute_import
-from __future__ import print_function
import ruamel.yaml as yaml
-from ruamel.yaml.compat import text_type, PY3
def test_marks(marks_filename, verbose=False):
- with open(marks_filename, 'r' if PY3 else 'rb') as fp0:
+ with open(marks_filename, 'r') as fp0:
inputs = fp0.read().split('---\n')[1:]
for input in inputs:
index = 0
@@ -19,7 +16,7 @@ def test_marks(marks_filename, verbose=False):
else:
column += 1
index += 1
- mark = yaml.Mark(marks_filename, index, line, column, text_type(input), index)
+ mark = yaml.Mark(marks_filename, index, line, column, str(input), index)
snippet = mark.get_snippet(indent=2, max_length=79)
if verbose:
print(snippet)
diff --git a/_test/lib/test_reader.py b/_test/lib/test_reader.py
index 6604f24..16b9cd7 100644
--- a/_test/lib/test_reader.py
+++ b/_test/lib/test_reader.py
@@ -1,17 +1,14 @@
-from __future__ import absolute_import
-from __future__ import print_function
import codecs # NOQA
import io
-from ruamel.yaml.compat import PY2
import ruamel.yaml.reader
def _run_reader(data, verbose):
try:
stream = ruamel.yaml.py.reader.Reader(data)
- while stream.peek() != u'\0':
+ while stream.peek() != '\0':
stream.forward()
except ruamel.yaml.py.reader.ReaderError as exc:
if verbose:
@@ -27,12 +24,8 @@ def test_stream_error(error_filename, verbose=False):
_run_reader(fp0.read(), verbose)
for encoding in ['utf-8', 'utf-16-le', 'utf-16-be']:
try:
- if PY2:
- with open(error_filename, 'rb') as fp0:
- data = unicode(fp0.read(), encoding) # NOQA
- else:
- with open(error_filename, 'rb') as fp0:
- data = fp0.read().decode(encoding)
+ with open(error_filename, 'rb') as fp0:
+ data = fp0.read().decode(encoding)
break
except UnicodeDecodeError:
pass
diff --git a/_test/lib/test_recursive.py b/_test/lib/test_recursive.py
index c87f879..88858e4 100644
--- a/_test/lib/test_recursive.py
+++ b/_test/lib/test_recursive.py
@@ -1,7 +1,5 @@
-from __future__ import absolute_import
-from __future__ import print_function
-import ruamel.yaml as yaml
+import ruamel.yaml
class AnInstance:
@@ -25,6 +23,7 @@ class AnInstanceWithState(AnInstance):
def test_recursive(recursive_filename, verbose=False):
+ yaml = ruamel.yaml.YAML(typ='safe', pure=True)
context = globals().copy()
with open(recursive_filename, 'rb') as fp0:
exec(fp0.read(), context)
@@ -33,9 +32,13 @@ def test_recursive(recursive_filename, verbose=False):
value2 = None
output2 = None
try:
- output1 = yaml.dump(value1)
- value2 = yaml.load(output1)
- output2 = yaml.dump(value2)
+ buf = ruamel.yaml.compat.StringIO()
+ output1 = yaml.dump(value1, buf)
+ yaml.load(output1)
+ value2 = buf.getvalue()
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(value2, buf)
+ output2 = buf.getvalue()
assert output1 == output2, (output1, output2)
finally:
if verbose:
diff --git a/_test/lib/test_representer.py b/_test/lib/test_representer.py
index a83d2b2..5b2415d 100644
--- a/_test/lib/test_representer.py
+++ b/_test/lib/test_representer.py
@@ -1,12 +1,11 @@
-from __future__ import absolute_import
-from __future__ import print_function
-import ruamel.yaml as yaml
+from ruamel.yaml import YAML
import test_constructor
import pprint
def test_representer_types(code_filename, verbose=False):
+ yaml = YAML(typ='safe', pure=True)
test_constructor._make_objects()
for allow_unicode in [False, True]:
for encoding in ['utf-8', 'utf-16-be', 'utf-16-le']:
diff --git a/_test/lib/test_resolver.py b/_test/lib/test_resolver.py
index 0a04e7a..24373a7 100644
--- a/_test/lib/test_resolver.py
+++ b/_test/lib/test_resolver.py
@@ -1,16 +1,13 @@
-from __future__ import absolute_import
-from __future__ import print_function
import ruamel.yaml as yaml
import pprint
-from ruamel.yaml.compat import PY3
def test_implicit_resolver(data_filename, detect_filename, verbose=False):
correct_tag = None
node = None
try:
- with open(detect_filename, 'r' if PY3 else 'rb') as fp0:
+ with open(detect_filename, 'r') as fp0:
correct_tag = fp0.read().strip()
with open(data_filename, 'rb') as fp0:
node = yaml.compose(fp0)
@@ -38,14 +35,14 @@ def _make_path_loader_and_dumper():
class MyDumper(yaml.Dumper):
pass
- yaml.add_path_resolver(u'!root', [], Loader=MyLoader, Dumper=MyDumper)
- yaml.add_path_resolver(u'!root/scalar', [], str, Loader=MyLoader, Dumper=MyDumper)
+ yaml.add_path_resolver('!root', [], Loader=MyLoader, Dumper=MyDumper)
+ yaml.add_path_resolver('!root/scalar', [], str, Loader=MyLoader, Dumper=MyDumper)
yaml.add_path_resolver(
- u'!root/key11/key12/*', ['key11', 'key12'], Loader=MyLoader, Dumper=MyDumper
+ '!root/key11/key12/*', ['key11', 'key12'], Loader=MyLoader, Dumper=MyDumper
)
- yaml.add_path_resolver(u'!root/key21/1/*', ['key21', 1], Loader=MyLoader, Dumper=MyDumper)
+ yaml.add_path_resolver('!root/key21/1/*', ['key21', 1], Loader=MyLoader, Dumper=MyDumper)
yaml.add_path_resolver(
- u'!root/key31/*/*/key14/map',
+ '!root/key31/*/*/key14/map',
['key31', None, None, 'key14'],
dict,
Loader=MyLoader,
diff --git a/_test/lib/test_structure.py b/_test/lib/test_structure.py
index 2656bbb..470d267 100644
--- a/_test/lib/test_structure.py
+++ b/_test/lib/test_structure.py
@@ -1,36 +1,33 @@
-from __future__ import absolute_import
-from __future__ import print_function
-import ruamel.yaml as yaml
+import ruamel.yaml
import canonical # NOQA
import pprint
-from ruamel.yaml.compat import text_type, PY3
def _convert_structure(loader):
- if loader.check_event(yaml.ScalarEvent):
+ if loader.check_event(ruamel.yaml.ScalarEvent):
event = loader.get_event()
if event.tag or event.anchor or event.value:
return True
else:
return None
- elif loader.check_event(yaml.SequenceStartEvent):
+ elif loader.check_event(ruamel.yaml.SequenceStartEvent):
loader.get_event()
sequence = []
- while not loader.check_event(yaml.SequenceEndEvent):
+ while not loader.check_event(ruamel.yaml.SequenceEndEvent):
sequence.append(_convert_structure(loader))
loader.get_event()
return sequence
- elif loader.check_event(yaml.MappingStartEvent):
+ elif loader.check_event(ruamel.yaml.MappingStartEvent):
loader.get_event()
mapping = []
- while not loader.check_event(yaml.MappingEndEvent):
+ while not loader.check_event(ruamel.yaml.MappingEndEvent):
key = _convert_structure(loader)
value = _convert_structure(loader)
mapping.append((key, value))
loader.get_event()
return mapping
- elif loader.check_event(yaml.AliasEvent):
+ elif loader.check_event(ruamel.yaml.AliasEvent):
loader.get_event()
return '*'
else:
@@ -40,17 +37,17 @@ def _convert_structure(loader):
def test_structure(data_filename, structure_filename, verbose=False):
nodes1 = []
- with open(structure_filename, 'r' if PY3 else 'rb') as fp:
+ with open(structure_filename, 'r') as fp:
nodes2 = eval(fp.read())
try:
with open(data_filename, 'rb') as fp:
- loader = yaml.Loader(fp)
+ loader = ruamel.yaml.Loader(fp)
while loader.check_event():
if loader.check_event(
- yaml.StreamStartEvent,
- yaml.StreamEndEvent,
- yaml.DocumentStartEvent,
- yaml.DocumentEndEvent,
+ ruamel.yaml.StreamStartEvent,
+ ruamel.yaml.StreamEndEvent,
+ ruamel.yaml.DocumentStartEvent,
+ ruamel.yaml.DocumentEndEvent,
):
loader.get_event()
continue
@@ -73,12 +70,12 @@ def _compare_events(events1, events2, full=False):
assert len(events1) == len(events2), (len(events1), len(events2))
for event1, event2 in zip(events1, events2):
assert event1.__class__ == event2.__class__, (event1, event2)
- if isinstance(event1, yaml.AliasEvent) and full:
+ if isinstance(event1, ruamel.yaml.AliasEvent) and full:
assert event1.anchor == event2.anchor, (event1, event2)
- if isinstance(event1, (yaml.ScalarEvent, yaml.CollectionStartEvent)):
- if (event1.tag not in [None, u'!'] and event2.tag not in [None, u'!']) or full:
+ if isinstance(event1, (ruamel.yaml.ScalarEvent, ruamel.yaml.CollectionStartEvent)):
+ if (event1.tag not in [None, '!'] and event2.tag not in [None, '!']) or full:
assert event1.tag == event2.tag, (event1, event2)
- if isinstance(event1, yaml.ScalarEvent):
+ if isinstance(event1, ruamel.yaml.ScalarEvent):
assert event1.value == event2.value, (event1, event2)
@@ -87,9 +84,9 @@ def test_parser(data_filename, canonical_filename, verbose=False):
events2 = None
try:
with open(data_filename, 'rb') as fp0:
- events1 = list(yaml.parse(fp0))
+ events1 = list(ruamel.yaml.parse(fp0))
with open(canonical_filename, 'rb') as fp0:
- events2 = list(yaml.canonical_parse(fp0))
+ events2 = list(ruamel.yaml.canonical_parse(fp0))
_compare_events(events1, events2)
finally:
if verbose:
@@ -107,9 +104,9 @@ def test_parser_on_canonical(canonical_filename, verbose=False):
events2 = None
try:
with open(canonical_filename, 'rb') as fp0:
- events1 = list(yaml.parse(fp0))
+ events1 = list(ruamel.yaml.parse(fp0))
with open(canonical_filename, 'rb') as fp0:
- events2 = list(yaml.canonical_parse(fp0))
+ events2 = list(ruamel.yaml.canonical_parse(fp0))
_compare_events(events1, events2, full=True)
finally:
if verbose:
@@ -125,7 +122,7 @@ test_parser_on_canonical.unittest = ['.canonical']
def _compare_nodes(node1, node2):
assert node1.__class__ == node2.__class__, (node1, node2)
assert node1.tag == node2.tag, (node1, node2)
- if isinstance(node1, yaml.ScalarNode):
+ if isinstance(node1, ruamel.yaml.ScalarNode):
assert node1.value == node2.value, (node1, node2)
else:
assert len(node1.value) == len(node2.value), (node1, node2)
@@ -142,9 +139,9 @@ def test_composer(data_filename, canonical_filename, verbose=False):
nodes2 = None
try:
with open(data_filename, 'rb') as fp0:
- nodes1 = list(yaml.compose_all(fp0))
+ nodes1 = list(ruamel.yaml.compose_all(fp0))
with open(canonical_filename, 'rb') as fp0:
- nodes2 = list(yaml.canonical_compose_all(fp0))
+ nodes2 = list(ruamel.yaml.canonical_compose_all(fp0))
assert len(nodes1) == len(nodes2), (len(nodes1), len(nodes2))
for node1, node2 in zip(nodes1, nodes2):
_compare_nodes(node1, node2)
@@ -162,39 +159,39 @@ test_composer.unittest = ['.data', '.canonical']
def _make_loader():
global MyLoader
- class MyLoader(yaml.Loader):
+ class MyLoader(ruamel.yaml.Loader):
def construct_sequence(self, node):
- return tuple(yaml.Loader.construct_sequence(self, node))
+ return tuple(ruamel.yaml.Loader.construct_sequence(self, node))
def construct_mapping(self, node):
pairs = self.construct_pairs(node)
- pairs.sort(key=(lambda i: text_type(i)))
+ pairs.sort(key=(lambda i: str(i)))
return pairs
def construct_undefined(self, node):
return self.construct_scalar(node)
- MyLoader.add_constructor(u'tag:yaml.org,2002:map', MyLoader.construct_mapping)
+ MyLoader.add_constructor('tag:yaml.org,2002:map', MyLoader.construct_mapping)
MyLoader.add_constructor(None, MyLoader.construct_undefined)
def _make_canonical_loader():
global MyCanonicalLoader
- class MyCanonicalLoader(yaml.CanonicalLoader):
+ class MyCanonicalLoader(ruamel.yaml.CanonicalLoader):
def construct_sequence(self, node):
- return tuple(yaml.CanonicalLoader.construct_sequence(self, node))
+ return tuple(ruamel.yaml.CanonicalLoader.construct_sequence(self, node))
def construct_mapping(self, node):
pairs = self.construct_pairs(node)
- pairs.sort(key=(lambda i: text_type(i)))
+ pairs.sort(key=(lambda i: str(i)))
return pairs
def construct_undefined(self, node):
return self.construct_scalar(node)
MyCanonicalLoader.add_constructor(
- u'tag:yaml.org,2002:map', MyCanonicalLoader.construct_mapping
+ 'tag:yaml.org,2002:map', MyCanonicalLoader.construct_mapping
)
MyCanonicalLoader.add_constructor(None, MyCanonicalLoader.construct_undefined)
@@ -204,11 +201,12 @@ def test_constructor(data_filename, canonical_filename, verbose=False):
_make_canonical_loader()
native1 = None
native2 = None
+ yaml = YAML(typ='safe')
try:
with open(data_filename, 'rb') as fp0:
- native1 = list(yaml.load_all(fp0, Loader=MyLoader))
+ native1 = list(yaml.load(fp0, Loader=MyLoader))
with open(canonical_filename, 'rb') as fp0:
- native2 = list(yaml.load_all(fp0, Loader=MyCanonicalLoader))
+ native2 = list(yaml.load(fp0, Loader=MyCanonicalLoader))
assert native1 == native2, (native1, native2)
finally:
if verbose:
diff --git a/_test/lib/test_tokens.py b/_test/lib/test_tokens.py
index cdb41ba..1483db8 100644
--- a/_test/lib/test_tokens.py
+++ b/_test/lib/test_tokens.py
@@ -1,9 +1,6 @@
-from __future__ import absolute_import
-from __future__ import print_function
import ruamel.yaml as yaml
import pprint
-from ruamel.yaml.compat import PY3
# Tokens mnemonic:
# directive: %
@@ -48,7 +45,7 @@ _replaces = {
def test_tokens(data_filename, tokens_filename, verbose=False):
tokens1 = []
- with open(tokens_filename, 'r' if PY3 else 'rb') as fp:
+ with open(tokens_filename, 'r') as fp:
tokens2 = fp.read().split()
try:
with open(data_filename, 'rb') as fp1:
diff --git a/_test/lib/test_yaml_ext.py b/_test/lib/test_yaml_ext.py
index e36ddd0..fd2d4ed 100644
--- a/_test/lib/test_yaml_ext.py
+++ b/_test/lib/test_yaml_ext.py
@@ -1,13 +1,9 @@
# coding: utf-8
-from __future__ import absolute_import
-from __future__ import print_function
-
import _ruamel_yaml
import ruamel.yaml
import types
import pprint
-from ruamel.yaml.compat import PY3
ruamel.yaml.PyBaseLoader = ruamel.yaml.BaseLoader
ruamel.yaml.PySafeLoader = ruamel.yaml.SafeLoader
@@ -311,9 +307,9 @@ def _compare_emitters(data, verbose):
c_value = getattr(c_event, attribute, None)
if (
attribute == 'tag'
- and value in [None, u'!']
- and py_value in [None, u'!']
- and c_value in [None, u'!']
+ and value in [None, '!']
+ and py_value in [None, '!']
+ and c_value in [None, '!']
):
continue
if attribute == 'explicit' and (py_value or c_value):
@@ -349,14 +345,7 @@ def wrap_ext_function(function):
finally:
_tear_down()
- if PY3:
- wrapper.__name__ = '%s_ext' % function.__name__
- else:
- try:
- wrapper.__name__ = '%s_ext' % function.__name__
- except TypeError:
- pass
- wrapper.unittest_name = '%s_ext' % function.__name__
+ wrapper.__name__ = '%s_ext' % function.__name__
wrapper.unittest = function.unittest
wrapper.skip = getattr(function, 'skip', []) + ['.skip-ext']
return wrapper
@@ -374,12 +363,8 @@ def wrap_ext(collections):
if isinstance(value, types.FunctionType) and hasattr(value, 'unittest'):
functions.append(wrap_ext_function(value))
for function in functions:
- if PY3:
- assert function.__name__ not in globals()
- globals()[function.__name__] = function
- else:
- assert function.unittest_name not in globals()
- globals()[function.unittest_name] = function
+ assert function.__name__ not in globals()
+ globals()[function.__name__] = function
import test_tokens # NOQA