Skip to content

Commit d17d29c

Browse files
committed
[GR-45906] Improve the embedder ux by returning the module itself when evaluating python code and the code does not end with an expression.
PullRequest: graalpython/2761
2 parents a307328 + 53ea2ff commit d17d29c

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/interop/InteropLibraryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -143,7 +143,7 @@ public void testPListRemovable() {
143143

144144
@Test
145145
public void testIsNull() {
146-
assertTrue(v("None").isNull());
146+
assertTrue(v("[None]").getArrayElement(0).isNull());
147147
}
148148

149149
@Test

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ def test_internal_languages_dont_eval():
291291

292292
assert polyglot.eval(language="python", string="21 * 2") == 42
293293

294+
def test_module_eval_returns_last_expr():
295+
assert polyglot.eval(language="python", string="x = 2; x") == 2
296+
297+
def test_module_eval_returns_module():
298+
mod = polyglot.eval(language="python", string="x = 2")
299+
assert mod.x == 2
300+
assert type(mod) == type(sys)
301+
294302
@skipIf(is_native, "not supported in native mode")
295303
def test_non_index_array_access():
296304
import java

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/exception/TopLevelExceptionHandler.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,25 @@ private Object run(VirtualFrame frame) {
292292
PArguments.setArgument(arguments, i, frame.getArguments()[i]);
293293
}
294294
PythonContext pythonContext = getContext();
295+
PythonModule mainModule = null;
295296
if (getSourceSection().getSource().isInternal()) {
296297
// internal sources are not run in the main module
297298
PArguments.setGlobals(arguments, pythonContext.factory().createDict());
298299
} else {
299-
PythonModule mainModule = pythonContext.getMainModule();
300+
mainModule = pythonContext.getMainModule();
300301
PDict mainDict = GetOrCreateDictNode.getUncached().execute(mainModule);
301302
PArguments.setGlobals(arguments, mainModule);
302303
PArguments.setSpecialArgument(arguments, mainDict);
303304
PArguments.setException(arguments, PException.NO_EXCEPTION);
304305
}
305306
Object state = IndirectCalleeContext.enterIndirect(getPythonLanguage(), pythonContext, arguments);
306307
try {
307-
return innerCallTarget.call(arguments);
308+
Object result = innerCallTarget.call(arguments);
309+
if (mainModule != null && result == PNone.NONE) {
310+
return mainModule;
311+
} else {
312+
return result;
313+
}
308314
} finally {
309315
IndirectCalleeContext.exit(getPythonLanguage(), pythonContext, state);
310316
}

0 commit comments

Comments
 (0)