Skip to content

Commit 90c0674

Browse files
author
James Reed
committed
fuck
1 parent d089ea3 commit 90c0674

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

test/test_jit.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff 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):

torch/csrc/jit/init.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff 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

torch/csrc/jit/print_handler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <torch/csrc/jit/print_handler.h>
22

33
#include <iostream>
4+
#include <string>
45

56
namespace torch {
67
namespace jit {

0 commit comments

Comments
 (0)