summaryrefslogtreecommitdiff
path: root/_test
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2021-03-09 09:02:50 +0100
committerAnthon van der Neut <anthon@mnt.org>2021-03-09 09:02:50 +0100
commite73562c6f14d1d71a9fea174d58465e1b13f68af (patch)
tree309851cca7d411b31c27753555871d493282c7f0 /_test
parent96839d9f64f4698bdc519cbfbd48d51178460714 (diff)
downloadruamel.yaml-e73562c6f14d1d71a9fea174d58465e1b13f68af.tar.gz
remove python 2 specific code
add future deprecation warning to old style functions
Diffstat (limited to '_test')
-rw-r--r--_test/data/construct-python-str-utf8-py2.code2
-rw-r--r--_test/data/construct-python-unicode-utf8-py2.code2
-rw-r--r--_test/data/construct-str-utf8-py2.code2
-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
-rw-r--r--_test/roundtrip.py93
-rw-r--r--_test/test_a_dedent.py1
-rw-r--r--_test/test_add_xxx.py68
-rw-r--r--_test/test_anchor.py70
-rw-r--r--_test/test_api_change.py12
-rw-r--r--_test/test_class_register.py8
-rw-r--r--_test/test_collections.py3
-rw-r--r--_test/test_comment_manipulation.py2
-rw-r--r--_test/test_comments.py16
-rw-r--r--_test/test_contextmanager.py2
-rw-r--r--_test/test_cyaml.py29
-rw-r--r--_test/test_deprecation.py2
-rw-r--r--_test/test_documents.py8
-rw-r--r--_test/test_float.py2
-rw-r--r--_test/test_indentation.py23
-rw-r--r--_test/test_int.py2
-rw-r--r--_test/test_issues.py43
-rw-r--r--_test/test_json_numbers.py5
-rw-r--r--_test/test_literal.py6
-rw-r--r--_test/test_none.py32
-rw-r--r--_test/test_numpy.py2
-rw-r--r--_test/test_program_config.py1
-rw-r--r--_test/test_spec_examples.py1
-rw-r--r--_test/test_string.py2
-rw-r--r--_test/test_tag.py4
-rw-r--r--_test/test_version.py4
-rw-r--r--_test/test_yamlfile.py26
-rw-r--r--_test/test_yamlobject.py8
-rw-r--r--_test/test_z_data.py13
-rw-r--r--_test/test_z_olddata.py2
47 files changed, 471 insertions, 646 deletions
diff --git a/_test/data/construct-python-str-utf8-py2.code b/_test/data/construct-python-str-utf8-py2.code
index 47b28ab..6ca7d8f 100644
--- a/_test/data/construct-python-str-utf8-py2.code
+++ b/_test/data/construct-python-str-utf8-py2.code
@@ -1 +1 @@
-u'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430'.encode('utf-8')
+'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430'.encode('utf-8')
diff --git a/_test/data/construct-python-unicode-utf8-py2.code b/_test/data/construct-python-unicode-utf8-py2.code
index 2793ac7..9f66032 100644
--- a/_test/data/construct-python-unicode-utf8-py2.code
+++ b/_test/data/construct-python-unicode-utf8-py2.code
@@ -1 +1 @@
-u'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430'
+'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430'
diff --git a/_test/data/construct-str-utf8-py2.code b/_test/data/construct-str-utf8-py2.code
index 2793ac7..9f66032 100644
--- a/_test/data/construct-str-utf8-py2.code
+++ b/_test/data/construct-str-utf8-py2.code
@@ -1 +1 @@
-u'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430'
+'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430'
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
diff --git a/_test/roundtrip.py b/_test/roundtrip.py
index af8a555..f8f4b21 100644
--- a/_test/roundtrip.py
+++ b/_test/roundtrip.py
@@ -1,15 +1,14 @@
# coding: utf-8
-from __future__ import print_function
-
"""
helper routines for testing round trip of commented YAML data
"""
import sys
import textwrap
+import io
from ruamel.std.pathlib import Path
-enforce = object()
+unset = object()
def dedent(data):
@@ -29,50 +28,86 @@ def round_trip_load(inp, preserve_quotes=None, version=None):
import ruamel.yaml # NOQA
dinp = dedent(inp)
- return ruamel.yaml.load(
- dinp,
- Loader=ruamel.yaml.RoundTripLoader,
- preserve_quotes=preserve_quotes,
- version=version,
- )
+ yaml = ruamel.yaml.YAML()
+ yaml.preserve_quotes = preserve_quotes
+ yaml.version = version
+ return yaml.load(dinp)
def round_trip_load_all(inp, preserve_quotes=None, version=None):
import ruamel.yaml # NOQA
dinp = dedent(inp)
- return ruamel.yaml.load_all(
- dinp,
- Loader=ruamel.yaml.RoundTripLoader,
- preserve_quotes=preserve_quotes,
- version=version,
- )
+ yaml = ruamel.yaml.YAML()
+ yaml.preserve_quotes = preserve_quotes
+ yaml.version = version
+ return yaml.load_all(dinp)
def round_trip_dump(
data,
- stream=None,
+ stream=None, *,
indent=None,
block_seq_indent=None,
+ default_flow_style=unset,
top_level_colon_align=None,
prefix_colon=None,
explicit_start=None,
explicit_end=None,
version=None,
+ allow_unicode=True,
):
import ruamel.yaml # NOQA
- return ruamel.yaml.round_trip_dump(
- data,
- stream=stream,
- indent=indent,
- block_seq_indent=block_seq_indent,
- top_level_colon_align=top_level_colon_align,
- prefix_colon=prefix_colon,
- explicit_start=explicit_start,
- explicit_end=explicit_end,
- version=version,
- )
+ yaml = ruamel.yaml.YAML()
+ yaml.indent(mapping=indent, sequence=indent, offset=block_seq_indent)
+ if default_flow_style is not unset:
+ yaml.default_flow_style = default_flow_style
+ yaml.top_level_colon_align = top_level_colon_align
+ yaml.prefix_colon = prefix_colon
+ yaml.explicit_start = explicit_start
+ yaml.explicit_end = explicit_end
+ yaml.version = version
+ yaml.allow_unicode = allow_unicode
+ if stream is not None:
+ yaml.dump(data, stream=stream)
+ return
+ buf = io.StringIO()
+ yaml.dump(data, stream=buf)
+ return buf.getvalue()
+
+
+def round_trip_dump_all(
+ data,
+ stream=None, *,
+ indent=None,
+ block_seq_indent=None,
+ default_flow_style=unset,
+ top_level_colon_align=None,
+ prefix_colon=None,
+ explicit_start=None,
+ explicit_end=None,
+ version=None,
+ allow_unicode=None,
+):
+ import ruamel.yaml # NOQA
+
+ yaml = ruamel.yaml.YAML()
+ yaml.indent(mapping=indent, sequence=indent, offset=block_seq_indent)
+ if default_flow_style is not unset:
+ yaml.default_flow_style = default_flow_style
+ yaml.top_level_colon_align = top_level_colon_align
+ yaml.prefix_colon = prefix_colon
+ yaml.explicit_start = explicit_start
+ yaml.explicit_end = explicit_end
+ yaml.version = version
+ yaml.allow_unicode = allow_unicode
+ if stream is not None:
+ yaml.dump(data, stream=stream)
+ return
+ buf = io.StringIO()
+ yaml.dump_all(data, stream=buf)
+ return buf.getvalue()
def diff(inp, outp, file_name='stdin'):
@@ -242,7 +277,7 @@ def YAML(**kw):
def round_trip(self, stream, **kw):
from ruamel.yaml.compat import StringIO, BytesIO # NOQA
- assert isinstance(stream, (ruamel.yaml.compat.text_type, str))
+ assert isinstance(stream, str)
lkw = kw.copy()
if stream and stream[0] == '\n':
stream = stream[1:]
@@ -259,7 +294,7 @@ def YAML(**kw):
def round_trip_all(self, stream, **kw):
from ruamel.yaml.compat import StringIO, BytesIO # NOQA
- assert isinstance(stream, (ruamel.yaml.compat.text_type, str))
+ assert isinstance(stream, str)
lkw = kw.copy()
if stream and stream[0] == '\n':
stream = stream[1:]
diff --git a/_test/test_a_dedent.py b/_test/test_a_dedent.py
index fc6157a..447bdde 100644
--- a/_test/test_a_dedent.py
+++ b/_test/test_a_dedent.py
@@ -1,3 +1,4 @@
+# coding: utf-8
from roundtrip import dedent
diff --git a/_test/test_add_xxx.py b/_test/test_add_xxx.py
index 085e352..8beac65 100644
--- a/_test/test_add_xxx.py
+++ b/_test/test_add_xxx.py
@@ -3,7 +3,7 @@
import re
import pytest # NOQA
-from roundtrip import dedent
+from roundtrip import dedent, round_trip_dump # NOQA
# from PyYAML docs
@@ -22,48 +22,50 @@ def dice_constructor(loader, node):
def dice_representer(dumper, data):
- return dumper.represent_scalar(u'!dice', u'{}d{}'.format(*data))
+ return dumper.represent_scalar('!dice', '{}d{}'.format(*data))
def test_dice_constructor():
import ruamel.yaml # NOQA
- ruamel.yaml.add_constructor(u'!dice', dice_constructor)
- data = ruamel.yaml.load('initial hit points: !dice 8d4', Loader=ruamel.yaml.Loader)
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
+ ruamel.yaml.add_constructor('!dice', dice_constructor)
+ data = yaml.load('initial hit points: !dice 8d4')
assert str(data) == "{'initial hit points': Dice(8,4)}"
def test_dice_constructor_with_loader():
import ruamel.yaml # NOQA
- ruamel.yaml.add_constructor(u'!dice', dice_constructor, Loader=ruamel.yaml.Loader)
- data = ruamel.yaml.load('initial hit points: !dice 8d4', Loader=ruamel.yaml.Loader)
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
+ ruamel.yaml.add_constructor('!dice', dice_constructor, Loader=ruamel.yaml.Loader)
+ data = yaml.load('initial hit points: !dice 8d4')
assert str(data) == "{'initial hit points': Dice(8,4)}"
def test_dice_representer():
import ruamel.yaml # NOQA
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
+ yaml.default_flow_style = False
ruamel.yaml.add_representer(Dice, dice_representer)
# ruamel.yaml 0.15.8+ no longer forces quotes tagged scalars
- assert (
- ruamel.yaml.dump(dict(gold=Dice(10, 6)), default_flow_style=False)
- == 'gold: !dice 10d6\n'
- )
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(dict(gold=Dice(10, 6)), buf)
+ assert buf.getvalue() == 'gold: !dice 10d6\n'
def test_dice_implicit_resolver():
import ruamel.yaml # NOQA
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
+ yaml.default_flow_style = False
pattern = re.compile(r'^\d+d\d+$')
- ruamel.yaml.add_implicit_resolver(u'!dice', pattern)
- assert (
- ruamel.yaml.dump(dict(treasure=Dice(10, 20)), default_flow_style=False)
- == 'treasure: 10d20\n'
- )
- assert ruamel.yaml.load('damage: 5d10', Loader=ruamel.yaml.Loader) == dict(
- damage=Dice(5, 10)
- )
+ ruamel.yaml.add_implicit_resolver('!dice', pattern)
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(dict(treasure=Dice(10, 20)), buf)
+ assert buf.getvalue() == 'treasure: 10d20\n'
+ assert yaml.load('damage: 5d10') == dict(damage=Dice(5, 10))
class Obj1(dict):
@@ -82,7 +84,7 @@ class Obj1(dict):
class YAMLObj1(object):
- yaml_tag = u'!obj:'
+ yaml_tag = '!obj:'
@classmethod
def from_yaml(cls, loader, suffix, node):
@@ -103,24 +105,30 @@ class YAMLObj1(object):
def test_yaml_obj():
import ruamel.yaml # NOQA
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
ruamel.yaml.add_representer(Obj1, YAMLObj1.to_yaml)
ruamel.yaml.add_multi_constructor(YAMLObj1.yaml_tag, YAMLObj1.from_yaml)
- x = ruamel.yaml.load('!obj:x.2\na: 1', Loader=ruamel.yaml.Loader)
+ x = yaml.load('!obj:x.2\na: 1')
print(x)
- assert ruamel.yaml.dump(x) == """!obj:x.2 "{'a': 1}"\n"""
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(x, buf)
+ assert buf.getvalue() == """!obj:x.2 "{'a': 1}"\n"""
def test_yaml_obj_with_loader_and_dumper():
import ruamel.yaml # NOQA
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
ruamel.yaml.add_representer(Obj1, YAMLObj1.to_yaml, Dumper=ruamel.yaml.Dumper)
ruamel.yaml.add_multi_constructor(
YAMLObj1.yaml_tag, YAMLObj1.from_yaml, Loader=ruamel.yaml.Loader
)
- x = ruamel.yaml.load('!obj:x.2\na: 1', Loader=ruamel.yaml.Loader)
+ x = yaml.load('!obj:x.2\na: 1')
# x = ruamel.yaml.load('!obj:x.2\na: 1')
print(x)
- assert ruamel.yaml.dump(x) == """!obj:x.2 "{'a': 1}"\n"""
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(x, buf)
+ assert buf.getvalue() == """!obj:x.2 "{'a': 1}"\n"""
# ToDo use nullege to search add_multi_representer and add_path_resolver
@@ -135,7 +143,7 @@ def test_issue_127():
class Ref(ruamel.yaml.YAMLObject):
yaml_constructor = ruamel.yaml.RoundTripConstructor
yaml_representer = ruamel.yaml.RoundTripRepresenter
- yaml_tag = u'!Ref'
+ yaml_tag = '!Ref'
def __init__(self, logical_id):
self.logical_id = logical_id
@@ -163,7 +171,11 @@ def test_issue_127():
- Five Six
- 'Seven Eight'
""")
- data = ruamel.yaml.round_trip_load(document, preserve_quotes=True)
- assert ruamel.yaml.round_trip_dump(data, indent=4, block_seq_indent=2) == document.replace(
- '\n Two and', ' Two and'
- )
+ yaml = ruamel.yaml.YAML()
+ yaml.preserve_quotes = True
+ yaml.default_flow_style = None
+ yaml.indent(sequence=4, offset=2)
+ data = yaml.load(document)
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(data, buf)
+ assert buf.getvalue() == document.replace('\n Two and', ' Two and')
diff --git a/_test/test_anchor.py b/_test/test_anchor.py
index dec1261..3c83886 100644
--- a/_test/test_anchor.py
+++ b/_test/test_anchor.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function
-
"""
testing of anchors and the aliases referring to them
"""
@@ -46,20 +44,20 @@ class TestAnchorsAliases:
"""test if id matches the anchor template"""
from ruamel.yaml.serializer import templated_id
- assert templated_id(u'id001')
- assert templated_id(u'id999')
- assert templated_id(u'id1000')
- assert templated_id(u'id0001')
- assert templated_id(u'id0000')
- assert not templated_id(u'id02')
- assert not templated_id(u'id000')
- assert not templated_id(u'x000')
+ assert templated_id('id001')
+ assert templated_id('id999')
+ assert templated_id('id1000')
+ assert templated_id('id0001')
+ assert templated_id('id0000')
+ assert not templated_id('id02')
+ assert not templated_id('id000')
+ assert not templated_id('x000')
# def test_re_matcher(self):
# import re
- # assert re.compile(u'id(?!000)\\d{3,}').match('id001')
- # assert not re.compile(u'id(?!000\\d*)\\d{3,}').match('id000')
- # assert re.compile(u'id(?!000$)\\d{3,}').match('id0001')
+ # assert re.compile('id(?!000)\\d{3,}').match('id001')
+ # assert not re.compile('id(?!000\\d*)\\d{3,}').match('id000')
+ # assert re.compile('id(?!000$)\\d{3,}').match('id0001')
def test_anchor_assigned(self):
from ruamel.yaml.comments import CommentedMap
@@ -283,7 +281,8 @@ class TestAnchorsAliases:
components:
server: {<<: *server_service}
""")
- data = ruamel.yaml.safe_load(ys)
+ yaml = ruamel.yaml.YAML(typ='safe', pure=True)
+ data = yaml.load(ys)
assert data['services']['shell']['components']['server']['port'] == 8000
def test_issue_130a(self):
@@ -308,7 +307,8 @@ class TestAnchorsAliases:
components:
server: {<<: *server_service}
""")
- data = ruamel.yaml.safe_load(ys)
+ yaml = ruamel.yaml.YAML(typ='safe', pure=True)
+ data = yaml.load(ys)
assert data['services']['shell']['components']['server']['port'] == 4000
@@ -332,9 +332,9 @@ class TestMergeKeysValues:
# in the following d always has "expanded" the merges
def test_merge_for(self):
- from ruamel.yaml import safe_load
+ from ruamel.yaml import YAML
- d = safe_load(self.yaml_str)
+ d = YAML(typ='safe', pure=True).load(self.yaml_str)
data = round_trip_load(self.yaml_str)
count = 0
for x in data[2]:
@@ -343,9 +343,9 @@ class TestMergeKeysValues:
assert count == len(d[2])
def test_merge_keys(self):
- from ruamel.yaml import safe_load
+ from ruamel.yaml import YAML
- d = safe_load(self.yaml_str)
+ d = YAML(typ='safe', pure=True).load(self.yaml_str)
data = round_trip_load(self.yaml_str)
count = 0
for x in data[2].keys():
@@ -354,9 +354,9 @@ class TestMergeKeysValues:
assert count == len(d[2])
def test_merge_values(self):
- from ruamel.yaml import safe_load
+ from ruamel.yaml import YAML
- d = safe_load(self.yaml_str)
+ d = YAML(typ='safe', pure=True).load(self.yaml_str)
data = round_trip_load(self.yaml_str)
count = 0
for x in data[2].values():
@@ -365,9 +365,9 @@ class TestMergeKeysValues:
assert count == len(d[2])
def test_merge_items(self):
- from ruamel.yaml import safe_load
+ from ruamel.yaml import YAML
- d = safe_load(self.yaml_str)
+ d = YAML(typ='safe', pure=True).load(self.yaml_str)
data = round_trip_load(self.yaml_str)
count = 0
for x in data[2].items():
@@ -376,10 +376,9 @@ class TestMergeKeysValues:
assert count == len(d[2])
def test_len_items_delete(self):
- from ruamel.yaml import safe_load
- from ruamel.yaml.compat import PY3
+ from ruamel.yaml import YAML
- d = safe_load(self.yaml_str)
+ d = YAML(typ='safe', pure=True).load(self.yaml_str)
data = round_trip_load(self.yaml_str)
x = data[2].items()
print('d2 items', d[2].items(), len(d[2].items()), x, len(x))
@@ -387,16 +386,13 @@ class TestMergeKeysValues:
print('ref', ref)
assert len(x) == ref
del data[2]['m']
- if PY3:
- ref -= 1
+ ref -= 1
assert len(x) == ref
del data[2]['d']
- if PY3:
- ref -= 1
+ ref -= 1
assert len(x) == ref
del data[2]['a']
- if PY3:
- ref -= 1
+ ref -= 1
assert len(x) == ref
def test_issue_196_cast_of_dict(self, capsys):
@@ -467,7 +463,7 @@ class TestMergeKeysValues:
class TestDuplicateKeyThroughAnchor:
def test_duplicate_key_00(self):
from ruamel.yaml import version_info
- from ruamel.yaml import safe_load, round_trip_load
+ from ruamel.yaml import YAML
from ruamel.yaml.constructor import DuplicateKeyFutureWarning, DuplicateKeyError
s = dedent("""\
@@ -481,14 +477,14 @@ class TestDuplicateKeyThroughAnchor:
pass
elif version_info < (0, 16, 0):
with pytest.warns(DuplicateKeyFutureWarning):
- safe_load(s)
+ YAML(typ='safe', pure=True).load(s)
with pytest.warns(DuplicateKeyFutureWarning):
- round_trip_load(s)
+ YAML(typ='rt').load(s)
else:
with pytest.raises(DuplicateKeyError):
- safe_load(s)
+ YAML(typ='safe', pure=True).load(s)
with pytest.raises(DuplicateKeyError):
- round_trip_load(s)
+ YAML(typ='rt').load(s)
def test_duplicate_key_01(self):
# so issue https://stackoverflow.com/a/52852106/1307905
diff --git a/_test/test_api_change.py b/_test/test_api_change.py
index b47a551..4ce61b6 100644
--- a/_test/test_api_change.py
+++ b/_test/test_api_change.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function
-
"""
testing of anchors and the aliases referring to them
"""
@@ -158,7 +156,7 @@ class TestLoadAll:
fn = Path(str(tmpdir)) / 'test.yaml'
fn.write_text(
- textwrap.dedent(u"""\
+ textwrap.dedent("""\
---
- a
---
@@ -196,10 +194,10 @@ class TestDumpLoadUnicode:
from ruamel.yaml import YAML
yaml = YAML()
- text_dict = {'text': u'HELLO_WORLD©'}
+ text_dict = {'text': 'HELLO_WORLD©'}
file_name = str(tmpdir) + '/tstFile.yaml'
yaml.dump(text_dict, open(file_name, 'w'))
- assert open(file_name, 'rb').read().decode('utf-8') == u'text: HELLO_WORLD©\n'
+ assert open(file_name, 'rb').read().decode('utf-8') == 'text: HELLO_WORLD©\n'
def test_read_unicode(self, tmpdir):
from ruamel.yaml import YAML
@@ -207,9 +205,9 @@ class TestDumpLoadUnicode:
yaml = YAML()
file_name = str(tmpdir) + '/tstFile.yaml'
with open(file_name, 'wb') as fp:
- fp.write(u'text: HELLO_WORLD©\n'.encode('utf-8'))
+ fp.write('text: HELLO_WORLD©\n'.encode('utf-8'))
text_dict = yaml.load(open(file_name, 'r'))
- assert text_dict['text'] == u'HELLO_WORLD©'
+ assert text_dict['text'] == 'HELLO_WORLD©'
class TestFlowStyle:
diff --git a/_test/test_class_register.py b/_test/test_class_register.py
index 126d93f..d996269 100644
--- a/_test/test_class_register.py
+++ b/_test/test_class_register.py
@@ -14,7 +14,7 @@ class User0(object):
class User1(object):
- yaml_tag = u'!user'
+ yaml_tag = '!user'
def __init__(self, name, age):
self.name = name
@@ -22,7 +22,7 @@ class User1(object):
@classmethod
def to_yaml(cls, representer, node):
- return representer.represent_scalar(cls.yaml_tag, u'{.name}-{.age}'.format(node, node))
+ return representer.represent_scalar(cls.yaml_tag, '{.name}-{.age}'.format(node, node))
@classmethod
def from_yaml(cls, constructor, node):
@@ -116,7 +116,7 @@ class TestDecorator(object):
@yaml_object(yml)
class User3(object):
- yaml_tag = u'!USER'
+ yaml_tag = '!USER'
def __init__(self, name, age):
self.name = name
@@ -125,7 +125,7 @@ class TestDecorator(object):
@classmethod
def to_yaml(cls, representer, node):
return representer.represent_scalar(
- cls.yaml_tag, u'{.name}-{.age}'.format(node, node)
+ cls.yaml_tag, '{.name}-{.age}'.format(node, node)
)
@classmethod
diff --git a/_test/test_collections.py b/_test/test_collections.py
index e6033bb..40af9db 100644
--- a/_test/test_collections.py
+++ b/_test/test_collections.py
@@ -16,6 +16,5 @@ from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NO
class TestOrderedDict:
def test_ordereddict(self):
from collections import OrderedDict
- import ruamel.yaml # NOQA
- assert ruamel.yaml.dump(OrderedDict()) == '!!omap []\n'
+ assert round_trip_dump(OrderedDict()) == '!!omap []\n'
diff --git a/_test/test_comment_manipulation.py b/_test/test_comment_manipulation.py
index 7f09a38..6d706f2 100644
--- a/_test/test_comment_manipulation.py
+++ b/_test/test_comment_manipulation.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function
-
import pytest # NOQA
from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA
diff --git a/_test/test_comments.py b/_test/test_comments.py
index 79acfc8..3173995 100644
--- a/_test/test_comments.py
+++ b/_test/test_comments.py
@@ -272,26 +272,24 @@ class TestComments:
- y # more comment
""")
data = round_trip_load(x)
- dumper = ruamel.yaml.RoundTripDumper
for utf in [True, False]:
- y = ruamel.yaml.dump(
- data, default_flow_style=False, Dumper=dumper, allow_unicode=utf
+ y = round_trip_dump(
+ data, default_flow_style=False, allow_unicode=utf
)
assert y == x
def test_dump_unicode_utf8(self):
import ruamel.yaml # NOQA
- x = dedent(u"""\
+ x = dedent("""\
ab:
- x # comment
- y # more comment
""")
data = round_trip_load(x)
- dumper = ruamel.yaml.RoundTripDumper
for utf in [True, False]:
- y = ruamel.yaml.dump(
- data, default_flow_style=False, Dumper=dumper, allow_unicode=utf
+ y = round_trip_dump(
+ data, default_flow_style=False, allow_unicode=utf
)
assert y == x
@@ -843,6 +841,6 @@ class TestBlockScalarWithComments:
]:
commented_line = test_block_scalar_commented_line_template.format(x)
- data = ruamel.yaml.round_trip_load(commented_line)
+ data = round_trip_load(commented_line)
- assert ruamel.yaml.round_trip_dump(data) == commented_line
+ assert round_trip_dump(data) == commented_line
diff --git a/_test/test_contextmanager.py b/_test/test_contextmanager.py
index b3bb3be..bdc8b78 100644
--- a/_test/test_contextmanager.py
+++ b/_test/test_contextmanager.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function
-
"""
testing of anchors and the aliases referring to them
"""
diff --git a/_test/test_cyaml.py b/_test/test_cyaml.py
index 6b618f3..593d171 100644
--- a/_test/test_cyaml.py
+++ b/_test/test_cyaml.py
@@ -5,6 +5,8 @@ import platform
import pytest
from textwrap import dedent
+NO_CLIB_VER = (3, 10)
+
@pytest.mark.skipif(
platform.python_implementation() in ['Jython', 'PyPy'],
@@ -14,30 +16,29 @@ def test_load_cyaml():
print("???????????????????????", platform.python_implementation())
import ruamel.yaml
- if sys.version_info >= (3, 8):
+ if sys.version_info >= NO_CLIB_VER:
return
+ yaml = ruamel.yaml.YAML(typ='safe', pure=False)
assert ruamel.yaml.__with_libyaml__
- from ruamel.yaml.cyaml import CLoader
- ruamel.yaml.load('abc: 1', Loader=CLoader)
+ yaml.load('abc: 1')
-@pytest.mark.skipif(sys.version_info >= (3, 8)
+@pytest.mark.skipif(sys.version_info >= NO_CLIB_VER
or platform.python_implementation() in ['Jython', 'PyPy'],
reason='no _PyGC_FINALIZED')
def test_dump_cyaml():
import ruamel.yaml
- if sys.version_info >= (3, 8):
+ if sys.version_info >= NO_CLIB_VER:
return
data = {'a': 1, 'b': 2}
- res = ruamel.yaml.dump(
- data,
- Dumper=ruamel.yaml.cyaml.CSafeDumper,
- default_flow_style=False,
- allow_unicode=True,
- )
- assert res == 'a: 1\nb: 2\n'
+ yaml = ruamel.yaml.YAML(typ='safe', pure=False)
+ yaml.default_flow_style = False
+ yaml.allow_unicode = True
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump(data, buf)
+ assert buf.getvalue() == 'a: 1\nb: 2\n'
@pytest.mark.skipif(
@@ -47,7 +48,7 @@ def test_load_cyaml_1_2():
# issue 155
import ruamel.yaml
- if sys.version_info >= (3, 8):
+ if sys.version_info >= NO_CLIB_VER:
return
assert ruamel.yaml.__with_libyaml__
inp = dedent("""\
@@ -67,7 +68,7 @@ def test_dump_cyaml_1_2():
import ruamel.yaml
from ruamel.yaml.compat import StringIO
- if sys.version_info >= (3, 8):
+ if sys.version_info >= NO_CLIB_VER:
return
assert ruamel.yaml.__with_libyaml__
yaml = ruamel.yaml.YAML(typ='safe')
diff --git a/_test/test_deprecation.py b/_test/test_deprecation.py
index 14acd71..b267e66 100644
--- a/_test/test_deprecation.py
+++ b/_test/test_deprecation.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function
-
import sys
import pytest # NOQA
diff --git a/_test/test_documents.py b/_test/test_documents.py
index 05ba7dd..5119f06 100644
--- a/_test/test_documents.py
+++ b/_test/test_documents.py
@@ -2,7 +2,7 @@
import pytest # NOQA
-from roundtrip import round_trip, round_trip_load_all
+from roundtrip import round_trip, round_trip_load_all, round_trip_dump_all
class TestDocument:
@@ -16,8 +16,6 @@ class TestDocument:
round_trip(inp, explicit_start=True, explicit_end=True)
def test_multi_doc_begin_end(self):
- from ruamel import yaml
-
inp = """\
---
- a
@@ -28,9 +26,7 @@ class TestDocument:
"""
docs = list(round_trip_load_all(inp))
assert docs == [['a'], ['b']]
- out = yaml.dump_all(
- docs, Dumper=yaml.RoundTripDumper, explicit_start=True, explicit_end=True
- )
+ out = round_trip_dump_all(docs, explicit_start=True, explicit_end=True)
assert out == '---\n- a\n...\n---\n- b\n...\n'
def test_multi_doc_no_start(self):
diff --git a/_test/test_float.py b/_test/test_float.py
index c996efd..8257208 100644
--- a/_test/test_float.py
+++ b/_test/test_float.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function, absolute_import, division, unicode_literals
-
import pytest # NOQA
from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA
diff --git a/_test/test_indentation.py b/_test/test_indentation.py
index 6321409..6e0fce2 100644
--- a/_test/test_indentation.py
+++ b/_test/test_indentation.py
@@ -1,22 +1,13 @@
# coding: utf-8
-from __future__ import absolute_import
-from __future__ import print_function
-from __future__ import unicode_literals
-
-
import pytest # NOQA
from roundtrip import round_trip, round_trip_load, round_trip_dump, dedent, YAML
def rt(s):
- import ruamel.yaml
- res = ruamel.yaml.dump(
- ruamel.yaml.load(s, Loader=ruamel.yaml.RoundTripLoader),
- Dumper=ruamel.yaml.RoundTripDumper,
- )
+ res = round_trip_dump(round_trip_load(s))
return res.strip() + '\n'
@@ -55,8 +46,6 @@ class TestIndent:
# first test by explicitly setting flow style
def test_added_inline_list(self):
- import ruamel.yaml
-
s1 = dedent("""
a:
- b
@@ -64,24 +53,22 @@ class TestIndent:
- d
""")
s = 'a: [b, c, d]\n'
- data = ruamel.yaml.load(s1, Loader=ruamel.yaml.RoundTripLoader)
+ data = round_trip_load(s1)
val = data['a']
val.fa.set_flow_style()
# print(type(val), '_yaml_format' in dir(val))
- output = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper)
+ output = round_trip_dump(data)
assert s == output
# ############ flow mappings
def test_roundtrip_flow_mapping(self):
- import ruamel.yaml
-
s = dedent("""\
- {a: 1, b: hallo}
- {j: fka, k: 42}
""")
- data = ruamel.yaml.load(s, Loader=ruamel.yaml.RoundTripLoader)
- output = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper)
+ data = round_trip_load(s)
+ output = round_trip_dump(data)
assert s == output
def test_roundtrip_sequence_of_inline_mappings_eol_comments(self):
diff --git a/_test/test_int.py b/_test/test_int.py
index 4e7de6b..aa300df 100644
--- a/_test/test_int.py
+++ b/_test/test_int.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function, absolute_import, division, unicode_literals
-
import pytest # NOQA
from roundtrip import dedent, round_trip_load, round_trip_dump
diff --git a/_test/test_issues.py b/_test/test_issues.py
index 673ba45..736dccb 100644
--- a/_test/test_issues.py
+++ b/_test/test_issues.py
@@ -1,8 +1,5 @@
# coding: utf-8
-from __future__ import absolute_import, print_function, unicode_literals
-
-
import pytest # NOQA
@@ -19,8 +16,6 @@ from roundtrip import (
class TestIssues:
def test_issue_61(self):
- import ruamel.yaml
-
s = dedent("""
def1: &ANCHOR1
key1: value1
@@ -30,24 +25,20 @@ class TestIssues:
comb:
<<: *ANCHOR
""")
- data = ruamel.yaml.round_trip_load(s)
+ data = round_trip_load(s)
assert str(data['comb']) == str(data['def'])
assert str(data['comb']) == "ordereddict([('key', 'value'), ('key1', 'value1')])"
def test_issue_82(self, tmpdir):
program_src = r'''
- from __future__ import print_function
-
from ruamel import yaml
-
import re
-
class SINumber(yaml.YAMLObject):
PREFIXES = {'k': 1e3, 'M': 1e6, 'G': 1e9}
yaml_loader = yaml.Loader
yaml_dumper = yaml.Dumper
- yaml_tag = u'!si'
+ yaml_tag = '!si'
yaml_implicit_pattern = re.compile(
r'^(?P<value>[0-9]+(?:\.[0-9]+)?)(?P<prefix>[kMG])$')
@@ -200,8 +191,6 @@ class TestIssues:
assert res == yaml_str.replace(' b ', ' B ').replace(' d\n', ' D\n')
def test_issue_176_test_slicing(self):
- from ruamel.yaml.compat import PY2
-
mss = round_trip_load('[0, 1, 2, 3, 4]')
assert len(mss) == 5
assert mss[2:2] == []
@@ -231,18 +220,10 @@ class TestIssues:
m[1::2] = [42, 43]
assert m == [0, 42, 2, 43, 4]
m = mss[:]
- if PY2:
- with pytest.raises(ValueError, match='attempt to assign'):
- m[1::2] = [42, 43, 44]
- else:
- with pytest.raises(TypeError, match='too many'):
- m[1::2] = [42, 43, 44]
- if PY2:
- with pytest.raises(ValueError, match='attempt to assign'):
- m[1::2] = [42]
- else:
- with pytest.raises(TypeError, match='not enough'):
- m[1::2] = [42]
+ with pytest.raises(TypeError, match='too many'):
+ m[1::2] = [42, 43, 44]
+ with pytest.raises(TypeError, match='not enough'):
+ m[1::2] = [42]
m = mss[:]
m += [5]
m[1::2] = [42, 43, 44]
@@ -288,7 +269,7 @@ class TestIssues:
program_src = r'''
from ruamel.yaml import YAML
- yaml_str = u"""\
+ yaml_str = """\
---
foo: ["bar"]
"""
@@ -409,8 +390,9 @@ class TestIssues:
import ruamel.yaml
from ruamel.yaml.compat import StringIO
+ yaml = ruamel.yaml.YAML(typ='safe')
buf = StringIO()
- ruamel.yaml.safe_dump(['012923'], buf)
+ yaml.dump(['012923'], buf)
assert buf.getvalue() == "['012923']\n"
def test_issue_223(self):
@@ -421,12 +403,13 @@ class TestIssues:
def test_issue_232(self):
import ruamel.yaml
- from ruamel import yaml
+
+ yaml = YAML(typ='safe', pure=True)
with pytest.raises(ruamel.yaml.parser.ParserError):
- yaml.safe_load(']')
+ yaml.load(']')
with pytest.raises(ruamel.yaml.parser.ParserError):
- yaml.safe_load('{]')
+ yaml.load('{]')
def test_issue_233(self):
from ruamel.yaml import YAML
diff --git a/_test/test_json_numbers.py b/_test/test_json_numbers.py
index 56b7b6f..d89453c 100644
--- a/_test/test_json_numbers.py
+++ b/_test/test_json_numbers.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function
-
import pytest # NOQA
import json
@@ -10,12 +8,13 @@ import json
def load(s, typ=float):
import ruamel.yaml
+ yaml = ruamel.yaml.YAML()
x = '{"low": %s }' % (s)
print('input: [%s]' % (s), repr(x))
# just to check it is loadable json
res = json.loads(x)
assert isinstance(res['low'], typ)
- ret_val = ruamel.yaml.load(x, ruamel.yaml.RoundTripLoader)
+ ret_val = yaml.load(x)
print(ret_val)
return ret_val['low']
diff --git a/_test/test_literal.py b/_test/test_literal.py
index 20eb8e9..7192207 100644
--- a/_test/test_literal.py
+++ b/_test/test_literal.py
@@ -1,5 +1,4 @@
# coding: utf-8
-from __future__ import print_function
import pytest # NOQA
@@ -64,15 +63,16 @@ class TestNoIndent:
def test_root_literal_scalar_no_indent_1_1_old_style(self):
from textwrap import dedent
- from ruamel.yaml import safe_load
+ from ruamel.yaml import YAML
+ yaml = YAML(typ='safe', pure=True)
s = 'testing123'
inp = """
%YAML 1.1
--- |
{}
"""
- d = safe_load(dedent(inp.format(s)))
+ d = yaml.load(dedent(inp.format(s)))
print(d)
assert d == s + '\n'
diff --git a/_test/test_none.py b/_test/test_none.py
index e313edc..42aef4c 100644
--- a/_test/test_none.py
+++ b/_test/test_none.py
@@ -1,51 +1,41 @@
# coding: utf-8
-
import pytest # NOQA
+from roundtrip import round_trip_load, round_trip_dump
class TestNone:
def test_dump00(self):
- import ruamel.yaml # NOQA
-
data = None
- s = ruamel.yaml.round_trip_dump(data)
+ s = round_trip_dump(data)
assert s == 'null\n...\n'
- d = ruamel.yaml.round_trip_load(s)
+ d = round_trip_load(s)
assert d == data
def test_dump01(self):
- import ruamel.yaml # NOQA
-
data = None
- s = ruamel.yaml.round_trip_dump(data, explicit_end=True)
+ s = round_trip_dump(data, explicit_end=True)
assert s == 'null\n...\n'
- d = ruamel.yaml.round_trip_load(s)
+ d = round_trip_load(s)
assert d == data
def test_dump02(self):
- import ruamel.yaml # NOQA
-
data = None
- s = ruamel.yaml.round_trip_dump(data, explicit_end=False)
+ s = round_trip_dump(data, explicit_end=False)
assert s == 'null\n...\n'
- d = ruamel.yaml.round_trip_load(s)
+ d = round_trip_load(s)
assert d == data
def test_dump03(self):
- import ruamel.yaml # NOQA
-
data = None
- s = ruamel.yaml.round_trip_dump(data, explicit_start=True)
+ s = round_trip_dump(data, explicit_start=True)
assert s == '---\n...\n'
- d = ruamel.yaml.round_trip_load(s)
+ d = round_trip_load(s)
assert d == data
def test_dump04(self):
- import ruamel.yaml # NOQA
-
data = None
- s = ruamel.yaml.round_trip_dump(data, explicit_start=True, explicit_end=False)
+ s = round_trip_dump(data, explicit_start=True, explicit_end=False)
assert s == '---\n...\n'
- d = ruamel.yaml.round_trip_load(s)
+ d = round_trip_load(s)
assert d == data
diff --git a/_test/test_numpy.py b/_test/test_numpy.py
index 2747fc4..573f0bd 100644
--- a/_test/test_numpy.py
+++ b/_test/test_numpy.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function, absolute_import, division, unicode_literals
-
try:
import numpy
except: # NOQA
diff --git a/_test/test_program_config.py b/_test/test_program_config.py
index dcd8351..821ca15 100644
--- a/_test/test_program_config.py
+++ b/_test/test_program_config.py
@@ -1,3 +1,4 @@
+# coding: utf-8
import pytest # NOQA
diff --git a/_test/test_spec_examples.py b/_test/test_spec_examples.py
index bae505d..cead787 100644
--- a/_test/test_spec_examples.py
+++ b/_test/test_spec_examples.py
@@ -1,3 +1,4 @@
+# coding: utf-8
from roundtrip import YAML
import pytest # NOQA
diff --git a/_test/test_string.py b/_test/test_string.py
index d1f7982..7c10fd4 100644
--- a/_test/test_string.py
+++ b/_test/test_string.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function
-
"""
various test cases for string scalars in YAML files
'|' for preserved newlines
diff --git a/_test/test_tag.py b/_test/test_tag.py
index 31a192e..3fd1e05 100644
--- a/_test/test_tag.py
+++ b/_test/test_tag.py
@@ -11,7 +11,7 @@ def register_xxx(**kw):
class XXX(yaml.comments.CommentedMap):
@staticmethod
def yaml_dump(dumper, data):
- return dumper.represent_mapping(u'!xxx', data)
+ return dumper.represent_mapping('!xxx', data)
@classmethod
def yaml_load(cls, constructor, node):
@@ -19,7 +19,7 @@ def register_xxx(**kw):
yield data
constructor.construct_mapping(node, data)
- yaml.add_constructor(u'!xxx', XXX.yaml_load, constructor=yaml.RoundTripConstructor)
+ yaml.add_constructor('!xxx', XXX.yaml_load, constructor=yaml.RoundTripConstructor)
yaml.add_representer(XXX, XXX.yaml_dump, representer=yaml.RoundTripRepresenter)
diff --git a/_test/test_version.py b/_test/test_version.py
index 742ec36..e110eed 100644
--- a/_test/test_version.py
+++ b/_test/test_version.py
@@ -8,7 +8,9 @@ from roundtrip import dedent, round_trip, round_trip_load
def load(s, version=None):
import ruamel.yaml # NOQA
- return ruamel.yaml.round_trip_load(dedent(s), version)
+ yaml = ruamel.yaml.YAML()
+ yaml.version = version
+ return yaml.load(dedent(s))
class TestVersions:
diff --git a/_test/test_yamlfile.py b/_test/test_yamlfile.py
index a02fcab..f1de872 100644
--- a/_test/test_yamlfile.py
+++ b/_test/test_yamlfile.py
@@ -1,11 +1,11 @@
-
-from __future__ import print_function
+# coding: utf-8
"""
various test cases for YAML files
"""
import sys
+import io
import pytest # NOQA
import platform
@@ -25,7 +25,7 @@ class TestYAML:
import ruamel.yaml # NOQA
x = ordereddict([('a', 1), ('b', 2)])
- res = ruamel.yaml.dump(x, default_flow_style=False)
+ res = round_trip_dump(x, default_flow_style=False)
assert res == dedent("""
!!omap
- a: 1
@@ -48,7 +48,7 @@ class TestYAML:
# OrderedDict mapped to !!omap
x = OrderedDict([('a', 1), ('b', 2)])
- res = ruamel.yaml.dump(x, Dumper=ruamel.yaml.RoundTripDumper, default_flow_style=False)
+ res = round_trip_dump(x, default_flow_style=False)
assert res == dedent("""
!!omap
- a: 1
@@ -65,7 +65,7 @@ class TestYAML:
# OrderedDict mapped to !!omap
x = ordereddict([('a', 1), ('b', 2)])
- res = ruamel.yaml.dump(x, Dumper=ruamel.yaml.RoundTripDumper, default_flow_style=False)
+ res = round_trip_dump(x, default_flow_style=False)
assert res == dedent("""
!!omap
- a: 1
@@ -89,8 +89,12 @@ class TestYAML:
import ruamel.yaml # NOQA
x = set(['a', 'b', 'c'])
- res = ruamel.yaml.dump(x, default_flow_style=False)
- assert res == dedent("""
+ # cannot use round_trip_dump, it doesn't show null in block style
+ buf = io.StringIO()
+ yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
+ yaml.default_flow_style = False
+ yaml.dump(x, buf)
+ assert buf.getvalue() == dedent("""
!!set
a: null
b: null
@@ -202,15 +206,19 @@ class TestYAML:
def test_load_all_perserve_quotes(self):
import ruamel.yaml # NOQA
+ yaml = ruamel.yaml.YAML()
+ yaml.preserve_quotes = True
s = dedent("""\
a: 'hello'
---
b: "goodbye"
""")
data = []
- for x in ruamel.yaml.round_trip_load_all(s, preserve_quotes=True):
+ for x in yaml.load_all(s):
data.append(x)
- out = ruamel.yaml.dump_all(data, Dumper=ruamel.yaml.RoundTripDumper)
+ buf = ruamel.yaml.compat.StringIO()
+ yaml.dump_all(data, buf)
+ out = buf.getvalue()
print(type(data[0]['a']), data[0]['a'])
# out = ruamel.yaml.round_trip_dump_all(data)
print(out)
diff --git a/_test/test_yamlobject.py b/_test/test_yamlobject.py
index 7c4f7ec..80fb213 100644
--- a/_test/test_yamlobject.py
+++ b/_test/test_yamlobject.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function
-
import sys
import pytest # NOQA
@@ -9,12 +7,12 @@ from roundtrip import save_and_run # NOQA
def test_monster(tmpdir):
- program_src = u'''\
+ program_src = '''\
import ruamel.yaml
from textwrap import dedent
class Monster(ruamel.yaml.YAMLObject):
- yaml_tag = u'!Monster'
+ yaml_tag = '!Monster'
def __init__(self, name, hp, ac, attacks):
self.name = name
@@ -48,7 +46,7 @@ def test_monster(tmpdir):
@pytest.mark.skipif(sys.version_info < (3, 0), reason='no __qualname__')
def test_qualified_name00(tmpdir):
"""issue 214"""
- program_src = u"""\
+ program_src = """\
from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
diff --git a/_test/test_z_data.py b/_test/test_z_data.py
index a4eec0d..dcd6bd0 100644
--- a/_test/test_z_data.py
+++ b/_test/test_z_data.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function, unicode_literals
-
import sys
import pytest # NOQA
import warnings # NOQA
@@ -9,7 +7,6 @@ import warnings # NOQA
from ruamel.std.pathlib import Path
base_path = Path('data') # that is ruamel.yaml.data
-PY2 = sys.version_info[0] == 2
class YAMLData(object):
@@ -70,7 +67,7 @@ class Assert(YAMLData):
@property
def value(self):
- from ruamel.yaml.compat import Mapping
+ from collections.abc import Mapping
if hasattr(self, '_pa'):
return self._pa
@@ -126,14 +123,10 @@ class TestYAMLData(object):
yaml.dump(data, buf)
expected = input.value if output is None else output.value
value = buf.getvalue()
- if PY2:
- value = value.decode('utf-8')
- print('value', value)
- # print('expected', expected)
assert value == expected
def load_assert(self, input, confirm, yaml_version=None):
- from ruamel.yaml.compat import Mapping
+ from collections.abc import Mapping
d = self.yaml_load(input.value, yaml_version=yaml_version)[1] # NOQA
print('confirm.value', confirm.value, type(confirm.value))
@@ -159,7 +152,7 @@ class TestYAMLData(object):
# this is executed by pytest the methods with names not starting with test_
# are helpers
def test_yaml_data(self, yaml, tmpdir):
- from ruamel.yaml.compat import Mapping
+ from collections.abc import Mapping
idx = 0
typ = None
diff --git a/_test/test_z_olddata.py b/_test/test_z_olddata.py
index 91d89db..3857652 100644
--- a/_test/test_z_olddata.py
+++ b/_test/test_z_olddata.py
@@ -1,7 +1,5 @@
# coding: utf-8
-from __future__ import print_function
-
import sys
import os
import pytest # NOQA