From: carlos c. <hap...@ho...> - 2006-12-08 17:07:24
|
Thanks for the prompt reply. I looked at the Demo folder and found usage of the Callable interface in rangetext.cxx. In version 5.36, rangetest.cxx has the following lines (starting at line 29): ******************************************* RangeSequence r2(1, 10, 2); if(r2[1] != Py::Int(3)) return "RangeSequence check failed. "; debug_check_ref_queue(); // calling an extension object method using getattr Py::Callable w(r2.getAttr("amethod")); Py::Tuple args(1); Py::Int j(3); args[0]=j; Py::List answer(w.apply(args)); if(answer[0] != r2) return ("Extension object test failed (1)"); if(answer[1] != args[0]) return ("Extension object test failed (2)"); ******************************************* I see that RangeSequence is a class extending a python sequence. However, I don't see how "amethod" could be an attribute of it. "amethod" is a method of the range class, but this class seems to have nothing to do with RangeSequence. How is this method an attribute of the extended class? Next, I see that 'w' is a Callable object, but I do not see how it can have an 'apply' method. Is the 'apply' method putting the arguments in 'args' into the method "amethod"? Lastly, I don't see at all how the Callable object is calling back into a python script. Can you please give me some explanations? Sorry if this all seems rather elementary, but I am trying my best to understand it. >From: Barry Scott <ba...@ba...> >Reply-To: Discuss PyCXX use and improvement ><cxx...@li...> >To: Discuss PyCXX use and improvement <cxx...@li...> >Subject: Re: Embedding Python >Date: Sun, 3 Dec 2006 19:33:04 +0000 > > >On Dec 1, 2006, at 18:54, carlos choy wrote: > I am thinking of using PyCXX principally for embedding Python. > >This works. You will need to create a module using PyCXX if you >wish the python code to be able to call you back. > >If you use threads there is extra work in the init and in the module >you will need to worry about. > > Here is some basic code for embedding that I intend to use: **************************************************************** #include <Python.h> int main(int argc, char *argv[]) { PyObject *pName, *pModule, *pDict, *pFunc, *pValue; > >Use appropriate Py:: objects. > if (argc < 3) { printf("Usage: exe_name python_source function_name\n"); return 1; } > > Give python an argc and argv before calling init. > // Initialize the Python Interpreter Py_Initialize(); // Build the name object pName = PyString_FromString(argv[1]); > > Py::String name( argv[1] ); > // Load the module object pModule = PyImport_Import(pName); > > Py::Module module( PyImport_Import( name ) ); > // pDict is a borrowed reference pDict = PyModule_GetDict(pModule); > >Py::Dict dict( module.getDict() ); > // pFunc is also a borrowed reference pFunc = PyDict_GetItemString(pDict, argv[2]); > > Py::Callable func( dict[ Py::String( argv[2] ) ] ); > if (PyCallable_Check(pFunc)) { PyObject_CallObject(pFunc, NULL); } else { PyErr_Print(); } > >No need to do the if an exception will be raise if an objects type >does not match. > > // no args > Py::Tuple args( 0 ); > > func.apply( args ); > // Clean up Py_DECREF(pModule); Py_DECREF(pName); > > No need to call dec. > But you will need to make sure that the Py:: onjects go out of scope >so that > they are deleted before calling finalize. > // Finish the Python Interpreter Py_Finalize(); return 0; } ********************** I assume the above will work. Is there a more PyCXX way of doing this? > >See comments in line above. > Lastly, I need to have callbacks into C++ code from Python. The Callable interface is documented, but samples. Can someone please post examples of how to use it? _________________________________________________________________ > >Define a module and add functions and classes. Look at the example >that comes with PyCXX in the Demo folder. For example look at >range.hxx and .cxx. > >If you want a full scale example look at the source of the Python SVN >extension, pysvn >at http://pysvn.tigris.org - download a source kit add you will see >examples of calling C++ from >python and calling python from C++. > >Barry > > > >------------------------------------------------------------------------- >Take Surveys. Earn Cash. Influence the Future of IT >Join SourceForge.net's Techsay panel and you'll get the chance to share >your >opinions on IT & business topics through brief surveys - and earn cash >http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV >_______________________________________________ >CXX-Users mailing list >CXX...@li... >https://lists.sourceforge.net/lists/listinfo/cxx-users _________________________________________________________________ Enter the "Telus Mobility Xbox a Day" contest for your chance to WIN! Telus Mobility is giving away an Microsoft Xbox® 360 every day from November 20 to December 31, 2006! Just download Windows Live (MSN) Messenger to your IM-capable TELUS mobile phone, and you could be a winner! http://www.telusmobility.com/msnxbox/ > >_________________________________________________________________ >Be one of the first to try Windows Live Mail. >http://ideas.live.com/programpage.aspx?versionId=5d21c51a-b161-4314-9b0e-4911fb2b2e6d _________________________________________________________________ Not only does WindowsOff to school, going on a trip, or moving? Windows Live (MSN) Messenger lets you stay in touch with friends and family wherever you go. Click here to find out how to sign up! Live OneCare provide all-in-one PC care to keep your computer protected and well-maintained, but it also makes creating backup files a breeze. Try it today! http://www.telusmobility.com/msnxbox/ |