B
Ben Sizer
I have Python embedded in a C++ application (yes, yes, I know, I'd
prefer it the other way around too) and essentially need to expose some
read-only values and functions to Python so it can be used to script
the host application.
When scripting a similar app in TCL, it's possible to associate each
command with some client data, so that the command can be written in
the script as a free function but it actually executes in some sort of
context, accessed via the client data pointer in C++. In Python, there
doesn't appear to be this mechanism, so I think I'd have to inject the
context in another way, either as some sort of module-level global, or
as an object, implementing the previously free functions as methods.
Is this correct? If so, what is the simplest way of implementing the
former method - inserting the pointer to the required context as a long
(via PyDict_SetItemString(globals, "context", PyInt_FromLong(pointer))
or similar) and then converting it back in the bound function? And for
the latter method, is it possible to make an arbitrary object and then
attach methods and the context data? Or will I have to create a whole
Python class for this (as in
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/54352)?
I'm not interested in wrapping whole C++ objects at this stage, and
libraries like Boost:ython aren't currently an option. I just need a
few pointers on doing it the low-level way for now.
prefer it the other way around too) and essentially need to expose some
read-only values and functions to Python so it can be used to script
the host application.
When scripting a similar app in TCL, it's possible to associate each
command with some client data, so that the command can be written in
the script as a free function but it actually executes in some sort of
context, accessed via the client data pointer in C++. In Python, there
doesn't appear to be this mechanism, so I think I'd have to inject the
context in another way, either as some sort of module-level global, or
as an object, implementing the previously free functions as methods.
Is this correct? If so, what is the simplest way of implementing the
former method - inserting the pointer to the required context as a long
(via PyDict_SetItemString(globals, "context", PyInt_FromLong(pointer))
or similar) and then converting it back in the bound function? And for
the latter method, is it possible to make an arbitrary object and then
attach methods and the context data? Or will I have to create a whole
Python class for this (as in
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/54352)?
I'm not interested in wrapping whole C++ objects at this stage, and
libraries like Boost:ython aren't currently an option. I just need a
few pointers on doing it the low-level way for now.