I am creating a python wrapper around a dll and am trying to make it compatible with both Python 2 and 3. Some of the functions in the dll only take in bytes and return bytes. This is fine on Py2 as I can just deal with strings, but on Py3 I need to convert unicode to bytes for input then bytes to unicode for the output.
For example:
import ctypes
from ctypes import util
path = util.find_library(lib)
dll = ctypes.windll.LoadLibrary(path)
def some_function(str_input):
#Will need to convert string to bytes in the case of Py3
bytes_output = dll.some_function(str_input)
return bytes_output # Want this to be str (bytes/unicode for py2/3)
What is the best way to ensure compatibility here? Would it be fine just to use sys.version_info
and encode/decode appropriately or what is the most accepted way to ensure compatibility between versions in this case?