[master 2/3] Use the pipes as contextmanagers to ensure they are closed

dashea installerbot-noreply at redhat.com
Fri Jun 26 19:38:41 UTC 2015


From: David Shea <dshea at redhat.com>

---
 blivet/iscsi.py | 62 ++++++++++++++++++++++++++++-----------------------------
 1 file changed, 31 insertions(+), 31 deletions(-)

diff --git a/blivet/iscsi.py b/blivet/iscsi.py
index afeb037..4fb8f5e 100644
--- a/blivet/iscsi.py
+++ b/blivet/iscsi.py
@@ -95,31 +95,30 @@ def _call_discover_targets(con_write, con_recv, ipaddr, port, authinfo):
     # Close the reading end of the pipe in the child process
     con_recv.close()
 
-    try:
-        found_nodes = libiscsi.discover_sendtargets(address=ipaddr,
-                                                    port=int(port),
-                                                    authinfo=authinfo)
-        if found_nodes is None:
-            found_nodes = []
-
-    except IOError as ex:
-        con_write.send((False, ex))
-        con_write.close()
-        return
+    with con_write:
+        try:
+            found_nodes = libiscsi.discover_sendtargets(address=ipaddr,
+                                                        port=int(port),
+                                                        authinfo=authinfo)
+            if found_nodes is None:
+                found_nodes = []
 
-    nodes = []
+        except IOError as ex:
+            con_write.send((False, ex))
+            return
 
-    # the node object is not pickable so it can't be send with pipe
-    # TODO: change libiscsi.node to pickable object
-    for node in found_nodes:
-        nodes.append({'name': node.name,
-                     'tpgt': node.tpgt,
-                     'address': node.address,
-                     'port': node.port,
-                     'iface': node.iface})
+        nodes = []
 
-    con_write.send((True, nodes))
+        # the node object is not pickable so it can't be send with pipe
+        # TODO: change libiscsi.node to pickable object
+        for node in found_nodes:
+            nodes.append({'name': node.name,
+                         'tpgt': node.tpgt,
+                         'address': node.address,
+                         'port': node.port,
+                         'iface': node.iface})
 
+        con_write.send((True, nodes))
 
 class iscsi(object):
     """ iSCSI utility class.
@@ -355,16 +354,17 @@ def discover(self, ipaddr, port="3260", username=None, password=None,
             # Close the writing end of the pipe in the parent
             con_write.close()
 
-            try:
-                (ok, data) = con_recv.recv()
-                if not ok:
-                    log.debug("iSCSI: exception raised when "
-                              "discover_sendtargets process called: %s",
-                              str(data))
-            except EOFError:
-                ok = False
-                log.error("iSCSI: can't receive response from "
-                          "_call_discover_targets")
+            with con_recv:
+                try:
+                    (ok, data) = con_recv.recv()
+                    if not ok:
+                        log.debug("iSCSI: exception raised when "
+                                  "discover_sendtargets process called: %s",
+                                  str(data))
+                except EOFError:
+                    ok = False
+                    log.error("iSCSI: can't receive response from "
+                              "_call_discover_targets")
 
             p.join()
 


-- 
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/a7875b4d8f8407d2fed3dc95426765c1893e58c6


More information about the anaconda-patches mailing list