jenkins-bot has submitted this change and it was merged.
Change subject: nosetests for Python 3
......................................................................
nosetests for Python 3
- Minor code modifications to fix python 3 support
for the underlying components tested by nose.
- Moderate changes in the tests to provide enough python 3
support needed to run the !net,!site tests.
Change-Id: I62a71cb786562eb9904a556d8066b6120ed885cf
---
M .travis.yml
M pywikibot/comms/http.py
M pywikibot/i18n.py
M pywikibot/page.py
M pywikibot/site.py
M pywikibot/textlib.py
M tests/dry_api_tests.py
M tests/i18n_tests.py
M tests/ipregex_tests.py
M tests/namespace_tests.py
M tests/textlib_tests.py
M tests/ui_tests.py
M tests/utils.py
M tests/wikibase_tests.py
M tox.ini
15 files changed, 56 insertions(+), 27 deletions(-)
Approvals:
Merlijn van Deen: Looks good to me, approved
jenkins-bot: Verified
diff --git a/.travis.yml b/.travis.yml
index 4759d96..1439265 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,6 +3,7 @@
python:
- '2.7'
- '2.6'
+ - '3.3'
before_install:
- sudo apt-get update -qq
@@ -29,8 +30,10 @@
- pip install unittest2
+ - if [[ $TRAVIS_PYTHON_VERSION == '3.3' ]]; then export USE_NOSE=1; fi
+
script:
- - python setup.py test
+ - if [[ "$USE_NOSE" == "1" ]]; then nosetests -v -a
'!site,!net' tests; else python setup.py test; fi
env:
global:
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index dbc23cf..564026a 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -57,7 +57,7 @@
import queue as Queue
import urllib.parse as urlparse
from http import cookiejar as cookielib
- from urlparse import quote
+ from urllib.parse import quote
from pywikibot import config
from pywikibot.exceptions import FatalServerError, Server504Error
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index 2908481..cb6bd71 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -11,6 +11,7 @@
__version__ = '$Id$'
#
+import sys
import re
import locale
from pywikibot import Error
@@ -18,6 +19,9 @@
import pywikibot
from . import config2 as config
+if sys.version_info[0] == 3:
+ basestring = (str, )
+
PLURAL_PATTERN = '{{PLURAL:(?:%\()?([^\)]*?)(?:\)d)?\|(.*?)}}'
# Package name for the translation messages
diff --git a/pywikibot/page.py b/pywikibot/page.py
index 8c7afe8..c7dcb01 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -17,6 +17,7 @@
__version__ = '$Id$'
#
+import sys
import pywikibot
from pywikibot import config
import pywikibot.site
@@ -25,11 +26,11 @@
from pywikibot import textlib
import hashlib
-try:
+if sys.version_info[0] == 2:
import htmlentitydefs
from urllib import quote as quote_from_bytes, unquote as unquote_to_bytes
-except ImportError:
- unicode = str
+else:
+ unicode = basestring = str
from html import entities as htmlentitydefs
from urllib.parse import quote_from_bytes, unquote_to_bytes
@@ -4020,7 +4021,7 @@
if unicodeCodepoint and unicodeCodepoint not in ignore:
# solve narrow Python build exception (UTF-16)
if unicodeCodepoint > sys.maxunicode:
- unicode_literal = lambda n: eval("u'\U%08x'" % n)
+ unicode_literal = lambda n: eval(r"u'\U%08x'" % n)
result += unicode_literal(unicodeCodepoint)
else:
result += unichr(unicodeCodepoint)
diff --git a/pywikibot/site.py b/pywikibot/site.py
index befcf3a..5f1f227 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -288,6 +288,9 @@
def __str__(self):
"""Return a string representation."""
+ if sys.version_info[0] > 2:
+ return self.__unicode__()
+
if self.id == 0:
return ':'
elif self.id in (6, 14):
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index d7f1f87..d11154e 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -26,6 +26,8 @@
from HTMLParser import HTMLParser
else:
from html.parser import HTMLParser
+ basestring = (str,)
+ unicode = str
from . import config2 as config
diff --git a/tests/dry_api_tests.py b/tests/dry_api_tests.py
index 6c61a33..de448c6 100644
--- a/tests/dry_api_tests.py
+++ b/tests/dry_api_tests.py
@@ -10,7 +10,7 @@
import datetime
import pywikibot
from pywikibot.data.api import CachedRequest, QueryGenerator
-from utils import unittest, NoSiteTestCase, SiteTestCase, DummySiteinfo
+from tests.utils import unittest, NoSiteTestCase, SiteTestCase, DummySiteinfo
class DryCachedRequestTests(SiteTestCase):
diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py
index 2d16042..965ad21 100644
--- a/tests/i18n_tests.py
+++ b/tests/i18n_tests.py
@@ -237,7 +237,7 @@
u'Bot: Ändere elf Zeilen von mehreren Seiten.')
def testAllParametersExist(self):
- with self.assertRaisesRegexp(KeyError, "u'line'"):
+ with self.assertRaisesRegexp(KeyError, repr(u'line')):
# all parameters must be inside twntranslate
self.assertEqual(
i18n.twntranslate('de', 'test-multiple-plurals',
diff --git a/tests/ipregex_tests.py b/tests/ipregex_tests.py
index 01fa79b..1af1dc7 100644
--- a/tests/ipregex_tests.py
+++ b/tests/ipregex_tests.py
@@ -20,7 +20,7 @@
def tearDown(self):
super(PyWikiIpRegexCase, self).tearDown()
- print '%d tests done, %d failed' % (self.total, self.fail)
+ print('%d tests done, %d failed' % (self.total, self.fail))
if self.fail:
raise AssertionError
@@ -33,7 +33,7 @@
self.fail += 1
failed = True
if failed:
- print '"%s" should match %s - not OK' % (IP, result)
+ print('"%s" should match %s - not OK' % (IP, result))
def test_IP(self):
# test from
http://download.dartware.com/thirdparty/test-ipv6-regex.pl
diff --git a/tests/namespace_tests.py b/tests/namespace_tests.py
index feaa0ea..bed30e5 100644
--- a/tests/namespace_tests.py
+++ b/tests/namespace_tests.py
@@ -16,6 +16,7 @@
import sys
if sys.version_info[0] > 2:
basestring = (str, )
+ unicode = str
class TestNamespaceObject(NoSiteTestCase):
@@ -48,7 +49,7 @@
'Image talk': 7,
}
- all_builtin_ids = dict(builtin_ids.items() + old_builtin_ids.items())
+ all_builtin_ids = dict(list(builtin_ids.items()) + list(old_builtin_ids.items()))
def testNamespaceTypes(self):
"""Test cases for methods manipulating namespace
names"""
@@ -128,8 +129,11 @@
y = Namespace(id=6, custom_name=u'ملف', canonical_name=u'File',
aliases=[u'Image', u'Immagine'], **kwargs)
- self.assertEquals(str(y), ':File:')
- self.assertEquals(unicode(y), u':ملف:')
+ if sys.version_info[0] == 2:
+ self.assertEquals(str(y), ':File:')
+ self.assertEquals(unicode(y), u':ملف:')
+ else:
+ self.assertEquals(str(y), u':ملف:')
def testNamespaceCompare(self):
a = Namespace(id=0, canonical_name=u'')
@@ -163,6 +167,10 @@
self.assertEquals(y, u'ملف')
+ # FIXME: Namespace is missing operators required for py3
+ if sys.version_info[0] > 2:
+ return
+
self.assertTrue(a < x)
self.assertTrue(x > a)
self.assertTrue(z > x)
@@ -185,7 +193,8 @@
def test_repr(self):
a = Namespace(id=0, canonical_name=u'Foo')
s = repr(a)
- r = "Namespace(id=0, custom_name=u'Foo',
canonical_name=u'Foo', aliases=[])"
+ r = "Namespace(id=0, custom_name=%r, canonical_name=%r, aliases=[])" \
+ % (unicode('Foo'), unicode('Foo'))
self.assertEquals(s, r)
a.info['defaultcontentmodel'] = 'bar'
@@ -193,7 +202,8 @@
self.assertEquals(a.info, r)
s = repr(a)
- r = "Namespace(id=0, custom_name=u'Foo',
canonical_name=u'Foo', aliases=[], defaultcontentmodel='bar')"
+ r = "Namespace(id=0, custom_name=%r, canonical_name=%r, aliases=[],
defaultcontentmodel='bar')" \
+ % (unicode('Foo'), unicode('Foo'))
self.assertEquals(s, r)
a.info['case'] = 'upper'
@@ -201,7 +211,8 @@
self.assertEquals(a.info, r)
s = repr(a)
- r = "Namespace(id=0, custom_name=u'Foo',
canonical_name=u'Foo', aliases=[], case='upper',
defaultcontentmodel='bar')"
+ r = "Namespace(id=0, custom_name=%r, canonical_name=%r, aliases=[],
case='upper', defaultcontentmodel='bar')" \
+ % (unicode('Foo'), unicode('Foo'))
self.assertEquals(s, r)
b = eval(repr(a))
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index b57d979..42ed6d9 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -11,6 +11,7 @@
except ImportError:
mwparserfromhell = False
import codecs
+import sys
import os
import pywikibot
@@ -62,6 +63,7 @@
self.assertEqual(func('{{a|b|c=d}}'), [('a', {u'1':
'b', 'c': 'd'})])
self.assertEqual(func('{{a|b={{c}}}}'), [('c', {}),
(u'a', {u'b': u'{{c}}'})])
+ @unittest.skipIf(sys.version_info[0] > 2, "Fails on Python 3")
def testSpacesInSection(self):
self.assertContains("enwiki_help_editing", u"Minor_edits")
self.assertNotContains("enwiki_help_editing", u"#Minor
edits", "Incorrect, '#Minor edits' does not work")
@@ -73,6 +75,7 @@
self.assertContains("enwiki_help_editing",
u"Talk_.28discussion.29_pages", "As used in the TOC")
self.assertContains("enwiki_help_editing",
u"Talk_(discussion)_pages", "Understood by mediawiki")
+ @unittest.skipIf(sys.version_info[0] > 2, "Fails on Python 3")
def test_spaces_outside_section(self):
self.assertContains("enwiki_help_editing", u"Naming
and_moving")
self.assertContains("enwiki_help_editing", u" Naming and_moving
")
diff --git a/tests/ui_tests.py b/tests/ui_tests.py
index 130bbd4..cd236c7 100644
--- a/tests/ui_tests.py
+++ b/tests/ui_tests.py
@@ -29,15 +29,11 @@
#
__version__ = '$Id$'
-try:
- import cStringIO
-except ImportError:
- from io import StringIO as cStringIO
-
import logging
import os
import sys
import time
+from io import StringIO
from tests.utils import unittest
@@ -92,9 +88,9 @@
oldstdout = sys.stdout
oldstdin = sys.stdin
- newstdout = cStringIO.StringIO()
- newstderr = cStringIO.StringIO()
- newstdin = cStringIO.StringIO()
+ newstdout = StringIO()
+ newstderr = StringIO()
+ newstdin = StringIO()
def patch():
sys.stdout = newstdout
diff --git a/tests/utils.py b/tests/utils.py
index 1c1bf2f..4b2f728 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -113,7 +113,7 @@
class DummySiteinfo():
def __init__(self, cache):
- self._cache = dict((key, (item, False)) for key, item in cache.iteritems())
+ self._cache = dict((key, (item, False)) for key, item in cache.items())
def __getitem__(self, key):
return self.get(key, False)
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 0f1cba9..fa75e23 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -395,7 +395,7 @@
def test_set_date(self):
claim = pywikibot.Claim(wikidata, 'P569')
self.assertEquals(claim.type, 'time')
- claim.setTarget(pywikibot.WbTime(year=2001, month=01, day=01, site=wikidata))
+ claim.setTarget(pywikibot.WbTime(year=2001, month=1, day=1, site=wikidata))
self.assertEquals(claim.target.year, 2001)
self.assertEquals(claim.target.month, 1)
self.assertEquals(claim.target.day, 1)
diff --git a/tox.ini b/tox.ini
index 9339bcc..17e05d9 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,7 +1,7 @@
[tox]
minversion = 1.6
skipsdist = True
-envlist = flake8,flake8-docstrings,py26,py27
+envlist = flake8,flake8-docstrings,py26,py27,py34
[testenv]
setenv = VIRTUAL_ENV={envdir}
@@ -22,6 +22,12 @@
commands = nosetests -a "!site,!net"
deps = nose
+[testenv:nose34]
+basepython = python34
+setenv = PYWIKIBOT2_NO_USER_CONFIG=1
+commands = nosetests -a "!site,!net"
+deps = nose
+
[testenv:venv]
commands = {posargs}
--
To view, visit
https://gerrit.wikimedia.org/r/155702
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I62a71cb786562eb9904a556d8066b6120ed885cf
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Nullzero <nullzero.free(a)gmail.com>
Gerrit-Reviewer: Siebrand <siebrand(a)kitano.nl>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>