Re: Default value lost in binding.Obtain()

PJ Eby <pje-Wh6+Hckhi6HFNGf7iClzIwC/[email protected]> Sat, 31 Oct 2015 10:49:35 -0400
Newsgroups gmane.comp.python.peak
Message-ID <CALeMXf4M5x2QdUr2N3hVbKMP3DXEz6Rj8Op8DVdQzsBmii6gmw@mail.gmail.com>
--001a114a9f4445535a052367a8ad
Content-Type: text/plain; charset=UTF-8

That's not a typo.  It's part of a strategy to work with multi-part
names.  If the name is a path like 'foo/bar' and acquiring 'foo'
fails, you should not end up with the 'bar' attribute of your default,
which is what would happen with your version.  My version still has a
bug, in that it doesn't work right with *single*-part names, because
the for-loop doesn't execute in that case.  This code really needs
proper tests, but there were apparently never any real tests for this
functionality in the first place.  Most of PEAK's early code was
developed before "test first" and "test-driven" were my standard
practice.  :-(

Anyway, here's a new version that should fix both issues.  I will have
to see about adding some proper tests for this thing and back-testing
to make sure they fail without the patch.


On Sat, Oct 31, 2015 at 3:09 AM, Alexey Smishlayev <[email protected]> wrote:
> It does except for a little typo, at line 60 of the patch it should read "ob
> = acquireComponent(component, attr, default)", then it works good. Thank
> you!
>
>
> Best regards,
> Alexey
>
>
> On 30/10/15 20:59, PJ Eby wrote:
>>
>> Does this patch work for you?
>>
>> On Wed, Oct 28, 2015 at 8:45 AM, Alexey Smishlayev <[email protected]>
>> wrote:
>>>
>>> Hi!
>>> How can I get an svn checkout to incorporate described changes and
>>> propose
>>> another patch? This is essential for our software and I'd like to see it
>>> merged in upstream as soon as possible.
>>>
>>> Best regards,
>>> Alexey
>>>
>>>
>>> On 30/09/15 01:53, PJ Eby wrote:
>>>>
>>>> Thanks for reporting the problem.  Unfortunately your patch will mask
>>>> any other sort of name error, including one that happens indirectly as
>>>> a side effect of the lookup.  I think the correct fix is going to be
>>>> to change the nameNotFound() method of config.IConfigurationRoot to
>>>> include a default (defaulting to NOT_GIVEN), and have
>>>> acquireComponent() take a default and pass it along to nameNotFound().
>>>> This would handle the case of a direct lookup failing by returning the
>>>> default, but still raise an error for any other part of the process
>>>> getting a naming error.
>>>>
>>>> On Tue, Sep 29, 2015 at 9:03 AM, Alexey Smishlayev <[email protected]>
>>>> wrote:
>>>>>
>>>>> Hello!
>>>>>
>>>>> 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:
>>>>>> None)
>>>>>>
>>>>> 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",
>>>>> "../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
>>>>>
>>>>> _______________________________________________
>>>>> PEAK mailing list
>>>>> [email protected]
>>>>> http://www.eby-sarna.com/mailman/listinfo/peak
>>>
>>>
>>> _______________________________________________
>>> PEAK mailing list
>>> [email protected]
>>> http://www.eby-sarna.com/mailman/listinfo/peak
>
>
>
> _______________________________________________
> PEAK mailing list
> [email protected]
> http://www.eby-sarna.com/mailman/listinfo/peak

--001a114a9f4445535a052367a8ad
Content-Type: application/octet-stream; name="obtain.patch"
Content-Disposition: attachment; filename="obtain.patch"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_igf7g6kc0

SW5kZXg6IHNyYy9wZWFrL2NvbmZpZy9jb25maWdfY29tcG9uZW50cy5weQo9PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09Ci0t
LSBzcmMvcGVhay9jb25maWcvY29uZmlnX2NvbXBvbmVudHMucHkJKHJldmlzaW9uIDI3NzgpCisr
KyBzc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mYXVsdD1OT1RfR0lWRU4pOgogICAgICAgICAiIiJB
IChub24tVVJMKSBjb21wb25lbnQgbmFtZSB3YXMgbm90IGZvdW5kIiIiCiAKIApJbmRleDogc3Jj
L3BlYWsvYmluZGluZy9jb21wb25lbnRzLnB5Cj09PT09PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0KLS0tIHNyYy9wZWFrL2JpbmRp
bmcvY29tcG9uZW50cy5weQkocmV2aXNpb24gMjc3OCkKKysrIHNyYy9wZWFrL2JpbmRpbmcvY29t
cG9uZW50cy5weQkod29ya2luZyBjb3B5KQpAQCAtMjg1LDcgKzI4NSw3IEBACiAKIAogCi1kZWYg
YWNxdWlyZUNvbXBvbmVudChjb21wb25lbnQsIG5hbWUpOgorZGVmIGFjcXVpcmVDb21wb25lbnQo
Y29tcG9uZW50LCBuYW1lLCBkZWZhdWx0PU5PVF9HSVZFTik6CiAKICAgICAiIiJBY3F1aXJlICdu
YW1lJyByZWxhdGl2ZSB0byAnY29tcG9uZW50Jywgdy9mYWxsYmFjayB0byBuYW1pbmcubG9va3Vw
KCkKIApAQCAtMzEyLDcgKzMxMiw3IEBACiAgICAgICAgIHJldHVybiBhZGFwdCgKICAgICAgICAg
ICAgIHByZXYsIElDb25maWd1cmF0aW9uUm9vdCwgTnVsbENvbmZpZ1Jvb3QKICAgICAgICAgKS5u
YW1lTm90Rm91bmQoCi0gICAgICAgICAgICBwcmV2LCBuYW1lLCBjb21wb25lbnQKKyAgICAgICAg
ICAgIHByZXYsIG5hbWUsIGNvbXBvbmVudCwgZGVmYXVsdD1kZWZhdWx0CiAgICAgICAgICkKIAog
CkBAIC0zNjgsNDYgKzM2OCw0NiBAQAogCiAKICAgICBkZWYgZmluZENvbXBvbmVudChzZWxmLCBj
b21wb25lbnQsIGRlZmF1bHQ9Tk9UX0dJVkVOKToKLQogICAgICAgICBpZiBub3Qgc2VsZjogICMg
ZW1wdHkgbmFtZSByZWZlcnMgdG8gc2VsZgogICAgICAgICAgICAgcmV0dXJuIGNvbXBvbmVudAog
CiAgICAgICAgIHBhcnRzID0gaXRlcihzZWxmKQogICAgICAgICBhdHRyID0gcGFydHMubmV4dCgp
ICAgICAgICAgICAgICAgICAjIGZpcnN0IHBhcnQKICAgICAgICAgcGMgPSBfZ2V0Rmlyc3RQYXRo
Q29tcG9uZW50KGF0dHIpCisgICAgICAgIGlmIHBjOgorICAgICAgICAgICAgb2IgPSBwYyhjb21w
b25lbnQpCisgICAgICAgIGVsaWYgZGVmYXVsdCBpcyBOT1RfR0lWRU46ICAgCisgICAgICAgICAg
ICBvYiA9IGFjcXVpcmVDb21wb25lbnQoY29tcG9uZW50LCBhdHRyKSAjIGxldCB0aGUgZXJyb3Ig
aGFwcGVuCisgICAgICAgIGVsaWYgbGVuKHNlbGYpPjE6CisgICAgICAgICAgICBvYiA9IGFjcXVp
cmVDb21wb25lbnQoY29tcG9uZW50LCBhdHRyLCBOT1RfRk9VTkQpCisgICAgICAgIGVsc2U6Cisg
ICAgICAgICAgICByZXR1cm4gYWNxdWlyZUNvbXBvbmVudChjb21wb25lbnQsIGF0dHIsIGRlZmF1
bHQpCiAKLQotICAgICAgICBpZiBwYzogIG9iID0gcGMoY29tcG9uZW50KQotICAgICAgICBlbHNl
OiAgIG9iID0gYWNxdWlyZUNvbXBvbmVudChjb21wb25lbnQsIGF0dHIpCi0KICAgICAgICAgcmVz
b2x2ZWQgPSBbXQogICAgICAgICBhcHBlbmQgPSByZXNvbHZlZC5hcHBlbmQKIAogICAgICAgICB0
cnk6CiAgICAgICAgICAgICBmb3IgYXR0ciBpbiBwYXJ0czoKKyAgICAgICAgICAgICAgICBpZiBv
YiBpcyBOT1RfRk9VTkQ6IGJyZWFrCiAgICAgICAgICAgICAgICAgcGMgPSBfZ2V0TmV4dFBhdGhD
b21wb25lbnQoYXR0cikKICAgICAgICAgICAgICAgICBpZiBwYzogIG9iID0gcGMob2IpCiAgICAg
ICAgICAgICAgICAgZWxzZTogICBvYiA9IGdldGF0dHIob2IsYXR0cikKICAgICAgICAgICAgICAg
ICBhcHBlbmQoYXR0cikKKyAgICAgICAgICAgIGVsc2U6CisgICAgICAgICAgICAgICAgcmV0dXJu
IG9iCiAKICAgICAgICAgZXhjZXB0IEF0dHJpYnV0ZUVycm9yOgorICAgICAgICAgICAgcGFzcwog
Ci0gICAgICAgICAgICBpZiBkZWZhdWx0IGlzIG5vdCBOT1RfR0lWRU46Ci0gICAgICAgICAgICAg
ICAgcmV0dXJuIGRlZmF1bHQKKyAgICAgICAgaWYgZGVmYXVsdCBpcyBub3QgTk9UX0dJVkVOOgor
ICAgICAgICAgICAgcmV0dXJuIGRlZmF1bHQKIAotICAgICAgICAgICAgcmFpc2UgZXhjZXB0aW9u
cy5OYW1lTm90Rm91bmQoCi0gICAgICAgICAgICAgICAgcmVzb2x2ZWROYW1lID0gQ29tcG9uZW50
TmFtZShyZXNvbHZlZCksCi0gICAgICAgICAgICAgICAgcmVtYWluaW5nTmFtZSA9IENvbXBvbmVu
dE5hbWUoW2F0dHJdICsgW2EgZm9yIGEgaW4gcGFydHNdKSwKLSAgICAgICAgICAgICAgICByZXNv
bHZlZE9iaiA9IG9iCi0gICAgICAgICAgICApCisgICAgICAgIHJhaXNlIGV4Y2VwdGlvbnMuTmFt
ZU5vdEZvdW5kKAorICAgICAgICAgICAgcmVzb2x2ZWROYW1lID0gQ29tcG9uZW50TmFtZShyZXNv
bHZlZCksCisgICAgICAgICAgICByZW1haW5pbmdOYW1lID0gQ29tcG9uZW50TmFtZShbYXR0cl0g
KyBbYSBmb3IgYSBpbiBwYXJ0c10pLAorICAgICAgICAgICAgcmVzb2x2ZWRPYmogPSBvYgorICAg
ICAgICApCiAKLSAgICAgICAgcmV0dXJuIG9iCi0KLQotCi0KLQotCiBfZ2V0Rmlyc3RQYXRoQ29t
cG9uZW50ID0gZGljdCggKAogICAgICgnJywgICBnZXRSb290Q29tcG9uZW50KSwKICAgICAoJy4n
LCAgbGFtYmRhIHg6eCksCg==
--001a114a9f4445535a052367a8ad
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
PEAK mailing list
[email protected]
http://www.eby-sarna.com/mailman/listinfo/peak
--001a114a9f4445535a052367a8ad--