Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
28b8f15
issue29615: _dispatch function is no longer called in an except block
Feb 23, 2017
c8d709d
issue29615 (review): use `functools.wraps` and remove the keyword-onl…
Feb 27, 2017
bf8c262
issue29615 (review): do not chain the exceptions, refrain from attemp…
Feb 27, 2017
9124cd1
issue29615 (review): remove unused `functools`
Feb 28, 2017
302f4f3
issue29615 (review): attempts to mimic the original behavior while ge…
Feb 28, 2017
2f7cec1
issue29615 (review): verifies KeyError`s (no exceptions actually) are…
Feb 28, 2017
3895f9b
issue29615 (review): entry for these changes in Misc/NEWS
Feb 28, 2017
d8f688c
Merge remote-tracking branch 'MASTER/master' into issue29615
Feb 28, 2017
8f88056
Merge remote-tracking branch 'MASTER/master' into issue29615
Feb 28, 2017
240bf23
issue29615 (review): tests that ensure no exception chaining takes pl…
Feb 28, 2017
aee470e
issue29615 (review): created a separate SimpleXMLRPCDispatcherTestCas…
Feb 28, 2017
ffb8317
issue29615 (review): defines test case level DispatchExc and uses it,…
Feb 28, 2017
8d9396e
issue29615 (review): proper assert test
Feb 28, 2017
17bbcea
issue29615 (review): removed the assert delegation from DispatchExc a…
Feb 28, 2017
d6d8bba
issue29615 (review): removed keyword arguments when calling `_dispatch`
Feb 28, 2017
3efea6c
issue29615 (review): no longer staticmethod
Feb 28, 2017
5fc1a7d
issue29615 (review): removed unnecessary else block
Feb 28, 2017
4f54e7d
issue29615 (review): fewer `raise`s
Feb 28, 2017
73b9fe9
issue29615 (review): reformulated and moved to the appropriate place
Feb 28, 2017
58c32d2
issue29615 (review): removed
Feb 28, 2017
19af5ec
issue29615 (review): uses `Exception.args`; shorter lines
Mar 1, 2017
97d5e17
issue29615 (review): consisten use if `if` condition
Mar 1, 2017
5d30bc8
issue29615 (review): uses assertTupleEqual to compare tuples; tests t…
Mar 1, 2017
8b63473
issue29615 (review): tests what happens when explicitly registered fu…
Mar 1, 2017
95af409
issue29615 (review): checks what happens when a registered instance's…
Mar 1, 2017
6f487e5
issue29615 (review): typography
Mar 1, 2017
0869e40
issue29615 (review): uses `assertEqual` instead of `assertTupleEqual`
Mar 1, 2017
71e81d2
issue29615 (review): shorter diff
Mar 1, 2017
bbee467
Merge remote-tracking branch 'MASTER/master' into issue29615
Mar 1, 2017
1823a4a
issue29615 (review): moving the NEWS item to the proper place
Mar 1, 2017
e3b7642
issue29615 (review): typo
Mar 1, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion Lib/test/test_xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,94 @@ def run_server():
self.assertEqual(p.method(), 5)
self.assertEqual(p.method(), 5)


class SimpleXMLRPCDispatcherTestCase(unittest.TestCase):
class DispatchExc(Exception):
"""Raised inside the dispatched functions when checking for
chained exceptions"""

def test_call_registered_func(self):
"""Calls explicitly registered function"""
# Makes sure any exception raised inside the function has no other
# exception chained to it

exp_params = 1, 2, 3

def dispatched_func(*params):
raise self.DispatchExc(params)

dispatcher = xmlrpc.server.SimpleXMLRPCDispatcher()
dispatcher.register_function(dispatched_func)
with self.assertRaises(self.DispatchExc) as exc_ctx:
dispatcher._dispatch('dispatched_func', exp_params)
self.assertEqual(exc_ctx.exception.args, (exp_params,))
self.assertIsNone(exc_ctx.exception.__cause__)
self.assertIsNone(exc_ctx.exception.__context__)

def test_call_instance_func(self):
"""Calls a registered instance attribute as a function"""
# Makes sure any exception raised inside the function has no other
# exception chained to it

exp_params = 1, 2, 3

class DispatchedClass:
def dispatched_func(self, *params):
raise SimpleXMLRPCDispatcherTestCase.DispatchExc(params)

dispatcher = xmlrpc.server.SimpleXMLRPCDispatcher()
dispatcher.register_instance(DispatchedClass())
with self.assertRaises(self.DispatchExc) as exc_ctx:
dispatcher._dispatch('dispatched_func', exp_params)
self.assertEqual(exc_ctx.exception.args, (exp_params,))
self.assertIsNone(exc_ctx.exception.__cause__)
self.assertIsNone(exc_ctx.exception.__context__)

def test_call_dispatch_func(self):
"""Calls the registered instance's `_dispatch` function"""
# Makes sure any exception raised inside the function has no other
# exception chained to it

exp_method = 'method'
exp_params = 1, 2, 3

class TestInstance:
def _dispatch(self, method, params):
raise SimpleXMLRPCDispatcherTestCase.DispatchExc(
method, params)

dispatcher = xmlrpc.server.SimpleXMLRPCDispatcher()
dispatcher.register_instance(TestInstance())
with self.assertRaises(self.DispatchExc) as exc_ctx:
dispatcher._dispatch(exp_method, exp_params)
self.assertEqual(exc_ctx.exception.args, (exp_method, exp_params))
self.assertIsNone(exc_ctx.exception.__cause__)
self.assertIsNone(exc_ctx.exception.__context__)

def test_registered_func_is_none(self):
"""Calls explicitly registered function which is None"""

dispatcher = xmlrpc.server.SimpleXMLRPCDispatcher()
dispatcher.register_function(None, name='method')
with self.assertRaises(Exception, expected_regex='method'):
dispatcher._dispatch('method', ('param',))

def test_instance_has_no_func(self):
"""Attempts to call nonexistent function on a registered instance"""

dispatcher = xmlrpc.server.SimpleXMLRPCDispatcher()
dispatcher.register_instance(object())
with self.assertRaises(Exception, expected_regex='method'):
dispatcher._dispatch('method', ('param',))

def test_cannot_locate_func(self):
"""Calls a function that the dispatcher cannot locate"""

dispatcher = xmlrpc.server.SimpleXMLRPCDispatcher()
with self.assertRaises(Exception, expected_regex='method'):
dispatcher._dispatch('method', ('param',))


class HelperTestCase(unittest.TestCase):
def test_escape(self):
self.assertEqual(xmlrpclib.escape("a&b"), "a&b")
Expand Down Expand Up @@ -1313,7 +1401,7 @@ def test_main():
KeepaliveServerTestCase1, KeepaliveServerTestCase2,
GzipServerTestCase, GzipUtilTestCase,
MultiPathServerTestCase, ServerProxyTestCase, FailingServerTestCase,
CGIHandlerTestCase)
CGIHandlerTestCase, SimpleXMLRPCDispatcherTestCase)


if __name__ == "__main__":
Expand Down
43 changes: 24 additions & 19 deletions Lib/xmlrpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,31 +392,36 @@ def _dispatch(self, method, params):
not be called.
"""

func = None
try:
# check to see if a matching function has been registered
# call the matching registered function
func = self.funcs[method]
except KeyError:
if self.instance is not None:
# check for a _dispatch method
if hasattr(self.instance, '_dispatch'):
return self.instance._dispatch(method, params)
else:
# call instance method directly
try:
func = resolve_dotted_attribute(
self.instance,
method,
self.allow_dotted_names
)
except AttributeError:
pass

if func is not None:
return func(*params)
pass
else:
if func is not None:
return func(*params)
raise Exception('method "%s" is not supported' % method)

if self.instance is not None:
if hasattr(self.instance, '_dispatch'):
# call the `_dispatch` method on the instance
return self.instance._dispatch(method, params)

# call the instance's method directly
try:
func = resolve_dotted_attribute(
self.instance,
method,
self.allow_dotted_names
)
except AttributeError:
pass
else:
if func is not None:
return func(*params)

raise Exception('method "%s" is not supported' % method)

class SimpleXMLRPCRequestHandler(BaseHTTPRequestHandler):
"""Simple XML-RPC request handler class.

Expand Down
4 changes: 4 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ Extension Modules
Library
-------

- bpo-29615: SimpleXMLRPCDispatcher no longer chains KeyError (or any other
exception) to exception(s) raised in the dispatched methods.
Patch by Petr Motejlek.

- bpo-7769: Method register_function() of xmlrpc.server.SimpleXMLRPCDispatcher
and its subclasses can now be used as a decorator.

Expand Down