diff mbox

[v2,3/3] pwclient: Fix Python 3 encoding of received strings

Message ID 1481625467-18217-4-git-send-email-thomas.monjalon@6wind.com
State Accepted
Headers show

Commit Message

Thomas Monjalon Dec. 13, 2016, 10:37 a.m. UTC
The conversion encode("utf-8") makes a byte stream which is
poorly printed with Python 3.
However this encoding is required for Popen.communicate() but must be
done after str.join() which applies to a real string.

Signed-off-by: Thomas Monjalon <thomas.monjalon@6wind.com>
Reviewed-by: Stephen Finucane <stephen@that.guru>
---
 patchwork/bin/pwclient | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Comments

Thomas Monjalon Dec. 15, 2016, 10:26 a.m. UTC | #1
2016-12-13 11:37, Thomas Monjalon:
> The conversion encode("utf-8") makes a byte stream which is
> poorly printed with Python 3.
[...]
>              for patch_id in non_empty(h, patch_ids):
>                  s = rpc.patch_get_mbox(patch_id)
>                  if len(s) > 0:
> -                    print(unicode(s).encode("utf-8"))
> +                    print(unicode(s))

I really do not understand these conversions.
I've just found a bug with this patch (sorry it is already applied).

When running the view command with python2 from a shell script there
is an error because of a non-ascii character:

  File "pwclient", line 768, in main
    print(unicode(s))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0142'
in position 468: ordinal not in range(128)

This error does not happen with python3.

Shell script to reproduce the error with a DPDK patch:

#! /bin/sh -e
python2 pwclient view 17892

.pwclientrc:
[options]                                                                                                        
default=dpdk
[dpdk]
url= http://dpdk.org/dev/patchwork/xmlrpc/

What is the magical invocation to make it work with Python 2 and 3?
diff mbox

Patch

diff --git a/patchwork/bin/pwclient b/patchwork/bin/pwclient
index 660ed76..7ea8878 100755
--- a/patchwork/bin/pwclient
+++ b/patchwork/bin/pwclient
@@ -269,7 +269,7 @@  def action_check_info(rpc, check_id):
     print(s)
     print('-' * len(s))
     for key, value in sorted(check.items()):
-        print("- %- 14s: %s" % (key, unicode(value).encode("utf-8")))
+        print("- %- 14s: %s" % (key, unicode(value)))
 
 
 def action_check_create(rpc, patch_id, context, state, url, description):
@@ -293,7 +293,7 @@  def action_info(rpc, patch_id):
     print(s)
     print('-' * len(s))
     for key, value in sorted(patch.items()):
-        print("- %- 14s: %s" % (key, unicode(value).encode("utf-8")))
+        print("- %- 14s: %s" % (key, unicode(value)))
 
 
 def action_get(rpc, patch_id):
@@ -311,7 +311,7 @@  def action_get(rpc, patch_id):
         i += 1
 
     with open(fname, 'w') as f:
-        f.write(unicode(s).encode("utf-8"))
+        f.write(unicode(s))
         print('Saved patch to %s' % fname)
 
 
@@ -748,15 +748,15 @@  def main():
             for patch_id in non_empty(h, patch_ids):
                 s = rpc.patch_get_mbox(patch_id)
                 if len(s) > 0:
-                    i.append(unicode(s).encode("utf-8"))
+                    i.append(unicode(s))
             if len(i) > 0:
-                pager.communicate(input="\n".join(i))
+                pager.communicate(input="\n".join(i).encode("utf-8"))
             pager.stdin.close()
         else:
             for patch_id in non_empty(h, patch_ids):
                 s = rpc.patch_get_mbox(patch_id)
                 if len(s) > 0:
-                    print(unicode(s).encode("utf-8"))
+                    print(unicode(s))
 
     elif action == 'info':
         for patch_id in non_empty(h, patch_ids):