Skip to content

Commit 900bc2b

Browse files
authored
Merge pull request #1417 from moreati/half-a-dozen-of-the-other
Eliminate imports of Ansible provided six
2 parents 09ea40a + e044695 commit 900bc2b

8 files changed

Lines changed: 109 additions & 11 deletions

File tree

ansible_mitogen/compat/six.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# SPDX-FileCopyrightText: 2010-2024 Benjamin Peterson
2+
# SPDX-FileCopyrightText: 2026 Mitogen authors <https://github.com/mitogen-hq>
3+
# SPDX-License-Identifier: MIT
4+
# Source: https://github.com/benjaminp/six/blob/1.17.0/six.py
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
24+
from __future__ import absolute_import
25+
26+
import sys
27+
import types
28+
29+
if sys.version_info >= (3, 3):
30+
from shlex import quote as shlex_quote
31+
else:
32+
from pipes import quote as shlex_quote
33+
34+
35+
if sys.version_info >= (3, 0):
36+
exec_ = getattr(__import__('builtins'), 'exec')
37+
38+
string_types = (str,)
39+
40+
def reraise(tp, value, tb=None):
41+
try:
42+
if value is None:
43+
value = tp()
44+
if value.__traceback__ is not tb:
45+
raise value.with_traceback(tb)
46+
raise value
47+
finally:
48+
value = None
49+
tb = None
50+
51+
else:
52+
string_types = (basestring,)
53+
54+
def exec_(_code_, _globs_=None, _locs_=None):
55+
"""Execute code in a namespace."""
56+
if _globs_ is None:
57+
frame = sys._getframe(1)
58+
_globs_ = frame.f_globals
59+
if _locs_ is None:
60+
_locs_ = frame.f_locals
61+
del frame
62+
elif _locs_ is None:
63+
_locs_ = _globs_
64+
exec("""exec _code_ in _globs_, _locs_""")
65+
66+
exec_("""def reraise(tp, value, tb=None):
67+
try:
68+
raise tp, value, tb
69+
finally:
70+
tb = None
71+
""")
72+
73+
74+
def with_metaclass(meta, *bases):
75+
"""Create a base class with a metaclass."""
76+
# This requires a bit of explanation: the basic idea is to make a dummy
77+
# metaclass for one level of class instantiation that replaces itself with
78+
# the actual metaclass.
79+
class metaclass(type):
80+
81+
def __new__(cls, name, this_bases, d):
82+
if sys.version_info[:2] >= (3, 7):
83+
# This version introduced PEP 560 that requires a bit
84+
# of extra care (we mimic what is done by __build_class__).
85+
resolved_bases = types.resolve_bases(bases)
86+
if resolved_bases is not bases:
87+
d['__orig_bases__'] = bases
88+
else:
89+
resolved_bases = bases
90+
return meta(name, resolved_bases, d)
91+
92+
@classmethod
93+
def __prepare__(cls, name, this_bases):
94+
return meta.__prepare__(name, bases)
95+
return type.__new__(metaclass, 'temporary_class', (), {})

ansible_mitogen/mixins.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@
4040
import ansible.plugins.action
4141
import ansible.utils.unsafe_proxy
4242
import ansible.vars.clean
43-
4443
from ansible.module_utils.common.text.converters import to_bytes, to_text
45-
from ansible.module_utils.six.moves import shlex_quote
4644

4745
import mitogen.core
4846
import mitogen.select
@@ -52,7 +50,7 @@
5250
import ansible_mitogen.target
5351
import ansible_mitogen.utils
5452
import ansible_mitogen.utils.unsafe
55-
53+
from ansible_mitogen.compat.six import shlex_quote
5654

5755
LOG = logging.getLogger(__name__)
5856

ansible_mitogen/plugins/action/mitogen_fetch.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@
1919

2020
import os
2121
import base64
22+
2223
from ansible.errors import AnsibleError, AnsibleActionFail, AnsibleActionSkip
2324
from ansible.module_utils.common.text.converters import to_bytes, to_text
24-
from ansible.module_utils.six import string_types
2525
from ansible.module_utils.parsing.convert_bool import boolean
2626
from ansible.plugins.action import ActionBase
2727
from ansible.utils.display import Display
2828
from ansible.utils.hashing import checksum, checksum_s, md5, secure_hash
2929
from ansible.utils.path import makedirs_safe, is_subpath
3030

31+
from ansible_mitogen.compat.six import string_types
32+
3133
display = Display()
3234

3335

ansible_mitogen/runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@
5858
import imp
5959

6060
import ansible.module_utils.common.warnings
61-
from ansible.module_utils.six.moves import shlex_quote
6261

6362
import mitogen.core
6463
import ansible_mitogen.target # TODO: circular import
6564
from mitogen.core import to_text
6665

66+
from ansible_mitogen.compat.six import shlex_quote
67+
6768
try:
6869
# Cannot use cStringIO as it does not support Unicode.
6970
from StringIO import StringIO

ansible_mitogen/services.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@
5050

5151
import ansible.constants
5252

53-
from ansible.module_utils.six import reraise
54-
5553
import mitogen.core
5654
import mitogen.service
5755
import ansible_mitogen.loaders
5856
import ansible_mitogen.module_finder
5957
import ansible_mitogen.target
6058
import ansible_mitogen.utils
6159
import ansible_mitogen.utils.unsafe
60+
from ansible_mitogen.compat.six import reraise
6261

6362

6463
LOG = logging.getLogger(__name__)

ansible_mitogen/transport_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@
6969
import ansible.constants as C
7070
import ansible.executor.interpreter_discovery
7171
import ansible.utils.unsafe_proxy
72-
73-
from ansible.module_utils.six import with_metaclass
7472
from ansible.module_utils.parsing.convert_bool import boolean
7573

7674
import ansible_mitogen.utils
75+
from ansible_mitogen.compat.six import with_metaclass
76+
7777
import mitogen.core
7878

7979

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ In progress (unreleased)
2626
password prompt doesn't contain U+003A COLON
2727
* :gh:issue:`1523` tests: Split auto, auto_legacy, auto_legacy_silent
2828
interpreter discovery tests
29+
* :gh:issue:`1385` :mod:`ansible_mitogen`: Replace imports of deprecated
30+
``ansible.module_utils.six``
2931

3032

3133
v0.3.48 (2026-05-22)

mitogen/ssh.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434

3535
import logging
3636
import re
37+
import sys
3738

38-
try:
39+
if sys.version_info >= (3, 3):
3940
from shlex import quote as shlex_quote
40-
except ImportError:
41+
else:
4142
from pipes import quote as shlex_quote
4243

4344
import mitogen.core

0 commit comments

Comments
 (0)