Skip to content

Commit

Permalink
add interp.flush
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Abraham <[email protected]>
  • Loading branch information
jabraham17 committed Feb 3, 2025
1 parent 6348fe3 commit 7dda356
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
27 changes: 23 additions & 4 deletions modules/packages/Python.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,14 @@
var interp = new Interpreter();
var func = new Function(interp, "lambda x,: print(x)");
var sys = new Module(interp, "sys");
var pyStdout = sys.getAttr(owned Value, "stdout");
var pyStdoutFlush = pyStdout.getAttr(owned Value, "flush");
writeln("Hello from Chapel");
writeln("Let's call some Python!");
IO.stdout.flush(); // flush the Chapel output buffer before calling Python
func(NoneType, "Hello, World!");
func(NoneType, "Goodbye, World!");
pyStdoutFlush(NoneType); // flush the Python output buffer before calling Chapel again
interp.flush(); // flush the Python output buffer before calling Chapel again
writeln("Back to Chapel");
Expand Down Expand Up @@ -728,6 +725,26 @@ module Python {
}
}

/*
Flush the standard output buffers of the Python interpreter. This is
useful when mixing Python and Chapel I/O to ensure that the output is
displayed in the correct order.
*/
inline proc flush(flushStderr: bool = false) throws {
var stdout = PySys_GetObject("stdout");
if stdout == nil then throw new ChapelException("stdout not found");

var flushStr = this.toPython("flush");
defer Py_DECREF(flushStr);

PyObject_CallMethodNoArgs(stdout, flushStr);
if flushStderr {
var stderr = PySys_GetObject("stderr");
if stderr == nil then throw new ChapelException("stderr not found");
PyObject_CallMethodNoArgs(stderr, flushStr);
}
}

@chpldoc.nodoc
inline proc importModule(in modName: string): PyObjectPtr throws {
var mod = PyImport_ImportModule(modName.c_str());
Expand Down Expand Up @@ -2031,6 +2048,8 @@ module Python {
extern "chpl_PY_MINOR_VERSION" const PY_MINOR_VERSION: c_ulong;
extern "chpl_PY_MICRO_VERSION" const PY_MICRO_VERSION: c_ulong;

extern proc PySys_GetObject(name: c_ptrConst(c_char)): PyObjectPtr;


/*
Sub Interpreters
Expand Down
60 changes: 28 additions & 32 deletions test/library/packages/Python/correctness/argPassingTest.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ use Python;
import Reflection;
use IO;

proc test_no_args(mod: borrowed Module, flush: borrowed Value) {
proc test_no_args(mod: borrowed Module) {
const funcName = "no_args";
var func = new Function(mod, funcName);

func(NoneType);

// error: wrong return type
try { func(int); }
catch e: PythonException { flush(NoneType); writeln("Caught PythonException: ", e.message()); IO.stdout.flush(); }
catch { flush(NoneType); writeln("Caught unknown exception"); IO.stdout.flush(); }
catch e: PythonException { mod.interpreter.flush(); writeln("Caught PythonException: ", e.message()); IO.stdout.flush(); }
catch { mod.interpreter.flush(); writeln("Caught unknown exception"); IO.stdout.flush(); }

// error: too many args
try { func(NoneType, 2); }
catch e: PythonException { flush(NoneType); writeln("Caught PythonException: ", e.message()); IO.stdout.flush(); }
catch { flush(NoneType); writeln("Caught unknown exception"); IO.stdout.flush(); }
catch e: PythonException { mod.interpreter.flush(); writeln("Caught PythonException: ", e.message()); IO.stdout.flush(); }
catch { mod.interpreter.flush(); writeln("Caught unknown exception"); IO.stdout.flush(); }
}
proc test_one_arg(mod: borrowed Module, flush: borrowed Value) {
proc test_one_arg(mod: borrowed Module) {
const funcName = "one_arg";
var func = new Function(mod, funcName);

func(NoneType, 1);

// error: not enough args
try { func(NoneType); }
catch e: PythonException { flush(NoneType); writeln("Caught PythonException: ", e.message()); IO.stdout.flush(); }
catch { flush(NoneType); writeln("Caught unknown exception"); IO.stdout.flush(); }
catch e: PythonException { mod.interpreter.flush(); writeln("Caught PythonException: ", e.message()); IO.stdout.flush(); }
catch { mod.interpreter.flush(); writeln("Caught unknown exception"); IO.stdout.flush(); }

// error: too many args
try { func(NoneType, 2, 3); }
catch e: PythonException { flush(NoneType); writeln("Caught PythonException: ", e.message()); IO.stdout.flush(); }
catch { flush(NoneType); writeln("Caught unknown exception"); IO.stdout.flush(); }
catch e: PythonException { mod.interpreter.flush(); writeln("Caught PythonException: ", e.message()); IO.stdout.flush(); }
catch { mod.interpreter.flush(); writeln("Caught unknown exception"); IO.stdout.flush(); }
}
proc test_two_args(mod: borrowed Module, flush: borrowed Value) {
proc test_two_args(mod: borrowed Module) {
const funcName = "two_args";
var func = new Function(mod, funcName);

Expand All @@ -44,16 +44,16 @@ proc test_two_args(mod: borrowed Module, flush: borrowed Value) {

// error: not enough args
try { func(NoneType, 3); }
catch e: PythonException { flush(NoneType); writeln("Caught PythonException: ", e.message()); IO.stdout.flush(); }
catch { flush(NoneType); writeln("Caught unknown exception"); IO.stdout.flush(); }
catch e: PythonException { mod.interpreter.flush(); writeln("Caught PythonException: ", e.message()); IO.stdout.flush(); }
catch { mod.interpreter.flush(); writeln("Caught unknown exception"); IO.stdout.flush(); }
}
proc test_three_args(mod: borrowed Module, flush: borrowed Value) {
proc test_three_args(mod: borrowed Module) {
const funcName = "three_args";
var func = new Function(mod, funcName);

func(NoneType, 1, 2, 3);
}
proc test_varargs(mod: borrowed Module, flush: borrowed Value) {
proc test_varargs(mod: borrowed Module) {
const funcName = "varargs";
var func = new Function(mod, funcName);

Expand All @@ -65,14 +65,14 @@ proc test_varargs(mod: borrowed Module, flush: borrowed Value) {
func(NoneType, 1, 2, 3, [1,2,3,], 4);
func(NoneType, 1, 2, 3, [1,2,3,], 4, ["key" => "value", "key2" => "value2"]);
}
proc test_one_arg_with_default(mod: borrowed Module, flush: borrowed Value) {
proc test_one_arg_with_default(mod: borrowed Module) {
const funcName = "one_arg_with_default";
var func = new Function(mod, funcName);

func(NoneType);
func(NoneType, 7);
}
proc test_three_args_with_default(mod: borrowed Module, flush: borrowed Value) {
proc test_three_args_with_default(mod: borrowed Module) {
const funcName = "three_args_with_default";
var func = new Function(mod, funcName);

Expand All @@ -81,7 +81,7 @@ proc test_three_args_with_default(mod: borrowed Module, flush: borrowed Value) {
func(NoneType, 8, 9, 10);
func(NoneType, 8, kwargs=["c" => 10]);
}
proc test_three_args_with_default_and_kwargs(mod: borrowed Module, flush: borrowed Value) {
proc test_three_args_with_default_and_kwargs(mod: borrowed Module) {
const funcName = "three_args_with_default_and_kwargs";
var func = new Function(mod, funcName);

Expand All @@ -91,7 +91,7 @@ proc test_three_args_with_default_and_kwargs(mod: borrowed Module, flush: borrow
func(NoneType, 8, kwargs=["b" => 10]);
func(NoneType, 8, kwargs=["c" => 11, "abc" => 12]);
}
proc test_varargs_and_kwargs(mod: borrowed Module, flush: borrowed Value) {
proc test_varargs_and_kwargs(mod: borrowed Module) {
const funcName = "varargs_and_kwargs";
var func = new Function(mod, funcName);

Expand All @@ -110,19 +110,15 @@ proc main() {
var modName = Reflection.getModuleName();
var m = new Module(interp, modName);

var sys = new Module(interp, "sys");
var stdout = sys.getAttr(owned Value, "stdout");
var flush = stdout.getAttr(owned Value, "flush");

test_no_args(m, flush);
test_one_arg(m, flush);
test_two_args(m, flush);
test_three_args(m, flush);
test_varargs(m, flush);
test_one_arg_with_default(m, flush);
test_three_args_with_default(m, flush);
test_three_args_with_default_and_kwargs(m, flush);
test_varargs_and_kwargs(m, flush);
test_no_args(m);
test_one_arg(m);
test_two_args(m);
test_three_args(m);
test_varargs(m);
test_one_arg_with_default(m);
test_three_args_with_default(m);
test_three_args_with_default_and_kwargs(m);
test_varargs_and_kwargs(m);

}

0 comments on commit 7dda356

Please sign in to comment.