[Python-checkins] r81437 - python/trunk/Lib/linecache.py
benjamin.peterson
python-checkins at python.org
Fri May 21 23:35:44 CEST 2010
Author: benjamin.peterson
Date: Fri May 21 23:35:44 2010
New Revision: 81437
Log:
simplify and modernize updatecache()
Modified:
python/trunk/Lib/linecache.py
Modified: python/trunk/Lib/linecache.py
==============================================================================
--- python/trunk/Lib/linecache.py (original)
+++ python/trunk/Lib/linecache.py Fri May 21 23:35:44 2010
@@ -72,13 +72,13 @@
if filename in cache:
del cache[filename]
- if not filename or filename[0] + filename[-1] == '<>':
+ if not filename or (filename.startswith('<') and filename.endswith('>')):
return []
fullname = filename
try:
stat = os.stat(fullname)
- except os.error, msg:
+ except OSError:
basename = filename
# Try for a __loader__, if available
@@ -115,20 +115,18 @@
fullname = os.path.join(dirname, basename)
except (TypeError, AttributeError):
# Not sufficiently string-like to do anything useful with.
+ continue
+ try:
+ stat = os.stat(fullname)
+ break
+ except os.error:
pass
- else:
- try:
- stat = os.stat(fullname)
- break
- except os.error:
- pass
else:
return []
try:
- fp = open(fullname, 'rU')
- lines = fp.readlines()
- fp.close()
- except IOError, msg:
+ with open(fullname, 'rU') as fp:
+ lines = fp.readlines()
+ except IOError:
return []
if lines and not lines[-1].endswith('\n'):
lines[-1] += '\n'
More information about the Python-checkins
mailing list