Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/rfc3986/compat.py: 86%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (c) 2014 Rackspace
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11# implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Compatibility module for Python 2 and 3 support."""
16import typing as t
18__all__ = (
19 "to_bytes",
20 "to_str",
21)
24@t.overload
25def to_str( # noqa: D103
26 b: t.Union[str, bytes],
27 encoding: str = "utf-8",
28) -> str: ...
31@t.overload
32def to_str(b: None, encoding: str = "utf-8") -> None: # noqa: D103
33 ...
36def to_str(
37 b: t.Optional[t.Union[str, bytes]],
38 encoding: str = "utf-8",
39) -> t.Optional[str]:
40 """Ensure that b is text in the specified encoding."""
41 if hasattr(b, "decode") and not isinstance(b, str):
42 b = b.decode(encoding)
43 return b
46@t.overload
47def to_bytes( # noqa: D103
48 s: t.Union[str, bytes],
49 encoding: str = "utf-8",
50) -> bytes: ...
53@t.overload
54def to_bytes(s: None, encoding: str = "utf-8") -> None: # noqa: D103
55 ...
58def to_bytes(
59 s: t.Optional[t.Union[str, bytes]],
60 encoding: str = "utf-8",
61) -> t.Optional[bytes]:
62 """Ensure that s is converted to bytes from the encoding."""
63 if hasattr(s, "encode") and not isinstance(s, bytes):
64 s = s.encode(encoding)
65 return s