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 returns unicode for Python 2 and str for Python 3.

Parameters:x – Input to convert to a unicode string.
Returns:str on Python 3.x, or unicode 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, the enc parameter indicates the encoding to use when converting the input to a non-unicode string. If the input is bytes and the Python version is 3.x, the enc 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 to bytes.

Parameters:
  • x – Input to convert to bytes.
  • enc (str, optional) – The encoding type used to encode any unicode string input. Defaults to UTF-8.
sdl2.ext.compat.isiterable(x)[source]

Checks whether the input is a non-string iterable.

Parameters:x – The object to check for iterability.
Returns:True if the input is a valid iterable, otherwise False.
Return type:bool
sdl2.ext.compat.platform_is_64bit()[source]

Checks whether the Python interpreter is 64-bit.

Returns:True if running on 64-bit Python, otherwise False.
Return type:bool