Visitar URL original
bpo-7769: enable xmlrpc.server.SimpleXMLRPCDispatcher.register_function used as decorator by zhangyangyu · Pull Request #231 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
make SimpleXMLRPCServer.SimpleXMLRPCServer.register_function as decor…
…ator
  • Loading branch information
zhangyangyu committed Feb 22, 2017
commit 87fef8d05d1d24b995daa41b980e95e879d54763
61 changes: 54 additions & 7 deletions Doc/library/xmlrpc.server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,22 @@ The :class:`SimpleXMLRPCServer` class is based on
alone XML-RPC servers.


.. method:: SimpleXMLRPCServer.register_function(function, name=None)
.. method:: SimpleXMLRPCServer.register_function(function=None, name=None)

Register a function that can respond to XML-RPC requests. If *name* is given,
it will be the method name associated with *function*, otherwise
``function.__name__`` will be used. *name* can be either a normal or Unicode

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"a normal or Unicode string" -- this is outdated in Python 3.

string, and may contain characters not legal in Python identifiers, including
the period character.

From version 3.7, this method can also be used as a decorator. When used as

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"From version 3.7" is not needed. There is already a versionchanged directive below.

a decorator, *name* can be given as a keyword-only argument to register
*function* under *name*. If no *name* is given, ``function.__name__`` will be
used.

.. versionadded:: 3.7
:meth:`register_function` can be used as a decorator.


.. method:: SimpleXMLRPCServer.register_instance(instance, allow_dotted_names=False)

Expand Down Expand Up @@ -185,6 +193,37 @@ server::
# Print list of available methods
print(s.system.listMethods())

Since version 3.7, :meth:`register_function` can also be used as a decorator. The

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Since version 3.7" is not needed.

previous server example can register functions in a decorator way::

from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I'd just merge two imports in one line.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to be consistent with previous examples. I don't want to change this alone. :-)


class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)

with SimpleXMLRPCServer(("localhost", 8000),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Use single quotes to be consistent with the rest of the example.

requestHandler=RequestHandler) as server:
server.register_introspection_functions()

# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name -> *name*?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it's not referring to the parameter here. So just leave it unchanged.

server.register_function(pow)

# Register a function under a different name, using
# register_function as a decorator. *name* can only be given
# as a keyword argument.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyword -> keyword-only?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "can only be given as a keyword argument" is same as "keyword-only argument". :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean there is already a shorter way to say the same thing in Python.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the term "keyword-only" is applicable only to parameters, not arguments. Parameters can be positional-only (can't be defined in Python syntax), positional-or-keyword or keyword-only. Arguments can be positional or keyword.

@zhangyangyu zhangyangyu Feb 28, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the sentence to “*name* is keyword-only". Since *name* is wrapped with asterisks, it's referring to a parameter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the name parameter is not keyword-only. It is positional-or-keyword.

The former wording looked correct to me, the changed wording don't.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:-(

@server.register_function(name='add')
def adder_function(x,y):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after comma.

return x + y

# Register a function under function.__name__.
@server.register_function
def mul(x, y):
return x * y

server.serve_forever()

The following example included in the :file:`Lib/xmlrpc/server.py` module shows
a server allowing dotted names and registering a multicall function.

Expand Down Expand Up @@ -252,18 +291,26 @@ This client which interacts with the demo XMLRPC server can be invoked as::
CGIXMLRPCRequestHandler
-----------------------

The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC
The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC
requests sent to Python CGI scripts.


.. method:: CGIXMLRPCRequestHandler.register_function(function, name=None)
.. method:: CGIXMLRPCRequestHandler.register_function(function=None, name=None)

Register a function that can respond to XML-RPC requests. If *name* is given,
it will be the method name associated with function, otherwise
*function.__name__* will be used. *name* can be either a normal or Unicode
string, and may contain characters not legal in Python identifiers, including
Register a function that can respond to XML-RPC requests. If *name* is given,
it will be the method name associated with *function*, otherwise
``function.__name__`` will be used. *name* can be either a normal or Unicode
string, and may contain characters not legal in Python identifiers, including
the period character.

From version 3.7, this method can also be used as a decorator. When used as

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"From version 3.7" is not needed.

a decorator, *name* can be given as a keyword-only argument to register
*function* under *name*. If no *name* is given, ``function.__name__`` will be
used.

.. versionadded:: 3.7
:meth:`register_function` can be used as a decorator.


.. method:: CGIXMLRPCRequestHandler.register_instance(instance)

Expand Down
13 changes: 7 additions & 6 deletions Lib/test/test_xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,10 +505,6 @@ class Fixture:
def getData():
return '42'

def my_function():
'''This is my function'''
return True

class MyXMLRPCServer(xmlrpc.server.SimpleXMLRPCServer):
def get_request(self):
# Ensure the socket is always non-blocking. On Linux, socket
Expand All @@ -535,9 +531,14 @@ def get_request(self):
serv.register_introspection_functions()
serv.register_multicall_functions()
serv.register_function(pow)
serv.register_function(lambda x,y: x+y, 'add')
serv.register_function(lambda x: x, 'têšt')
serv.register_function(my_function)
@serv.register_function
def my_function():
'''This is my function'''
return True
@serv.register_function(name='add')
def _(x, y):
return x + y
testInstance = TestInstanceClass()
serv.register_instance(testInstance, allow_dotted_names=True)
evt.set()
Expand Down
8 changes: 7 additions & 1 deletion Lib/xmlrpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def export_add(self, x, y):

from xmlrpc.client import Fault, dumps, loads, gzip_encode, gzip_decode
from http.server import BaseHTTPRequestHandler
from functools import partial
import http.server
import socketserver
import sys
Expand Down Expand Up @@ -204,17 +205,22 @@ def register_instance(self, instance, allow_dotted_names=False):
self.instance = instance
self.allow_dotted_names = allow_dotted_names

def register_function(self, function, name=None):
def register_function(self, function=None, name=None):
"""Registers a function to respond to XML-RPC requests.

The optional name argument can be used to set a Unicode name
for the function.
"""
# decorator factory
if function is None:
return partial(self.register_function, name=name)

if name is None:
name = function.__name__
self.funcs[name] = function

return function

def register_introspection_functions(self):
"""Registers the XML-RPC introspection methods in the system
namespace.
Expand Down