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

compare functools to #1092 #13

Open
wants to merge 6 commits into
base: prototypical_getsets_mappingproxy
Choose a base branch
from
Open
Show file tree
Hide file tree
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
47 changes: 30 additions & 17 deletions src/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ Sk.abstr = {};
*/
Sk.abstr.typeName = function (obj) {
if (obj != null && obj.tp$name !== undefined) {
return obj.tp$name;
let name = obj.hp$name;
if (name !== undefined) {
return name;
}
name = obj.tp$name;
if (name.includes(".")) {
name = name.slice(name.lastIndexOf(".") + 1);
}
return name;
} else {
Sk.asserts.fail(obj + " passed to typeName");
return "<invalid type>";
}
};
Expand All @@ -47,15 +56,13 @@ const binop_name_to_symbol = {
};

function binop_type_error(v, w, name) {
const vtypename = Sk.abstr.typeName(v);
const wtypename = Sk.abstr.typeName(w);
throw new Sk.builtin.TypeError("unsupported operand type(s) for " + binop_name_to_symbol[name] + ": '" + vtypename + "' and '" + wtypename + "'");
throw new Sk.builtin.TypeError("unsupported operand type(s) for " + binop_name_to_symbol[name] + ": '" + v.tp$name + "' and '" + w.tp$name + "'");
};

function biniop_type_error(v, w, name) {
const vtypename = Sk.abstr.typeName(v);
const wtypename = Sk.abstr.typeName(w);
throw new Sk.builtin.TypeError("unsupported operand type(s) for " + binop_name_to_symbol[name] + "=: '" + vtypename + "' and '" + wtypename + "'");
throw new Sk.builtin.TypeError("unsupported operand type(s) for " + binop_name_to_symbol[name] + "=: '" + v.tp$name + "' and '" + w.tp$name + "'");
};

const uop_name_to_symbol = {
Expand All @@ -64,8 +71,7 @@ const uop_name_to_symbol = {
Invert: "~",
};
function unop_type_error(v, name) {
var vtypename = Sk.abstr.typeName(v);
throw new Sk.builtin.TypeError("bad operand type for unary " + uop_name_to_symbol[name] + ": '" + vtypename + "'");
throw new Sk.builtin.TypeError("bad operand type for unary " + uop_name_to_symbol[name] + ": '" + v.tp$name + "'");
};

/**
Expand Down Expand Up @@ -521,6 +527,23 @@ Sk.abstr.mappingUnpackIntoKeywordArray = function (jsArray, pyMapping, pyCodeObj
);
};

Sk.abstr.keywordArrayFromPyDict = function (dict) {
const arr = [];
dict.$items().forEach(([key, val]) => {
if (!Sk.builtin.checkString(key)) {
throw new Sk.builtin.TypeError("keywords must be strings");
}
arr.push(key.$jsstr());
arr.push(val);
});
return arr;
};

Sk.abstr.keywordArrayToPyDict = function (kwarray) {
kwarray = kwarray.map((x, i) => i % 2 ? x : new Sk.builtin.str(x));
return new Sk.builtin.dict(kwarray);
};

/**
*
* @function
Expand Down Expand Up @@ -1186,13 +1209,6 @@ Sk.abstr.buildNativeClass = function (typename, options) {
/**@type {FunctionConstructor} */
let typeobject = options.constructor;

let mod;
if (typename.includes(".")) {
// you should define the module like "collections.defaultdict" for static classes
const mod_typename = typename.split(".");
typename = mod_typename.pop();
mod = mod_typename.join(".");
}
// set the prototypical chains for inheritance
Sk.abstr.setUpInheritance(typename, typeobject, options.base, options.meta);

Expand All @@ -1214,9 +1230,6 @@ Sk.abstr.buildNativeClass = function (typename, options) {
Sk.abstr.setUpGetSets(typeobject, options.getsets);
Sk.abstr.setUpClassMethods(typeobject, options.classmethods);

if (mod !== undefined) {
type_proto.__module__ = new Sk.builtin.str(mod);
}

const proto = options.proto || {};
Object.entries(proto).forEach(([p, val]) => {
Expand Down
11 changes: 10 additions & 1 deletion src/builtindict.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Sk.builtins = {
"hasattr" : null,
"id" : null,

"reduce" : new Sk.builtin.func(Sk.builtin.reduce),
"sorted" : null,
"any" : null,
"all" : null,
Expand Down Expand Up @@ -477,6 +476,7 @@ Sk.setupObjects = function (py3) {
Sk.builtins["map"] = Sk.builtin.map_;
Sk.builtins["zip"] = Sk.builtin.zip_;
Sk.builtins["range"] = Sk.builtin.range_;
delete Sk.builtins["reduce"];
delete Sk.builtins["xrange"];
delete Sk.builtins["StandardError"];
delete Sk.builtins["unicode"];
Expand Down Expand Up @@ -519,6 +519,15 @@ Sk.setupObjects = function (py3) {
null,
"builtins"
);
Sk.builtins["reduce"] = new Sk.builtin.sk_method(
{
$meth: Sk.builtin.reduce,
$name: "reduce",
$flags: { MinArgs: 2, MaxArgs: 3 },
},
null,
"builtins"
);
Sk.builtins["filter"] = new Sk.builtin.func(Sk.builtin.filter);
Sk.builtins["map"] = new Sk.builtin.func(Sk.builtin.map);
Sk.builtins["zip"] = new Sk.builtin.func(Sk.builtin.zip);
Expand Down
2 changes: 1 addition & 1 deletion src/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Sk.builtin.bytes = Sk.abstr.buildNativeClass("bytes", {
});
return Sk.misceval.chain(r, () => new Sk.builtin.bytes(source));
}
throw new Sk.builtin.TypeError("cannot convert '" + Sk.abstr.typeName(source) + "' object into bytes");
throw new Sk.builtin.TypeError("cannot convert '" + Sk.abstr.typeName(pySource) + "' object into bytes");
},
$r() {
let num;
Expand Down
15 changes: 13 additions & 2 deletions src/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Sk.builtin.func = Sk.abstr.buildNativeClass("function", {
this.$name = (code.co_name && code.co_name.v) || code.name || "<native JS>";
this.$d = Sk.builtin.dict ? new Sk.builtin.dict() : undefined;
this.$doc = code.$doc;
this.$module = (Sk.globals && Sk.globals["__name__"]) || Sk.builtin.none.none$;
this.$module = (Sk.globals && Sk.globals["__name__"]);
this.$qualname = (code.co_qualname && code.co_qualname.v) || this.$name;

if (closure2 !== undefined) {
Expand Down Expand Up @@ -127,9 +127,20 @@ Sk.builtin.func = Sk.abstr.buildNativeClass("function", {
},
__doc__: {
$get() {
return new Sk.builtin.str(this.$doc);
return this.$doc || Sk.builtin.none.none$;
},
$set(value) {
this.$doc = value;
}
},
__module__: {
$get() {
return this.$module || Sk.builtin.none.none$;
},
$set(value) {
this.$module = value;
}
}
},
proto: {
$memoiseFlags() {
Expand Down
2 changes: 1 addition & 1 deletion src/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ Sk.generic.getSetDict = {
},
$set(value) {
if (value === undefined) {
this.$d = new Sk.builtin.dict();
throw new Sk.builtin.TypeError("cannot delete __dict__");
} else if (value instanceof Sk.builtin.dict) {
this.$d = value;
} else {
Expand Down
Loading