Skip to content

Commit bd64869

Browse files
committed
fix None kwargs to print in vm and in wasm_builtins
1 parent a2ab447 commit bd64869

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

tests/snippets/printing.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
except TypeError:
66
pass
77
else:
8-
assert False, "Expected TypeError on wrong type passed to end"
8+
assert False, 'Expected TypeError on wrong type passed to end'
99

1010
try:
1111
print('test', sep=['a'])
1212
except TypeError:
1313
pass
1414
else:
15-
assert False, "Expected TypeError on wrong type passed to sep"
15+
assert False, 'Expected TypeError on wrong type passed to sep'
16+
17+
try:
18+
print('test', end=None, sep=None, flush=None)
19+
except:
20+
assert False, 'Expected None passed to end, sep, and flush to not raise errors'

vm/src/builtins.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,9 @@ pub fn builtin_print(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
572572
trace!("print called with {:?}", args);
573573

574574
// Handle 'sep' kwarg:
575-
let sep_arg = args.get_optional_kwarg("sep");
575+
let sep_arg = args
576+
.get_optional_kwarg("sep")
577+
.filter(|obj| !obj.is(&vm.get_none()));
576578
if let Some(ref obj) = sep_arg {
577579
if !objtype::isinstance(obj, &vm.ctx.str_type()) {
578580
return Err(vm.new_type_error(format!(
@@ -584,7 +586,9 @@ pub fn builtin_print(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
584586
let sep_str = sep_arg.as_ref().map(|obj| objstr::get_value_as_ref(obj));
585587

586588
// Handle 'end' kwarg:
587-
let end_arg = args.get_optional_kwarg("end");
589+
let end_arg = args
590+
.get_optional_kwarg("end")
591+
.filter(|obj| !obj.is(&vm.get_none()));
588592
if let Some(ref obj) = end_arg {
589593
if !objtype::isinstance(obj, &vm.ctx.str_type()) {
590594
return Err(vm.new_type_error(format!(

wasm/lib/src/wasm_builtins.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate web_sys;
1111
use crate::js_to_py;
1212
use js_sys::Array;
1313
use rustpython_vm::obj::{objstr, objtype};
14-
use rustpython_vm::pyobject::{PyFuncArgs, PyObjectRef, PyResult};
14+
use rustpython_vm::pyobject::{IdProtocol, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
1515
use rustpython_vm::VirtualMachine;
1616
use wasm_bindgen::{JsCast, JsValue};
1717
use web_sys::{console, window, HtmlTextAreaElement};
@@ -33,7 +33,9 @@ pub fn print_to_html(text: &str, selector: &str) -> Result<(), JsValue> {
3333

3434
pub fn format_print_args(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<String, PyObjectRef> {
3535
// Handle 'sep' kwarg:
36-
let sep_arg = args.get_optional_kwarg("sep");
36+
let sep_arg = args
37+
.get_optional_kwarg("sep")
38+
.filter(|obj| !obj.is(&vm.get_none()));
3739
if let Some(ref obj) = sep_arg {
3840
if !objtype::isinstance(obj, &vm.ctx.str_type()) {
3941
return Err(vm.new_type_error(format!(
@@ -45,7 +47,9 @@ pub fn format_print_args(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<St
4547
let sep_str = sep_arg.as_ref().map(|obj| objstr::get_value_as_ref(obj));
4648

4749
// Handle 'end' kwarg:
48-
let end_arg = args.get_optional_kwarg("end");
50+
let end_arg = args
51+
.get_optional_kwarg("end")
52+
.filter(|obj| !obj.is(&vm.get_none()));
4953
if let Some(ref obj) = end_arg {
5054
if !objtype::isinstance(obj, &vm.ctx.str_type()) {
5155
return Err(vm.new_type_error(format!(
@@ -63,10 +67,17 @@ pub fn format_print_args(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<St
6367
for a in args.args {
6468
if first {
6569
first = false;
70+
} else if let Some(ref sep_str) = sep_str {
71+
output.push_str(sep_str);
6672
} else {
67-
output.push_str(" ");
73+
output.push(' ');
6874
}
6975
output.push_str(&vm.to_pystr(&a)?);
76+
}
77+
78+
if let Some(end_str) = end_str {
79+
output.push_str(end_str.as_ref())
80+
} else {
7081
output.push('\n');
7182
}
7283
Ok(output)

0 commit comments

Comments
 (0)