Skip to content

Commit e793c53

Browse files
committed
refactor(plugins/bitwarden): remove dead Python 2 compatibility code
1 parent d0a051b commit e793c53

1 file changed

Lines changed: 8 additions & 28 deletions

File tree

plugins/module_utils/bitwarden.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
from __future__ import absolute_import, division, print_function
88

9+
# This module requires Python 3.8+ (secrets, f-strings with =, os.replace, json.JSONDecodeError). This should be fine since it will always run on localhost and the Ansible Controller has to be Python 3.9+ anyway
10+
911
import email.encoders
1012
import email.mime.application
1113
import email.mime.multipart
1214
import email.mime.nonmultipart
1315
import email.parser
14-
import email.utils
16+
import email.policy
1517
import json
1618
import mimetypes
1719
import os
@@ -20,14 +22,7 @@
2022
from urllib.error import HTTPError, URLError
2123

2224
from ansible.module_utils.common.collections import Mapping
23-
from ansible.module_utils.six import PY2, PY3, string_types
24-
from ansible.module_utils.six.moves import cStringIO
25-
26-
try:
27-
import email.policy
28-
except ImportError:
29-
# Py2
30-
import email.generator
25+
from ansible.module_utils.six import string_types
3126

3227
from ansible.module_utils._text import to_bytes, to_native, to_text
3328
from ansible.module_utils.urls import (ConnectionError, SSLValidationError,
@@ -120,30 +115,15 @@ def prepare_multipart_no_base64(fields):
120115

121116
m.attach(part)
122117

123-
if PY3:
124-
# Ensure headers are not split over multiple lines
125-
# The HTTP policy also uses CRLF by default
126-
b_data = m.as_bytes(policy=email.policy.HTTP)
127-
else:
128-
# Py2
129-
# We cannot just call ``as_string`` since it provides no way
130-
# to specify ``maxheaderlen``
131-
fp = cStringIO() # cStringIO seems to be required here
132-
# Ensure headers are not split over multiple lines
133-
g = email.generator.Generator(fp, maxheaderlen=0)
134-
g.flatten(m)
135-
# ``fix_eols`` switches from ``\n`` to ``\r\n``
136-
b_data = email.utils.fix_eols(fp.getvalue())
118+
# Ensure headers are not split over multiple lines
119+
# The HTTP policy also uses CRLF by default
120+
b_data = m.as_bytes(policy=email.policy.HTTP)
137121
del m
138122

139123
headers, sep, b_content = b_data.partition(b'\r\n\r\n')
140124
del b_data
141125

142-
if PY3:
143-
parser = email.parser.BytesHeaderParser().parsebytes
144-
else:
145-
# Py2
146-
parser = email.parser.HeaderParser().parsestr
126+
parser = email.parser.BytesHeaderParser().parsebytes
147127

148128
return (
149129
parser(headers)['content-type'], # Message converts to native strings

0 commit comments

Comments
 (0)