diff --git a/bindings/python/ns3_module_core.py b/bindings/python/ns3_module_core.py index 466bf07ad..9719a9fdd 100644 --- a/bindings/python/ns3_module_core.py +++ b/bindings/python/ns3_module_core.py @@ -945,11 +945,6 @@ def register_Ns3TypeId_methods(root_module, cls): 'ns3::TypeId', [param('std::string', 'name')], is_static=True) - ## type-id.h: static bool ns3::TypeId::LookupByNameFailSafe(std::string name, ns3::TypeId * tid) [member function] - cls.add_method('LookupByNameFailSafe', - 'bool', - [param('std::string', 'name'), param('ns3::TypeId *', 'tid', transfer_ownership=False)], - is_static=True) ## type-id.h: ns3::Ptr ns3::TypeId::LookupTraceSourceByName(std::string name) const [member function] cls.add_method('LookupTraceSourceByName', 'ns3::Ptr< ns3::TraceSourceAccessor const >', diff --git a/bindings/python/ns3_module_internet_stack.py b/bindings/python/ns3_module_internet_stack.py index 0d28fcf24..439307348 100644 --- a/bindings/python/ns3_module_internet_stack.py +++ b/bindings/python/ns3_module_internet_stack.py @@ -358,10 +358,11 @@ def register_Ns3Ipv4L3Protocol_methods(root_module, cls): cls.add_method('Insert', 'void', [param('ns3::Ptr< ns3::Ipv4L4Protocol >', 'protocol')]) - ## ipv4-l3-protocol.h: ns3::Ptr ns3::Ipv4L3Protocol::GetProtocol(int protocolNumber) [member function] + ## ipv4-l3-protocol.h: ns3::Ptr ns3::Ipv4L3Protocol::GetProtocol(int protocolNumber) const [member function] cls.add_method('GetProtocol', 'ns3::Ptr< ns3::Ipv4L4Protocol >', - [param('int', 'protocolNumber')]) + [param('int', 'protocolNumber')], + is_const=True) ## ipv4-l3-protocol.h: void ns3::Ipv4L3Protocol::Remove(ns3::Ptr protocol) [member function] cls.add_method('Remove', 'void', diff --git a/bindings/python/ns3module_helpers.cc b/bindings/python/ns3module_helpers.cc index cb2fab4a9..03dc52445 100644 --- a/bindings/python/ns3module_helpers.cc +++ b/bindings/python/ns3module_helpers.cc @@ -188,3 +188,35 @@ error: return NULL; } + +PyObject * +_wrap_TypeId_LookupByNameFailSafe(PyNs3TypeId *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, + PyObject **return_exception) +{ + bool ok; + const char *name; + Py_ssize_t name_len; + ns3::TypeId tid; + PyNs3TypeId *py_tid; + const char *keywords[] = {"name", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "s#", (char **) keywords, &name, &name_len)) { + PyObject *exc_type, *traceback; + PyErr_Fetch(&exc_type, return_exception, &traceback); + Py_XDECREF(exc_type); + Py_XDECREF(traceback); + return NULL; + } + ok = ns3::TypeId::LookupByNameFailSafe(std::string(name, name_len), &tid); + if (!ok) + { + PyErr_Format(PyExc_KeyError, "The ns3 type with name `%s' is not registered", name); + return NULL; + } + + py_tid = PyObject_New(PyNs3TypeId, &PyNs3TypeId_Type); + py_tid->obj = new ns3::TypeId (tid); + PyNs3TypeId_wrapper_registry[(void *) py_tid->obj] = (PyObject *) py_tid; + + return (PyObject *) py_tid; +} diff --git a/bindings/python/ns3modulegen.py b/bindings/python/ns3modulegen.py index 1aa9c8c16..7cd34e182 100755 --- a/bindings/python/ns3modulegen.py +++ b/bindings/python/ns3modulegen.py @@ -85,6 +85,7 @@ def main(): ns3modulegen_core_customizations.Simulator_customizations(root_module) ns3modulegen_core_customizations.CommandLine_customizations(root_module) + ns3modulegen_core_customizations.TypeId_customizations(root_module) for local_module in LOCAL_MODULES: diff --git a/bindings/python/ns3modulegen_core_customizations.py b/bindings/python/ns3modulegen_core_customizations.py index 2766eb07c..d84fcc12e 100644 --- a/bindings/python/ns3modulegen_core_customizations.py +++ b/bindings/python/ns3modulegen_core_customizations.py @@ -517,3 +517,10 @@ def Attribute_customizations(module): and param.default_value_type is None: param.default_value_type = 'ns3::EmptyAttributeValue' + +def TypeId_customizations(module): + TypeId = module['ns3::TypeId'] + TypeId.add_custom_method_wrapper("LookupByNameFailSafe", "_wrap_TypeId_LookupByNameFailSafe", + flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) + + diff --git a/bindings/python/ns3modulescan.py b/bindings/python/ns3modulescan.py index 942f77496..a2d1a0016 100644 --- a/bindings/python/ns3modulescan.py +++ b/bindings/python/ns3modulescan.py @@ -73,7 +73,7 @@ type_annotations = { 'params': {'info':{'transfer_ownership': 'false'}} }, 'static bool ns3::TypeId::LookupByNameFailSafe(std::string name, ns3::TypeId * tid) [member function]': { - 'params': {'tid': {'transfer_ownership': 'false'}} + 'ignore': None, # manually wrapped in }, 'bool ns3::TraceSourceAccessor::ConnectWithoutContext(ns3::ObjectBase * obj, ns3::CallbackBase const & cb) const [member function]': { 'params': {'obj': {'transfer_ownership':'false'}} diff --git a/utils/python-unit-tests.py b/utils/python-unit-tests.py index 43ce5a53e..8495661fb 100644 --- a/utils/python-unit-tests.py +++ b/utils/python-unit-tests.py @@ -118,5 +118,11 @@ class TestSimulator(unittest.TestCase): self.assert_(c1 is c2) + def testTypeId(self): + typeId1 = ns3.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory") + self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory") + + self.assertRaises(KeyError, ns3.TypeId.LookupByNameFailSafe, "__InvalidTypeName__") + if __name__ == '__main__': unittest.main()