1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Use io.StringIO and io.BytesIO instead of StringIO.StringIO for compat with py3

This commit is contained in:
Toshio Kuratomi 2016-02-26 16:42:18 -08:00
parent c29f51804b
commit b70bf3b056
8 changed files with 38 additions and 27 deletions

View file

@ -34,7 +34,12 @@ import datetime
import getpass
import pwd
import ConfigParser
import StringIO
# py2 vs py3; replace with six via ziploader
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from string import maketrans

View file

@ -19,7 +19,11 @@
import re
import socket
from StringIO import StringIO
# py2 vs py3; replace with six via ziploader
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
try:
import paramiko

View file

@ -17,7 +17,7 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import StringIO
from io import StringIO
import os
import ConfigParser
import re
@ -28,8 +28,8 @@ from ansible.plugins.lookup import LookupBase
class LookupModule(LookupBase):
def read_properties(self, filename, key, dflt, is_regexp):
config = StringIO.StringIO()
config.write('[java_properties]\n' + open(filename).read())
config = StringIO()
config.write(u'[java_properties]\n' + open(filename).read())
config.seek(0, os.SEEK_SET)
self.cp.readfp(config)
return self.get_value(key, 'java_properties', dflt, is_regexp)

View file

@ -23,8 +23,9 @@ import ast
import contextlib
import os
import re
from io import StringIO
from ansible.compat.six import string_types, text_type, binary_type, StringIO
from ansible.compat.six import string_types, text_type, binary_type
from jinja2 import Environment
from jinja2.loaders import FileSystemLoader
from jinja2.exceptions import TemplateSyntaxError, UndefinedError

View file

@ -23,9 +23,9 @@ __metaclass__ = type
import copy
import json
import sys
from io import BytesIO
from ansible.compat.tests import unittest
from ansible.compat.six import StringIO
from ansible.module_utils import basic
from ansible.module_utils.basic import heuristic_log_sanitize
@ -41,7 +41,7 @@ class TestAnsibleModuleExitJson(unittest.TestCase):
basic.MODULE_COMPLEX_ARGS = '{}'
self.old_stdout = sys.stdout
self.fake_stream = StringIO()
self.fake_stream = BytesIO()
sys.stdout = self.fake_stream
self.module = basic.AnsibleModule(argument_spec=dict())
@ -127,7 +127,7 @@ class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
def test_exit_json_removes_values(self):
self.maxDiff = None
for args, return_val, expected in self.dataset:
sys.stdout = StringIO()
sys.stdout = BytesIO()
basic.MODULE_COMPLEX_ARGS = json.dumps(args)
module = basic.AnsibleModule(
argument_spec = dict(
@ -146,7 +146,7 @@ class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
expected = copy.deepcopy(expected)
del expected['changed']
expected['failed'] = True
sys.stdout = StringIO()
sys.stdout = BytesIO()
basic.MODULE_COMPLEX_ARGS = json.dumps(args)
module = basic.AnsibleModule(
argument_spec = dict(

View file

@ -22,16 +22,16 @@ __metaclass__ = type
import errno
import sys
import time
from io import BytesIO
from ansible.compat.tests import unittest
from ansible.compat.six import StringIO, BytesIO
from ansible.compat.tests.mock import call, MagicMock, Mock, patch, sentinel
from ansible.module_utils import basic
from ansible.module_utils.basic import AnsibleModule
class OpenStringIO(StringIO):
"""StringIO with dummy close() method
class OpenBytesIO(BytesIO):
"""BytesIO with dummy close() method
So that you can inspect the content after close() was called.
"""
@ -77,7 +77,7 @@ class TestAnsibleModuleRunCommand(unittest.TestCase):
self.subprocess = patch('ansible.module_utils.basic.subprocess').start()
self.cmd = Mock()
self.cmd.returncode = 0
self.cmd.stdin = OpenStringIO()
self.cmd.stdin = OpenBytesIO()
self.cmd.stdout.fileno.return_value = sentinel.stdout
self.cmd.stderr.fileno.return_value = sentinel.stderr
self.subprocess.Popen.return_value = self.cmd

View file

@ -20,8 +20,9 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from io import StringIO
from six import text_type, binary_type
from six.moves import StringIO
from collections import Sequence, Set, Mapping
from ansible.compat.tests import unittest
@ -44,7 +45,7 @@ class TestAnsibleLoaderBasic(unittest.TestCase):
pass
def test_parse_number(self):
stream = StringIO("""
stream = StringIO(u"""
1
""")
loader = AnsibleLoader(stream, 'myfile.yml')
@ -53,7 +54,7 @@ class TestAnsibleLoaderBasic(unittest.TestCase):
# No line/column info saved yet
def test_parse_string(self):
stream = StringIO("""
stream = StringIO(u"""
Ansible
""")
loader = AnsibleLoader(stream, 'myfile.yml')
@ -64,7 +65,7 @@ class TestAnsibleLoaderBasic(unittest.TestCase):
self.assertEqual(data.ansible_pos, ('myfile.yml', 2, 17))
def test_parse_utf8_string(self):
stream = StringIO("""
stream = StringIO(u"""
Cafè Eñyei
""")
loader = AnsibleLoader(stream, 'myfile.yml')
@ -75,7 +76,7 @@ class TestAnsibleLoaderBasic(unittest.TestCase):
self.assertEqual(data.ansible_pos, ('myfile.yml', 2, 17))
def test_parse_dict(self):
stream = StringIO("""
stream = StringIO(u"""
webster: daniel
oed: oxford
""")
@ -93,7 +94,7 @@ class TestAnsibleLoaderBasic(unittest.TestCase):
self.assertEqual(data[u'oed'].ansible_pos, ('myfile.yml', 3, 22))
def test_parse_list(self):
stream = StringIO("""
stream = StringIO(u"""
- a
- b
""")
@ -109,7 +110,7 @@ class TestAnsibleLoaderBasic(unittest.TestCase):
self.assertEqual(data[1].ansible_pos, ('myfile.yml', 3, 19))
def test_parse_short_dict(self):
stream = StringIO("""{"foo": "bar"}""")
stream = StringIO(u"""{"foo": "bar"}""")
loader = AnsibleLoader(stream, 'myfile.yml')
data = loader.get_single_data()
self.assertEqual(data, dict(foo=u'bar'))
@ -117,7 +118,7 @@ class TestAnsibleLoaderBasic(unittest.TestCase):
self.assertEqual(data.ansible_pos, ('myfile.yml', 1, 1))
self.assertEqual(data[u'foo'].ansible_pos, ('myfile.yml', 1, 9))
stream = StringIO("""foo: bar""")
stream = StringIO(u"""foo: bar""")
loader = AnsibleLoader(stream, 'myfile.yml')
data = loader.get_single_data()
self.assertEqual(data, dict(foo=u'bar'))
@ -126,12 +127,12 @@ class TestAnsibleLoaderBasic(unittest.TestCase):
self.assertEqual(data[u'foo'].ansible_pos, ('myfile.yml', 1, 6))
def test_error_conditions(self):
stream = StringIO("""{""")
stream = StringIO(u"""{""")
loader = AnsibleLoader(stream, 'myfile.yml')
self.assertRaises(ParserError, loader.get_single_data)
def test_front_matter(self):
stream = StringIO("""---\nfoo: bar""")
stream = StringIO(u"""---\nfoo: bar""")
loader = AnsibleLoader(stream, 'myfile.yml')
data = loader.get_single_data()
self.assertEqual(data, dict(foo=u'bar'))
@ -140,7 +141,7 @@ class TestAnsibleLoaderBasic(unittest.TestCase):
self.assertEqual(data[u'foo'].ansible_pos, ('myfile.yml', 2, 6))
# Initial indent (See: #6348)
stream = StringIO(""" - foo: bar\n baz: qux""")
stream = StringIO(u""" - foo: bar\n baz: qux""")
loader = AnsibleLoader(stream, 'myfile.yml')
data = loader.get_single_data()
self.assertEqual(data, [{u'foo': u'bar', u'baz': u'qux'}])
@ -154,7 +155,7 @@ class TestAnsibleLoaderBasic(unittest.TestCase):
class TestAnsibleLoaderPlay(unittest.TestCase):
def setUp(self):
stream = StringIO("""
stream = StringIO(u"""
- hosts: localhost
vars:
number: 1

View file

@ -19,7 +19,7 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from six import StringIO
from io import StringIO
from ansible.compat.tests import unittest
from ansible.playbook.play_context import PlayContext