[XML-SIG] Marshalling

Paul Prescod paul@prescod.net
Fri, 18 Dec 1998 00:28:35 -0600


"A.M. Kuchling" wrote:
> 
>        I'd be interested in seeing what people think of
> xml.marshal.generic; does its structure seem easily amenable to
> further subclassing to implement other data serializers?  Also, does
> anyone know of other DTDs for data serialization?  I'd like to take a
> crack at implementing them all, and seeing if they're all fairly clean 
> to implement.

It looks like you've put a lot of thought into it, so please forgive my
random, partially thought-out questions:

 * why have a single class for marshalling and unmarshalling?

 * this stuff is a little weird: "m = self.__class__()" Could we put all
of the mutable data in a separate class and avoid it? Maybe I'm just
skittish about strange idioms...

 * Could m_unimplemented be called by default for unhandled classes?

 * Maybe string handling should be safer...i.e. control characters

User defined types issues:

 1. What do we do about instances? I suggest looping over data-properties
and saving them as named structs. The names should be unique URIs.

 2. what do we do about built-in types (i.e. complex)? I suggest using
copy_reg to deconstruct ... and using URI-named structs again.

 3. pickle uses various magic methods: __reduce__, __getinitargs__,
__getstate__. Should XML marshalling support some or all of that stuff?

My modest contribution is the following code which handles the mapping
from URIs to types and also registers types with copy_reg .

"""Type_reg.py

Type registry -- mapping from URLs to builders and decomposers.
"""
import copy_reg

registry={}

def register( url, type, pickle_function, constructor ):
	copy_reg.pickle( type, pickle_function, constructor )
	registry[url]=type, constructor

def rebuild( url, args ):
	type, cons = registry[url]
	return apply( cons, args  )

def decompose( obj ):
	pickle_function = copy_reg.dispatch_table[type( obj )]
	return pickle_function( obj )[1]

register( "http://www.python.org/doc/ref/types.html#complex", 
	type( 1j ), copy_reg.pickle_complex, complex )

### Todo: register various date/time types

--

 Paul Prescod  - ISOGEN Consulting Engineer speaking for only himself
 http://itrc.uwaterloo.ca/~papresco

"Sports utility vehicles are gated communities on wheels" - Anon