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 getpass
import pwd import pwd
import ConfigParser 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 from string import maketrans

View file

@ -19,7 +19,11 @@
import re import re
import socket 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: try:
import paramiko import paramiko

View file

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

View file

@ -23,8 +23,9 @@ import ast
import contextlib import contextlib
import os import os
import re 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 import Environment
from jinja2.loaders import FileSystemLoader from jinja2.loaders import FileSystemLoader
from jinja2.exceptions import TemplateSyntaxError, UndefinedError from jinja2.exceptions import TemplateSyntaxError, UndefinedError

View file

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

View file

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

View file

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

View file

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