psychopy.org | Reference | Downloads | Github

Python3 and iohub.fix_encoding


#1

Copying this from https://github.com/psychopy/psychopy/issues/1759

(with a little edit at the end)

Trying out 1.90-dev3, using Python 3.6 under Anaconda, I get an error when trying to import iohub:

TypeError: startswith first arg must be bytes or a tuple of bytes, not str

I think fix_win_sys_argv(encoding) only needs to be called in Python2 right? How about changing fix_encoding to add a check for the python version:

def fix_encoding():
    """Fixes various encoding problems on all platforms.

    Should be called at the very begining of the process.

    """
    ret = True
    if sys.platform == 'win32':
        ret &= fix_win_codec()

    ret &= fix_default_encoding()

    if sys.platform == 'win32' and sys.version_info[0]<3:
        encoding = sys.getdefaultencoding()
        ret &= fix_win_sys_argv(encoding)
        ret &= fix_win_console(encoding)
    return ret

This addresses Python bugs issue 2128, The last comment there is that the bug fix is not fixable in Python2, the “fix” is to upgrade to Python3.

iohub. fix_win_sys_argv() addresses Issure 2128. I don’t know about iohub.fix_win_console, so perhaps the test for Python3 would only be needed for the call to iohub.fix_win_sys_argv().


#3

Since fix_win_sys_argv() is the function that addresses Python issue 2128, perhaps only that should be version checked.

if sys.platform == 'win32':
    encoding = sys.getdefaultencoding()
    if sys.version_info[0] < 3:
        ret &= fix_win_sys_argv(encoding)
    ret &= fix_win_console(encoding)
return ret