base usage of xmlrpclib.Fault
I found two problem with usage of xlmrpclib.Fault:
- first, faultCode parameter of xmlrpclib.Fault must be an integer or at least
an ascii only string.
For example in xmlrpclib:
$ cat server.py
from SimpleXMLRPCServer import SimpleXMLRPCServer
server = SimpleXMLRPCServer(("localhost", 8001))
server.serve_forever()
$ cat client.py
import xmlrpclib
proxy = xmlrpclib.ServerProxy("http://localhost:8001/")
try:
proxy.add(2, 5)
except xmlrpclib.Fault, err:
print "A fault occurred - Fault code: %d" % err.faultCode, type(err.faultCode)
output of "python client.py":
A fault occurred - Fault code: 1 <type 'int'>
Moreover in xmlrpclib.Fault faultCode and faultString parameters are handled
differently (see
http://hg.python.org/cpython/file/8527427914a2/Lib/xmlrpclib.py#l269):
in xmlrpclib.Fault.__repr__ method "repr(faultString)" is used in order to avoid
encoding problem. repr builtin is not used with faultCode.
Here http://hg.tryton.org/tryton/file/acdc4008c2fb/tryton/jsonrpc.py#l304
faultCode is a string.
Second, in jsonrpc.Fault.__init__
http://hg.tryton.org/tryton/file/acdc4008c2fb/tryton/jsonrpc.py#l32 builtin str
is used with faultString: an encoding exception can occur.
I have submitted this patch http://codereview.tryton.org/213004 :
- discard custom implementation of Fault class: unicode error avoided in
jsonrpc.Fault.__init__ when faultString parameter is not ascii compliant
- use an integer for faultCode: unicode error avoided when repr(err) is called
when faultCode parameter is not ascii compliant. Another solution: "super(Fault,
self).__init__(repr(faultCode), faultString, **extra)"
- fix a typo s/excpected/expected/
A test (drop the database 'invalid_database_name_for_test' !):
from jsonrpc import ServerProxy
connection = ServerProxy("localhost", "8000")
db_name = "invalid_database_name_for_test"
tryton_admin_pass = "adminpass"
try:
connection.common.db.drop(None, None, db_name, tryton_admin_pass)
except Exception, err:
print err