A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Wed, 15 Oct 2008 15:55:09 +0100
changeset 3753a84a48233eb3
parent 3744 bb6876ea0851
child 3754 ee45d2d71f01
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
bindings/python/ns3_module_core.py
bindings/python/ns3_module_internet_stack.py
bindings/python/ns3module_helpers.cc
bindings/python/ns3modulegen.py
bindings/python/ns3modulegen_core_customizations.py
bindings/python/ns3modulescan.py
utils/python-unit-tests.py
     1.1 --- a/bindings/python/ns3_module_core.py	Wed Oct 15 09:49:39 2008 +0200
     1.2 +++ b/bindings/python/ns3_module_core.py	Wed Oct 15 15:55:09 2008 +0100
     1.3 @@ -945,11 +945,6 @@
     1.4                     'ns3::TypeId', 
     1.5                     [param('std::string', 'name')], 
     1.6                     is_static=True)
     1.7 -    ## type-id.h: static bool ns3::TypeId::LookupByNameFailSafe(std::string name, ns3::TypeId * tid) [member function]
     1.8 -    cls.add_method('LookupByNameFailSafe', 
     1.9 -                   'bool', 
    1.10 -                   [param('std::string', 'name'), param('ns3::TypeId *', 'tid', transfer_ownership=False)], 
    1.11 -                   is_static=True)
    1.12      ## type-id.h: ns3::Ptr<ns3::TraceSourceAccessor const> ns3::TypeId::LookupTraceSourceByName(std::string name) const [member function]
    1.13      cls.add_method('LookupTraceSourceByName', 
    1.14                     'ns3::Ptr< ns3::TraceSourceAccessor const >', 
     2.1 --- a/bindings/python/ns3_module_internet_stack.py	Wed Oct 15 09:49:39 2008 +0200
     2.2 +++ b/bindings/python/ns3_module_internet_stack.py	Wed Oct 15 15:55:09 2008 +0100
     2.3 @@ -358,10 +358,11 @@
     2.4      cls.add_method('Insert', 
     2.5                     'void', 
     2.6                     [param('ns3::Ptr< ns3::Ipv4L4Protocol >', 'protocol')])
     2.7 -    ## ipv4-l3-protocol.h: ns3::Ptr<ns3::Ipv4L4Protocol> ns3::Ipv4L3Protocol::GetProtocol(int protocolNumber) [member function]
     2.8 +    ## ipv4-l3-protocol.h: ns3::Ptr<ns3::Ipv4L4Protocol> ns3::Ipv4L3Protocol::GetProtocol(int protocolNumber) const [member function]
     2.9      cls.add_method('GetProtocol', 
    2.10                     'ns3::Ptr< ns3::Ipv4L4Protocol >', 
    2.11 -                   [param('int', 'protocolNumber')])
    2.12 +                   [param('int', 'protocolNumber')], 
    2.13 +                   is_const=True)
    2.14      ## ipv4-l3-protocol.h: void ns3::Ipv4L3Protocol::Remove(ns3::Ptr<ns3::Ipv4L4Protocol> protocol) [member function]
    2.15      cls.add_method('Remove', 
    2.16                     'void', 
     3.1 --- a/bindings/python/ns3module_helpers.cc	Wed Oct 15 09:49:39 2008 +0200
     3.2 +++ b/bindings/python/ns3module_helpers.cc	Wed Oct 15 15:55:09 2008 +0100
     3.3 @@ -188,3 +188,35 @@
     3.4      return NULL;
     3.5  }
     3.6  
     3.7 +
     3.8 +PyObject *
     3.9 +_wrap_TypeId_LookupByNameFailSafe(PyNs3TypeId *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs,
    3.10 +                                  PyObject **return_exception)
    3.11 +{
    3.12 +    bool ok;
    3.13 +    const char *name;
    3.14 +    Py_ssize_t name_len;
    3.15 +    ns3::TypeId tid;
    3.16 +    PyNs3TypeId *py_tid;
    3.17 +    const char *keywords[] = {"name", NULL};
    3.18 +    
    3.19 +    if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "s#", (char **) keywords, &name, &name_len)) {
    3.20 +        PyObject *exc_type, *traceback;
    3.21 +        PyErr_Fetch(&exc_type, return_exception, &traceback);
    3.22 +        Py_XDECREF(exc_type);
    3.23 +        Py_XDECREF(traceback);
    3.24 +        return NULL;
    3.25 +    }
    3.26 +    ok = ns3::TypeId::LookupByNameFailSafe(std::string(name, name_len), &tid);
    3.27 +    if (!ok)
    3.28 +    {
    3.29 +        PyErr_Format(PyExc_KeyError, "The ns3 type with name `%s' is not registered", name);
    3.30 +        return NULL;
    3.31 +    }
    3.32 +
    3.33 +    py_tid = PyObject_New(PyNs3TypeId, &PyNs3TypeId_Type);
    3.34 +    py_tid->obj = new ns3::TypeId (tid);
    3.35 +    PyNs3TypeId_wrapper_registry[(void *) py_tid->obj] = (PyObject *) py_tid;    
    3.36 +    
    3.37 +    return (PyObject *) py_tid;
    3.38 +}
     4.1 --- a/bindings/python/ns3modulegen.py	Wed Oct 15 09:49:39 2008 +0200
     4.2 +++ b/bindings/python/ns3modulegen.py	Wed Oct 15 15:55:09 2008 +0100
     4.3 @@ -85,6 +85,7 @@
     4.4  
     4.5      ns3modulegen_core_customizations.Simulator_customizations(root_module)
     4.6      ns3modulegen_core_customizations.CommandLine_customizations(root_module)
     4.7 +    ns3modulegen_core_customizations.TypeId_customizations(root_module)
     4.8  
     4.9  
    4.10      for local_module in LOCAL_MODULES:
     5.1 --- a/bindings/python/ns3modulegen_core_customizations.py	Wed Oct 15 09:49:39 2008 +0200
     5.2 +++ b/bindings/python/ns3modulegen_core_customizations.py	Wed Oct 15 15:55:09 2008 +0100
     5.3 @@ -517,3 +517,10 @@
     5.4                              and param.default_value_type is None:
     5.5                          param.default_value_type = 'ns3::EmptyAttributeValue'
     5.6  
     5.7 +
     5.8 +def TypeId_customizations(module):
     5.9 +    TypeId = module['ns3::TypeId']
    5.10 +    TypeId.add_custom_method_wrapper("LookupByNameFailSafe", "_wrap_TypeId_LookupByNameFailSafe",
    5.11 +                                     flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])
    5.12 +    
    5.13 +
     6.1 --- a/bindings/python/ns3modulescan.py	Wed Oct 15 09:49:39 2008 +0200
     6.2 +++ b/bindings/python/ns3modulescan.py	Wed Oct 15 15:55:09 2008 +0100
     6.3 @@ -73,7 +73,7 @@
     6.4          'params': {'info':{'transfer_ownership': 'false'}}
     6.5          },
     6.6      'static bool ns3::TypeId::LookupByNameFailSafe(std::string name, ns3::TypeId * tid) [member function]': {
     6.7 -        'params': {'tid': {'transfer_ownership': 'false'}}
     6.8 +        'ignore': None, # manually wrapped in 
     6.9          },
    6.10      'bool ns3::TraceSourceAccessor::ConnectWithoutContext(ns3::ObjectBase * obj, ns3::CallbackBase const & cb) const [member function]': {
    6.11          'params': {'obj': {'transfer_ownership':'false'}}
     7.1 --- a/utils/python-unit-tests.py	Wed Oct 15 09:49:39 2008 +0200
     7.2 +++ b/utils/python-unit-tests.py	Wed Oct 15 15:55:09 2008 +0100
     7.3 @@ -118,5 +118,11 @@
     7.4  
     7.5          self.assert_(c1 is c2)
     7.6  
     7.7 +    def testTypeId(self):
     7.8 +        typeId1 = ns3.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
     7.9 +        self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory")
    7.10 +        
    7.11 +        self.assertRaises(KeyError, ns3.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
    7.12 +
    7.13  if __name__ == '__main__':
    7.14      unittest.main()