Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added python3 support, please review #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 23 additions & 6 deletions pyramid_xmlrpc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import xmlrpclib
from pyramid.compat import PY3

if PY3:
import xmlrpc.client
else:
import xmlrpclib
import webob

def xmlrpc_marshal(data):
""" Marshal a Python data structure into an XML document suitable
for use as an XML-RPC response and return the document. If
``data`` is an ``xmlrpclib.Fault`` instance, it will be marshalled
into a suitable XML-RPC fault response."""
if isinstance(data, xmlrpclib.Fault):
return xmlrpclib.dumps(data)
if PY3:
if isinstance(data, xmlrpc.client.Fault):
return xmlrpc.client.dumps(data)
else:
return xmlrpc.client.dumps((data,), methodresponse=True)
else:
return xmlrpclib.dumps((data,), methodresponse=True)
if isinstance(data, xmlrpclib.Fault):
return xmlrpclib.dumps(data)
else:
return xmlrpclib.dumps((data,), methodresponse=True)

def xmlrpc_response(data):
""" Marshal a Python data structure into a webob ``Response``
Expand All @@ -19,7 +30,10 @@ def xmlrpc_response(data):
xml = xmlrpc_marshal(data)
response = webob.Response(xml)
response.content_type = 'text/xml'
response.content_length = len(xml)
if PY3:
pass
else:
response.content_length = len(xml)
return response

def parse_xmlrpc_request(request):
Expand All @@ -30,7 +44,10 @@ def parse_xmlrpc_request(request):
if request.content_length > (1 << 23):
# protect from DOS (> 8MB body)
raise ValueError('Body too large (%s bytes)' % request.content_length)
params, method = xmlrpclib.loads(request.body)
if PY3:
params, method = xmlrpc.client.loads(request.body)
else:
params, method = xmlrpclib.loads(request.body)
return params, method

def xmlrpc_view(wrapped):
Expand Down
102 changes: 77 additions & 25 deletions pyramid_xmlrpc/tests.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,78 @@
import unittest
from pyramid import testing
from pyramid.compat import PY3

class TestXMLRPCMarshal(unittest.TestCase):
def _callFUT(self, value):
from pyramid_xmlrpc import xmlrpc_marshal
return xmlrpc_marshal(value)

def test_xmlrpc_marshal_normal(self):
data = 1
marshalled = self._callFUT(data)
import xmlrpclib
self.assertEqual(marshalled, xmlrpclib.dumps((data,),
methodresponse=True))
if PY3:
import xmlrpc
self.assertEqual(marshalled,
xmlrpc.client.dumps((data,),
methodresponse=True))
else:
import xmlrpclib
self.assertEqual(marshalled,
xmlrpclib.dumps((data,),
methodresponse=True))

def test_xmlrpc_marshal_fault(self):
import xmlrpclib
fault = xmlrpclib.Fault(1, 'foo')
data = self._callFUT(fault)
self.assertEqual(data, xmlrpclib.dumps(fault))
if PY3:
import xmlrpc
fault = xmlrpc.client.Fault(1, 'foo')
data = self._callFUT(fault)
self.assertEqual(data, xmlrpc.client.dumps(fault))
else:
import xmlrpclib
fault = xmlrpclib.Fault(1, 'foo')
data = self._callFUT(fault)
self.assertEqual(data, xmlrpclib.dumps(fault))

class TestXMLRPResponse(unittest.TestCase):
def _callFUT(self, value):
from pyramid_xmlrpc import xmlrpc_response
return xmlrpc_response(value)

def test_xmlrpc_response(self):
import xmlrpclib
if PY3:
import xmlrpc.client
else:
import xmlrpclib
data = 1
response = self._callFUT(data)
self.assertEqual(response.content_type, 'text/xml')
self.assertEqual(response.body, xmlrpclib.dumps((1,),
methodresponse=True))
if PY3:
self.assertEqual(response.body,
bytes(xmlrpc.client.dumps((1,),
methodresponse=True),
encoding='utf-8'))
else:
self.assertEqual(response.body,
xmlrpclib.dumps((1,),
methodresponse=True))

self.assertEqual(response.content_length, len(response.body))
self.assertEqual(response.status, '200 OK')

class TestParseXMLRPCRequest(unittest.TestCase):
def _callFUT(self, request):
from pyramid_xmlrpc import parse_xmlrpc_request
return parse_xmlrpc_request(request)

def test_normal(self):
import xmlrpclib
param = 1
packet = xmlrpclib.dumps((param,), methodname='__call__')
if PY3:
import xmlrpc.client
param = 1
packet = xmlrpc.client.dumps((param,), methodname='__call__')
else:
import xmlrpclib
param = 1
packet = xmlrpclib.dumps((param,), methodname='__call__')
request = testing.DummyRequest()
request.body = packet
request.content_length = len(packet)
Expand All @@ -69,28 +99,43 @@ def unwrapped(context, what):
context = testing.DummyModel()
request = testing.DummyRequest()
param = 'what'
import xmlrpclib
packet = xmlrpclib.dumps((param,), methodname='__call__')
if PY3:
import xmlrpc.client
packet = xmlrpc.client.dumps((param,), methodname='__call__')
else:
import xmlrpclib
packet = xmlrpclib.dumps((param,), methodname='__call__')
request = testing.DummyRequest()
request.body = packet
request.content_length = len(packet)
response = wrapped(context, request)
self.assertEqual(response.body, xmlrpclib.dumps((param,),
methodresponse=True))
if PY3:
self.assertEqual(response.body,
bytes(xmlrpc.client.dumps((param,),
methodresponse=True),
encoding='utf-8'))
else:
self.assertEqual(response.body,
xmlrpclib.dumps((param,),
methodresponse=True))

class TestBaseClass(unittest.TestCase):

def test_normal(self):

from pyramid_xmlrpc import XMLRPCView
class Test(XMLRPCView):
def a_method(self,param):
return param

# set up a request
param = 'what'
import xmlrpclib
packet = xmlrpclib.dumps((param,), methodname='a_method')
if PY3:
import xmlrpc.client
packet = xmlrpc.client.dumps((param,), methodname='a_method')
else:
import xmlrpclib
packet = xmlrpclib.dumps((param,), methodname='a_method')
request = testing.DummyRequest()
request.body = packet
request.content_length = len(packet)
Expand All @@ -105,5 +150,12 @@ def a_method(self,param):

# exercise it
response = instance()
self.assertEqual(response.body, xmlrpclib.dumps((param,),
methodresponse=True))
if PY3:
self.assertEqual(response.body,
bytes(xmlrpc.client.dumps((param,),
methodresponse=True, ),
encoding='utf-8'))
else:
self.assertEqual(response.body,
xmlrpclib.dumps((param,),
methodresponse=True))