0

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.)?

Alex
  • 41,580
  • 88
  • 260
  • 469

1 Answers1

1

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Sure, but the problem is I cannot use your suggestion on the client side, except I put ALL the code in a `try..except` clause to handle the message correctly. The other way would be to overwrite some method in `xmlrpclib.ServerProxy` which handles the printing out of `xmlrpc` fault. Maybe you know which method to use? – Alex Jan 24 '13 at 09:55
  • Ok, I guess I just need to overwrite the xmlrpclib.Fault method with your suggestion on the client side (or actually only the `__repr__` method.) – Alex Jan 24 '13 at 10:35
  • @Alex: Updated after a quick check of the `xmlrpclib` code. You have to use the `faultString` value. – Martijn Pieters Jan 24 '13 at 10:35
  • You seem to have forgotten the `.decode('string_escape')` functionality you had in the original answer. It should be added to have a valid answer to this question. – Alex Jan 24 '13 at 10:41
  • @Alex: That depends on the faultstring value itself; your post indicates that the fault string itself is *not* escaped, otherwise it would end up with *double* escapes when printing the fault `repr()`. Decoding the string in the replacement `repr()` wouldn't do anything in that case. – Martijn Pieters Jan 24 '13 at 10:44
  • The fault string is escaped, so using `self.faultString.decode('string_escape')` indeed works. But you should mention some of that in your answer, as the use of `decode('string_escape')` is crucial to the answer, I guess.. – Alex Jan 24 '13 at 10:54
  • @Alex: I tested throwing a `ValueError` with newlines on the server side, the client side `fault.faultString` is *not* escaped. You do *not* need to decode that. `print fault.faultString` works without decoding. I've applied the monkeypatch in my answer, then printed the fault directly (`print fault`) as well and the newlines work just fine. – Martijn Pieters Jan 24 '13 at 10:57