[Python-checkins] r84467 - in python/branches/py3k/Lib: keyword.py pdb.py platform.py turtle.py

florent.xicluna python-checkins at python.org
Fri Sep 3 21:52:04 CEST 2010


Author: florent.xicluna
Date: Fri Sep 3 21:52:03 2010
New Revision: 84467
Log:
Use a context manager for some file objects.
Modified:
 python/branches/py3k/Lib/keyword.py
 python/branches/py3k/Lib/pdb.py
 python/branches/py3k/Lib/platform.py
 python/branches/py3k/Lib/turtle.py
Modified: python/branches/py3k/Lib/keyword.py
==============================================================================
--- python/branches/py3k/Lib/keyword.py	(original)
+++ python/branches/py3k/Lib/keyword.py	Fri Sep 3 21:52:03 2010
@@ -61,21 +61,19 @@
 else: optfile = "Lib/keyword.py"
 
 # scan the source file for keywords
- fp = open(iptfile)
- strprog = re.compile('"([^"]+)"')
- lines = []
- for line in fp:
- if '{1, "' in line:
- match = strprog.search(line)
- if match:
- lines.append(" '" + match.group(1) + "',\n")
- fp.close()
+ with open(iptfile) as fp:
+ strprog = re.compile('"([^"]+)"')
+ lines = []
+ for line in fp:
+ if '{1, "' in line:
+ match = strprog.search(line)
+ if match:
+ lines.append(" '" + match.group(1) + "',\n")
 lines.sort()
 
 # load the output skeleton from the target
- fp = open(optfile)
- format = fp.readlines()
- fp.close()
+ with open(optfile) as fp:
+ format = fp.readlines()
 
 # insert the lines of keywords
 try:
Modified: python/branches/py3k/Lib/pdb.py
==============================================================================
--- python/branches/py3k/Lib/pdb.py	(original)
+++ python/branches/py3k/Lib/pdb.py	Fri Sep 3 21:52:03 2010
@@ -155,21 +155,15 @@
 if 'HOME' in os.environ:
 envHome = os.environ['HOME']
 try:
- rcFile = open(os.path.join(envHome, ".pdbrc"))
+ with open(os.path.join(envHome, ".pdbrc")) as rcFile:
+ self.rcLines.extend(rcFile)
 except IOError:
 pass
- else:
- for line in rcFile.readlines():
- self.rcLines.append(line)
- rcFile.close()
 try:
- rcFile = open(".pdbrc")
+ with open(".pdbrc") as rcFile:
+ self.rcLines.extend(rcFile)
 except IOError:
 pass
- else:
- for line in rcFile.readlines():
- self.rcLines.append(line)
- rcFile.close()
 
 self.commands = {} # associates a command list to breakpoint numbers
 self.commands_doprompt = {} # for each bp num, tells if the prompt
Modified: python/branches/py3k/Lib/platform.py
==============================================================================
--- python/branches/py3k/Lib/platform.py	(original)
+++ python/branches/py3k/Lib/platform.py	Fri Sep 3 21:52:03 2010
@@ -200,9 +200,8 @@
 """
 if os.path.exists('/var/adm/inst-log/info'):
 # SuSE Linux stores distribution information in that file
- info = open('/var/adm/inst-log/info').readlines()
 distname = 'SuSE'
- for line in info:
+ for line in open('/var/adm/inst-log/info'):
 tv = line.split()
 if len(tv) == 2:
 tag,value = tv
@@ -217,8 +216,7 @@
 
 if os.path.exists('/etc/.installed'):
 # Caldera OpenLinux has some infos in that file (thanks to Colin Kong)
- info = open('/etc/.installed').readlines()
- for line in info:
+ for line in open('/etc/.installed'):
 pkg = line.split('-')
 if len(pkg) >= 2 and pkg[0] == 'OpenLinux':
 # XXX does Caldera support non Intel platforms ? If yes,
@@ -327,9 +325,8 @@
 return _dist_try_harder(distname,version,id)
 
 # Read the first line
- f = open('/etc/'+file, 'r')
- firstline = f.readline()
- f.close()
+ with open('/etc/'+file, 'r') as f:
+ firstline = f.readline()
 _distname, _version, _id = _parse_release_file(firstline)
 
 if _distname and full_distribution_name:
Modified: python/branches/py3k/Lib/turtle.py
==============================================================================
--- python/branches/py3k/Lib/turtle.py	(original)
+++ python/branches/py3k/Lib/turtle.py	Fri Sep 3 21:52:03 2010
@@ -169,9 +169,8 @@
 
 def config_dict(filename):
 """Convert content of config-file into dictionary."""
- f = open(filename, "r")
- cfglines = f.readlines()
- f.close()
+ with open(filename, "r") as f:
+ cfglines = f.readlines()
 cfgdict = {}
 for line in cfglines:
 line = line.strip()


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /