Skip to content

Instantly share code, notes, and snippets.

@SEJeff
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SEJeff/891fb08dc908ec58a6cb to your computer and use it in GitHub Desktop.
Save SEJeff/891fb08dc908ec58a6cb to your computer and use it in GitHub Desktop.
Python 2.6 bug with IOError in a context manager
#!/usr/bin/env python
from __future__ import print_function
class Context(object):
def __enter__(self):
yield
def __exit__(self, type, value, tracebck):
print("type: {0}".format(str(type(value))))
print("repr: {0}".format(repr(value)))
print("istuple: {0}, isioerror: {1}".format(isinstance(value, tuple), isinstance(value, IOError)))
# Silence any exceptions
return True
# Feel free to replace /etc/shadow with any file the current user doesn't
# have read access to
with Context():
fh = open('/etc/shadow')
[jeff@omniscience ~]$ for ver in 2.{6,7} 3.4; do
> echo testing python${ver}
> python${ver} demonstrate_py26_bug.py
> done
testing python2.6
type: (13, 'Permission denied', '/etc/shadow')
repr: (13, 'Permission denied', '/etc/shadow')
istuple: True, isioerror: False
testing python2.7
type: [Errno 13] Permission denied: '/etc/shadow'
repr: IOError(13, 'Permission denied')
istuple: False, isioerror: True
testing python3.4
type: [Errno 13] Permission denied: '/etc/shadow'
repr: PermissionError(13, 'Permission denied')
istuple: False, isioerror: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment