diff --git a/generic/tests/trans_hugepage_defrag.py b/generic/tests/trans_hugepage_defrag.py index 44a2ad8225f4061b45c90f9dc41f6fd1c2b81fff..578493212fb1850f8aafeb8c18c1a8947e1c93e4 100644 --- a/generic/tests/trans_hugepage_defrag.py +++ b/generic/tests/trans_hugepage_defrag.py @@ -31,10 +31,11 @@ def run(test, params, env): :param param: Memory parameter. """ - for line in file('/proc/meminfo', 'r').readlines(): - if line.startswith("%s" % param): - output = re.split(r'\s+', line)[1] - return int(output) + with open('/proc/meminfo', 'r') as f: + for line in f.readlines(): + if line.startswith("%s" % param): + output = re.split(r'\s+', line)[1] + return int(output) def set_libhugetlbfs(number): """ @@ -43,17 +44,16 @@ def run(test, params, env): :param number: Number of pages (either string or numeric). """ logging.info("Trying to setup %d hugepages on host", number) - f = file("/proc/sys/vm/nr_hugepages", "w+") - pre_ret = f.read() - logging.debug("Number of huge pages on libhugetlbfs (pre-write): %s" % - pre_ret.strip()) - f.write(str(number)) - f.seek(0) - ret = f.read() - logging.debug( - "Number of huge pages on libhugetlbfs: (post-write): %s" % - ret.strip()) - return int(ret) + with open("/proc/sys/vm/nr_hugepages", "w+") as f: + pre_ret = f.read() + logging.debug("Number of huge pages on libhugetlbfs" + " (pre-write): %s" % pre_ret.strip()) + f.write(str(number)) + f.seek(0) + ret = f.read() + logging.debug("Number of huge pages on libhugetlbfs:" + " (post-write): %s" % ret.strip()) + return int(ret) def change_feature_status(test, status, feature_path, test_config): """ diff --git a/generic/tests/trans_hugepage_swapping.py b/generic/tests/trans_hugepage_swapping.py index e18e1b0abcda0c071024d31a82b58399f2275733..e07930a22d5c64bd889dc6a22a7c314a3c4411c9 100644 --- a/generic/tests/trans_hugepage_swapping.py +++ b/generic/tests/trans_hugepage_swapping.py @@ -22,10 +22,11 @@ def run(test, params, env): Get the memory arguments from system """ args_list_tmp = args_list.copy() - for line in file('/proc/meminfo', 'r').readlines(): - for key in args_list_tmp.keys(): - if line.startswith("%s" % args_list_tmp[key]): - args_list_tmp[key] = int(re.split(r'\s+', line)[1]) + with open('/proc/meminfo', 'r') as f: + for line in f.readlines(): + for key in args_list_tmp.keys(): + if line.startswith("%s" % args_list_tmp[key]): + args_list_tmp[key] = int(re.split(r'\s+', line)[1]) return args_list_tmp try: