Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/requests/_internal_utils.py : 25%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
3"""
4requests._internal_utils
5~~~~~~~~~~~~~~
7Provides utility functions that are consumed internally by Requests
8which depend on extremely few external helpers (such as compat)
9"""
11from .compat import is_py2, builtin_str, str
14def to_native_string(string, encoding='ascii'):
15 """Given a string object, regardless of type, returns a representation of
16 that string in the native string type, encoding and decoding where
17 necessary. This assumes ASCII unless told otherwise.
18 """
19 if isinstance(string, builtin_str):
20 out = string
21 else:
22 if is_py2:
23 out = string.encode(encoding)
24 else:
25 out = string.decode(encoding)
27 return out
30def unicode_is_ascii(u_string):
31 """Determine if unicode string only contains ASCII characters.
33 :param str u_string: unicode string to check. Must be unicode
34 and not Python 2 `str`.
35 :rtype: bool
36 """
37 assert isinstance(u_string, str)
38 try:
39 u_string.encode('ascii')
40 return True
41 except UnicodeEncodeError:
42 return False