simplify and modernize updatecache()

This commit is contained in:
Benjamin Peterson 2010-05-21 21:35:44 +00:00
parent b9e7c01294
commit 26da187193
1 changed files with 10 additions and 12 deletions

View File

@ -72,13 +72,13 @@ def updatecache(filename, module_globals=None):
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,8 +115,7 @@ def updatecache(filename, module_globals=None):
fullname = os.path.join(dirname, basename)
except (TypeError, AttributeError):
# Not sufficiently string-like to do anything useful with.
pass
else:
continue
try:
stat = os.stat(fullname)
break
@ -125,10 +124,9 @@ def updatecache(filename, module_globals=None):
else:
return []
try:
fp = open(fullname, 'rU')
with open(fullname, 'rU') as fp:
lines = fp.readlines()
fp.close()
except IOError, msg:
except IOError:
return []
if lines and not lines[-1].endswith('\n'):
lines[-1] += '\n'