Skip to content

Commit

Permalink
Convert all Python code to Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Sep 23, 2021
1 parent e46afd5 commit 536d46c
Show file tree
Hide file tree
Showing 58 changed files with 610 additions and 617 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def read_python_code(filename):
print("ERROR: Suds library setup script needs to be run from the folder "
"containing it.")
print()
print("Current folder: %s" % current_folder)
print("Script folder: %s" % script_folder)
print(("Current folder: %s" % current_folder))
print(("Script folder: %s" % script_folder))
sys.exit(-2)

# Load the suds library version information directly into this module without
Expand Down
34 changes: 17 additions & 17 deletions suds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Project properties
#

from version import __build__, __version__
from .version import __build__, __version__


#
Expand All @@ -34,19 +34,19 @@

class MethodNotFound(Exception):
def __init__(self, name):
Exception.__init__(self, u"Method not found: '%s'" % name)
Exception.__init__(self, "Method not found: '%s'" % name)

class PortNotFound(Exception):
def __init__(self, name):
Exception.__init__(self, u"Port not found: '%s'" % name)
Exception.__init__(self, "Port not found: '%s'" % name)

class ServiceNotFound(Exception):
def __init__(self, name):
Exception.__init__(self, u"Service not found: '%s'" % name)
Exception.__init__(self, "Service not found: '%s'" % name)

class TypeNotFound(Exception):
def __init__(self, name):
Exception.__init__(self, u"Type not found: '%s'" % tostr(name))
Exception.__init__(self, "Type not found: '%s'" % tostr(name))

class BuildError(Exception):
msg = """
Expand All @@ -71,7 +71,7 @@ def __init__(self, name):
class WebFault(Exception):
def __init__(self, fault, document):
if hasattr(fault, 'faultstring'):
Exception.__init__(self, u"Server raised fault: '%s'" %
Exception.__init__(self, "Server raised fault: '%s'" %
fault.faultstring)
self.fault = fault
self.document = document
Expand Down Expand Up @@ -104,15 +104,15 @@ def objid(obj):

def tostr(object, encoding=None):
""" get a unicode safe string representation of an object """
if isinstance(object, basestring):
if isinstance(object, str):
if encoding is None:
return object
else:
return object.encode(encoding)
if isinstance(object, tuple):
s = ['(']
for item in object:
if isinstance(item, basestring):
if isinstance(item, str):
s.append(item)
else:
s.append(tostr(item))
Expand All @@ -122,7 +122,7 @@ def tostr(object, encoding=None):
if isinstance(object, list):
s = ['[']
for item in object:
if isinstance(item, basestring):
if isinstance(item, str):
s.append(item)
else:
s.append(tostr(item))
Expand All @@ -131,21 +131,21 @@ def tostr(object, encoding=None):
return ''.join(s)
if isinstance(object, dict):
s = ['{']
for item in object.items():
if isinstance(item[0], basestring):
for item in list(object.items()):
if isinstance(item[0], str):
s.append(item[0])
else:
s.append(tostr(item[0]))
s.append(' = ')
if isinstance(item[1], basestring):
if isinstance(item[1], str):
s.append(item[1])
else:
s.append(tostr(item[1]))
s.append(', ')
s.append('}')
return ''.join(s)
try:
return unicode(object)
return str(object)
except:
return str(object)

Expand All @@ -155,7 +155,7 @@ def tostr(object, encoding=None):
#

if sys.version_info < (3, 0):
from cStringIO import StringIO as BytesIO
from io import StringIO as BytesIO
else:
from io import BytesIO

Expand All @@ -165,7 +165,7 @@ class UnicodeMixin(object):
# For Python 3, __str__() and __unicode__() should be identical.
__str__ = lambda x: x.__unicode__()
else:
__str__ = lambda x: unicode(x).encode('utf-8')
__str__ = lambda x: str(x).encode('utf-8')

# Used instead of byte literals because they are not supported on Python
# versions prior to 2.6.
Expand All @@ -177,8 +177,8 @@ def byte_str(s='', encoding='utf-8', input_encoding='utf-8', errors='strict'):
strings encoded using the given input encoding.
"""
assert isinstance(s, basestring)
if isinstance(s, unicode):
assert isinstance(s, str)
if isinstance(s, str):
return s.encode(encoding, errors)
if s and encoding != input_encoding:
return s.decode(input_encoding, errors).encode(encoding, errors)
Expand Down
4 changes: 2 additions & 2 deletions suds/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __check_for_extra_arguments(self, args_required, args_allowed):
return

if self.__kwargs:
param_name = self.__kwargs.keys()[0]
param_name = list(self.__kwargs.keys())[0]
if param_name in self.__params_with_arguments:
msg = "got multiple values for parameter '%s'"
else:
Expand Down Expand Up @@ -263,7 +263,7 @@ def __match_ancestry(self, ancestry):
if len(stack) == 1:
return stack[0], ancestry
previous = stack[0]
for frame, n in zip(stack[1:], xrange(len(ancestry))):
for frame, n in zip(stack[1:], range(len(ancestry))):
if frame.id() is not ancestry[n]:
return previous, ancestry[n:]
previous = frame
Expand Down
6 changes: 3 additions & 3 deletions suds/bindings/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def param_defs(self, method):
@return: A collection of parameter definitions
@rtype: [I{pdef},..]
"""
raise Exception, 'not implemented'
raise Exception('not implemented')

def get_message(self, method, args, kwargs):
"""
Expand Down Expand Up @@ -286,7 +286,7 @@ def bodycontent(self, method, args, kwargs):
@return: The XML content for the <body/>
@rtype: [L{Element},..]
"""
raise Exception, 'not implemented'
raise Exception('not implemented')

def headercontent(self, method):
"""
Expand Down Expand Up @@ -339,7 +339,7 @@ def replycontent(self, method, body):
@return: The body content.
@rtype: [L{Element},...]
"""
raise Exception, 'not implemented'
raise Exception('not implemented')

def body(self, content):
"""
Expand Down
2 changes: 1 addition & 1 deletion suds/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, resolver):

def build(self, name):
""" build a an object for the specified typename as defined in the schema """
if isinstance(name, basestring):
if isinstance(name, str):
type = self.resolver.find(name)
if type is None:
raise TypeNotFound(name)
Expand Down
4 changes: 2 additions & 2 deletions suds/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import os
from tempfile import gettempdir as tmp
try:
import cPickle as pickle
import pickle as pickle
except Exception:
import pickle

Expand Down Expand Up @@ -136,7 +136,7 @@ def setduration(self, **duration):
@type duration: {unit:value}
"""
if len(duration) == 1:
arg = duration.items()[0]
arg = list(duration.items())[0]
if not arg[0] in self.units:
raise Exception('must be: %s' % str(self.units))
self.duration = arg
Expand Down
52 changes: 26 additions & 26 deletions suds/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
from suds.transport.https import HttpAuthenticated
from suds.umx.basic import Basic as UmxBasic
from suds.wsdl import Definitions
import sudsobject
from . import sudsobject

from cookielib import CookieJar
from http.cookiejar import CookieJar
from copy import deepcopy
import httplib
from urlparse import urlparse
import http.client
from urllib.parse import urlparse

from logging import getLogger
log = getLogger(__name__)
Expand Down Expand Up @@ -181,7 +181,7 @@ def __unicode__(self):
if ( suds.__build__ ):
s.append(' build: %s' % suds.__build__)
for sd in self.sd:
s.append('\n\n%s' % unicode(sd))
s.append('\n\n%s' % str(sd))
return ''.join(s)


Expand Down Expand Up @@ -223,7 +223,7 @@ def create(self, name):
else:
try:
result = self.builder.build(type)
except Exception, e:
except Exception as e:
log.error("create '%s' failed", name, exc_info=True)
raise BuildError(name, e)
timer.stop()
Expand Down Expand Up @@ -312,20 +312,20 @@ def __find(self, name):
"""
service = None
if not len(self.__services):
raise Exception, 'No services defined'
raise Exception('No services defined')
if isinstance(name, int):
try:
service = self.__services[name]
name = service.name
except IndexError:
raise ServiceNotFound, 'at [%d]' % name
raise ServiceNotFound('at [%d]' % name)
else:
for s in self.__services:
if name == s.name:
service = s
break
if service is None:
raise ServiceNotFound, name
raise ServiceNotFound(name)
return PortSelector(self.__client, service.ports, name)

def __ds(self):
Expand Down Expand Up @@ -413,21 +413,21 @@ def __find(self, name):
"""
port = None
if not len(self.__ports):
raise Exception, 'No ports defined: %s' % self.__qn
raise Exception('No ports defined: %s' % self.__qn)
if isinstance(name, int):
qn = '%s[%d]' % (self.__qn, name)
try:
port = self.__ports[name]
except IndexError:
raise PortNotFound, qn
raise PortNotFound(qn)
else:
qn = '.'.join((self.__qn, name))
for p in self.__ports:
if name == p.name:
port = p
break
if port is None:
raise PortNotFound, qn
raise PortNotFound(qn)
qn = '.'.join((self.__qn, port.name))
return MethodSelector(self.__client, port.methods, qn)

Expand Down Expand Up @@ -488,7 +488,7 @@ def __getitem__(self, name):
m = self.__methods.get(name)
if m is None:
qn = '.'.join((self.__qn, name))
raise MethodNotFound, qn
raise MethodNotFound(qn)
return Method(self.__client, m)


Expand Down Expand Up @@ -519,10 +519,10 @@ def __call__(self, *args, **kwargs):
client = clientclass(self.client, self.method)
try:
return client.invoke(args, kwargs)
except WebFault, e:
except WebFault as e:
if self.faults():
raise
return (httplib.INTERNAL_SERVER_ERROR, e)
return (http.client.INTERNAL_SERVER_ERROR, e)

def faults(self):
""" get faults option """
Expand Down Expand Up @@ -613,7 +613,7 @@ def send(self, soapenv):
reply = self.options.transport.send(request)
timer.stop()
metrics.log.debug('waited %s on server reply', timer)
except TransportError, e:
except TransportError as e:
content = e.fp and e.fp.read() or ''
return self.process_reply(reply=content, status=e.httpcode,
description=tostr(e), original_soapenv=original_soapenv)
Expand All @@ -623,12 +623,12 @@ def send(self, soapenv):
def process_reply(self, reply, status=None, description=None,
original_soapenv=None):
if status is None:
status = httplib.OK
if status in (httplib.ACCEPTED, httplib.NO_CONTENT):
status = http.client.OK
if status in (http.client.ACCEPTED, http.client.NO_CONTENT):
return
failed = True
try:
if status == httplib.OK:
if status == http.client.OK:
log.debug('HTTP succeeded:\n%s', reply)
else:
log.debug('HTTP failed - %d - %s:\n%s', status, description,
Expand Down Expand Up @@ -657,19 +657,19 @@ def process_reply(self, reply, status=None, description=None,
# An INSTANCE MUST use a "500 Internal Server Error" HTTP status
# code if the response message is a SOAP Fault.
replyroot = None
if status in (httplib.OK, httplib.INTERNAL_SERVER_ERROR):
if status in (http.client.OK, http.client.INTERNAL_SERVER_ERROR):
replyroot = _parse(reply)
plugins.message.parsed(reply=replyroot)
fault = self.get_fault(replyroot)
if fault:
if status != httplib.INTERNAL_SERVER_ERROR:
if status != http.client.INTERNAL_SERVER_ERROR:
log.warn("Web service reported a SOAP processing "
"fault using an unexpected HTTP status code %d. "
"Reporting as an internal server error.", status)
if self.options.faults:
raise WebFault(fault, replyroot)
return (httplib.INTERNAL_SERVER_ERROR, fault)
if status != httplib.OK:
return (http.client.INTERNAL_SERVER_ERROR, fault)
if status != http.client.OK:
if self.options.faults:
# (todo)
# Use a more specific exception class here.
Expand All @@ -688,7 +688,7 @@ def process_reply(self, reply, status=None, description=None,
failed = False
if self.options.faults:
return result
return (httplib.OK, result)
return (http.client.OK, result)
finally:
if failed and original_soapenv:
log.error(original_soapenv)
Expand Down Expand Up @@ -717,7 +717,7 @@ def headers(self):
@rtype: dict
"""
action = self.method.soap.action
if isinstance(action, unicode):
if isinstance(action, str):
action = action.encode('utf-8')
stock = {'Content-Type':'text/xml; charset=utf-8', 'SOAPAction':action}
result = dict(stock, **self.options.headers)
Expand All @@ -742,7 +742,7 @@ class SimClient(SoapClient):
@classmethod
def simulation(cls, kwargs):
""" get whether loopback has been specified in the I{kwargs}. """
return kwargs.has_key(SimClient.injkey)
return SimClient.injkey in kwargs

def invoke(self, args, kwargs):
"""
Expand Down
Loading

0 comments on commit 536d46c

Please sign in to comment.