From patchwork Tue Sep 18 13:42:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Emmanuel Gil Peyrot X-Patchwork-Id: 10604357 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 74FF013AD for ; Tue, 18 Sep 2018 13:43:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 63AC529107 for ; Tue, 18 Sep 2018 13:43:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 57C6B29178; Tue, 18 Sep 2018 13:43:25 +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 9F9A329107 for ; Tue, 18 Sep 2018 13:43:24 +0000 (UTC) Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id C88DA2678BB; Tue, 18 Sep 2018 15:43:03 +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 A439A2678A7; Tue, 18 Sep 2018 15:42:59 +0200 (CEST) Received: from jabberfr.org (jabberfr.org [62.210.214.37]) by alsa0.perex.cz (Postfix) with ESMTP id E3EC926789F; Tue, 18 Sep 2018 15:42:56 +0200 (CEST) Received: from localhost (jabberfr.org [local]) by jabberfr.org (OpenSMTPD) with ESMTPA id 754b4e2e; Tue, 18 Sep 2018 13:42:52 +0000 (UTC) From: Emmanuel Gil Peyrot To: patch@alsa-project.org Date: Tue, 18 Sep 2018 15:42:34 +0200 Message-Id: <20180918134237.8489-5-linkmauve@jabberfr.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180918134237.8489-1-linkmauve@jabberfr.org> References: <20180918134237.8489-1-linkmauve@jabberfr.org> MIME-Version: 1.0 Cc: linkmauve@linkmauve.fr, alsa-devel@alsa-project.org Subject: [alsa-devel] [PATCH - hwmixvolume v2 4/7] 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: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP From: Emmanuel Gil Peyrot 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 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):