author | Raj Bhattacharjea <raj.b@gatech.edu> |
Thu, 22 Jan 2009 15:13:47 -0500 | |
changeset 4228 | b9158015b8aa |
parent 4086 | 37dbf76b4c66 |
permissions | -rw-r--r-- |
3929
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
1 |
#include "ns3/ref-count-base.h" |
3408 | 2 |
#include "ns3module.h" |
3 |
||
4 |
||
5 |
namespace ns3{ |
|
6 |
||
7 |
void PythonCompleteConstruct (Ptr<Object> object, TypeId typeId, const AttributeList &attributes) |
|
8 |
{ |
|
9 |
object->SetTypeId (typeId); |
|
10 |
object->Object::Construct (attributes); |
|
11 |
} |
|
12 |
||
13 |
} |
|
14 |
||
15 |
||
16 |
||
17 |
class PythonEventImpl : public ns3::EventImpl |
|
18 |
{ |
|
19 |
private: |
|
20 |
PyObject *m_callback; |
|
21 |
PyObject *m_args; |
|
22 |
public: |
|
23 |
PythonEventImpl (PyObject *callback, PyObject *args) |
|
24 |
{ |
|
25 |
m_callback = callback; |
|
26 |
Py_INCREF(m_callback); |
|
27 |
m_args = args; |
|
28 |
Py_INCREF(m_args); |
|
29 |
} |
|
30 |
virtual ~PythonEventImpl () |
|
31 |
{ |
|
3722
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
32 |
PyGILState_STATE __py_gil_state; |
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
33 |
__py_gil_state = (PyEval_ThreadsInitialized() ? PyGILState_Ensure() : (PyGILState_STATE) 0); |
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
34 |
|
3408 | 35 |
Py_DECREF(m_callback); |
36 |
Py_DECREF(m_args); |
|
3722
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
37 |
|
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
38 |
if (PyEval_ThreadsInitialized()) |
38ace50047e1
Need to acquire the Python GIL also in ~PythonEventImpl, as it may be called at any time from the C++ code, from any thread.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3702
diff
changeset
|
39 |
PyGILState_Release(__py_gil_state); |
3408 | 40 |
} |
41 |
virtual void Notify () |
|
42 |
{ |
|
3702
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
43 |
PyGILState_STATE __py_gil_state; |
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
44 |
__py_gil_state = (PyEval_ThreadsInitialized() ? PyGILState_Ensure() : (PyGILState_STATE) 0); |
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
45 |
|
3408 | 46 |
PyObject *retval = PyObject_CallObject(m_callback, m_args); |
47 |
if (retval) { |
|
48 |
if (retval != Py_None) { |
|
49 |
PyErr_SetString(PyExc_TypeError, "event callback should return None"); |
|
50 |
PyErr_Print(); |
|
51 |
} |
|
52 |
Py_DECREF(retval); |
|
53 |
} else { |
|
54 |
PyErr_Print(); |
|
55 |
} |
|
3702
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
56 |
|
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
57 |
if (PyEval_ThreadsInitialized()) |
db14e253e614
Add missing Python GIL locking in PythonEventImpl::Notify
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
58 |
PyGILState_Release(__py_gil_state); |
3408 | 59 |
} |
60 |
}; |
|
61 |
||
62 |
||
63 |
PyObject * |
|
64 |
_wrap_Simulator_Schedule(PyNs3Simulator *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, |
|
65 |
PyObject **return_exception) |
|
66 |
{ |
|
67 |
PyObject *exc_type, *traceback; |
|
68 |
PyObject *py_time; |
|
69 |
PyObject *py_callback; |
|
70 |
PyObject *user_args; |
|
71 |
ns3::Ptr<PythonEventImpl> py_event_impl; |
|
72 |
PyNs3EventId *py_EventId; |
|
73 |
||
74 |
if (kwargs && PyObject_Length(kwargs) > 0) { |
|
75 |
PyErr_SetString(PyExc_TypeError, "keyword arguments not supported"); |
|
76 |
goto error; |
|
77 |
} |
|
78 |
||
79 |
if (PyTuple_GET_SIZE(args) < 2) { |
|
80 |
PyErr_SetString(PyExc_TypeError, "ns3.Simulator.Schedule needs at least 2 arguments"); |
|
81 |
goto error; |
|
82 |
} |
|
83 |
py_time = PyTuple_GET_ITEM(args, 0); |
|
84 |
py_callback = PyTuple_GET_ITEM(args, 1); |
|
85 |
||
86 |
if (!PyObject_IsInstance(py_time, (PyObject*) &PyNs3Time_Type)) { |
|
87 |
PyErr_SetString(PyExc_TypeError, "Parameter 1 should be a ns3.Time instance"); |
|
88 |
goto error; |
|
89 |
} |
|
90 |
if (!PyCallable_Check(py_callback)) { |
|
91 |
PyErr_SetString(PyExc_TypeError, "Parameter 2 should be callable"); |
|
92 |
goto error; |
|
93 |
} |
|
94 |
user_args = PyTuple_GetSlice(args, 2, PyTuple_GET_SIZE(args)); |
|
95 |
py_event_impl = ns3::Create<PythonEventImpl>(py_callback, user_args); |
|
96 |
Py_DECREF(user_args); |
|
97 |
||
98 |
py_EventId = PyObject_New(PyNs3EventId, &PyNs3EventId_Type); |
|
99 |
py_EventId->obj = new ns3::EventId( |
|
100 |
ns3::Simulator::Schedule(*((PyNs3Time *) py_time)->obj, py_event_impl)); |
|
101 |
return (PyObject *) py_EventId; |
|
102 |
||
103 |
error: |
|
104 |
PyErr_Fetch(&exc_type, return_exception, &traceback); |
|
105 |
Py_XDECREF(exc_type); |
|
106 |
Py_XDECREF(traceback); |
|
107 |
return NULL; |
|
108 |
} |
|
109 |
||
110 |
||
111 |
PyObject * |
|
112 |
_wrap_Simulator_ScheduleNow(PyNs3Simulator *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, |
|
113 |
PyObject **return_exception) |
|
114 |
{ |
|
115 |
PyObject *exc_type, *traceback; |
|
116 |
PyObject *py_callback; |
|
117 |
PyObject *user_args; |
|
118 |
ns3::Ptr<PythonEventImpl> py_event_impl; |
|
119 |
PyNs3EventId *py_EventId; |
|
120 |
||
121 |
if (kwargs && PyObject_Length(kwargs) > 0) { |
|
122 |
PyErr_SetString(PyExc_TypeError, "keyword arguments not supported"); |
|
123 |
goto error; |
|
124 |
} |
|
125 |
||
126 |
if (PyTuple_GET_SIZE(args) < 1) { |
|
127 |
PyErr_SetString(PyExc_TypeError, "ns3.Simulator.Schedule needs at least 1 argument"); |
|
128 |
goto error; |
|
129 |
} |
|
130 |
py_callback = PyTuple_GET_ITEM(args, 0); |
|
131 |
||
132 |
if (!PyCallable_Check(py_callback)) { |
|
133 |
PyErr_SetString(PyExc_TypeError, "Parameter 2 should be callable"); |
|
134 |
goto error; |
|
135 |
} |
|
136 |
user_args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
|
137 |
py_event_impl = ns3::Create<PythonEventImpl>(py_callback, user_args); |
|
138 |
Py_DECREF(user_args); |
|
139 |
||
140 |
py_EventId = PyObject_New(PyNs3EventId, &PyNs3EventId_Type); |
|
141 |
py_EventId->obj = new ns3::EventId(ns3::Simulator::ScheduleNow(py_event_impl)); |
|
142 |
return (PyObject *) py_EventId; |
|
143 |
||
144 |
error: |
|
145 |
PyErr_Fetch(&exc_type, return_exception, &traceback); |
|
146 |
Py_XDECREF(exc_type); |
|
147 |
Py_XDECREF(traceback); |
|
148 |
return NULL; |
|
149 |
} |
|
150 |
||
151 |
||
152 |
PyObject * |
|
153 |
_wrap_Simulator_ScheduleDestroy(PyNs3Simulator *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, |
|
154 |
PyObject **return_exception) |
|
155 |
{ |
|
156 |
PyObject *exc_type, *traceback; |
|
157 |
PyObject *py_callback; |
|
158 |
PyObject *user_args; |
|
159 |
ns3::Ptr<PythonEventImpl> py_event_impl; |
|
160 |
PyNs3EventId *py_EventId; |
|
161 |
||
162 |
if (kwargs && PyObject_Length(kwargs) > 0) { |
|
163 |
PyErr_SetString(PyExc_TypeError, "keyword arguments not supported"); |
|
164 |
goto error; |
|
165 |
} |
|
166 |
||
167 |
if (PyTuple_GET_SIZE(args) < 1) { |
|
168 |
PyErr_SetString(PyExc_TypeError, "ns3.Simulator.Schedule needs at least 1 argument"); |
|
169 |
goto error; |
|
170 |
} |
|
171 |
py_callback = PyTuple_GET_ITEM(args, 0); |
|
172 |
||
173 |
if (!PyCallable_Check(py_callback)) { |
|
174 |
PyErr_SetString(PyExc_TypeError, "Parameter 2 should be callable"); |
|
175 |
goto error; |
|
176 |
} |
|
177 |
user_args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
|
178 |
py_event_impl = ns3::Create<PythonEventImpl>(py_callback, user_args); |
|
179 |
Py_DECREF(user_args); |
|
180 |
||
181 |
py_EventId = PyObject_New(PyNs3EventId, &PyNs3EventId_Type); |
|
182 |
py_EventId->obj = new ns3::EventId(ns3::Simulator::ScheduleDestroy(py_event_impl)); |
|
183 |
return (PyObject *) py_EventId; |
|
184 |
||
185 |
error: |
|
186 |
PyErr_Fetch(&exc_type, return_exception, &traceback); |
|
187 |
Py_XDECREF(exc_type); |
|
188 |
Py_XDECREF(traceback); |
|
189 |
return NULL; |
|
190 |
} |
|
191 |
||
3753
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
192 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
193 |
PyObject * |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
194 |
_wrap_TypeId_LookupByNameFailSafe(PyNs3TypeId *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
195 |
PyObject **return_exception) |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
196 |
{ |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
197 |
bool ok; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
198 |
const char *name; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
199 |
Py_ssize_t name_len; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
200 |
ns3::TypeId tid; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
201 |
PyNs3TypeId *py_tid; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
202 |
const char *keywords[] = {"name", NULL}; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
203 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
204 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "s#", (char **) keywords, &name, &name_len)) { |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
205 |
PyObject *exc_type, *traceback; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
206 |
PyErr_Fetch(&exc_type, return_exception, &traceback); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
207 |
Py_XDECREF(exc_type); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
208 |
Py_XDECREF(traceback); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
209 |
return NULL; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
210 |
} |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
211 |
ok = ns3::TypeId::LookupByNameFailSafe(std::string(name, name_len), &tid); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
212 |
if (!ok) |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
213 |
{ |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
214 |
PyErr_Format(PyExc_KeyError, "The ns3 type with name `%s' is not registered", name); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
215 |
return NULL; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
216 |
} |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
217 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
218 |
py_tid = PyObject_New(PyNs3TypeId, &PyNs3TypeId_Type); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
219 |
py_tid->obj = new ns3::TypeId (tid); |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
220 |
PyNs3TypeId_wrapper_registry[(void *) py_tid->obj] = (PyObject *) py_tid; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
221 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
222 |
return (PyObject *) py_tid; |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3722
diff
changeset
|
223 |
} |
3929
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
224 |
|
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
225 |
|
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
226 |
class CommandLinePythonValueSetter : public ns3::RefCountBase |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
227 |
{ |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
228 |
PyObject *m_namespace; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
229 |
std::string m_variable; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
230 |
public: |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
231 |
CommandLinePythonValueSetter (PyObject *ns, std::string const &variable) { |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
232 |
Py_INCREF(ns); |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
233 |
m_namespace = ns; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
234 |
m_variable = variable; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
235 |
} |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
236 |
bool Parse (std::string value) { |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
237 |
PyObject *pyvalue = PyString_FromStringAndSize (value.data(), value.size()); |
3952
503e8d54ee1d
Const fix (needed for older Python versions)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3929
diff
changeset
|
238 |
PyObject_SetAttrString (m_namespace, (char *) m_variable.c_str(), pyvalue); |
3929
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
239 |
if (PyErr_Occurred()) { |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
240 |
PyErr_Print(); |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
241 |
return false; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
242 |
} |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
243 |
return true; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
244 |
} |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
245 |
virtual ~CommandLinePythonValueSetter () { |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
246 |
Py_DECREF (m_namespace); |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
247 |
m_namespace = NULL; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
248 |
} |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
249 |
|
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
250 |
}; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
251 |
|
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
252 |
PyObject * |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
253 |
_wrap_CommandLine_AddValue(PyNs3CommandLine *self, PyObject *args, PyObject *kwargs, |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
254 |
PyObject **return_exception) |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
255 |
{ |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
256 |
const char *name, *help, *variable = NULL; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
257 |
PyObject *py_namespace = NULL; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
258 |
const char *keywords[] = {"name", "help", "variable", "namespace", NULL}; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
259 |
|
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
260 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "ss|sO", (char **) keywords, &name, &help, &variable, &py_namespace)) { |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
261 |
PyObject *exc_type, *traceback; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
262 |
PyErr_Fetch(&exc_type, return_exception, &traceback); |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
263 |
Py_XDECREF(exc_type); |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
264 |
Py_XDECREF(traceback); |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
265 |
return NULL; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
266 |
} |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
267 |
|
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
268 |
if (variable == NULL) { |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
269 |
variable = name; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
270 |
} |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
271 |
if (py_namespace == NULL) { |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
272 |
py_namespace = (PyObject *) self; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
273 |
} |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
274 |
|
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
275 |
ns3::Ptr<CommandLinePythonValueSetter> setter = ns3::Create<CommandLinePythonValueSetter> (py_namespace, variable); |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
276 |
self->obj->AddValue (name, help, ns3::MakeCallback (&CommandLinePythonValueSetter::Parse, setter)); |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
277 |
|
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
278 |
Py_INCREF(Py_None); |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
279 |
return Py_None; |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
280 |
} |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
281 |
|
4086
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
282 |
|
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
283 |
PyObject * |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
284 |
_wrap_Simulator_Run(PyNs3Simulator *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
285 |
PyObject **return_exception) |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
286 |
{ |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
287 |
const char *keywords[] = {"signal_check_frequency", NULL}; |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
288 |
int signal_check_frequency = 100; |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
289 |
|
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
290 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "|i", (char **) keywords, &signal_check_frequency)) { |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
291 |
PyObject *exc_type, *traceback; |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
292 |
PyErr_Fetch(&exc_type, return_exception, &traceback); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
293 |
Py_XDECREF(exc_type); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
294 |
Py_XDECREF(traceback); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
295 |
return NULL; |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
296 |
} |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
297 |
|
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
298 |
PyThreadState *py_thread_state = NULL; |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
299 |
|
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
300 |
if (signal_check_frequency == -1) |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
301 |
{ |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
302 |
if (PyEval_ThreadsInitialized ()) |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
303 |
py_thread_state = PyEval_SaveThread(); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
304 |
ns3::Simulator::Run(); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
305 |
if (py_thread_state) |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
306 |
PyEval_RestoreThread(py_thread_state); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
307 |
} else { |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
308 |
while (!ns3::Simulator::IsFinished()) |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
309 |
{ |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
310 |
if (PyEval_ThreadsInitialized()) |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
311 |
py_thread_state = PyEval_SaveThread(); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
312 |
|
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
313 |
for (int n = signal_check_frequency; n > 0 && !ns3::Simulator::IsFinished(); --n) |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
314 |
{ |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
315 |
ns3::Simulator::RunOneEvent(); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
316 |
} |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
317 |
|
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
318 |
if (py_thread_state) |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
319 |
PyEval_RestoreThread(py_thread_state); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
320 |
PyErr_CheckSignals(); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
321 |
if (PyErr_Occurred()) |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
322 |
return NULL; |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
323 |
} |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
324 |
} |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
325 |
Py_INCREF(Py_None); |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
326 |
return Py_None; |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
327 |
} |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3952
diff
changeset
|
328 |