From: Benjamin Peterson Date: Fri, 21 May 2010 21:35:44 +0000 (+0000) Subject: simplify and modernize updatecache() X-Git-Tag: v2.7rc1~113 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=26da187193978ba497f8380fa3b1ad3fc67e94ad;p=thirdparty%2FPython%2Fcpython.git simplify and modernize updatecache() --- diff --git a/Lib/linecache.py b/Lib/linecache.py index da62f0d1751a..811f27fe3657 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -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,20 +115,18 @@ def updatecache(filename, module_globals=None): 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'