@@ -67,10 +67,20 @@ static PyModule *s_builtin_module = nullptr;
6767
6868namespace {
6969
70- PyResult<PyObject *> print (const PyTuple *args, const PyDict *kwargs, Interpreter &)
70+ PyResult<PyObject *> print (const PyTuple *args, const PyDict *kwargs, Interpreter &interpreter )
7171{
72- std::string separator = " " ;
73- std::string end = " \n " ;
72+ auto *separator = PyString::create (" " ).unwrap ();
73+ auto *end = PyString::create (" \n " ).unwrap ();
74+ // TODO: handle error case?
75+ PyObject *file =
76+ PyObject::from (interpreter.get_imported_module (PyString::create (" sys" ).unwrap ())
77+ ->symbol_table ()
78+ ->map ()
79+ .at (String{ " stdout" }))
80+ .unwrap ();
81+ // sys.stdout may be None when FILE* stdout isn't connected
82+ if (!file || file == py_none ()) { return Ok (py_none ()); }
83+ bool flush = true ;
7484 if (kwargs) {
7585 static const Value separator_keyword = String{ " sep" };
7686 static const Value end_keyword = String{ " end" };
@@ -84,7 +94,7 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete
8494 return Err (type_error (
8595 " sep must be None or a string, not {}" , obj.unwrap ()->type ()->name ()));
8696 }
87- separator = std::get<String>(maybe_str).s ;
97+ separator = PyString::create ( std::get<String>(maybe_str).s ). unwrap () ;
8898 }
8999 if (auto it = kwargs->map ().find (end_keyword); it != kwargs->map ().end ()) {
90100 auto maybe_str = it->second ;
@@ -95,9 +105,12 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete
95105 return Err (type_error (
96106 " end must be None or a string, not {}" , obj.unwrap ()->type ()->name ()));
97107 }
98- end = std::get<String>(maybe_str).s ;
108+ end = PyString::create ( std::get<String>(maybe_str).s ). unwrap () ;
99109 }
100110 }
111+ auto file_write_ = file->get_method (PyString::create (" write" ).unwrap ());
112+ if (file_write_.is_err ()) { return file_write_; }
113+ auto file_write = file_write_.unwrap ();
101114 auto strfunc = [](const PyResult<PyObject *> &arg) -> PyResult<PyString *> {
102115 if (arg.is_err ()) return Err (arg.unwrap_err ());
103116 return arg.unwrap ()->str ();
@@ -106,7 +119,16 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete
106119 auto arg_it = args->begin ();
107120 auto arg_it_end = args->end ();
108121 if (arg_it == arg_it_end) {
109- std::cout << std::endl;
122+ if (auto result =
123+ file_write->call (PyTuple::create (PyString::create (end).unwrap ()).unwrap (), nullptr );
124+ result.is_err ()) {
125+ return result;
126+ }
127+ if (flush) {
128+ auto file_flush_ = file->get_method (PyString::create (" flush" ).unwrap ());
129+ if (file_flush_.is_err ()) { return file_flush_; }
130+ return file_flush_.unwrap ()->call (nullptr , nullptr );
131+ }
110132 return Ok (py_none ());
111133 }
112134 --arg_it_end;
@@ -117,7 +139,14 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete
117139 if (reprobj_.is_err ()) { return reprobj_; }
118140 auto reprobj = reprobj_.unwrap ();
119141 spdlog::debug (" repr result: {}" , reprobj->value ());
120- std::cout << reprobj->value () << separator;
142+ if (auto result = file_write->call (PyTuple::create (reprobj).unwrap (), nullptr );
143+ result.is_err ()) {
144+ return result;
145+ }
146+ if (auto result = file_write->call (PyTuple::create (separator).unwrap (), nullptr );
147+ result.is_err ()) {
148+ return result;
149+ }
121150 std::advance (arg_it, 1 );
122151 }
123152
@@ -126,8 +155,21 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete
126155 if (reprobj_.is_err ()) { return reprobj_; }
127156 auto reprobj = reprobj_.unwrap ();
128157 spdlog::debug (" repr result: {}" , reprobj->value ());
129- std::cout << reprobj->value () << end;
158+ if (auto result = file_write->call (PyTuple::create (reprobj).unwrap (), nullptr );
159+ result.is_err ()) {
160+ return result;
161+ }
130162
163+ if (auto result =
164+ file_write->call (PyTuple::create (PyString::create (end).unwrap ()).unwrap (), nullptr );
165+ result.is_err ()) {
166+ return result;
167+ }
168+ if (flush) {
169+ auto file_flush_ = file->get_method (PyString::create (" flush" ).unwrap ());
170+ if (file_flush_.is_err ()) { return file_flush_; }
171+ return file_flush_.unwrap ()->call (nullptr , nullptr );
172+ }
131173 return Ok (py_none ());
132174}
133175
0 commit comments