mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
_dispatch(): Do no re-define the resolve_dotted_atttribute() function
every time this gets called; move it out as a global helper function. Simplify the call to the _dispatch() method of the registered instance.
This commit is contained in:
parent
599db7de63
commit
787fd8cdeb
1 changed files with 17 additions and 21 deletions
|
@ -147,22 +147,6 @@ class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||||
not be called by SimpleXMLRPCServer.
|
not be called by SimpleXMLRPCServer.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def resolve_dotted_attribute(obj, attr):
|
|
||||||
"""resolve_dotted_attribute(math, 'cos.__doc__') => math.cos.__doc__
|
|
||||||
|
|
||||||
Resolves a dotted attribute name to an object. Raises
|
|
||||||
an AttributeError if any attribute in the chain starts
|
|
||||||
with a '_'.
|
|
||||||
"""
|
|
||||||
for i in attr.split('.'):
|
|
||||||
if i.startswith('_'):
|
|
||||||
raise AttributeError(
|
|
||||||
'attempt to access private attribute "%s"' % i
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
obj = getattr(obj,i)
|
|
||||||
return obj
|
|
||||||
|
|
||||||
func = None
|
func = None
|
||||||
try:
|
try:
|
||||||
# check to see if a matching function has been registered
|
# check to see if a matching function has been registered
|
||||||
|
@ -171,14 +155,11 @@ class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||||
if self.server.instance is not None:
|
if self.server.instance is not None:
|
||||||
# check for a _dispatch method
|
# check for a _dispatch method
|
||||||
if hasattr(self.server.instance, '_dispatch'):
|
if hasattr(self.server.instance, '_dispatch'):
|
||||||
return apply(
|
return self.server.instance._dispatch(method, params)
|
||||||
getattr(self.server.instance,'_dispatch'),
|
|
||||||
(method, params)
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
# call instance method directly
|
# call instance method directly
|
||||||
try:
|
try:
|
||||||
func = resolve_dotted_attribute(
|
func = _resolve_dotted_attribute(
|
||||||
self.server.instance,
|
self.server.instance,
|
||||||
method
|
method
|
||||||
)
|
)
|
||||||
|
@ -196,6 +177,21 @@ class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||||
if self.server.logRequests:
|
if self.server.logRequests:
|
||||||
BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size)
|
BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size)
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_dotted_attribute(obj, attr):
|
||||||
|
"""Resolves a dotted attribute name to an object. Raises
|
||||||
|
an AttributeError if any attribute in the chain starts with a '_'.
|
||||||
|
"""
|
||||||
|
for i in attr.split('.'):
|
||||||
|
if i.startswith('_'):
|
||||||
|
raise AttributeError(
|
||||||
|
'attempt to access private attribute "%s"' % i
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
obj = getattr(obj,i)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
class SimpleXMLRPCServer(SocketServer.TCPServer):
|
class SimpleXMLRPCServer(SocketServer.TCPServer):
|
||||||
"""Simple XML-RPC server.
|
"""Simple XML-RPC server.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue