Skip to content

Commit aae2d03

Browse files
committed
GR-10407: fix cell var registration for local imports
- added relevant unittest - minor name change in python_unittests runner script
1 parent 1b2acb3 commit aae2d03

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

graalpython/com.oracle.graal.python.test/src/python_unittests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
import os
4040
import re
4141
import subprocess
42+
import sys
4243
from collections import defaultdict
4344
from json import dumps
4445
from multiprocessing import Pool
4546
from pprint import pformat
47+
from time import gmtime, strftime
4648

4749
import argparse
48-
import sys
49-
from time import gmtime, strftime
5050

5151
# global CLI flags
5252
flags = None
@@ -522,7 +522,7 @@ def format_val(row, k):
522522
'''
523523

524524
missing_modules_info = ul('missing modules', [
525-
'<b>{}</b>&nbsp;<span class="text-muted">count: {}</span>'.format(name, cnt)
525+
'<b>{}</b>&nbsp;<span class="text-muted">imported by {} unittests</span>'.format(name, cnt)
526526
for cnt, name in sorted(((cnt, name) for name, cnt in missing_modules.items()), reverse=True)
527527
])
528528

graalpython/com.oracle.graal.python.test/src/tests/test_scope.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,3 +812,18 @@ def method(self):
812812
assert not done
813813
a.method()
814814
assert done
815+
816+
817+
def test_import_name():
818+
from os import environ
819+
820+
def func_1(a):
821+
def func_2(x=environ):
822+
x['my-key'] = a
823+
return x
824+
return func_2()
825+
826+
val = func_1('test-val')
827+
assert 'my-key' in val
828+
assert val['my-key'] == 'test-val'
829+
del environ['my-key']

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/ScopeTranslator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ public T visitLambdef_body(Python3Parser.Lambdef_bodyContext ctx) {
180180
public T visitImport_as_name(Python3Parser.Import_as_nameContext ctx) {
181181
List<TerminalNode> name = ctx.NAME();
182182
if (name.size() == 1) {
183-
environment.createLocal(name.get(0).getText());
183+
String asName = name.get(0).getText();
184+
environment.createLocal(asName);
184185
} else if (name.size() == 2) {
185-
environment.createLocal(name.get(1).getText());
186+
String asName = name.get(1).getText();
187+
environment.createLocal(asName);
186188
} else {
187189
throw new RuntimeException("Impossible case");
188190
}
@@ -369,7 +371,6 @@ public T visitDefparameter(Python3Parser.DefparameterContext ctx) {
369371
if (ctx.test() != null) {
370372
String identifier = ctx.test().getText();
371373
environment.registerCellVariable(identifier);
372-
return null;
373374
}
374375
}
375376
return super.visitDefparameter(ctx);

0 commit comments

Comments
 (0)