]> granicus.if.org Git - python/commitdiff
Closes #13291: NameError in xmlrpc package.
authorFlorent Xicluna <florent.xicluna@gmail.com>
Sun, 30 Oct 2011 19:18:50 +0000 (20:18 +0100)
committerFlorent Xicluna <florent.xicluna@gmail.com>
Sun, 30 Oct 2011 19:18:50 +0000 (20:18 +0100)
Lib/test/test_xmlrpc.py
Lib/xmlrpc/client.py
Lib/xmlrpc/server.py
Misc/NEWS

index 4ccc6ff9b6741f99fe75bb6bbffbe3aa75ed3aab..e7b048b661dcf1d4f4932d869d70d1de41497bb4 100644 (file)
@@ -66,15 +66,6 @@ class XMLRPCTestCase(unittest.TestCase):
         (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,),
@@ -233,6 +224,45 @@ class DateTimeTestCase(unittest.TestCase):
         t2 = xmlrpclib._datetime(d)
         self.assertEqual(t1, tref)
 
+    def test_comparison(self):
+        now = datetime.datetime.now()
+        dtime = xmlrpclib.DateTime(now.timetuple())
+
+        # datetime vs. DateTime
+        self.assertTrue(dtime == now)
+        self.assertTrue(now == dtime)
+        then = now + datetime.timedelta(seconds=4)
+        self.assertTrue(then >= dtime)
+        self.assertTrue(dtime < then)
+
+        # str vs. DateTime
+        dstr = now.strftime("%Y%m%dT%H:%M:%S")
+        self.assertTrue(dtime == dstr)
+        self.assertTrue(dstr == dtime)
+        dtime_then = xmlrpclib.DateTime(then.timetuple())
+        self.assertTrue(dtime_then >= dstr)
+        self.assertTrue(dstr < dtime_then)
+
+        # some other types
+        dbytes = dstr.encode('ascii')
+        dtuple = now.timetuple()
+        with self.assertRaises(TypeError):
+            dtime == 1970
+        with self.assertRaises(TypeError):
+            dtime != dbytes
+        with self.assertRaises(TypeError):
+            dtime == bytearray(dbytes)
+        with self.assertRaises(TypeError):
+            dtime != dtuple
+        with self.assertRaises(TypeError):
+            dtime < float(1970)
+        with self.assertRaises(TypeError):
+            dtime > dbytes
+        with self.assertRaises(TypeError):
+            dtime <= bytearray(dbytes)
+        with self.assertRaises(TypeError):
+            dtime >= dtuple
+
 class BinaryTestCase(unittest.TestCase):
 
     # XXX What should str(Binary(b"\xff")) return?  I'm chosing "\xff"
@@ -346,6 +376,10 @@ def http_multi_server(evt, numrequests, requestHandler=None):
     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 +400,7 @@ def http_multi_server(evt, numrequests, requestHandler=None):
             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 +630,16 @@ class MultiPathServerTestCase(BaseServerTestCase):
         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):
index 19d4d6986fc3ffca4654b7a7e287a8b2ad217757..9312344b472017b032921c8fe49cb5b3ae1e67bf 100644 (file)
@@ -302,7 +302,7 @@ class DateTime:
         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"):
index ac252f11d36ebe7e5815ee558b51c989e98c227e..9d93b1c7fb0f23408de1fe7f06301d4139ed8c6b 100644 (file)
@@ -602,7 +602,7 @@ class MultiPathXMLRPCServer(SimpleXMLRPCServer):
                                     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 +620,10 @@ class MultiPathXMLRPCServer(SimpleXMLRPCServer):
             # (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):
index 16c808c44c190f3d7d133efcf8e95fc760296901..80c435a3ada556c4979027c7040b7a7bf77f7dda 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #13291: NameError in xmlrpc package.
+
 - Issue #13258: Use callable() built-in in the standard library.
 
 - Issue #13273: fix a bug that prevented HTMLParser to properly detect some