[PEAK] PyProtocols: adapting None

Marko Mikulicic marko at autistici.org
Thu Mar 30 11:07:34 EST 2006


Hello,

I'm new to PyProtocols and I fell in love with it.
However I stumbled across a problem trying to adapt generic objects  
to some protocol, passing through most of the basic objects and
adapting some special object type. The problem is that I want also to  
being able to adapt None using NO_ADAPTER_NEEDED,
or in other words, to be able to return None from an adapter factory,  
without interpreting the None as a failure to adapt.

I thought something like

class AdaptedNoneType(object):
	pass
AdaptedNone = AdaptedNoneType()

def NO_ADAPTER_NEEDED(obj):
	if obj is None:
	    return AdaptedNone

and then having in "def adapt" _conform and _adapt conditional branches:
....
            if result is AdaptedNone:
                 return None
             if result is not None:
                 return result
...


------ sample patch:
diff /tmp/PyProtocols/PyProtocols-1.0a0/protocols/adapters.py ./ 
adapters.py
9a10
 > from api import AdaptedNone
23a25,26
 >     if obj is None:
 > 	return AdaptedNone
diff /tmp/PyProtocols/PyProtocols-1.0a0/protocols/api.py ./api.py
6c6
<     'declareAdapter', 'adviseObject',
---
 >     'declareAdapter', 'adviseObject','AdaptedNone',
9a10,13
 > class AdaptedNoneType(object):
 >     pass
 > AdaptedNone = AdaptedNoneType()
 >
41d44
<
59a63,64
 > 	    if result is AdaptedNone:
 > 		return None
71a77,78
 > 	    if result is AdaptedNone:
 > 		return None
80c87
<
---
 >

I removed the _speedup.so and applied this patch and it works.
It shouldn't be too difficult to modify the pyrex speedup, but I have  
no experience with it.

Is there interest in this kind of solution?

regards,
Marko Mikulicic



More information about the PEAK mailing list