File tree Expand file tree Collapse file tree 3 files changed +26
-10
lines changed
Expand file tree Collapse file tree 3 files changed +26
-10
lines changed Original file line number Diff line number Diff line change @@ -12934,16 +12934,22 @@ def test_sys_stdout_override(self):
1293412934 def foo():
1293512935 print('foo')
1293612936
12937+ class Redirect(object):
12938+ def __init__(self):
12939+ self.s = ''
12940+
12941+ def write(self, s : str):
12942+ self.s += s
12943+
1293712944 old_stdout = sys.stdout
12938- with tempfile.TemporaryFile() as f:
12939- try:
12940- sys.stdout = f
12941- foo()
12942- finally:
12943- sys.stdout = old_stdout
12945+ redirect = Redirect()
12946+ try:
12947+ sys.stdout = redirect
12948+ foo()
12949+ finally:
12950+ sys.stdout = old_stdout
1294412951
12945- f.seek(0)
12946- FileCheck().check('foo').run(f.read())
12952+ FileCheck().check('foo').run(redirect.s)
1294712953
1294812954 def test_optional_tuple(self):
1294912955 def fn(x=None):
Original file line number Diff line number Diff line change @@ -569,8 +569,17 @@ void initJITBindings(PyObject* module) {
569569
570570 setPrintHandler ([](const std::string& str) {
571571 py::gil_scoped_acquire acquire;
572- auto _stdout = py::module::import (" sys" ).attr (" stdout" );
573- _stdout.attr (" write" )(py::bytes (str));
572+ try {
573+ auto _stdout = py::module::import (" sys" ).attr (" stdout" );
574+ _stdout.attr (" write" )(str);
575+ } catch (pybind11::error_already_set e) {
576+ TORCH_WARN (
577+ " Exception occured when printing to Python stdout: " ,
578+ e.what (),
579+ " . Falling back to C++ stdout."
580+ " Please report a bug to PyTorch" );
581+ std::cout << str << std::endl;
582+ }
574583 });
575584}
576585} // namespace jit
Original file line number Diff line number Diff line change 11#include < torch/csrc/jit/print_handler.h>
22
33#include < iostream>
4+ #include < string>
45
56namespace torch {
67namespace jit {
You can’t perform that action at this time.
0 commit comments