[PEAK] Default value lost in binding.Obtain()

Alexey Smishlayev alexey at xtech2.lv
Tue Sep 29 09:03:26 EDT 2015


Today, fiddling with the binding.Obtain() I noticed that the default 
value gets lost.

In my application, I have a component tree and I want an attribute to be 
defined in any component in that tree, so I created binding:

 >     callback = binding.Obtain("callbackFunction", default=lambda arg: 

So, in case that no component defines a callbackFunction, the lambda 
function would be used as a fallback.
Unfortunately, "default" option is ignored unless target name contains 
slashes (e.g. "/callbackFunction", "./callBackFunction", 

I went to the peak/binding/components.py trying to fix it and propose 
the attached patch.
The problem is that default value was not propagated to the 
acquireComponent() call, so an exception occurred.

Best regards,
Alexey Smishlayev
-------------- next part --------------
--- components.py	2015-09-29 15:50:55.000000000 +0300
+++ components.py	2015-09-29 15:52:11.000000000 +0300
@@ -378,7 +378,14 @@
         if pc:  ob = pc(component)
-        else:   ob = acquireComponent(component, attr)
+        else:
+            try:
+                ob = acquireComponent(component, attr)
+            except exceptions.NameNotFound, _exc:
+                if default is not NOT_GIVEN:
+                    return default
+                else:
+                    raise _exc
         resolved = []
         append = resolved.append

More information about the PEAK mailing list