Skip to content

Commit

Permalink
Issue skulpt#42
Browse files Browse the repository at this point in the history
Make list() and dict() work as zero parameter constructors
  • Loading branch information
csev committed Jan 24, 2013
1 parent fa45dbd commit fd83176
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 4 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
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 fd83176

Please sign in to comment.