Skip to content

Commit

Permalink
BugFix: Issue 23 -- from xxx import * fails
Browse files Browse the repository at this point in the history
Define the importStar function to add all symbols in a module to the proper environment.
  • Loading branch information
bnmnetp committed Apr 21, 2013
1 parent b078118 commit f93bc43
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 19 deletions.
6 changes: 3 additions & 3 deletions dist/skulpt.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions src/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Compiler.prototype._gr = function(hint, rest)

/**
* Function to test if an interrupt should occur if the program has been running for too long.
* This function is executed at every test/branch operation.
* This function is executed at every test/branch operation.
*/
Compiler.prototype._interruptTest = function() { // Added by RNL
out("if (Sk.execStart === undefined) {Sk.execStart=new Date()}");
Expand Down Expand Up @@ -740,7 +740,7 @@ Compiler.prototype.cif = function(s)
var constant = this.exprConstant(s.test);
if (constant === 0)
{
if (s.orelse)
if (s.orelse)
this.vseqstmt(s.orelse);
}
else if (constant === 1)
Expand Down Expand Up @@ -999,7 +999,7 @@ Compiler.prototype.cfromimport = function(s)
if (i === 0 && alias.name.v === "*")
{
goog.asserts.assert(n === 1);
out("Sk.importStar(", mod, ");");
out("Sk.importStar(", mod, ",$loc, $gbl);");
return;
}

Expand Down Expand Up @@ -1221,7 +1221,7 @@ Compiler.prototype.buildcodeobj = function(n, coname, decorator_list, args, call
// yields.
//
// todo; possibly this should be outside?
//
//
var frees = "";
if (hasFree)
{
Expand Down Expand Up @@ -1698,7 +1698,7 @@ Compiler.prototype.cprint = function(s)
out('Sk.misceval.print_(', /*dest, ',',*/ "new Sk.builtins['str'](", this.vexpr(s.values[i]), ').v);');
if (s.nl)
out('Sk.misceval.print_(', /*dest, ',*/ '"\\n");');
};
};
Compiler.prototype.cmod = function(mod)
{
//print("-----");
Expand All @@ -1710,8 +1710,8 @@ Compiler.prototype.cmod = function(mod)
this.u.varDeclsCode = "var $blk=" + entryBlock + ",$exc=[],$gbl={},$loc=$gbl;$gbl.__name__=$modname;";

// Add the try block that pops the try/except stack if one exists
// Github Issue #38
// Google Code Issue: 109 / 114
// Github Issue #38
// Google Code Issue: 109 / 114

// Old code:
//this.u.switchCode = "while(true){switch($blk){";
Expand All @@ -1721,16 +1721,16 @@ Compiler.prototype.cmod = function(mod)
this.u.switchCode = "while(true){try{ switch($blk){";
this.u.suffixCode = "} }catch(err){if ($exc.length>0) { $blk=$exc.pop(); continue; } else { throw err; }} }});";

// Note - this change may need to be adjusted for all the other instances of
// switchCode and suffixCode in this file. Not knowing how to test those
// other cases I left them alone. At least the changes to
// setupExcept and endExcept will insure that the generated JavaScript
// will be syntactically correct. The worst that will happen is that when
// Note - this change may need to be adjusted for all the other instances of
// switchCode and suffixCode in this file. Not knowing how to test those
// other cases I left them alone. At least the changes to
// setupExcept and endExcept will insure that the generated JavaScript
// will be syntactically correct. The worst that will happen is that when
// code in a try block blows up, we will not know to run the except block.
// The other problem is that we might catch something that is really an internal
// error - it might be nice to add code in the above catch block that looked at
// the kind of exception and only popped the stack for exceptions that are
// from the original code rather than artifacts of some code generation or
// error - it might be nice to add code in the above catch block that looked at
// the kind of exception and only popped the stack for exceptions that are
// from the original code rather than artifacts of some code generation or
// exeution environment error. We at least err on the side of exceptions
// being revealed to the user. drchuck - Wed Jan 23 19:20:18 EST 2013

Expand Down
10 changes: 9 additions & 1 deletion src/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ Sk.builtin.__import__ = function(name, globals, locals, fromlist)
return ret;
};

Sk.importStar = function(module,loc) {
var props = Object['getOwnPropertyNames'](module['$d'])
for(var i in props) {
loc[props[i]] = module['$d'][props[i]];
}
}

goog.exportSymbol("Sk.importMain", Sk.importMain);
goog.exportSymbol("Sk.importMainWithBody", Sk.importMainWithBody);
goog.exportSymbol("Sk.builtin.__import__", Sk.builtin.__import__);
goog.exportSymbol("Sk.builtin.__import__", Sk.builtin.__import__);
goog.exportSymbol("Sk.importStar", Sk.importStar);
7 changes: 7 additions & 0 deletions test/run/t345.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from math import *

print cos(pi)
print pi
print log(100,2)
print pow(2,24)

4 changes: 4 additions & 0 deletions test/run/t345.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-1.0
3.141592653589793
6.643856189774725
16777216.0
4 changes: 4 additions & 0 deletions test/run/t345.py.real.force
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-1.0
3.141592653589793
6.643856189774725
16777216.0
54 changes: 54 additions & 0 deletions test/run/t345.py.symtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
-- Identifiers --
name: cos
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: log
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: pi
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: pow
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: [
]
35 changes: 35 additions & 0 deletions test/run/t345.trans
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Module(body=[ImportFrom(module='math',
names=[alias(name='*',
asname=None)],
level=0),
Print(dest=None,
values=[Call(func=Name(id='cos',
ctx=Load()),
args=[Name(id='pi',
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Name(id='pi',
ctx=Load())],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='log',
ctx=Load()),
args=[Num(n=100),
Num(n=2)],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='pow',
ctx=Load()),
args=[Num(n=2),
Num(n=24)],
keywords=[],
starargs=None,
kwargs=None)],
nl=True)])

0 comments on commit f93bc43

Please sign in to comment.