linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scripts/spdxcheck.py: improve Python 3 compat
@ 2018-09-16 21:12 Thomas Weißschuh
  2018-09-17 15:40 ` Jeremy Cline
  0 siblings, 1 reply; 2+ messages in thread
From: Thomas Weißschuh @ 2018-09-16 21:12 UTC (permalink / raw)
  To: Thomas Gleixner, Andrew Morton, Jonathan Corbet, Joe Perches,
	Jeremy Cline, linux-kernel
  Cc: Thomas Weißschuh

When reading lines from a text-mode fd strings are returned.
These can not be decoded again into strings, breaking the logic in
parser.
Just make sure all files are opened in binary mode on Python 3, so the
current logic keeps working.

This remains compatible with Python 2 and should have no functional
change.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 scripts/spdxcheck.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/scripts/spdxcheck.py b/scripts/spdxcheck.py
index 839e190bbd7a..8f472f995d70 100755
--- a/scripts/spdxcheck.py
+++ b/scripts/spdxcheck.py
@@ -250,12 +250,15 @@ if __name__ == '__main__':
 
     try:
         if len(args.path) and args.path[0] == '-':
-            parser.parse_lines(sys.stdin, args.maxlines, '-')
+            parser.parse_lines(
+                    # always get the binary fd
+                    getattr(sys.stdin, 'buffer', sys.stdin),
+                    args.maxlines, '-')
         else:
             if args.path:
                 for p in args.path:
                     if os.path.isfile(p):
-                        parser.parse_lines(open(p), args.maxlines, p)
+                        parser.parse_lines(open(p, 'rb'), args.maxlines, p)
                     elif os.path.isdir(p):
                         scan_git_subtree(repo.head.reference.commit.tree, p)
                     else:
-- 
2.18.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] scripts/spdxcheck.py: improve Python 3 compat
  2018-09-16 21:12 [PATCH] scripts/spdxcheck.py: improve Python 3 compat Thomas Weißschuh
@ 2018-09-17 15:40 ` Jeremy Cline
  0 siblings, 0 replies; 2+ messages in thread
From: Jeremy Cline @ 2018-09-17 15:40 UTC (permalink / raw)
  To: Thomas Weißschuh, Thomas Gleixner, Andrew Morton,
	Jonathan Corbet, Joe Perches, linux-kernel

On 09/16/2018 05:12 PM, Thomas Weißschuh wrote:
> When reading lines from a text-mode fd strings are returned.
> These can not be decoded again into strings, breaking the logic in
> parser.
> Just make sure all files are opened in binary mode on Python 3, so the
> current logic keeps working.
> 
> This remains compatible with Python 2 and should have no functional
> change.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  scripts/spdxcheck.py | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/scripts/spdxcheck.py b/scripts/spdxcheck.py
> index 839e190bbd7a..8f472f995d70 100755
> --- a/scripts/spdxcheck.py
> +++ b/scripts/spdxcheck.py
> @@ -250,12 +250,15 @@ if __name__ == '__main__':
>  
>      try:
>          if len(args.path) and args.path[0] == '-':
> -            parser.parse_lines(sys.stdin, args.maxlines, '-')
> +            parser.parse_lines(
> +                    # always get the binary fd
> +                    getattr(sys.stdin, 'buffer', sys.stdin),
> +                    args.maxlines, '-')

I think a slightly more "Pythonic" way to do this would be:

try:
    parser.parse_lines(sys.stdin.buffer, args.maxlines, '-')
except AttributeError:
    # Python 2 doesn't have a binary buffer interface on stdin
    parser.parse_lines(sys.stdin, args.maxlines, '-')

but they're equivalent so it's completely personal preference.
	
>          else:
>              if args.path:
>                  for p in args.path:
>                      if os.path.isfile(p):
> -                        parser.parse_lines(open(p), args.maxlines, p)
> +                        parser.parse_lines(open(p, 'rb'), args.maxlines, p)
>                      elif os.path.isdir(p):
>                          scan_git_subtree(repo.head.reference.commit.tree, p)
>                      else:
> 

For what it's worth:

Reviewed-by: Jeremy Cline <jcline@redhat.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-09-17 15:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-16 21:12 [PATCH] scripts/spdxcheck.py: improve Python 3 compat Thomas Weißschuh
2018-09-17 15:40 ` Jeremy Cline

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).