This should probably be `'%sb' % ('r+' if mode == 'a' else mode)` -- causes test failures on Python 3.