jenkins-bot has submitted this change and it was merged.
Change subject: Make tests/api_tests.py run on py3k
......................................................................
Make tests/api_tests.py run on py3k
Syntactic/import changes:
comms/http.py syntax/import
data/wikidataquery.py import
pywikibot/textlib.py syntax
More elaborate changes:
comms/threadedhttp.py: import, plus adapt DummyResponse/DummyRequest
to have Python3 urllib's required parameters
pywikibot/throttle.py: close throttle file after use
userinterfaces/terminal_interface_unix.py: only encode when target stream
is a bytes stream (i.e. doesn't have encoding
property)
tests/api_tests.py: cleanup to more specific assertions.
Change-Id: Ib639e7dc70ffc5f192aefacf64e2770c80629c8a
---
M pywikibot/comms/http.py
M pywikibot/comms/threadedhttp.py
M pywikibot/data/wikidataquery.py
M pywikibot/textlib.py
M pywikibot/throttle.py
M pywikibot/userinterfaces/terminal_interface_unix.py
M tests/api_tests.py
7 files changed, 34 insertions(+), 15 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index a3a25fc..97f5f40 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -35,7 +35,7 @@
else:
from ssl import SSLError as SSLHandshakeError
import queue as Queue
- import urllib as urlparse
+ import urllib.parse as urlparse
from http import cookiejar as cookielib
from pywikibot import config
@@ -76,7 +76,7 @@
# Build up HttpProcessors
-pywikibot.log('Starting %(numthreads)i threads...' % locals())
+pywikibot.log(u'Starting %(numthreads)i threads...' % locals())
for i in range(numthreads):
proc = threadedhttp.HttpProcessor(http_queue, cookie_jar, connection_pool)
proc.setDaemon(True)
@@ -92,7 +92,7 @@
message = u'Waiting for %i network thread(s) to finish. Press ctrl-c to
abort' % len(threads)
if hasattr(sys, 'last_type'):
# we quit because of an exception
- print sys.last_type
+ print(sys.last_type)
pywikibot.critical(message)
else:
pywikibot.log(message)
diff --git a/pywikibot/comms/threadedhttp.py b/pywikibot/comms/threadedhttp.py
index 3b6f6a8..f59060d 100644
--- a/pywikibot/comms/threadedhttp.py
+++ b/pywikibot/comms/threadedhttp.py
@@ -31,8 +31,10 @@
if sys.version_info[0] == 2:
import cookielib
+ from urllib import splittype, splithost, unquote
else:
from http import cookiejar as cookielib
+ from urllib.parse import splittype, splithost, unquote
import pywikibot
from pywikibot import config
@@ -392,10 +394,10 @@
self.url = url
self.headers = headers
self.origin_req_host = cookielib.request_host(self)
- self.type, r = urllib.splittype(url)
- self.host, r = urllib.splithost(r)
+ self.type, r = splittype(url)
+ self.host, r = splithost(r)
if self.host:
- self.host = urllib.unquote(self.host)
+ self.host = unquote(self.host)
def get_full_url(self):
return self.url
@@ -424,6 +426,8 @@
# TODO to match urllib2, this should be set to True when the
# request is the result of a redirect
return False
+
+ unverifiable = property(is_unverifiable)
class DummyResponse(object):
@@ -457,3 +461,9 @@
# to split carefully here - header.split(',') won't do it.
HEADERVAL = re.compile(r'\s*(([^,]|(,\s*\d))+)')
return [h[0] for h in HEADERVAL.findall(self.response[k])]
+
+ def get_all(self, k, failobj=None):
+ rv = self.getheaders(k)
+ if not rv:
+ return failobj
+ return rv
diff --git a/pywikibot/data/wikidataquery.py b/pywikibot/data/wikidataquery.py
index fdd66c7..79a315a 100644
--- a/pywikibot/data/wikidataquery.py
+++ b/pywikibot/data/wikidataquery.py
@@ -8,7 +8,11 @@
# Distributed under the terms of the MIT license.
import json
-import urllib2
+import sys
+if sys.version_info[0] == 2:
+ from urllib2 import quote
+else:
+ from urllib.parse import quote
from pywikibot.comms import http
import pickle
import os
@@ -421,7 +425,7 @@
Get the query string for a given query or queryset
@return query string including lables and props
"""
- qStr = "q=%s" % urllib2.quote(str(q))
+ qStr = "q=%s" % quote(str(q))
if labels:
qStr += "&labels=%s" % ','.join(labels)
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 4535b4d..15e29b5 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -1180,7 +1180,7 @@
timeR = r'(?P<time>(?P<hour>[0-2]\d)[:\.h](?P<minute>[0-5]\d))'
timeznR = r'\((?P<tzinfo>[A-Z]+)\)'
yearR = r'(?P<year>(19|20)\d\d)'
- monthR = ur'(?P<month>(%s))' % (u'|'.join(self.origNames2monthNum))
+ monthR = r'(?P<month>(%s))' % (u'|'.join(self.origNames2monthNum))
dayR = r'(?P<day>(3[01]|[12]\d|0?[1-9]))'
self.ptimeR = re.compile(timeR)
diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py
index 4e56464..95cc6b1 100644
--- a/pywikibot/throttle.py
+++ b/pywikibot/throttle.py
@@ -108,6 +108,7 @@
'site': this_site})
if not pid and this_pid >= my_pid:
my_pid = this_pid + 1 # next unused process id
+ f.close()
if not pid:
pid = my_pid
diff --git a/pywikibot/userinterfaces/terminal_interface_unix.py
b/pywikibot/userinterfaces/terminal_interface_unix.py
index 1da2a6a..30e64ec 100755
--- a/pywikibot/userinterfaces/terminal_interface_unix.py
+++ b/pywikibot/userinterfaces/terminal_interface_unix.py
@@ -42,4 +42,7 @@
# just to be sure, reset the color
text += unixColors['default']
- targetStream.write(text.encode(self.encoding, 'replace'))
+ if hasattr(targetStream, 'encoding'):
+ targetStream.write(text)
+ else:
+ targetStream.write(text.encode(self.encoding, 'replace'))
diff --git a/tests/api_tests.py b/tests/api_tests.py
index f7401f2..7d7e14e 100644
--- a/tests/api_tests.py
+++ b/tests/api_tests.py
@@ -19,9 +19,9 @@
def testObjectCreation(self):
"""Test that api.Request() creates an object with desired attributes"""
req = api.Request(site=mysite, action="test", foo="", bar="test")
- self.assert_(req)
+ self.assertTrue(req)
self.assertEqual(req.site, mysite)
- self.assert_("foo" in req.params)
+ self.assertIn("foo", req.params)
self.assertEqual(req["bar"], "test")
# test item assignment
req["one"] = "1"
@@ -29,8 +29,9 @@
# test compliance with dict interface
# req.keys() should contain "action", "foo", "bar", "one"
self.assertEqual(len(req.keys()), 4)
- self.assert_("test" in req.values())
- self.assert_(all(len(item) == 2 for item in req.items()))
+ self.assertIn("test", req.values())
+ for item in req.items():
+ self.assertEqual(len(item), 2, item)
class TestPageGenerator(PywikibotTestCase):
@@ -70,7 +71,7 @@
for page in results:
self.assertEqual(type(page), pywikibot.Page)
self.assertEqual(page.site, mysite)
- self.assert_(page.title() in titles)
+ self.assertIn(page.title(), titles)
class TestCachedRequest(unittest.TestCase):
--
To view, visit https://gerrit.wikimedia.org/r/130023
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib639e7dc70ffc5f192aefacf64e2770c80629c8a
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits