Skip to content

CRITICAL: getattr() does not have 3rd argument #633

Description

@faerot

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.

Activity

  1. AlexECX commented on May 12, 2019

    @AlexECX

    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

  2. faerot commented on Jul 6, 2019

    @faerot
    Author

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

  3. JdeH commented on Aug 14, 2019

    @JdeH
    Collaborator

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

  4. self-assigned this
    on Aug 14, 2019
  5. phfaist commented on Jul 12, 2022

    @phfaist

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions