File tree 4 files changed +54
-13
lines changed
include/roboptim/core/python
4 files changed +54
-13
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ namespace roboptim
25
25
namespace python
26
26
{
27
27
// / \brief Helper to run Python commands in the Python interpreter.
28
+ // / WARNING: this is not thread-safe for now (GIL etc.).
28
29
class ToPython
29
30
{
30
31
public:
@@ -50,6 +51,9 @@ namespace roboptim
50
51
void operator >> (std::ostream& o);
51
52
52
53
private:
54
+ // / \brief Number of instances.
55
+ static int instances_;
56
+
53
57
// / \brief Buffering callback.
54
58
// / \param s string added to the buffer.
55
59
void buffering (const std::string& s);
Original file line number Diff line number Diff line change @@ -189,22 +189,32 @@ namespace roboptim
189
189
/* *******************************
190
190
* Implementation of ToPython *
191
191
********************************/
192
+ int ToPython::instances_ = 0 ;
192
193
193
194
ToPython::ToPython () : buffer_ ()
194
195
{
195
- PyImport_AppendInittab (" redir" , PyInit_redir);
196
- Py_Initialize ();
197
- PyImport_ImportModule (" redir" );
198
-
199
- // Switch sys.stdout to custom handler
200
- stdout_write_type write = boost::bind (&ToPython::buffering,
201
- this , _1);
202
- set_stdout (write );
196
+ if (!instances_)
197
+ {
198
+ PyImport_AppendInittab (" redir" , PyInit_redir);
199
+ Py_Initialize ();
200
+ PyImport_ImportModule (" redir" );
201
+
202
+ // Switch sys.stdout to custom handler
203
+ stdout_write_type write = boost::bind (&ToPython::buffering,
204
+ this , _1);
205
+ set_stdout (write );
206
+ }
207
+
208
+ // Increment the instance counter
209
+ instances_++;
203
210
}
204
211
205
212
ToPython::~ToPython ()
206
213
{
207
- Py_Exit (0 );
214
+ // Decrement the instance counter
215
+ instances_--;
216
+
217
+ if (!instances_) Py_Finalize ();
208
218
}
209
219
210
220
const ToPython& ToPython::operator << (const char * cmd) const
Original file line number Diff line number Diff line change
1
+ foo
2
+ bar
3
+ 42
Original file line number Diff line number Diff line change 24
24
using namespace roboptim ;
25
25
using namespace roboptim ::python;
26
26
27
- boost::shared_ptr<boost::test_tools::output_test_stream> output;
27
+ typedef boost::shared_ptr<boost::test_tools::output_test_stream>
28
+ output_ptr;
28
29
29
- BOOST_FIXTURE_TEST_SUITE (core, TestSuiteConfiguration )
30
+ BOOST_AUTO_TEST_SUITE (to_python )
30
31
31
- BOOST_AUTO_TEST_CASE (to_python )
32
+ BOOST_AUTO_TEST_CASE (redir )
32
33
{
33
- output = retrievePattern (" to-python" );
34
+ output_ptr output = retrievePattern (" to-python" );
34
35
35
36
ToPython tp;
36
37
@@ -53,4 +54,27 @@ BOOST_AUTO_TEST_CASE (to_python)
53
54
BOOST_CHECK (output->match_pattern ());
54
55
}
55
56
57
+ BOOST_AUTO_TEST_CASE (simultaneous)
58
+ {
59
+ output_ptr output = retrievePattern (" to-python-simultaneous" );
60
+
61
+ ToPython tp1;
62
+
63
+ tp1 << " print(\" foo\" )" ;
64
+
65
+ {
66
+ ToPython tp2;
67
+ tp2 << " print(\" bar\" )" ;
68
+ tp2 >> (*output);
69
+ }
70
+
71
+ tp1 << " print(42)" ;
72
+
73
+ // Flush to output
74
+ tp1 >> (*output);
75
+
76
+ std::cout << output->str () << std::endl;
77
+ BOOST_CHECK (output->match_pattern ());
78
+ }
79
+
56
80
BOOST_AUTO_TEST_SUITE_END ()
You can’t perform that action at this time.
0 commit comments