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."""
15import typing as t
17__all__ = (
18 "to_bytes",
19 "to_str",
20)
23@t.overload
24def to_str( # noqa: D103
25 b: t.Union[str, bytes],
26 encoding: str = "utf-8",
27) -> str: ...
30@t.overload
31def to_str(b: None, encoding: str = "utf-8") -> None: # noqa: D103
32 ...
35def to_str(
36 b: t.Optional[t.Union[str, bytes]],
37 encoding: str = "utf-8",
38) -> t.Optional[str]:
39 """Ensure that b is text in the specified encoding."""
40 if hasattr(b, "decode") and not isinstance(b, str):
41 b = b.decode(encoding)
42 return b
45@t.overload
46def to_bytes( # noqa: D103
47 s: t.Union[str, bytes],
48 encoding: str = "utf-8",
49) -> bytes: ...
52@t.overload
53def to_bytes(s: None, encoding: str = "utf-8") -> None: # noqa: D103
54 ...
57def to_bytes(
58 s: t.Optional[t.Union[str, bytes]],
59 encoding: str = "utf-8",
60) -> t.Optional[bytes]:
61 """Ensure that s is converted to bytes from the encoding."""
62 if hasattr(s, "encode") and not isinstance(s, bytes):
63 s = s.encode(encoding)
64 return s