API Reference¶
- webencodings.lookup(label)[source]¶
Look for an encoding by its label.
This is the spec’s get an encoding algorithm. Supported labels are listed there.
- class webencodings.Encoding[source]¶
A character encoding that can be used for decoding or encoding.
- name¶
Canonical name of the encoding
- codec_info¶
The actual implementation of the encoding, a stdlib
CodecInfoobject. Seecodecs.register().
- webencodings.UTF8 = <Encoding utf-8>¶
The UTF-8 encoding. Should be used for new content and formats.
- webencodings.decode(input, fallback_encoding, errors='replace')[source]¶
Decode a single string.
- Parameters:
input – A byte string
fallback_encoding – An
Encodingobject or a label string. The encoding to use ifinputdoes not have a BOM.errors – Type of error handling. See
codecs.register().
- Raises:
LookupErrorfor an unknown encoding label.- Returns:
A
(output, encoding)tuple of an Unicode string and anEncoding.
- webencodings.encode(input, encoding=<Encoding utf-8>, errors='strict')[source]¶
Encode a single string.
- Parameters:
input – An Unicode string.
encoding – An
Encodingobject or a label string.errors – Type of error handling. See
codecs.register().
- Raises:
LookupErrorfor an unknown encoding label.- Returns:
A byte string.
- webencodings.iter_decode(input, fallback_encoding, errors='replace')[source]¶
“Pull”-based decoder.
- Parameters:
input –
An iterable of byte strings.
The input is first consumed just enough to determine the encoding based on the precense of a BOM, then consumed on demand when the return value is.
fallback_encoding – An
Encodingobject or a label string. The encoding to use ifinputdoes not have a BOM.errors – Type of error handling. See
codecs.register().
- Raises:
LookupErrorfor an unknown encoding label.- Returns:
An
(output, encoding)tuple.outputis an iterable of Unicode strings,encodingis theEncodingthat is being used.
- webencodings.iter_encode(input, encoding=<Encoding utf-8>, errors='strict')[source]¶
“Pull”-based encoder.
- Parameters:
input – An iterable of Unicode strings.
encoding – An
Encodingobject or a label string.errors – Type of error handling. See
codecs.register().
- Raises:
LookupErrorfor an unknown encoding label.- Returns:
An iterable of byte strings.
- class webencodings.IncrementalDecoder(fallback_encoding, errors='replace')[source]¶
“Push”-based decoder.
- Parameters:
fallback_encoding – An
Encodingobject or a label string. The encoding to use ifinputdoes not have a BOM.errors – Type of error handling. See
codecs.register().
- Raises:
LookupErrorfor an unknown encoding label.
- class webencodings.IncrementalEncoder(encoding=<Encoding utf-8>, errors='strict')[source]¶
“Push”-based encoder.
- Parameters:
encoding – An
Encodingobject or a label string.errors – Type of error handling. See
codecs.register().
- Raises:
LookupErrorfor an unknown encoding label.
- webencodings.ascii_lower(string)[source]¶
Transform (only) ASCII letters to lower case: A-Z is mapped to a-z.
- Parameters:
string – An Unicode string.
- Returns:
A new Unicode string.
This is used for ASCII case-insensitive matching of encoding labels. The same matching is also used, among other things, for CSS keywords.
This is different from the
str.lower()method of Unicode strings which also affect non-ASCII characters, sometimes mapping them into the ASCII range:>>> keyword = 'Bac\N{KELVIN SIGN}ground' >>> assert keyword.lower() == 'background' >>> assert ascii_lower(keyword) != keyword.lower() >>> assert ascii_lower(keyword) == 'bac\N{KELVIN SIGN}ground'