Mpaa has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/337224 )

Change subject: piper.py: fix bytes/str handling in python3
......................................................................

piper.py: fix bytes/str handling in python3

Avoid to convert to bytes in python3.

In pywikibot.tools:
- introduce StringType (in addition to already existing StringTypes and
  UnicodeType) to distinguish between str/unicode in py2 without
  affecting python3

Bug: T157215
Change-Id: I2c3d7611b5aca32b4b51339c4a162851e13bacf7
---
M pywikibot/tools/__init__.py
M scripts/piper.py
2 files changed, 10 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/24/337224/1

diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index 526fd93..4bd8e4f 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -30,11 +30,13 @@
 if not PY2:
     import queue as Queue
 
+    StringType = basestring = (str,)
     StringTypes = basestring = (str,)
     UnicodeType = unicode = str
 else:
     import Queue
 
+    StringType = types.StringType
     StringTypes = types.StringTypes
     UnicodeType = types.UnicodeType
 
diff --git a/scripts/piper.py b/scripts/piper.py
index d15e4c8..555e371 100755
--- a/scripts/piper.py
+++ b/scripts/piper.py
@@ -47,6 +47,7 @@
 from pywikibot import pagegenerators
 from pywikibot.bot import (MultipleSitesBot, ExistingPageBot,
                            NoRedirectPageBot, AutomaticTWSummaryBot)
+from pywikibot.tools import StringType, UnicodeType
 
 # This is required for the text that is shown when you run this script
 # with the parameter -help.
@@ -86,9 +87,10 @@
         @return: processed text after piping
         @rtype: unicode
         """
-        text = text.encode('utf-8')
+        if not isinstance(text, StringType):  # py2-py3 compatibility
+            text = text.encode('utf-8')
         pipe = pipes.Template()
-        pipe.append(program.encode('ascii'), '--')
+        pipe.append(str(program), '--')  # py2-py3 compatibility
 
         # Create a temporary filename to save the piped stuff to
         tempFilename = '%s.%s' % (tempfile.mktemp(), 'txt')
@@ -96,8 +98,10 @@
             file.write(text)
 
         # Now retrieve the munged text
-        with open(tempFilename, 'r') as mungedText:
-            unicode_text = mungedText.read().decode('utf-8')
+        with open(tempFilename, 'r') as file:
+            unicode_text = file.read()
+        if not isinstance(unicode_text, UnicodeType):  # py2-py3 compatibility
+            unicode_text = unicode_text.decode('utf-8')
 
         # clean up
         os.unlink(tempFilename)

-- 
To view, visit https://gerrit.wikimedia.org/r/337224
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2c3d7611b5aca32b4b51339c4a162851e13bacf7
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to