All of lore.kernel.org
 help / color / mirror / Atom feed
From: Emmanuel Gil Peyrot <linkmauve@jabberfr.org>
To: patch@alsa-project.org
Cc: linkmauve@linkmauve.fr, alsa-devel@alsa-project.org
Subject: [PATCH - hwmixvolume v2 4/7] hwmixvolume: use a with context to open files
Date: Tue, 18 Sep 2018 15:42:34 +0200	[thread overview]
Message-ID: <20180918134237.8489-5-linkmauve@jabberfr.org> (raw)
In-Reply-To: <20180918134237.8489-1-linkmauve@jabberfr.org>

From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>

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@linkmauve.fr>

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@alsa-project.org
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

  parent reply	other threads:[~2018-09-18 13:42 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20180918134237.8489-1-linkmauve@jabberfr.org>
2018-09-18 13:42 ` [PATCH - hwmixvolume v2 1/7] hwmixvolume: use four spaces instead of one tab for indent Emmanuel Gil Peyrot
2018-09-19  0:54   ` James Cameron
2018-09-20  7:13     ` Takashi Iwai
2018-09-20  8:45       ` James Cameron
2018-09-20  9:20         ` Takashi Iwai
2018-09-20  7:13   ` Takashi Iwai
2018-09-18 13:42 ` [PATCH - hwmixvolume v2 2/7] hwmixvolume: replace PyGTK with gobject-introspection Emmanuel Gil Peyrot
2018-09-18 13:42 ` [PATCH - hwmixvolume v2 3/7] hwmixvolume: switch to GTK+ 3.0 and GLib 2.0 Emmanuel Gil Peyrot
2018-09-19 13:22   ` Takashi Sakamoto
2018-09-19 13:36     ` Emmanuel Gil Peyrot
2018-09-19 14:14       ` Takashi Sakamoto
2018-09-18 13:42 ` Emmanuel Gil Peyrot [this message]
2018-09-18 13:42 ` [PATCH - hwmixvolume v2 5/7] hwmixvolume: switch the shebang to python Emmanuel Gil Peyrot
2018-09-18 13:42 ` [PATCH - hwmixvolume v2 6/7] hwmixvolume: add my copyright Emmanuel Gil Peyrot
2018-09-18 13:42 ` [PATCH - hwmixvolume v2 7/7] hwmixvolume: mention the new dependencies in the README Emmanuel Gil Peyrot
2018-09-19 14:15 ` [PATCH - hwmixvolume v2 0/7] Migrate to GTK+ 3.0 and allow python3 Takashi Sakamoto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180918134237.8489-5-linkmauve@jabberfr.org \
    --to=linkmauve@jabberfr.org \
    --cc=alsa-devel@alsa-project.org \
    --cc=linkmauve@linkmauve.fr \
    --cc=patch@alsa-project.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.