Re: Pyro 4.20 serpent serializer issues
Irmen de Jong <[email protected]>
| Newsgroups | gmane.comp.python.pyro |
|---|---|
| Message-ID | <[email protected]> |
On 11-6-2013 15:01, Éric Piel wrote:
> On 09/06/13 01:38, Irmen de Jong wrote:
> :
>> Decimals are encoded as a literal string (because otherwise you will lose precision).
>> The receiver however needs to 'know' this and will have to convert the string back to a
>> decimal.Decimal. Similarly, datetime.datetime are encoded as a ISO formatted date+time
>> string, and the receiver will have to convert it back explicitly (if desired).
> :
>> The key issue here is that when not using pickle, you probably will have to redesign
>> your remote method contracts if they depend a lot on custom types on both ends. If you
>> stick to Python literals mainly, the pain will be a lot less or even non-existent...
>
> Hi Irmen,
> Just to be sure I understand correctly, does this mean that in Pyro 4.20
> (with the default serializer), a method returning a decimal.Decimal will
> return a string when called remotely via Pyro?
Yep, that's right.
The string form makes sure no precision is lost. You can convert it back to a Decimal if
you need (but yeah, that will have to be an explicit step in your code).
The new serializer (serpent), and the json one even more so, trade expressive power for
security (and human readability, if that matters). You gain some, you loose some.
You can check it out by enabling LOGWIRE and then looking in the log to see what Pyro
puts on the wire. Or if you're interested in serpent specifically, you can check it out
directly by playing with serpent.dumps().
If you don't like the way serpent serializes certain types, you can register your own
custom hooks (Pyro already does this, for a few types). Example:
>>> import serpent
>>> def serialize_decimal(obj, serializer, stream, indent):
... serializer._serialize("decimal.Decimal('%s')" % obj, stream, indent)
...
>>> serpent.register_class(decimal.Decimal, serialize_decimal)
>>> print serpent.dumps(decimal.Decimal("1234.56789"))
# serpent utf-8 python3.2
"decimal.Decimal('1234.56789')"
>>>
Ofcourse, this specific example can no longer be deserialized by serpent itself: the
serialized data is no longer a python literal expression.
For security reasons (again), Pyro only converts a limited set of well known types back
into actual objects of that type. It is not possible (yet?) to extend that mechanism.
(This doesn't apply to pickle)
If you find that you loose to much expressive power by using serpent or one of the other
serializers, you can always switch back to pickle. But you'll then have to deal with its
security problems.
Cheers
Irmen
------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:
Build for Windows Store.
http://p.sf.net/sfu/windows-dev2dev