def test_dotted_attribute(self):
# Raises an AttributeError because private methods are not allowed.
self.assertRaises(AttributeError,
SimpleXMLRPCServer.resolve_dotted_attribute, str, '__add')
self.assertTrue(SimpleXMLRPCServer.resolve_dotted_attribute(str, 'title'))
# Get the test to run faster by sending a request with test_simple1.
# This avoids waiting for the socket timeout.
self.test_simple1()
python类resolve_dotted_attribute()的实例源码
def test_dotted_attribute(self):
# Raises an AttributeError because private methods are not allowed.
self.assertRaises(AttributeError,
SimpleXMLRPCServer.resolve_dotted_attribute, str, '__add')
self.assertTrue(SimpleXMLRPCServer.resolve_dotted_attribute(str, 'title'))
# Get the test to run faster by sending a request with test_simple1.
# This avoids waiting for the socket timeout.
self.test_simple1()
def _dispatch(self, method, params):
func = None
try:
func = self.funcs[method]
except KeyError:
if self.instance is not None:
if hasattr(self.instance, '_dispatch'):
return self.instance._dispatch(method, params)
else:
try:
func = SimpleXMLRPCServer.resolve_dotted_attribute(
self.instance,
method,
True
)
except AttributeError:
pass
if func is not None:
try:
if isinstance(params, types.ListType):
response = func(*params)
else:
response = func(**params)
return response
# except TypeError:
# return Fault(-32602, 'Invalid parameters.')
except:
err_lines = traceback.format_exc().splitlines()
trace_string = '%s | %s' % (err_lines[-3], err_lines[-1])
fault = jsonrpclib.Fault(-32603, 'Server error: %s' %
trace_string)
return fault
else:
return Fault(-32601, 'Method %s not supported.' % method)
def test_dotted_attribute(self):
# Raises an AttributeError because private methods are not allowed.
self.assertRaises(AttributeError,
SimpleXMLRPCServer.resolve_dotted_attribute, str, '__add')
self.assertTrue(SimpleXMLRPCServer.resolve_dotted_attribute(str, 'title'))
# Get the test to run faster by sending a request with test_simple1.
# This avoids waiting for the socket timeout.
self.test_simple1()
def test_dotted_attribute(self):
# Raises an AttributeError because private methods are not allowed.
self.assertRaises(AttributeError,
SimpleXMLRPCServer.resolve_dotted_attribute, str, '__add')
self.assertTrue(SimpleXMLRPCServer.resolve_dotted_attribute(str, 'title'))
# Get the test to run faster by sending a request with test_simple1.
# This avoids waiting for the socket timeout.
self.test_simple1()
def _dispatch(self, method, params, config=None):
"""
Default method resolver and caller
:param method: Name of the method to call
:param params: List of arguments to give to the method
:param config: Request-specific configuration
:return: The result of the method
"""
config = config or self.json_config
func = None
try:
# Look into registered methods
func = self.funcs[method]
except KeyError:
if self.instance is not None:
# Try with the registered instance
try:
# Instance has a custom dispatcher
return getattr(self.instance, '_dispatch')(method, params)
except AttributeError:
# Resolve the method name in the instance
try:
func = xmlrpcserver.resolve_dotted_attribute(
self.instance, method, True)
except AttributeError:
# Unknown method
pass
if func is not None:
try:
# Call the method
if isinstance(params, utils.ListType):
return func(*params)
else:
return func(**params)
except TypeError as ex:
# Maybe the parameters are wrong
fault = Fault(-32602, 'Invalid parameters: {0}'.format(ex),
config=config)
_logger.warning("Invalid call parameters: %s", fault)
return fault
except:
# Method exception
err_lines = traceback.format_exc().splitlines()
trace_string = '{0} | {1}'.format(err_lines[-3], err_lines[-1])
fault = Fault(-32603, 'Server error: {0}'.format(trace_string),
config=config)
_logger.exception("Server-side exception: %s", fault)
return fault
else:
# Unknown method
fault = Fault(-32601, 'Method {0} not supported.'.format(method),
config=config)
_logger.warning("Unknown method: %s", fault)
return fault
# ------------------------------------------------------------------------------