Revision: 3b9c881aa165
Branch: default
Author: Mikko Korpela <[email protected]>
Date: Tue Nov 12 11:59:43 2013 UTC
Log: OperatingSystem#grep_file: use context manager for file
http://code.google.com/p/robotframework/source/detail?r=3b9c881aa165
Modified:
/src/robot/libraries/OperatingSystem.py
=======================================
--- /src/robot/libraries/OperatingSystem.py Tue Nov 12 11:39:39 2013 UTC
+++ /src/robot/libraries/OperatingSystem.py Tue Nov 12 11:59:43 2013 UTC
@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+from __future__ import with_statement
import os
import sys
import tempfile
@@ -330,11 +331,8 @@
"""
path = self._absnorm(path)
self._link("Getting file '%s'", path)
- f = open(path, 'rb')
- try:
+ with open(path, 'rb') as f:
return f.read()
- finally:
- f.close()
def grep_file(self, path, pattern, encoding='UTF-8'):
"""Returns the lines of the specified file that match the
`pattern`.
@@ -364,17 +362,14 @@
lines = []
total_lines = 0
self._link("Reading file '%s'", path)
- f = open(path, 'rb')
- try:
+ with open(path, 'rU') as f:
for line in f:
total_lines += 1
- line = unicode(line,
encoding).replace('\r\n', '\n').rstrip('\n')
+ line = unicode(line, encoding).rstrip('\n')
if fnmatch.fnmatchcase(line, pattern):
lines.append(line)
self._info('%d out of %d lines matched' % (len(lines),
total_lines))
return '\n'.join(lines)
- finally:
- f.close()
def log_file(self, path, encoding='UTF-8'):
"""Wrapper for `Get File` that also logs the returned file.
--
---
You received this message because you are subscribed to the Google Groups "robotframework-commit" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.