Is there a way to use a python encoding so xmlrpc fault messages are shown on the client side with correct CR?

StackOverflow https://stackoverflow.com/questions/14497395

سؤال

On the server side of a xmlrpc server in python I have the following line of code within a function overwriting SimpleXMLRPCServer._marshaled_dispatch:

response = xmlrpclib.dumps(
            xmlrpclib.Fault(1, "some error\nnext line\n"),
            encoding=self.encoding, allow_none=self.allow_none)

to create some custom error/fault message to be show on the client side. However, this code will show something like the following on the client side

xmlrpclib.Fault: <Fault 1: "some error\nnext line\n">

whereas I want to have something like

xmlrpclib.Fault: <Fault 1: "some error
next line
">

i.e. where the newline character is actually 'used' and not printed.

Any ideas I can accomplish this (per server side, i.e. modification of the line just shown above, and without using a third party package.)?

هل كانت مفيدة؟

المحلول

You are looking at the representation of the Fault object; the string message itself is contained in the .faultString attribute:

print fault.faultString

The __repr__ of the Fault class otherwise represents that value using repr(); you cannot get around that without changing the xmlrpclib.Fault class itself (by replacing it's __repr__ method or adding a __str__ method to it).

You could monkey patch that into the class:

from xmlrpclib import Fault

def fault_repr(self):
    return "<Fault %s: %s>" % (self.faultCode, self.faultString)

Fault.__repr__ = fault_repr
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top