diff mbox series

utils/check-package: drop six usage

Message ID 20230403013530.3502-1-vfazio@gmail.com
State Accepted
Headers show
Series utils/check-package: drop six usage | expand

Commit Message

Vincent Fazio April 3, 2023, 1:35 a.m. UTC
The shebang in check-package now defines python3. There is no longer a
need to maintain support with python 2.x.

See-also: 02b165dc71 (check-package: fix Python3 support)

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
 utils/check-package | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

Comments

James Knight April 7, 2023, 5:10 p.m. UTC | #1
On Sun, Apr 2, 2023 at 9:35 PM Vincent Fazio <vfazio@gmail.com> wrote:
>
> The shebang in check-package now defines python3. There is no longer a
> need to maintain support with python 2.x.

Tested-by: James Knight <james.d.knight@live.com>
Yann E. MORIN April 10, 2023, 5:44 p.m. UTC | #2
On 2023-04-02 20:35 -0500, Vincent Fazio spake thusly:
> The shebang in check-package now defines python3. There is no longer a
> need to maintain support with python 2.x.
> 
> See-also: 02b165dc71 (check-package: fix Python3 support)
> 
> Signed-off-by: Vincent Fazio <vfazio@gmail.com>
> ---
>  utils/check-package | 10 +++-------
>  1 file changed, 3 insertions(+), 7 deletions(-)
> 
> diff --git a/utils/check-package b/utils/check-package
> index 98a25bd0b2..46f2897b51 100755
> --- a/utils/check-package
> +++ b/utils/check-package
> @@ -6,7 +6,6 @@ import inspect
>  import magic
>  import os
>  import re
> -import six
>  import sys
>  
>  import checkpackagelib.base
> @@ -218,12 +217,9 @@ def check_file_using_lib(fname):
>          if fail > 0:
>              failed.add(name)
>          nwarnings += warn
> -    if six.PY3:
> -        f = open(fname, "r", errors="surrogateescape")
> -    else:
> -        f = open(fname, "r")
> +
>      lastline = ""
> -    for lineno, text in enumerate(f.readlines()):
> +    for lineno, text in enumerate(open(fname, "r", errors="surrogateescape").readlines()):

Err.. This leaks a file object, no?

Even if the script is short-lived, it is better to properly close()
files once they are unused.

Here, I believe using a context manager is adequate (and relatively easy
now that we do no longer have to deal with two ways if opening the
file):

    with open(fname, "r", errors="surrogateescape") as f:
        for lineno, text in enumerate(f.readlines()):
            ...

Regards,
Yann E. MORIN.

>          nlines += 1
>          for name, cf in objects:
>              if cf.disable.search(lastline):
> @@ -233,7 +229,7 @@ def check_file_using_lib(fname):
>                  failed.add(name)
>              nwarnings += warn
>          lastline = text
> -    f.close()
> +
>      for name, cf in objects:
>          warn, fail = print_warnings(cf.after(), name in xfail)
>          if fail > 0:
> -- 
> 2.25.1
> 
> _______________________________________________
> buildroot mailing list
> buildroot@buildroot.org
> https://lists.buildroot.org/mailman/listinfo/buildroot
Yann E. MORIN April 10, 2023, 7:04 p.m. UTC | #3
Vincent, All,

On 2023-04-02 20:35 -0500, Vincent Fazio spake thusly:
> The shebang in check-package now defines python3. There is no longer a
> need to maintain support with python 2.x.
> 
> See-also: 02b165dc71 (check-package: fix Python3 support)
> 
> Signed-off-by: Vincent Fazio <vfazio@gmail.com>

Applied to master, thanks.

> ---
>  utils/check-package | 10 +++-------
>  1 file changed, 3 insertions(+), 7 deletions(-)
> 
> diff --git a/utils/check-package b/utils/check-package
> index 98a25bd0b2..46f2897b51 100755
> --- a/utils/check-package
> +++ b/utils/check-package
> @@ -6,7 +6,6 @@ import inspect
>  import magic
>  import os
>  import re
> -import six
>  import sys
>  
>  import checkpackagelib.base
> @@ -218,12 +217,9 @@ def check_file_using_lib(fname):
>          if fail > 0:
>              failed.add(name)
>          nwarnings += warn
> -    if six.PY3:
> -        f = open(fname, "r", errors="surrogateescape")
> -    else:
> -        f = open(fname, "r")
> +
>      lastline = ""
> -    for lineno, text in enumerate(f.readlines()):
> +    for lineno, text in enumerate(open(fname, "r", errors="surrogateescape").readlines()):

As seen on IRC: it does not leak a fileobject: it is not bound to a
variable, so the object is garbage-collected as soon as it is no longer
referenced, i.e. right after the function call ends.

Regards,
Yann E. MORIN.

>          nlines += 1
>          for name, cf in objects:
>              if cf.disable.search(lastline):
> @@ -233,7 +229,7 @@ def check_file_using_lib(fname):
>                  failed.add(name)
>              nwarnings += warn
>          lastline = text
> -    f.close()
> +
>      for name, cf in objects:
>          warn, fail = print_warnings(cf.after(), name in xfail)
>          if fail > 0:
> -- 
> 2.25.1
> 
> _______________________________________________
> buildroot mailing list
> buildroot@buildroot.org
> https://lists.buildroot.org/mailman/listinfo/buildroot
Arnout Vandecappelle April 10, 2023, 7:11 p.m. UTC | #4
On 10/04/2023 19:44, Yann E. MORIN wrote:
> On 2023-04-02 20:35 -0500, Vincent Fazio spake thusly:
>> The shebang in check-package now defines python3. There is no longer a
>> need to maintain support with python 2.x.
>>
>> See-also: 02b165dc71 (check-package: fix Python3 support)
>>
>> Signed-off-by: Vincent Fazio <vfazio@gmail.com>
>> ---
>>   utils/check-package | 10 +++-------
>>   1 file changed, 3 insertions(+), 7 deletions(-)
>>
>> diff --git a/utils/check-package b/utils/check-package
>> index 98a25bd0b2..46f2897b51 100755
>> --- a/utils/check-package
>> +++ b/utils/check-package
>> @@ -6,7 +6,6 @@ import inspect
>>   import magic
>>   import os
>>   import re
>> -import six
>>   import sys
>>   
>>   import checkpackagelib.base
>> @@ -218,12 +217,9 @@ def check_file_using_lib(fname):
>>           if fail > 0:
>>               failed.add(name)
>>           nwarnings += warn
>> -    if six.PY3:
>> -        f = open(fname, "r", errors="surrogateescape")
>> -    else:
>> -        f = open(fname, "r")
>> +
>>       lastline = ""
>> -    for lineno, text in enumerate(f.readlines()):
>> +    for lineno, text in enumerate(open(fname, "r", errors="surrogateescape").readlines()):
> 
> Err.. This leaks a file object, no?
> 
> Even if the script is short-lived, it is better to properly close()
> files once they are unused.

  It doesn't leak. It just doesn't close the file until some unspecified time in 
the flow. In this case it's actually pretty well specified: it closes after the 
readlines() call ends because the file object has no more references.

  So no, a context manager isn't really needed here.

  Regards,
  Arnout

> 
> Here, I believe using a context manager is adequate (and relatively easy
> now that we do no longer have to deal with two ways if opening the
> file):
> 
>      with open(fname, "r", errors="surrogateescape") as f:
>          for lineno, text in enumerate(f.readlines()):
>              ...
> 
> Regards,
> Yann E. MORIN.
> 
>>           nlines += 1
>>           for name, cf in objects:
>>               if cf.disable.search(lastline):
>> @@ -233,7 +229,7 @@ def check_file_using_lib(fname):
>>                   failed.add(name)
>>               nwarnings += warn
>>           lastline = text
>> -    f.close()
>> +
>>       for name, cf in objects:
>>           warn, fail = print_warnings(cf.after(), name in xfail)
>>           if fail > 0:
>> -- 
>> 2.25.1
>>
>> _______________________________________________
>> buildroot mailing list
>> buildroot@buildroot.org
>> https://lists.buildroot.org/mailman/listinfo/buildroot
>
Peter Korsgaard April 22, 2023, 9:10 p.m. UTC | #5
>>>>> "Vincent" == Vincent Fazio <vfazio@gmail.com> writes:

 > The shebang in check-package now defines python3. There is no longer a
 > need to maintain support with python 2.x.

 > See-also: 02b165dc71 (check-package: fix Python3 support)

 > Signed-off-by: Vincent Fazio <vfazio@gmail.com>

Committed to 2023.02.x, thanks.
diff mbox series

Patch

diff --git a/utils/check-package b/utils/check-package
index 98a25bd0b2..46f2897b51 100755
--- a/utils/check-package
+++ b/utils/check-package
@@ -6,7 +6,6 @@  import inspect
 import magic
 import os
 import re
-import six
 import sys
 
 import checkpackagelib.base
@@ -218,12 +217,9 @@  def check_file_using_lib(fname):
         if fail > 0:
             failed.add(name)
         nwarnings += warn
-    if six.PY3:
-        f = open(fname, "r", errors="surrogateescape")
-    else:
-        f = open(fname, "r")
+
     lastline = ""
-    for lineno, text in enumerate(f.readlines()):
+    for lineno, text in enumerate(open(fname, "r", errors="surrogateescape").readlines()):
         nlines += 1
         for name, cf in objects:
             if cf.disable.search(lastline):
@@ -233,7 +229,7 @@  def check_file_using_lib(fname):
                 failed.add(name)
             nwarnings += warn
         lastline = text
-    f.close()
+
     for name, cf in objects:
         warn, fail = print_warnings(cf.after(), name in xfail)
         if fail > 0: