Last active
August 29, 2015 14:12
-
-
Save SEJeff/891fb08dc908ec58a6cb to your computer and use it in GitHub Desktop.
Python 2.6 bug with IOError in a context manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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