[Kimchi-devel] [kimchi-devel][PATCH 7/7] Fix file handler leak in netinfo.py

Aline Manera alinefm at linux.vnet.ibm.com
Thu Jan 22 17:50:29 UTC 2015


Reviewed-by: Aline Manera <alinefm at linux.vnet.ibm.com>

On 20/01/2015 06:45, lvroyce at linux.vnet.ibm.com wrote:
> From: Royce Lv <lvroyce at linux.vnet.ibm.com>
>
> Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
> ---
>   src/kimchi/netinfo.py | 16 ++++++++++------
>   1 file changed, 10 insertions(+), 6 deletions(-)
>
> diff --git a/src/kimchi/netinfo.py b/src/kimchi/netinfo.py
> index 991e91f..1cc4854 100644
> --- a/src/kimchi/netinfo.py
> +++ b/src/kimchi/netinfo.py
> @@ -85,7 +85,9 @@ def all_interfaces():
>
>
>   def slaves(bonding):
> -    return open(BONDING_SLAVES % bonding).readline().split()
> +    with open(BONDING_SLAVES % bonding) as bonding_file:
> +        res = bonding_file.readline().split()
> +    return res
>
>
>   def ports(bridge):
> @@ -108,7 +110,8 @@ def operstate(dev):
>   def link_detected(dev):
>       # try to read interface carrier (link) status
>       try:
> -        carrier = open(NET_STATE % dev).readline().strip()
> +        with open(NET_STATE % dev) as dev_file:
> +            carrier = dev_file.readline().strip()
>       # when IOError is raised, interface is down
>       except IOError:
>           return "n/a"
> @@ -123,10 +126,11 @@ def get_vlan_device(vlan):
>       dev = None
>
>       if os.path.exists(PROC_NET_VLAN + vlan):
> -        for line in open(PROC_NET_VLAN + vlan):
> -            if "Device:" in line:
> -                dummy, dev = line.split()
> -                break
> +        with open(PROC_NET_VLAN + vlan) as vlan_file:
> +            for line in vlan_file:
> +                if "Device:" in line:
> +                    dummy, dev = line.split()
> +                    break
>       return dev
>
>




More information about the Kimchi-devel mailing list