[PATCH - hwmixvolume v2 4/7] hwmixvolume: use a with context to open files

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Emmanuel Gil Peyrot <linkmauve@xxxxxxxxxxxx>

This feature has been added in Python 2.5 and automatically closes an
open file once the context exits.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@xxxxxxxxxxxx>

diff --git a/hwmixvolume/hwmixvolume b/hwmixvolume/hwmixvolume
index 8e0b6b8..7f8ba8e 100755
--- a/hwmixvolume/hwmixvolume
+++ b/hwmixvolume/hwmixvolume
@@ -138,26 +138,20 @@ class Stream:
             subdevice = info.index
         filename = "/proc/asound/card%d/pcm%dp/sub%d/status" % (card, device, subdevice)
         try:
-            f = open(filename, "r")
+            with open(filename, "r") as f:
+                for line in f:
+                    if line[:9] == "owner_pid":
+                        return int(line.split(':')[1].strip())
         except IOError:
             return None
-        try:
-            for line in f.readlines():
-                if line[:9] == "owner_pid":
-                    return int(line.split(':')[1].strip())
-        finally:
-            f.close()
         return None
 
     def get_pid_cmdline(self, pid):
         try:
-            f = open("/proc/%d/cmdline" % pid, "r")
+            with open("/proc/%d/cmdline" % pid, "r") as f:
+                cmdline = f.read()
         except IOError:
             return None
-        try:
-            cmdline = f.read()
-        finally:
-            f.close()
         return cmdline.replace('\x00', ' ').strip()
 
 class MixerWindow(Gtk.Window):
-- 
2.19.0

_______________________________________________
Alsa-devel mailing list
Alsa-devel@xxxxxxxxxxxxxxxx
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel




[Index of Archives]     [ALSA User]     [Linux Audio Users]     [Kernel Archive]     [Asterisk PBX]     [Photo Sharing]     [Linux Sound]     [Video 4 Linux]     [Gimp]     [Yosemite News]

  Powered by Linux