Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 3rd argument to getattr #727

Open
wants to merge 1 commit into
base: dev_fall_2019
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions transcrypt/modules/org/transcrypt/__builtin__.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,28 @@ export function setattr (obj, name, value) {
obj [name] = value; // Will not work in combination with static retrieval of aliased attributes, too expensive
};

export function getattr (obj, name) {
return name in obj ? obj [name] : obj ['py_' + name];
export var getattr = function (obj, name, failsafe) {
if (obj[name] !== undefined) {
return obj [name];
} else if (obj['py_' + name] !== undefined) {
return obj ['py_' + name];
} else if (failsafe !== undefined) {
return failsafe;
} else {
let message = "has no attribute '{}'";
if (obj.__class__ !== undefined) {
message = ("'{}' object " + message).format(obj.__class__.__name__, name);
} else if (obj.__name__ !== undefined) {
message = ("type object '{}' " + message).format(obj.__name__, name);
} else if (obj.prototype !== undefined) {
message = ("type object '{}' " + message).format(obj.name || py_typeof(obj), name);
} else {
message = ("'{}' object " + message).format(obj.name || py_typeof(obj), name);
}
var __except0__ = AttributeError (message, new Error());
__except0__.__cause__ = null;
throw __except0__;
}
};

export function hasattr (obj, name) {
Expand Down