Skip to content

Commit

Permalink
Merge pull request skulpt#163 from rixner/master
Browse files Browse the repository at this point in the history
Make several functions return properly wrapped Python values.
  • Loading branch information
rixner committed Sep 24, 2013
2 parents 2c1a18f + c3861e4 commit 2cc5723
Show file tree
Hide file tree
Showing 8 changed files with 843 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ Sk.builtin.dict.prototype['get'] = new Sk.builtin.func(function(self, k, d)

Sk.builtin.dict.prototype['has_key'] = new Sk.builtin.func(function(self, k)
{
return self.sq$contains(k);
return Sk.builtin.bool(self.sq$contains(k));
});

Sk.builtin.dict.prototype['items'] = new Sk.builtin.func(function(self)
Expand Down
19 changes: 11 additions & 8 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ Sk.builtin.list.prototype.list_sort_ = function(self, cmp, key, reverse) {
if (mucked) {
throw new Sk.builtin.OperationError("list modified during sort");
}

return Sk.builtin.none.none$;
}

/**
Expand All @@ -390,7 +392,7 @@ Sk.builtin.list.prototype.list_reverse_ = function(self)
newarr.push(old[i]);
}
self.v = newarr;
return null;
return Sk.builtin.none.none$;
}

//Sk.builtin.list.prototype.__reversed__ = todo;
Expand All @@ -406,7 +408,7 @@ Sk.builtin.list.prototype['append'] = new Sk.builtin.func(function(self, item)
Sk.builtin.pyCheckArgs("append", arguments, 2, 2);

self.v.push(item);
return null;
return Sk.builtin.none.none$;
});

Sk.builtin.list.prototype['insert'] = new Sk.builtin.func(function(self, i, x)
Expand All @@ -416,10 +418,11 @@ Sk.builtin.list.prototype['insert'] = new Sk.builtin.func(function(self, i, x)
throw new Sk.builtin.TypeError("an integer is required");
};

i = Sk.builtin.asnum$(i);
i = Sk.builtin.asnum$(i);
if (i < 0) i = 0;
else if (i > self.v.length) i = self.v.length;
self.v.splice(i, 0, x);
return Sk.builtin.none.none$;
});

Sk.builtin.list.prototype['extend'] = new Sk.builtin.func(function(self, b)
Expand All @@ -438,11 +441,11 @@ Sk.builtin.list.prototype['extend'] = new Sk.builtin.func(function(self, b)
// Concatenate
self.v.push.apply(self.v, newb);

return null;
return Sk.builtin.none.none$;
};
for (var it = b.tp$iter(), i = it.tp$iternext(); i !== undefined; i = it.tp$iternext())
self.v.push(i);
return null;
return Sk.builtin.none.none$;
});

Sk.builtin.list.prototype['pop'] = new Sk.builtin.func(function(self, i)
Expand Down Expand Up @@ -470,8 +473,8 @@ Sk.builtin.list.prototype['remove'] = new Sk.builtin.func(function(self, item)
Sk.builtin.pyCheckArgs("remove", arguments, 2, 2);

var idx = Sk.builtin.list.prototype['index'].func_code(self, item);
self.v.splice(idx, 1);
return null;
self.v.splice(Sk.builtin.asnum$(idx), 1);
return Sk.builtin.none.none$;
});

Sk.builtin.list.prototype['index'] = new Sk.builtin.func(function(self, item)
Expand All @@ -483,7 +486,7 @@ Sk.builtin.list.prototype['index'] = new Sk.builtin.func(function(self, item)
for (var i = 0; i < len; ++i)
{
if (Sk.misceval.richCompareBool(obj[i], item, "Eq"))
return i;
return Sk.builtin.assk$(i, Sk.builtin.nmber.int$);
}
throw new Sk.builtin.ValueError("list.index(x): x not in list");
});
Expand Down
18 changes: 9 additions & 9 deletions src/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Sk.builtin.set.prototype['isdisjoint'] = new Sk.builtin.func(function(self, othe
return Sk.builtin.bool.false$;
}
}
return Sk.builtin.bool(true);
return Sk.builtin.bool.true$;
});

Sk.builtin.set.prototype['issubset'] = new Sk.builtin.func(function(self, other)
Expand Down Expand Up @@ -214,10 +214,10 @@ Sk.builtin.set.prototype['update'] = new Sk.builtin.func(function(self, other)
{
Sk.builtin.set.prototype['add'].func_code(self, item);
}
return null;
return Sk.builtin.none.none$;
});

Sk.builtin.set.prototype['intersection_update'] = new Sk.builtin.func(function(self)
Sk.builtin.set.prototype['intersection_update'] = new Sk.builtin.func(function(self, other)
{
for (var it = self.tp$iter(), item = it.tp$iternext(); item !== undefined; item = it.tp$iternext())
{
Expand All @@ -230,7 +230,7 @@ Sk.builtin.set.prototype['intersection_update'] = new Sk.builtin.func(function(s
}
}
}
return null;
return Sk.builtin.none.none$;
});

Sk.builtin.set.prototype['difference_update'] = new Sk.builtin.func(function(self, other)
Expand All @@ -246,22 +246,22 @@ Sk.builtin.set.prototype['difference_update'] = new Sk.builtin.func(function(sel
}
}
}
return null;
return Sk.builtin.none.none$;
});

Sk.builtin.set.prototype['symmetric_difference_update'] = new Sk.builtin.func(function(self, other)
{
var sd = Sk.builtin.set.prototype['symmetric_difference'].func_code(self, other);
self.set_reset_();
Sk.builtin.set.prototype['update'].func_code(self, sd);
return null;
return Sk.builtin.none.none$;
});


Sk.builtin.set.prototype['add'] = new Sk.builtin.func(function(self, item)
{
self.v.mp$ass_subscript(item, true);
return null;
return Sk.builtin.none.none$;
});

Sk.builtin.set.prototype['discard'] = new Sk.builtin.func(function(self, item)
Expand All @@ -276,7 +276,7 @@ Sk.builtin.set.prototype['discard'] = new Sk.builtin.func(function(self, item)
}
//self.v.mp$ass_subscript(item, null);
}
return null;
return Sk.builtin.none.none$;
});

Sk.builtin.set.prototype['pop'] = new Sk.builtin.func(function(self)
Expand All @@ -294,7 +294,7 @@ Sk.builtin.set.prototype['pop'] = new Sk.builtin.func(function(self)
Sk.builtin.set.prototype['remove'] = new Sk.builtin.func(function(self, item)
{
self.v.mp$del_subscript(item);
return null;
return Sk.builtin.none.none$;
});


Expand Down
2 changes: 1 addition & 1 deletion src/tuple.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Sk.builtin.tuple.prototype['index'] = new Sk.builtin.func(function(self, item)
for (var i = 0; i < len; ++i)
{
if (Sk.misceval.richCompareBool(obj[i], item, "Eq"))
return i;
return Sk.builtin.assk$(i, Sk.builtin.nmber.int$);
}
throw new Sk.builtin.ValueError("tuple.index(x): x not in tuple");
});
Expand Down
75 changes: 75 additions & 0 deletions test/run/t501.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
lst = [1, 2, 3]
check = 1 in lst
print type(check)

d = {1: 2}
check = d.has_key(1)
print type(check)
check = 3 in d
print type(check)

lst2 = [1, 2, 3, 4]
check = lst2.reverse()
print type(check)
check = lst2.append(8)
print type(check)
check = lst2.insert(2, 3)
print type(check)
check = lst2.extend(lst)
print type(check)
check = lst2.remove(4)
print type(check)
check = lst2.index(2)
print type(check)
check = lst2.count(3)
print type(check)
check = lst2.sort()
print type(check)

s = set([1, 2, 3])
check = s.isdisjoint(s)
print type(check)
print s
check = s.issubset(s)
print type(check)
print s
check = s.update(s)
print type(check)
print s
s2 = set([2, 3])
check = s.intersection_update(s2)
print type(check)
print s
check = s.difference_update(s2)
print type(check)
print s
check = s.symmetric_difference_update(s2)
print type(check)
print s
check = s.add(4)
print type(check)
print s
check = s.discard(4)
print type(check)
print s
check = s.remove(3)
print type(check)
print s

t = (1, 2, 3)
check = t.index(2)
print type(check)
check = t.count(3)
print type(check)

s = "abcabcabc"
check = s.count('a')
print type(check)
check = s.find('bc')
print type(check)
check = s.index('cab')
print type(check)
check = s.rfind('bc')
print type(check)
check = s.rindex('cab')
print type(check)
36 changes: 36 additions & 0 deletions test/run/t501.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<type 'bool'>
<type 'bool'>
<type 'bool'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'int'>
<type 'int'>
<type 'NoneType'>
<type 'bool'>
set([1, 2, 3])
<type 'bool'>
set([1, 2, 3])
<type 'NoneType'>
set([1, 2, 3])
<type 'NoneType'>
set([2, 3])
<type 'NoneType'>
set([])
<type 'NoneType'>
set([2, 3])
<type 'NoneType'>
set([2, 3, 4])
<type 'NoneType'>
set([2, 3])
<type 'NoneType'>
set([2])
<type 'int'>
<type 'int'>
<type 'int'>
<type 'int'>
<type 'int'>
<type 'int'>
<type 'int'>
114 changes: 114 additions & 0 deletions test/run/t501.py.symtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
-- Identifiers --
name: check
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
name: d
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
name: lst
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
name: lst2
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
name: s
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
name: s2
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
name: set
is_referenced: True
is_imported: False
is_parameter: False
is_global: True
is_declared_global: False
is_local: False
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
name: t
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
name: type
is_referenced: True
is_imported: False
is_parameter: False
is_global: True
is_declared_global: False
is_local: False
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
Loading

0 comments on commit 2cc5723

Please sign in to comment.