Skip to content

Commit

Permalink
remove cgi module usage, which causes deprecation warnings on Python …
Browse files Browse the repository at this point in the history
…3.11 (#2794)
  • Loading branch information
wimglenn authored Nov 18, 2022
1 parent f5297b4 commit de07ce2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 6 additions & 4 deletions botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# language governing permissions and limitations under the License.
import base64
import binascii
import cgi
import datetime
import email.message
import functools
import hashlib
import io
Expand Down Expand Up @@ -3011,10 +3011,12 @@ def get_encoding_from_headers(headers, default='ISO-8859-1'):
if not content_type:
return None

content_type, params = cgi.parse_header(content_type)
message = email.message.Message()
message['content-type'] = content_type
charset = message.get_param("charset")

if 'charset' in params:
return params['charset'].strip("'\"")
if charset is not None:
return charset

if 'text' in content_type:
return default
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
determine_content_length,
ensure_boolean,
fix_s3_host,
get_encoding_from_headers,
get_service_module_name,
has_header,
instance_cache,
Expand Down Expand Up @@ -3372,3 +3373,18 @@ def seek(self, *args, **kwargs):
)
def test_is_s3_accelerate_url(url, expected):
assert is_s3_accelerate_url(url) == expected


@pytest.mark.parametrize(
'headers, default, expected',
(
({}, 'ISO-8859-1', None),
({'Content-Type': 'text/html; charset=utf-8'}, 'default', 'utf-8'),
({'Content-Type': 'text/html; charset="utf-8"'}, 'default', 'utf-8'),
({'Content-Type': 'text/html'}, 'ascii', 'ascii'),
({'Content-Type': 'application/json'}, 'ISO-8859-1', None),
),
)
def test_get_encoding_from_headers(headers, default, expected):
charset = get_encoding_from_headers(HeadersDict(headers), default=default)
assert charset == expected

0 comments on commit de07ce2

Please sign in to comment.