Transcrypt icon indicating copy to clipboard operation
Transcrypt copied to clipboard

CRITICAL: getattr() does not have 3rd argument

Open faerot opened this issue 6 years ago • 4 comments

I was surprised that getattr() function does not allow third default argument, which is critically important basic functionality as JavaScript does not raise AttributeErrors when accessing missing attributes and we cannot rely on those.

faerot avatar May 11 '19 10:05 faerot

Funny, I was reminded of it while working on #630 as it was messing with my tests. I've been working with this in the meantime:

boost_transcript.js

export var getattr = function (obj, name, failsafe) {
    if (name in obj) {
        return obj [name];
    } else if (obj ['py_' + name]) {
        return obj ['py_' + name];
    } else if (failsafe !== undefined) {
        return failsafe;
    } else {
        var __except0__ = AttributeError ('object has no attribute ' + name);
        __except0__.__cause__ = null;
        throw __except0__;
    }
};

If whe can't have getattr throw an exception (but why?), then removing the last else will make the function return undefined as usual

AlexECX avatar May 12 '19 02:05 AlexECX

}else if (failsafe) { this should probably be }else if (failsafe !== undefined) { as we can pass False, empty string or None as default values

faerot avatar Jul 06 '19 07:07 faerot

There's no good reason why this is left out. It will be added in the next minor version. Thanks for the suggestion!

JdeH avatar Aug 14 '19 09:08 JdeH

Hi! I ran across the same issue recently (with Transcrypt 3.9.0 from pypi). Is there anything holding back the associated pull request #727? Thanks!

phfaist avatar Jul 12 '22 08:07 phfaist