# HG changeset patch # Parent 895411e917917a2ef0372f00fc59047d559d2afa Closes #13291: NameError in xmlrpc package. diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py --- a/Lib/test/test_xmlrpc.py +++ b/Lib/test/test_xmlrpc.py @@ -66,15 +66,6 @@ (newdt,), m = xmlrpclib.loads(s, use_datetime=0) self.assertEqual(newdt, xmlrpclib.DateTime('00010210T11:41:23')) - def test_cmp_datetime_DateTime(self): - now = datetime.datetime.now() - dt = xmlrpclib.DateTime(now.timetuple()) - self.assertTrue(dt == now) - self.assertTrue(now == dt) - then = now + datetime.timedelta(seconds=4) - self.assertTrue(then >= dt) - self.assertTrue(dt < then) - def test_bug_1164912 (self): d = xmlrpclib.DateTime() ((new_d,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((d,), @@ -186,7 +177,7 @@ self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, s) def test_dotted_attribute(self): - # this will raise AttirebuteError because code don't want us to use + # this will raise AttributeError because code don't want us to use # private methods self.assertRaises(AttributeError, xmlrpc.server.resolve_dotted_attribute, str, '__add') @@ -233,6 +224,29 @@ t2 = xmlrpclib._datetime(d) self.assertEqual(t1, tref) + def test_comparison(self): + now = datetime.datetime.now() + dt = xmlrpclib.DateTime(now.timetuple()) + + # datetime vs. DateTime + self.assertTrue(dt == now) + self.assertTrue(now == dt) + then = now + datetime.timedelta(seconds=4) + self.assertTrue(then >= dt) + self.assertTrue(dt < then) + + # str vs. DateTime + ds = now.strftime("%Y%m%dT%H:%M:%S") + self.assertTrue(dt == ds) + self.assertTrue(ds == dt) + dt_then = xmlrpclib.DateTime(then.timetuple()) + self.assertTrue(dt_then >= ds) + self.assertTrue(ds < dt_then) + + # int vs. DateTime + with self.assertRaises(TypeError): + dt < 1970 + class BinaryTestCase(unittest.TestCase): # XXX What should str(Binary(b"\xff")) return? I'm chosing "\xff" @@ -346,6 +360,10 @@ class MyRequestHandler(requestHandler): rpc_paths = [] + class BrokenDispatcher: + def _marshaled_dispatch(self, data, dispatch_method=None, path=None): + raise RuntimeError("broken dispatcher") + serv = MyXMLRPCServer(("localhost", 0), MyRequestHandler, logRequests=False, bind_and_activate=False) serv.socket.settimeout(3) @@ -366,6 +384,7 @@ d.register_multicall_functions() serv.get_dispatcher(paths[0]).register_function(pow) serv.get_dispatcher(paths[1]).register_function(lambda x,y: x+y, 'add') + serv.add_dispatcher("/is/broken", BrokenDispatcher()) evt.set() # handle up to 'numrequests' requests @@ -595,11 +614,16 @@ p = xmlrpclib.ServerProxy(URL+"/foo") self.assertEqual(p.pow(6,8), 6**8) self.assertRaises(xmlrpclib.Fault, p.add, 6, 8) + def test_path2(self): p = xmlrpclib.ServerProxy(URL+"/foo/bar") self.assertEqual(p.add(6,8), 6+8) self.assertRaises(xmlrpclib.Fault, p.pow, 6, 8) + def test_path3(self): + p = xmlrpclib.ServerProxy(URL+"/is/broken") + self.assertRaises(xmlrpclib.Fault, p.add, 6, 8) + #A test case that verifies that a server using the HTTP/1.1 keep-alive mechanism #does indeed serve subsequent requests on the same connection class BaseKeepaliveServerTestCase(BaseServerTestCase): diff --git a/Lib/xmlrpc/client.py b/Lib/xmlrpc/client.py --- a/Lib/xmlrpc/client.py +++ b/Lib/xmlrpc/client.py @@ -133,7 +133,8 @@ name (None if not present). """ -import re, time, operator +import base64 +import time import http.client from xml.parsers import expat import socket @@ -230,7 +231,7 @@ ## # Indicates an XML-RPC fault response package. This exception is # raised by the unmarshalling layer, if the XML-RPC response contains -# a fault string. This exception can also used as a class, to +# a fault string. This exception can also be used as a class, to # generate a fault XML-RPC message. # # @param faultCode The XML-RPC fault code. @@ -244,8 +245,8 @@ self.faultString = faultString def __repr__(self): return ( - "" % - (self.faultCode, repr(self.faultString)) + "" % + (self.faultCode, self.faultString) ) # -------------------------------------------------------------------- @@ -302,7 +303,7 @@ elif datetime and isinstance(other, datetime.datetime): s = self.value o = other.strftime("%Y%m%dT%H:%M:%S") - elif isinstance(other, (str, unicode)): + elif isinstance(other, str): s = self.value o = other elif hasattr(other, "timetuple"): @@ -352,7 +353,7 @@ return self.value def __repr__(self): - return "" % (repr(self.value), id(self)) + return "" % (self.value, id(self)) def decode(self, data): self.value = str(data).strip() @@ -378,9 +379,6 @@ # # @param data An 8-bit string containing arbitrary data. -import base64 -import io - class Binary: """Wrapper for binary data.""" @@ -1098,7 +1096,7 @@ """Handles an HTTP transaction to an XML-RPC server.""" # client identifier (may be overridden) - user_agent = "xmlrpclib.py/%s (by www.pythonware.com)" % __version__ + user_agent = "xmlrpc.client/%s (by www.pythonware.com)" % __version__ #if true, we'll request gzip encoding accept_gzip_encoding = True @@ -1192,7 +1190,6 @@ auth, host = urllib.parse.splituser(host) if auth: - import base64 auth = urllib.parse.unquote_to_bytes(auth) auth = base64.encodebytes(auth).decode("utf-8") auth = "".join(auth.split()) # get rid of whitespace diff --git a/Lib/xmlrpc/server.py b/Lib/xmlrpc/server.py --- a/Lib/xmlrpc/server.py +++ b/Lib/xmlrpc/server.py @@ -329,7 +329,6 @@ if method is None: return "" else: - import pydoc return pydoc.getdoc(method) def system_multicall(self, call_list): @@ -560,7 +559,7 @@ Simple XML-RPC server that allows functions and a single instance to be installed to handle requests. The default implementation attempts to dispatch XML-RPC calls to the functions or instance - installed in the server. Override the _dispatch method inhereted + installed in the server. Override the _dispatch method inherited from SimpleXMLRPCDispatcher to change this behavior. """ @@ -602,7 +601,7 @@ encoding, bind_and_activate) self.dispatchers = {} self.allow_none = allow_none - self.encoding = encoding + self.encoding = encoding or 'utf-8' def add_dispatcher(self, path, dispatcher): self.dispatchers[path] = dispatcher @@ -620,9 +619,10 @@ # (each dispatcher should have handled their own # exceptions) exc_type, exc_value = sys.exc_info()[:2] - response = xmlrpclib.dumps( - xmlrpclib.Fault(1, "%s:%s" % (exc_type, exc_value)), + response = dumps( + Fault(1, "%s:%s" % (exc_type, exc_value)), encoding=self.encoding, allow_none=self.allow_none) + response = response.encode(self.encoding) return response class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher):