@@ -36,31 +36,40 @@ namespace xpyt
3636 xstream (std::string stream_name);
3737 virtual ~xstream ();
3838
39- void write (const std::string& message);
39+ py::object get_write ();
40+ void set_write (const py::object& func);
4041 void flush ();
4142 bool isatty ();
4243
4344 private:
4445
4546 std::string m_stream_name;
47+ py::object m_write_func;
4648 };
4749
4850 /* *************************
4951 * xstream implementation *
5052 **************************/
5153
5254 xstream::xstream (std::string stream_name)
53- : m_stream_name(stream_name)
55+ : m_stream_name(stream_name), m_write_func(py::cpp_function([stream_name](const std::string& message) {
56+ xeus::get_interpreter ().publish_stream (stream_name, message);
57+ }))
5458 {
5559 }
5660
5761 xstream::~xstream ()
5862 {
5963 }
6064
61- void xstream::write ( const std::string& message )
65+ py::object xstream::get_write ( )
6266 {
63- xeus::get_interpreter ().publish_stream (m_stream_name, message);
67+ return m_write_func;
68+ }
69+
70+ void xstream::set_write (const py::object& func)
71+ {
72+ m_write_func = func;
6473 }
6574
6675 void xstream::flush ()
@@ -83,25 +92,38 @@ namespace xpyt
8392 xterminal_stream ();
8493 virtual ~xterminal_stream ();
8594
86- void write (const std::string& message);
95+ py::object get_write ();
96+ void set_write (const py::object& func);
8797 void flush ();
98+
99+ private:
100+
101+ py::object m_write_func;
88102 };
89103
90104 /* **********************************
91105 * xterminal_stream implementation *
92106 ***********************************/
93107
94108 xterminal_stream::xterminal_stream ()
109+ : m_write_func(py::cpp_function([](const std::string& message) {
110+ std::cout << message;
111+ }))
95112 {
96113 }
97114
98115 xterminal_stream::~xterminal_stream ()
99116 {
100117 }
101118
102- void xterminal_stream::write (const std::string& message)
119+ py::object xterminal_stream::get_write ()
120+ {
121+ return m_write_func;
122+ }
123+
124+ void xterminal_stream::set_write (const py::object& func)
103125 {
104- std::cout << message ;
126+ m_write_func = func ;
105127 }
106128
107129 void xterminal_stream::flush ()
@@ -118,13 +140,13 @@ namespace xpyt
118140
119141 py::class_<xstream>(stream_module, " Stream" )
120142 .def (py::init<std::string>())
121- .def (" write" , &xstream::write )
143+ .def_property (" write" , &xstream::get_write, &xstream::set_write )
122144 .def (" flush" , &xstream::flush)
123145 .def (" isatty" , &xstream::isatty);
124146
125147 py::class_<xterminal_stream>(stream_module, " TerminalStream" )
126148 .def (py::init<>())
127- .def (" write" , &xterminal_stream::write )
149+ .def_property (" write" , &xterminal_stream::get_write, &xterminal_stream::set_write )
128150 .def (" flush" , &xterminal_stream::flush);
129151
130152 return stream_module;
0 commit comments