[Zope3-checkins] SVN: Zope3/trunk/src/zope/ - fixed issue 634: use adapters instead of dict lookup for premarshalling

Christian Theune cvs-admin at zope.org
Sat Jun 17 20:30:29 EDT 2006


Log message for revision 68728:
   - fixed issue 634: use adapters instead of dict lookup for premarshalling
  

Changed:
  U   Zope3/trunk/src/zope/app/configure.zcml
  A   Zope3/trunk/src/zope/publisher/configure.zcml
  U   Zope3/trunk/src/zope/publisher/interfaces/xmlrpc.py
  U   Zope3/trunk/src/zope/publisher/tests/test_xmlrpc.py
  U   Zope3/trunk/src/zope/publisher/xmlrpc.py
  A   Zope3/trunk/src/zope/publisher/xmlrpc.txt

-=-
Modified: Zope3/trunk/src/zope/app/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/configure.zcml	2006-06-18 00:30:19 UTC (rev 68727)
+++ Zope3/trunk/src/zope/app/configure.zcml	2006-06-18 00:30:21 UTC (rev 68728)
@@ -21,6 +21,7 @@
   <include package="zope.annotation" />
   <include package="zope.app.dependable" />
   <include package="zope.app.content" />
+  <include package="zope.publisher" />
 
   <include file="menus.zcml" />
 

Added: Zope3/trunk/src/zope/publisher/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/publisher/configure.zcml	2006-06-18 00:30:19 UTC (rev 68727)
+++ Zope3/trunk/src/zope/publisher/configure.zcml	2006-06-18 00:30:21 UTC (rev 68728)
@@ -0,0 +1,28 @@
+<configure xmlns="http://namespaces.zope.org/zope">
+
+  <adapter
+      for="list"
+      factory=".xmlrpc.ListPreMarshaller"
+      />
+
+  <adapter
+      for="tuple"
+      factory=".xmlrpc.ListPreMarshaller"
+      />
+
+  <adapter
+      factory=".xmlrpc.FaultPreMarshaller"
+      for="xmlrpclib.Fault"
+      />
+
+  <adapter
+      factory=".xmlrpc.DateTimePreMarshaller"
+      for="xmlrpclib.DateTime"
+      />
+
+  <adapter
+      factory=".xmlrpc.DictPreMarshaller"
+      for="dict"
+      />
+
+</configure>


Property changes on: Zope3/trunk/src/zope/publisher/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: Zope3/trunk/src/zope/publisher/interfaces/xmlrpc.py
===================================================================
--- Zope3/trunk/src/zope/publisher/interfaces/xmlrpc.py	2006-06-18 00:30:19 UTC (rev 68727)
+++ Zope3/trunk/src/zope/publisher/interfaces/xmlrpc.py	2006-06-18 00:30:21 UTC (rev 68728)
@@ -18,6 +18,8 @@
 
 __docformat__ = "reStructuredText"
 
+from zope.interface import Interface
+
 from zope.publisher.interfaces import IPublication
 from zope.publisher.interfaces import IPublishTraverse
 from zope.publisher.interfaces.http import IHTTPRequest
@@ -40,3 +42,12 @@
 class IXMLRPCRequest(IHTTPRequest):
     """XML-RPC Request
     """
+
+class IXMLRPCPremarshaller(Interface):
+    """Pre-Marshaller to remove proxies for xmlrpclib"""
+
+    def __call__(self):
+        """Return the given object without proxies."""
+
+
+

Modified: Zope3/trunk/src/zope/publisher/tests/test_xmlrpc.py
===================================================================
--- Zope3/trunk/src/zope/publisher/tests/test_xmlrpc.py	2006-06-18 00:30:19 UTC (rev 68727)
+++ Zope3/trunk/src/zope/publisher/tests/test_xmlrpc.py	2006-06-18 00:30:21 UTC (rev 68728)
@@ -15,17 +15,16 @@
 
 $Id$
 """
-import unittest
-from zope.testing.doctest import DocTestSuite
-from zope.testing.cleanup import CleanUp
 
+import zope.app.testing.functional
 
-def test_suite():
-    return unittest.TestSuite((
-        DocTestSuite('zope.publisher.xmlrpc',
-                     tearDown=lambda ignored=None: CleanUp().cleanUp()),
-        ))
+from zope.testing import doctest
 
-if __name__ == '__main__':
-    unittest.main(defaultTest='test_suite')
 
+def test_suite():
+    suite = doctest.DocFileSuite( 
+        "xmlrpc.txt",
+        module_relative=True, package="zope.publisher",
+        optionflags=doctest.ELLIPSIS)
+    suite.layer = zope.app.testing.functional.Functional
+    return suite

Modified: Zope3/trunk/src/zope/publisher/xmlrpc.py
===================================================================
--- Zope3/trunk/src/zope/publisher/xmlrpc.py	2006-06-18 00:30:19 UTC (rev 68727)
+++ Zope3/trunk/src/zope/publisher/xmlrpc.py	2006-06-18 00:30:21 UTC (rev 68728)
@@ -21,9 +21,11 @@
 import xmlrpclib
 from StringIO import StringIO
 
+import zope.component
+import zope.interface
 from zope.interface import implements
-from zope.publisher.interfaces.xmlrpc import IXMLRPCPublisher
-from zope.publisher.interfaces.xmlrpc import IXMLRPCRequest
+from zope.publisher.interfaces.xmlrpc import \
+        IXMLRPCPublisher, IXMLRPCRequest, IXMLRPCPremarshaller
 
 from zope.publisher.http import HTTPRequest, HTTPResponse, DirectResult
 
@@ -159,86 +161,52 @@
         # XML-RPC prefers a status of 200 ("ok") even when reporting errors.
         self.setStatus(200)
 
+class PreMarshallerBase(object):
+    """Abstract base class for pre-marshallers."""
+    zope.interface.implements(IXMLRPCPremarshaller)
 
-def premarshal_dict(data):
-    return dict([(premarshal(k), premarshal(v))
-                 for (k, v) in data.items()])
+    def __init__(self, data):
+        self.data = data
 
-def premarshal_list(data):
-    return map(premarshal, data)
+    def __call__(self):
+        raise Exception, "Not implemented"
 
-def premarshal_fault(data):
-    return xmlrpclib.Fault(
-        premarshal(data.faultCode),
-        premarshal(data.faultString),
-        )
+class DictPreMarshaller(PreMarshallerBase):
+    """Pre-marshaller for dicts"""
 
-def premarshal_datetime(data):
-    return xmlrpclib.DateTime(data.value)
+    def __call__(self):
+        return dict([(premarshal(k), premarshal(v))
+                     for (k, v) in self.data.items()])
 
-premarshal_dispatch_table = {
-    dict: premarshal_dict,
-    list: premarshal_list,
-    tuple: premarshal_list,
-    xmlrpclib.Fault: premarshal_fault,
-    xmlrpclib.DateTime: premarshal_datetime,
-    }
-premarshal_dispatch = premarshal_dispatch_table.get
+class ListPreMarshaller(PreMarshallerBase):
+    """Pre-marshaller for list"""
 
-def premarshal(data):
-    """Premarshal data before handing it to xmlrpclib for marhalling
+    def __call__(self):
+        return map(premarshal, self.data)
 
-    The initial purpose of this function is to remove security proxies
-    without resorting to removeSecurityProxy.   This way, we can avoid
-    inadvertently providing access to data that should be protected.
+class FaultPreMarshaller(PreMarshallerBase):
+    """Pre-marshaller for xmlrpc.Fault"""
 
-    Suppose we have a sample data structure:
+    def __call__(self):
+        return xmlrpclib.Fault(
+            premarshal(self.data.faultCode),
+            premarshal(self.data.faultString),
+            )
 
-      >>> sample = {'foo': (1, ['x', 'y', 1.2])}
+class DateTimePreMarshaller(PreMarshallerBase):
+    """Pre-marshaller for xmlrpc.DateTime"""
 
-    if we put the sample in a security proxy:
+    def __call__(self):
+        return xmlrpclib.DateTime(self.data.value)
 
-      >>> from zope.security.checker import ProxyFactory
-      >>> proxied_sample = ProxyFactory(sample)
+def premarshal(data):
+    """Premarshal data before handing it to xmlrpclib for marhalling
 
-    We can still get to the data, but the non-rock data is proxied:
-
-      >>> from zope.security.proxy import Proxy
-      >>> proxied_sample['foo']
-      (1, ['x', 'y', 1.2])
-
-      >>> type(proxied_sample['foo']) is Proxy
-      True
-      >>> type(proxied_sample['foo'][1]) is Proxy
-      True
-
-    But we can strip the proxies using premarshal:
-
-      >>> stripped = premarshal(proxied_sample)
-      >>> stripped
-      {'foo': [1, ['x', 'y', 1.2]]}
-
-      >>> type(stripped['foo']) is Proxy
-      False
-      >>> type(stripped['foo'][1]) is Proxy
-      False
-
-    So xmlrpclib will be happy. :)
-
-    We can also use premarshal to strip proxies off of Fault objects.
-    We have to make a security declaration first though:
-
-      >>> from zope.security.checker import NamesChecker, defineChecker
-      >>> defineChecker(xmlrpclib.Fault,
-      ...               NamesChecker(['faultCode', 'faultString']))
-
-      >>> fault = xmlrpclib.Fault(1, 'waaa')
-      >>> proxied_fault = ProxyFactory(fault)
-      >>> stripped_fault = premarshal(proxied_fault)
-      >>> type(stripped_fault) is Proxy
-      False
+    The initial purpose of this function is to remove security proxies
+    without resorting to removeSecurityProxy.   This way, we can avoid
+    inadvertently providing access to data that should be protected.
     """
-    premarshaller = premarshal_dispatch(data.__class__)
+    premarshaller = IXMLRPCPremarshaller(data, alternate=None)
     if premarshaller is not None:
-        return premarshaller(data)
+        return premarshaller()
     return data

Added: Zope3/trunk/src/zope/publisher/xmlrpc.txt
===================================================================
--- Zope3/trunk/src/zope/publisher/xmlrpc.txt	2006-06-18 00:30:19 UTC (rev 68727)
+++ Zope3/trunk/src/zope/publisher/xmlrpc.txt	2006-06-18 00:30:21 UTC (rev 68728)
@@ -0,0 +1,50 @@
+================
+XMLRPC publisher
+================
+
+Pre-marshal / Proxy removal
+===========================
+
+  >>> sample = {'foo': (1, ['x', 'y', 1.2])}
+
+if we put the sample in a security proxy:
+
+  >>> from zope.security.checker import ProxyFactory
+  >>> proxied_sample = ProxyFactory(sample)
+
+We can still get to the data, but the non-rock data is proxied:
+
+  >>> from zope.security.proxy import Proxy
+  >>> proxied_sample['foo']
+  (1, ['x', 'y', 1.2])
+
+  >>> type(proxied_sample['foo']) is Proxy
+  True
+  >>> type(proxied_sample['foo'][1]) is Proxy
+  True
+
+But we can strip the proxies using premarshal:
+
+  >>> from zope.publisher.xmlrpc import premarshal
+
+  >>> stripped = premarshal(proxied_sample)
+  >>> stripped
+  {'foo': [1, ['x', 'y', 1.2]]}
+
+  >>> type(stripped['foo']) is Proxy
+  False
+  >>> type(stripped['foo'][1]) is Proxy
+  False
+
+So xmlrpclib will be happy. :)
+
+We can also use premarshal to strip proxies off of Fault objects.
+We have to make a security declaration first though:
+
+  >>> import xmlrpclib
+  >>> fault = xmlrpclib.Fault(1, 'waaa')
+  >>> proxied_fault = ProxyFactory(fault)
+  >>> stripped_fault = premarshal(proxied_fault)
+  >>> type(stripped_fault) is Proxy
+  False
+


Property changes on: Zope3/trunk/src/zope/publisher/xmlrpc.txt
___________________________________________________________________
Name: svn:keywords
   + Id Rev Date
Name: svn:eol-style
   + native



More information about the Zope3-Checkins mailing list