A mapping from bytes numbers (in range(0,256)) to strings. String values are percent-encoded byte values, unless the key < 128, and in either of the specified safe set, or the always safe set.
| 824 | raise AttributeError(f'module {__name__!r} has no attribute {name!r}') |
| 825 | |
| 826 | class _Quoter(dict): |
| 827 | """A mapping from bytes numbers (in range(0,256)) to strings. |
| 828 | |
| 829 | String values are percent-encoded byte values, unless the key < 128, and |
| 830 | in either of the specified safe set, or the always safe set. |
| 831 | """ |
| 832 | # Keeps a cache internally, via __missing__, for efficiency (lookups |
| 833 | # of cached keys don't call Python code at all). |
| 834 | def __init__(self, safe): |
| 835 | """safe: bytes object.""" |
| 836 | self.safe = _ALWAYS_SAFE.union(safe) |
| 837 | |
| 838 | def __repr__(self): |
| 839 | return f"<Quoter {dict(self)!r}>" |
| 840 | |
| 841 | def __missing__(self, b): |
| 842 | # Handle a cache miss. Store quoted string in cache and return. |
| 843 | res = chr(b) if b in self.safe else '%{:02X}'.format(b) |
| 844 | self[b] = res |
| 845 | return res |
| 846 | |
| 847 | def quote(string, safe='/', encoding=None, errors=None): |
| 848 | """quote('abc def') -> 'abc%20def' |
no outgoing calls
no test coverage detected