From f2c6a600b5a1cd9a0e237085388954363d801558 Mon Sep 17 00:00:00 2001 From: AlexECX <33321263+AlexECX@users.noreply.github.com> Date: Thu, 14 May 2020 15:00:08 -0400 Subject: [PATCH] Add 3rd argument to getattr --- .../modules/org/transcrypt/__builtin__.js | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/transcrypt/modules/org/transcrypt/__builtin__.js b/transcrypt/modules/org/transcrypt/__builtin__.js index 0ca180861..a85c9e08d 100644 --- a/transcrypt/modules/org/transcrypt/__builtin__.js +++ b/transcrypt/modules/org/transcrypt/__builtin__.js @@ -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) {