Skip to content

Commit

Permalink
Merge pull request skulpt#44 from csev/master
Browse files Browse the repository at this point in the history
Issues 40 and 42 - Improve open() message, make list() and dict() work.
  • Loading branch information
bnmnetp committed Jan 26, 2013
2 parents fa45dbd + 5d984a6 commit f532aaa
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ Sk.builtin.dict = function dict(L)

this.size = 0;

for (var i = 0; i < L.length; i += 2)
if ( L !== undefined )
{
this.mp$ass_subscript(L[i], L[i+1]);
for (var i = 0; i < L.length; i += 2)
{
this.mp$ass_subscript(L[i], L[i+1]);
}
}

this.__class__ = Sk.builtin.dict;
Expand Down
10 changes: 10 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,13 @@ Sk.builtin.TimeLimitError = function(args) { Sk.builtin.Exception.apply(this, ar
goog.inherits(Sk.builtin.TimeLimitError, Sk.builtin.Exception);
Sk.builtin.TimeLimitError.prototype.tp$name = "TimeLimitError";
goog.exportSymbol("Sk.builtin.TimeLimitError", Sk.builtin.TimeLimitError);

/**
* @constructor
* @extends Sk.builtin.Exception
* @param {...*} args
*/
Sk.builtin.IOError = function(args) { Sk.builtin.Exception.apply(this, arguments); }
goog.inherits(Sk.builtin.IOError, Sk.builtin.Exception);
Sk.builtin.IOError.prototype.tp$name = "IOError";
goog.exportSymbol("Sk.builtin.IOError", Sk.builtin.IOError);
9 changes: 6 additions & 3 deletions src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ Sk.builtin.file = function(name, mode, buffering)
this.name = name;
this.closed = false;
if ( Sk.inBrowser ) { // todo: Maybe provide a replaceable function for non-import files
if (document.all) {
this.data$ = document.getElementById(name.v).innerText;
var elem = document.getElementById(name.v);
if ( elem == null) {
throw new Sk.builtin.IOError("[Errno 2] No such file or directory: '"+name.v+"'");
} else if (document.all) {
this.data$ = elem.innerText;
} else { // stupid Firefox
this.data$ = document.getElementById(name.v).textContent;
this.data$ = elem.textContent;
}
} else {
this.data$ = Sk.read(name.v);
Expand Down
8 changes: 6 additions & 2 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ Sk.builtin.list = function(L)
{
if (!(this instanceof Sk.builtin.list)) return new Sk.builtin.list(L);

if (Object.prototype.toString.apply(L) === '[object Array]')
if ( L === undefined )
{
this.v = [];
}
else if (Object.prototype.toString.apply(L) === '[object Array]')
{
this.v = L;
}
Expand Down Expand Up @@ -322,4 +326,4 @@ Sk.builtin.list.prototype['sort'] = new Sk.builtin.func(function(self, cmp, key,
return null;
});

goog.exportSymbol("Sk.builtin.list", Sk.builtin.list);
goog.exportSymbol("Sk.builtin.list", Sk.builtin.list);
21 changes: 21 additions & 0 deletions test/run/t337.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
l = list()
print l
l.append(1)
print l
d = dict()
print d
d["zap"] = 1
print d

# Make sure this still works
l2 = list(l)
print l2

l3 = list(d)
print l3

# It would be nice to make this work
# But that would be a separate issue
# d2 = dict(d.items())
# print d2

6 changes: 6 additions & 0 deletions test/run/t337.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[]
[1]
{}
{'zap': 1}
[1]
['zap']
78 changes: 78 additions & 0 deletions test/run/t337.py.symtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
-- Identifiers --
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: dict
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: l
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: l2
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: l3
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: list
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: [
]
71 changes: 71 additions & 0 deletions test/run/t337.trans
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Module(body=[Assign(targets=[Name(id='l',
ctx=Store())],
value=Call(func=Name(id='list',
ctx=Load()),
args=[],
keywords=[],
starargs=None,
kwargs=None)),
Print(dest=None,
values=[Name(id='l',
ctx=Load())],
nl=True),
Expr(value=Call(func=Attribute(value=Name(id='l',
ctx=Load()),
attr='append',
ctx=Load()),
args=[Num(n=1)],
keywords=[],
starargs=None,
kwargs=None)),
Print(dest=None,
values=[Name(id='l',
ctx=Load())],
nl=True),
Assign(targets=[Name(id='d',
ctx=Store())],
value=Call(func=Name(id='dict',
ctx=Load()),
args=[],
keywords=[],
starargs=None,
kwargs=None)),
Print(dest=None,
values=[Name(id='d',
ctx=Load())],
nl=True),
Assign(targets=[Subscript(value=Name(id='d',
ctx=Load()),
slice=Index(value=Str(s='zap')),
ctx=Store())],
value=Num(n=1)),
Print(dest=None,
values=[Name(id='d',
ctx=Load())],
nl=True),
Assign(targets=[Name(id='l2',
ctx=Store())],
value=Call(func=Name(id='list',
ctx=Load()),
args=[Name(id='l',
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)),
Print(dest=None,
values=[Name(id='l2',
ctx=Load())],
nl=True),
Assign(targets=[Name(id='l3',
ctx=Store())],
value=Call(func=Name(id='list',
ctx=Load()),
args=[Name(id='d',
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)),
Print(dest=None,
values=[Name(id='l3',
ctx=Load())],
nl=True)])

0 comments on commit f532aaa

Please sign in to comment.