Bugzilla – Bug 293
Could use an API to "stash" the Python wrapper for an object
Last modified: 2008-08-25 12:40:26 UTC
One problem that I have with the current python bindings is that it is not easy to check for two objects being the same. Lets see an example: device = node.GetDevice(0) chan1 = device.GetChannel() chan2 = device.GetChannel() print (chan1 is chan2) # always prints False The problem is that chan1 and chan2 are two different Python wrappers for the same C++ object. They should be two references to the same wrapper. Anyway, I need to fix this in PyBindGen. However, the way I'll fix this it won't be very scalable, because I am basically forced to create a global map for each class and look up the wrapper for a C++ object there. In PyGtk, from which I copy many of the implementation ideas use in PyBindGen, this problem is solved because GObject has a generic mechanism of stashing arbitrary data into an object, g_object_get/set_data(): http://library.gnome.org/devel/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-set-qdata-full This makes it very efficient to stash a python wrapper for an object, and then get it back later. Any chance something similar could be added to ns3::ObjectBase?
The pybindgen feature was added: http://bazaar.launchpad.net/~gjc/pybindgen/trunk/revision/563?start_revid=563 At the moment it uses a std::map<void*, PyObject*>, with O(log(N)) lookup time (about 16 lookups for 100000 objects). It probably isn't awful performance, considering typical Python scripts do not usually manipulate that many objects. But of course O(1) lookup would be even better... ;-)
(In reply to comment #1) > The pybindgen feature was added: > > http://bazaar.launchpad.net/~gjc/pybindgen/trunk/revision/563?start_revid=563 > > At the moment it uses a std::map<void*, PyObject*>, with O(log(N)) lookup time > (about 16 lookups for 100000 objects). It probably isn't awful performance, > considering typical Python scripts do not usually manipulate that many objects. > > But of course O(1) lookup would be even better... ;-) What you could conceivably do is automatically aggregate to each object another object which contains the pointer back to the python object.
(In reply to comment #2) > (In reply to comment #1) [...] > > But of course O(1) lookup would be even better... ;-) > > What you could conceivably do is automatically aggregate to each object another > object which contains the pointer back to the python object. Great idea! Ashamed it didn't occur to me... :P