Skip to content

Commit

Permalink
Fixes argument checks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ebertmi committed Feb 17, 2015
1 parent 57a997a commit 5bc6f2e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/misceval.js
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,8 @@ Sk.misceval.buildClass = function (globals, func, name, bases) {

// file's __name__ is class's __module__
locals.__module__ = globals["__name__"];
var _name = Sk.builtin.str(name);
var _bases = Sk.builtin.tuple(bases);
var _name = new Sk.builtin.str(name);
var _bases = new Sk.builtin.tuple(bases);
var _locals = [];
var key;

Expand All @@ -1018,10 +1018,10 @@ Sk.misceval.buildClass = function (globals, func, name, bases) {
//The current property key not a direct property of p
continue;
}
_locals.push(Sk.builtin.str(key)); // push key
_locals.push(new Sk.builtin.str(key)); // push key
_locals.push(locals[key]); // push associated value
}
_locals = Sk.builtin.dict(_locals);
_locals = new Sk.builtin.dict(_locals);

klass = Sk.misceval.callsim(meta, _name, _bases, _locals);
return klass;
Expand Down
19 changes: 9 additions & 10 deletions src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,18 @@ Sk.builtin.type = function (name, bases, dict) {
}
else {

// this checks if the dict has been already successfully unwrapped
// in case of creating classes with class keyword, skulpt automatically
// unwrap the arguments and sets the module in the buildClass function
if(!dict["tp$name"] && dict["tp$name"] !== "dict") {
// argument dict must be of type dict
if(dict.tp$name !== "dict") {
throw new Sk.builtin.TypeError("type() argument 3 must be dict, not " + Sk.abstr.typeName(dict));
}

// checks if name is builtin type of js object
// checks if name must be string
if(!Sk.builtin.checkString(name)) {
throw new Sk.builtin.TypeError("type() argument 1 must be str, not " + Sk.abstr.typeName(name));
}

// unwrap bases if required
if(!bases["tp$name"] && bases["tp$name"] !== "tuple") {
// argument bases must be of type tuple
if(bases.tp$name !== "tuple") {
throw new Sk.builtin.TypeError("type() argument 2 must be tuple, not " + Sk.abstr.typeName(bases));
}

Expand Down Expand Up @@ -101,8 +99,9 @@ Sk.builtin.type = function (name, bases, dict) {
};

// set __module__ if not present (required by direct type(name, bases, dict) calls)
if(dict.mp$lookup(Sk.builtin.str("__module__")) === undefined) {
dict.mp$ass_subscript(Sk.builtin.str("__module__"), Sk.globals["__name__"]);
var module_lk = new Sk.builtin.str("__module__");
if(dict.mp$lookup(module_lk) === undefined) {
dict.mp$ass_subscript(module_lk, Sk.globals["__name__"]);
}

// copy properties into our klass object
Expand Down Expand Up @@ -133,7 +132,7 @@ Sk.builtin.type = function (name, bases, dict) {
if (reprf !== undefined) {
return Sk.misceval.apply(reprf, undefined, undefined, undefined, []);
}
mod = dict.mp$subscript(Sk.builtin.str("__module__")); // lookup __module__
mod = dict.mp$subscript(module_lk); // lookup __module__
cname = "";
if (mod) {
cname = mod.v + ".";
Expand Down
2 changes: 1 addition & 1 deletion test/run/t520.py.real
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ True <type 'bool'>
True <type 'bool'>
1 <type 'int'>
0 <type 'int'>
115 <type 'int'>
120 <type 'int'>
False <type 'bool'>
2 changes: 1 addition & 1 deletion test/run/t520.py.real.force
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ True <type 'bool'>
True <type 'bool'>
1 <type 'int'>
0 <type 'int'>
115 <type 'int'>
120 <type 'int'>
False <type 'bool'>

0 comments on commit 5bc6f2e

Please sign in to comment.