From 382eedd6200d1284f2887cbed2ceb81b21dcd124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20B=C3=A9langer?= Date: Thu, 20 Jan 2022 22:13:38 -0500 Subject: [PATCH 1/4] Allow print 'end' kwarg. --- include/lang/py/py.js | 4 ++-- pyinterp/pyinterp/pyinterp/__init__.py | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/lang/py/py.js b/include/lang/py/py.js index 0edbf64e..77e76498 100644 --- a/include/lang/py/py.js +++ b/include/lang/py/py.js @@ -352,8 +352,8 @@ LangPy.prototype.initRunTimeState = function (reboot) { } }; -function runtime_print(msg, rte) { - rte.vm.replAddTranscript(msg + '\n', 'cb-repl-output'); +function runtime_print(msg, end, rte) { + rte.vm.replAddTranscript(msg + end, 'cb-repl-output'); rte.vm.lang.refreshUI(); }; diff --git a/pyinterp/pyinterp/pyinterp/__init__.py b/pyinterp/pyinterp/pyinterp/__init__.py index 7ec89507..7622e4be 100644 --- a/pyinterp/pyinterp/pyinterp/__init__.py +++ b/pyinterp/pyinterp/pyinterp/__init__.py @@ -7268,6 +7268,14 @@ def om_print_code(rte, _): args_seq = OM_get_list_seq(args) args_len = len(args_seq) + end = rte_lookup_locals(rte, 'end') + + if not om_isinstance(end, class_str): + ctx = make_out_of_ast_context(rte, om_None) + return sem_raise_with_message(ctx, class_TypeError, "end must be None or a string, not int") + + end = OM_get_boxed_value(end) + result = [] for _ in args_seq: result.append(None) @@ -7281,7 +7289,7 @@ def print_value(rte, str_result): return stmt_end(make_out_of_ast_context(rte, lambda rte: loop(rte, new_index)), 0) else: # sep=' ' - runtime_print(' '.join(result), rte) + runtime_print(' '.join(result), end, rte) return unwind_return(rte, om_None) next_ctx = make_out_of_ast_context(rte, print_value) @@ -7290,7 +7298,7 @@ def print_value(rte, str_result): if args_len > 0: return loop(rte, 0) else: - runtime_print('', rte) + runtime_print('', end, rte) return unwind_return(rte, om_None) def om_alert_code(rte, _): @@ -8622,7 +8630,8 @@ def fresh_rte(options): rte = make_new_rte(globals_env, locals_env, builtins_env, builtins_module, options) # Vanilla Python builtins - om_builtin_print = om_make_builtin_function_with_signature('print', om_print_code, make_vararg_only_signature('args')) + om_builtin_print = om_make_builtin_function_with_signature('print', om_print_code, + make_signature((), (), 'args', ('end',), ('\n',), None, ())) om_builtin_breakpoint = om_make_builtin_function_with_signature('breakpoint', om_breakpoint_code, empty_signature) om_builtin_input = om_make_builtin_function_with_signature('input', om_input_code, make_posonly_defaults_signature(('obj',), (om_str(''),))) om_builtin_hex = om_make_builtin_function_with_signature('hex', om_hex_code, make_posonly_only_signature(('obj',))) From e7877c5e3e011c9521488d53664494ca6912503b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20B=C3=A9langer?= Date: Thu, 20 Jan 2022 23:11:53 -0500 Subject: [PATCH 2/4] Default 'end' value has to be Python object. --- pyinterp/pyinterp/pyinterp/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyinterp/pyinterp/pyinterp/__init__.py b/pyinterp/pyinterp/pyinterp/__init__.py index 7622e4be..ceb851fd 100644 --- a/pyinterp/pyinterp/pyinterp/__init__.py +++ b/pyinterp/pyinterp/pyinterp/__init__.py @@ -8631,7 +8631,7 @@ def fresh_rte(options): # Vanilla Python builtins om_builtin_print = om_make_builtin_function_with_signature('print', om_print_code, - make_signature((), (), 'args', ('end',), ('\n',), None, ())) + make_signature((), (), 'args', ('end',), (om_str("\n"),), None, ())) om_builtin_breakpoint = om_make_builtin_function_with_signature('breakpoint', om_breakpoint_code, empty_signature) om_builtin_input = om_make_builtin_function_with_signature('input', om_input_code, make_posonly_defaults_signature(('obj',), (om_str(''),))) om_builtin_hex = om_make_builtin_function_with_signature('hex', om_hex_code, make_posonly_only_signature(('obj',))) From 2536b0d3794f3a618de4d98b29a066c35e9a3f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20B=C3=A9langer?= Date: Fri, 21 Jan 2022 13:44:59 -0500 Subject: [PATCH 3/4] Handle 'sep' in print. --- include/lang/py/py.js | 4 ++-- pyinterp/pyinterp/pyinterp/__init__.py | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/lang/py/py.js b/include/lang/py/py.js index 77e76498..fd33f22a 100644 --- a/include/lang/py/py.js +++ b/include/lang/py/py.js @@ -352,8 +352,8 @@ LangPy.prototype.initRunTimeState = function (reboot) { } }; -function runtime_print(msg, end, rte) { - rte.vm.replAddTranscript(msg + end, 'cb-repl-output'); +function runtime_print(msg, rte) { + rte.vm.replAddTranscript(msg, 'cb-repl-output'); rte.vm.lang.refreshUI(); }; diff --git a/pyinterp/pyinterp/pyinterp/__init__.py b/pyinterp/pyinterp/pyinterp/__init__.py index ceb851fd..8c34f8c4 100644 --- a/pyinterp/pyinterp/pyinterp/__init__.py +++ b/pyinterp/pyinterp/pyinterp/__init__.py @@ -7268,12 +7268,18 @@ def om_print_code(rte, _): args_seq = OM_get_list_seq(args) args_len = len(args_seq) + sep = rte_lookup_locals(rte, 'sep') end = rte_lookup_locals(rte, 'end') - if not om_isinstance(end, class_str): + if not (om_isinstance(sep, class_str) or om_isinstance(sep, class_NoneType)): ctx = make_out_of_ast_context(rte, om_None) - return sem_raise_with_message(ctx, class_TypeError, "end must be None or a string, not int") + return sem_raise_with_message(ctx, class_TypeError, "sep must be None or a string") + if not (om_isinstance(end, class_str) or om_isinstance(end, class_NoneType)): + ctx = make_out_of_ast_context(rte, om_None) + return sem_raise_with_message(ctx, class_TypeError, "end must be None or a string") + + sep = OM_get_boxed_value(sep) end = OM_get_boxed_value(end) result = [] @@ -7288,8 +7294,7 @@ def print_value(rte, str_result): if new_index < args_len: return stmt_end(make_out_of_ast_context(rte, lambda rte: loop(rte, new_index)), 0) else: - # sep=' ' - runtime_print(' '.join(result), end, rte) + runtime_print(sep.join(result) + end, rte) return unwind_return(rte, om_None) next_ctx = make_out_of_ast_context(rte, print_value) @@ -8631,7 +8636,7 @@ def fresh_rte(options): # Vanilla Python builtins om_builtin_print = om_make_builtin_function_with_signature('print', om_print_code, - make_signature((), (), 'args', ('end',), (om_str("\n"),), None, ())) + make_signature((), (), 'args', ('sep', 'end'), (om_str(" "), om_str("\n")), None, ())) om_builtin_breakpoint = om_make_builtin_function_with_signature('breakpoint', om_breakpoint_code, empty_signature) om_builtin_input = om_make_builtin_function_with_signature('input', om_input_code, make_posonly_defaults_signature(('obj',), (om_str(''),))) om_builtin_hex = om_make_builtin_function_with_signature('hex', om_hex_code, make_posonly_only_signature(('obj',))) From 051d8d25fde9159ef01ff56d669accadbd501b1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20B=C3=A9langer?= Date: Fri, 21 Jan 2022 13:53:27 -0500 Subject: [PATCH 4/4] Fix runtime_print call. --- pyinterp/pyinterp/pyinterp/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyinterp/pyinterp/pyinterp/__init__.py b/pyinterp/pyinterp/pyinterp/__init__.py index 8c34f8c4..87b566f4 100644 --- a/pyinterp/pyinterp/pyinterp/__init__.py +++ b/pyinterp/pyinterp/pyinterp/__init__.py @@ -7303,7 +7303,7 @@ def print_value(rte, str_result): if args_len > 0: return loop(rte, 0) else: - runtime_print('', end, rte) + runtime_print('' + end, rte) return unwind_return(rte, om_None) def om_alert_code(rte, _):