1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-08-28 17:04:11 +02:00

python3 compatibility: make all unit test pass

There are many places can't be covered by 2to3, especially the
str/unicode -> str/bytes change done in python3. This is a big patch
incorporating all changes made in order to make python3 suite.py run
without any single failure.

It

 * adapt the introspect_handler_3 for case there are no __defaults__
 * adds the ET_ENCODING variable for ET.fromstring ("unicode" in py3,
  "utf-8" in py2)
 * (re)adds various builtins to both python versions
    - memoryview to python 2.6
    - bytes compatible with py3 to 2.6 and 2.7

and it changes few parts of tests/common.py in order to be compatible
with python3

 * new urlcompare method compares all components or url + parsed query
   string in a dictionary, so the ordering, neither quoting does not matter
 * bytes builtin has been added to 2.x and used in assertEqualMultiline
This commit is contained in:
Michal Vyskocil
2013-04-10 11:34:59 +02:00
committed by Adrian Schröter
parent f0186dbde8
commit b787ca2b39
4 changed files with 90 additions and 47 deletions

View File

@@ -7,12 +7,41 @@ import sys
from xml.etree import cElementTree as ET
EXPECTED_REQUESTS = []
if sys.version_info[0:2] in ((2, 6), (2, 7)):
bytes = lambda x, *args: x
try:
#python 2.x
from cStringIO import StringIO
from urllib2 import HTTPHandler, addinfourl, build_opener
from urlparse import urlparse, parse_qs
except ImportError:
from io import StringIO
from urllib.request import HTTPHandler, addinfourl, build_opener
from urllib.parse import urlparse, parse_qs
def urlcompare(url, *args):
"""compare all components of url except query string - it is converted to
dict, therefor different ordering does not makes url's different, as well
as quoting of a query string"""
components = urlparse(url)
query_args = parse_qs(components.query)
components = components._replace(query=None)
if not args:
return False
for url in args:
components2 = urlparse(url)
query_args2 = parse_qs(components2.query)
components2 = components2._replace(query=None)
if components != components2 or \
query_args != query_args2:
return False
return True
class RequestWrongOrder(Exception):
"""raised if an unexpected request is issued to urllib2"""
@@ -44,7 +73,7 @@ class MyHTTPHandler(HTTPHandler):
def http_open(self, req):
r = self.__exp_requests.pop(0)
if req.get_full_url() != r[1] or req.get_method() != r[0]:
if not urlcompare(req.get_full_url(), r[1]) or req.get_method() != r[0]:
raise RequestWrongOrder(req.get_full_url(), r[1], req.get_method(), r[0])
if req.get_method() in ('GET', 'DELETE'):
return self.__mock_GET(r[1], **r[2])
@@ -63,7 +92,7 @@ class MyHTTPHandler(HTTPHandler):
elif exp is None:
raise RuntimeError('exp or expfile required')
if exp is not None:
if req.get_data() != exp:
if req.get_data() != bytes(exp, "utf-8"):
raise RequestDataMismatch(req.get_full_url(), repr(req.get_data()), repr(exp))
return self.__get_response(req.get_full_url(), **kwargs)