Index: Doc/lib/libxmlrpclib.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libxmlrpclib.tex,v retrieving revision 1.21 diff -u -d -r1.21 libxmlrpclib.tex --- Doc/lib/libxmlrpclib.tex 19 Jan 2005 03:39:16 -0000 1.21 +++ Doc/lib/libxmlrpclib.tex 10 Feb 2005 21:07:24 -0000 @@ -19,7 +19,7 @@ \begin{classdesc}{ServerProxy}{uri\optional{, transport\optional{, encoding\optional{, verbose\optional{, - allow_none}}}}} + allow_none\optional{, use_datetime}}}}}} A \class{ServerProxy} instance is an object that manages communication with a remote XML-RPC server. The required first argument is a URI (Uniform Resource Indicator), and will normally be the URL of the @@ -33,6 +33,9 @@ This is a commonly-used extension to the XML-RPC specification, but isn't supported by all clients and servers; see \url{http://ontosys.com/xml-rpc/extensions.html} for a description. +The \var{use_datetime} flag can be used to cause date/time values to be +presented as \class{\refmodule{datetime}.datetime} objects; this is false +by default. Both the HTTP and HTTPS transports support the URL syntax extension for HTTP Basic Authentication: \code{http://user:pass@host:port/path}. The @@ -62,8 +65,8 @@ elements. Arrays are returned as lists} \lineii{structures}{A Python dictionary. Keys must be strings, values may be any conformable type.} - \lineii{dates}{in seconds since the epoch; pass in an instance of the - \class{DateTime} wrapper class} + \lineii{dates}{in seconds since the epoch (pass in an instance of the + \class{DateTime} class) or as \class{datetime} instances} \lineii{binary data}{pass in an instance of the \class{Binary} wrapper class} \end{tableii} @@ -87,6 +90,7 @@ \class{Server} is retained as an alias for \class{ServerProxy} for backwards compatibility. New code should use \class{ServerProxy}. +\versionchanged[The \var{use_datetime} flag was added]{2.5} \end{classdesc} @@ -96,7 +100,7 @@ client software in several languages. Contains pretty much everything an XML-RPC client developer needs to know.} \seetitle[http://xmlrpc-c.sourceforge.net/hacks.php] - {XML-RPC-Hacks page}{Extensions for various open-source + {XML-RPC Hacks page}{Extensions for various open-source libraries to support introspection and multicall.} \end{seealso} @@ -149,7 +153,8 @@ Introspection methods are currently supported by servers written in PHP, C and Microsoft .NET. Partial introspection support is included in recent updates to UserLand Frontier. Introspection support for -Perl, Python and Java is available at the XML-RPC Hacks page. +Perl, Python and Java is available at the \ulink{XML-RPC +Hacks}{http://xmlrpc-c.sourceforge.net/hacks.php} page. \subsection{Boolean Objects \label{boolean-objects}} @@ -170,8 +175,9 @@ \subsection{DateTime Objects \label{datetime-objects}} -This class may be initialized with seconds since the epoch, a -time tuple, or an ISO 8601 time/date string. It has the following +This class may be initialized with seconds since the epoch, a time +tuple, an ISO 8601 time/date string or a +{}\class{\refmodule{datetime}.datetime} instance. It has the following methods, supported mainly for internal use by the marshalling/unmarshalling code: @@ -180,11 +186,12 @@ \end{methoddesc} \begin{methoddesc}{encode}{out} -Write the XML-RPC encoding of this DateTime item to the out stream object. +Write the XML-RPC encoding of this \class{DateTime} item to the +\var{out} stream object. \end{methoddesc} It also supports certain of Python's built-in operators through -\method{__cmp__} and \method{__repr__} methods. +\method{__cmp__()} and \method{__repr__()} methods. \subsection{Binary Objects \label{binary-objects}} @@ -296,7 +303,6 @@ \begin{funcdesc}{dumps}{params\optional{, methodname\optional{, methodresponse\optional{, encoding\optional{, allow_none}}}}} - Convert \var{params} into an XML-RPC request. or into a response if \var{methodresponse} is true. \var{params} can be either a tuple of arguments or an instance of the @@ -308,12 +314,17 @@ provide a true value for \var{allow_none}. \end{funcdesc} -\begin{funcdesc}{loads}{data} +\begin{funcdesc}{loads}{data\optional{, use_datetime}} Convert an XML-RPC request or response into Python objects, a \code{(\var{params}, \var{methodname})}. \var{params} is a tuple of argument; \var{methodname} is a string, or \code{None} if no method name is present in the packet. If the XML-RPC packet represents a fault condition, this function will raise a \exception{Fault} exception. +The \var{use_datetime} flag can be used to cause date/time values to be +presented as \class{\refmodule{datetime}.datetime} objects; this is false +by default. + +\versionchanged[The \var{use_datetime} flag was added]{2.5} \end{funcdesc} Index: Lib/xmlrpclib.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/xmlrpclib.py,v retrieving revision 1.39 diff -u -d -r1.39 xmlrpclib.py --- Lib/xmlrpclib.py 10 Feb 2005 18:33:29 -0000 1.39 +++ Lib/xmlrpclib.py 10 Feb 2005 21:07:25 -0000 @@ -394,6 +394,10 @@ value.decode(data) return value +def _datetime_type(data): + t = time.strptime(data, "%Y%m%dT%H:%M:%S") + return datetime.datetime(*tuple(t)[:6]) + ## # Wrapper for binary data. This can be used to transport any kind # of binary data over XML-RPC, using BASE64 encoding. @@ -742,7 +746,7 @@ # and again, if you don't understand what's going on in here, # that's perfectly ok. - def __init__(self): + def __init__(self, use_datetime=0): self._type = None self._stack = [] self._marks = [] @@ -750,6 +754,9 @@ self._methodname = None self._encoding = "utf-8" self.append = self._stack.append + self._use_datetime = use_datetime + if use_datetime and not datetime: + raise ValueError, "the datetime module is not available" def close(self): # return response tuple and target method @@ -867,6 +874,8 @@ def end_dateTime(self, data): value = DateTime() value.decode(data) + if self._use_datetime: + value = _datetime_type(data) self.append(value) dispatch["dateTime.iso8601"] = end_dateTime @@ -968,17 +977,23 @@ # # return A (parser, unmarshaller) tuple. -def getparser(): +def getparser(use_datetime=0): """getparser() -> parser, unmarshaller Create an instance of the fastest available parser, and attach it to an unmarshalling object. Return both objects. """ + if use_datetime and not datetime: + raise ValueError, "the datetime module is not available" if FastParser and FastUnmarshaller: - target = FastUnmarshaller(True, False, _binary, _datetime, Fault) + if use_datetime: + mkdatetime = _datetime_type + else: + mkdatetime = _datetime + target = FastUnmarshaller(True, False, _binary, mkdatetime, Fault) parser = FastParser(target) else: - target = Unmarshaller() + target = Unmarshaller(use_datetime=use_datetime) if FastParser: parser = FastParser(target) elif SgmlopParser: @@ -1081,7 +1096,7 @@ # (None if not present). # @see Fault -def loads(data): +def loads(data, use_datetime=0): """data -> unmarshalled data, method name Convert an XML-RPC packet to unmarshalled data plus a method @@ -1090,7 +1105,7 @@ If the XML-RPC packet represents a fault condition, this function raises a Fault exception. """ - p, u = getparser() + p, u = getparser(use_datetime=use_datetime) p.feed(data) p.close() return u.close(), u.getmethodname() @@ -1122,6 +1137,9 @@ # client identifier (may be overridden) user_agent = "xmlrpclib.py/%s (by www.pythonware.com)" % __version__ + def __init__(self, use_datetime=0): + self._use_datetime = use_datetime + ## # Send a complete request, and parse the response. # @@ -1168,7 +1186,7 @@ def getparser(self): # get parser and unmarshaller - return getparser() + return getparser(use_datetime=self._use_datetime) ## # Get authorization info from host parameter @@ -1362,7 +1380,7 @@ """ def __init__(self, uri, transport=None, encoding=None, verbose=0, - allow_none=0): + allow_none=0, use_datetime=0): # establish a "logical" server connection # get the url @@ -1376,9 +1394,9 @@ if transport is None: if type == "https": - transport = SafeTransport() + transport = SafeTransport(use_datetime=use_datetime) else: - transport = Transport() + transport = Transport(use_datetime=use_datetime) self.__transport = transport self.__encoding = encoding Index: Lib/test/test_xmlrpc.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_xmlrpc.py,v retrieving revision 1.6 diff -u -d -r1.6 test_xmlrpc.py --- Lib/test/test_xmlrpc.py 10 Feb 2005 18:33:30 -0000 1.6 +++ Lib/test/test_xmlrpc.py 10 Feb 2005 21:07:25 -0000 @@ -37,6 +37,15 @@ self.assertEquals(r, (xmlrpclib.DateTime('20050210T11:41:23'),)) self.assertEquals(m, None) + def test_unmarshal_datetime_time(self): + dt = datetime.datetime(2005, 02, 10, 11, 41, 23) + data = """ + 20050210T11:41:23 + """ + r, m = xmlrpclib.loads(data, use_datetime=True) + self.assertEquals(r, (dt,)) + self.assertEquals(m, None) + def test_dump_big_long(self): self.assertRaises(OverflowError, xmlrpclib.dumps, (2L**99,))