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.
| 997 | |
| 998 | |
| 999 | class _Quoter(dict): |
| 1000 | """A mapping from bytes numbers (in range(0,256)) to strings. |
| 1001 | |
| 1002 | String values are percent-encoded byte values, unless the key < 128, and |
| 1003 | in either of the specified safe set, or the always safe set. |
| 1004 | """ |
| 1005 | # Keeps a cache internally, via __missing__, for efficiency (lookups |
| 1006 | # of cached keys don't call Python code at all). |
| 1007 | def __init__(self, safe): |
| 1008 | """safe: bytes object.""" |
| 1009 | self.safe = _ALWAYS_SAFE.union(safe) |
| 1010 | |
| 1011 | def __repr__(self): |
| 1012 | return f"<Quoter {dict(self)!r}>" |
| 1013 | |
| 1014 | def __missing__(self, b): |
| 1015 | # Handle a cache miss. Store quoted string in cache and return. |
| 1016 | res = chr(b) if b in self.safe else '%{:02X}'.format(b) |
| 1017 | self[b] = res |
| 1018 | return res |
| 1019 | |
| 1020 | def quote(string, safe='/', encoding=None, errors=None): |
| 1021 | """quote('abc def') -> 'abc%20def' |
no outgoing calls
no test coverage detected
searching dependent graphs…