diff mbox series

[-,hwmixvolume,4/9] hwmixvolume: use a with context to open files

Message ID 20180808155637.5592-5-linkmauve@jabberfr.org (mailing list archive)
State New, archived
Headers show
Series [-,hwmixvolume,1/9] hwmixvolume: replace PyGTK with gobject-introspection | expand

Commit Message

Emmanuel Gil Peyrot Aug. 8, 2018, 3:56 p.m. UTC
Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>

Comments

Takashi Sakamoto Aug. 14, 2018, 3:59 a.m. UTC | #1
Hi,

On Aug 9 2018 00:56, Emmanuel Gil Peyrot wrote:
> Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> 
> 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())

I'm not oppose to use with (far from it I greatly prefer it.), however 
the with statement was firstly supported in Python 2.5:
https://docs.python.org/3.6/whatsnew/2.5.html#what-s-new-in-python-2-5

This mean that you drop python 2.4 or former, in this point I have no 
objections because it was already unsupported.

If you use Python 2.5 or later, you can use 'file()' instead of 
'open()'. Furthermore, FILE object returned by 'file()' (or 'open()') 
supports 'iterator' and you have no need to call '.readlines()' anymore,
thus:

```
 > +            with file(filename, "r") as f:
 > +                for line in f:
...
```

>           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):

Regards

Takashi Sakamoto
Takashi Sakamoto Aug. 15, 2018, 3:36 p.m. UTC | #2
On Aug 14 2018 12:59, Takashi Sakamoto wrote:
> If you use Python 2.5 or later, you can use 'file()' instead of 
> 'open()'. Furthermore, FILE object returned by 'file()' (or 'open()') 
> supports 'iterator' and you have no need to call '.readlines()' anymore,
> thus:
> 
> ```
>  > +            with file(filename, "r") as f:
>  > +                for line in f:
> ...
> ```

Oops. The 'file()' is removed at Python 3.0[1]...

[1] https://docs.python.org/release/3.0/whatsnew/3.0.html#builtins


Regards

Takashi Sakamoto
diff mbox series

Patch

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):