From patchwork Wed Aug 8 15:56:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emmanuel Gil Peyrot X-Patchwork-Id: 10563611 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5BB04139A for ; Sun, 12 Aug 2018 06:04:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 48FC5284C7 for ; Sun, 12 Aug 2018 06:04:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3D4EF2962E; Sun, 12 Aug 2018 06:04:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CC8C0284C7 for ; Sun, 12 Aug 2018 06:04:16 +0000 (UTC) Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 4923D2676D8; Sun, 12 Aug 2018 08:03:52 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id A7CD62678DB; Wed, 8 Aug 2018 17:56:49 +0200 (CEST) Received: from jabberfr.org (jabberfr.org [62.210.214.37]) by alsa0.perex.cz (Postfix) with ESMTP id E036726786A; Wed, 8 Aug 2018 17:56:46 +0200 (CEST) Received: from localhost (jabberfr.org [local]) by jabberfr.org (OpenSMTPD) with ESMTPA id 43e42dc5; Wed, 8 Aug 2018 15:56:39 +0000 (UTC) From: Emmanuel Gil Peyrot To: patch@alsa-project.org Date: Wed, 8 Aug 2018 17:56:32 +0200 Message-Id: <20180808155637.5592-5-linkmauve@jabberfr.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180808155637.5592-1-linkmauve@jabberfr.org> References: <20180808155637.5592-1-linkmauve@jabberfr.org> X-Mailman-Approved-At: Sun, 12 Aug 2018 08:03:43 +0200 Cc: Emmanuel Gil Peyrot , Emmanuel Gil Peyrot , alsa-devel@alsa-project.org Subject: [alsa-devel] [PATCH - hwmixvolume 4/9] hwmixvolume: use a with context to open files X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Emmanuel Gil Peyrot diff --git a/hwmixvolume/hwmixvolume b/hwmixvolume/hwmixvolume index 267228c..e82f32d 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.readlines(): + 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):