sdl2.ext.compat - Python Version Compatibility Helpers
The sdl2.ext.compat
module provides various helper functions for writing
code that works seamlessly on both Python 2.7 and Python 3.x.
- ISPYTHON2
True
, if executed in a Python 2.x compatible interpreter,False
otherwise.
- ISPYTHON3
True
, if executed in a Python 3.x compatible interpreter,False
otherwise.
- sdl2.ext.compat.utf8(x)[source]
Converts input to a unicode string in a Python 2/3 agnostic manner.
If a
bytes
object is passed, it will be decoded as UTF-8. This function returnsunicode
for Python 2 andstr
for Python 3.- Parameters
x – Input to convert to a unicode string.
- Returns
str
on Python 3.x, orunicode
on Python 2.7.
- sdl2.ext.compat.stringify(x, enc='utf-8')[source]
Converts input to a
str
in a Python 2/3 agnostic manner.If the input is
unicode
and the Python version is 2.7, theenc
parameter indicates the encoding to use when converting the input to a non-unicode string. If the input isbytes
and the Python version is 3.x, theenc
parameter indicates the encoding to use to decode the input into a unicode string.- Parameters
x – Input to convert to a
str
.enc (str, optional) – The encoding type used to encode or decode the input, depending on the input type and the major Python version. Defaults to UTF-8.
- sdl2.ext.compat.byteify(x, enc='utf-8')[source]
Converts input to
bytes
in a Python 2/3 agnostic manner.If the input is a unicode string, the
enc
parameter indicates the encoding to use when encoding the input tobytes
.- Parameters
x – Input to convert to
bytes
.enc (str, optional) – The encoding type used to encode any unicode string input. Defaults to UTF-8.