Hi there guys and girls, I'm new (like many people I guess) to python and embedded python and have been struggling to get c++ and python to play nicely for the past couple of weeks. Up till now I've overcome most obstacles by rtfm, but there are still a few things I don't get.
My setup: embedding and extending pyhton into c++ using swig on windows (mingw). I've been able to create classes in c++ and share instances of these with the embedded python interpreter. So I can instantiate an instance in c++ add this to a PyDict object, then run a script that manipulates it via python. Or of course I can instantiate the instance in a script and then retrieve it from a PyDict after running the script. ### 1st question: What's the best way to go about this? At the moment I've got a base class that python and c++ share (via swig), then in c++ I've got a container class that handles setting up a dictionary, running the script and getting the base class instance back from python. Is this the best way to do it? ### 2nd question: What's up with the local and global dictionaries in PyRun_String? When creating a new dictionary for the script to run in I know I have to add: PyDict_Update(dict, PyObject_GetAttrString(PyImport_AddModule("__main__"), "__dict__")); and / or ?! PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins() ); What's the difference between these two? At the moment I'm just putting everything into my dictionary before I run a script otherwise I get errors regarding missing functions. Then actually running the script... result = PyRun_String(const_cast<char*>(temp_script.c_str()), Py_file_input, dict, dict); This all seems to be working, though it's very unstable. If someone can point me to a nice explanation of the environment that needs to be set up to execute scripts properly it would be nice. I've read through everything I could find regarding the c API but couldn't find the difference between the global and local dict being past to PyRun_String. Most people just pass the same dictionary. ### 3rd question: I've got a seperate folder with python modules, and tell python about this using PyObject* pModule = PyImport_ImportModuleEx("site", dict, dict, NULL); PyObject* pFunc = PyDict_GetItemString(PyModule_GetDict(pModule), "addsitedir"); PyObject_CallFunction(pFunc, "s", "scripts/"); I have to do this because setting .pth files in windows doesn't work... Doing this does work, though I just hit a snag: I wrote a function that uses ceil() from the math module which should get imported automatically. When this function is defined in the script that I pass to PyRun_String it works perfectly. Though when I place it in a module in the site directory I get an error saying the global name ceil is not defined. Even when I import the math module explicitly I get the error. I don't get it. Why would it work in the script being passed to PyRun_string but not when imported from a module? I guess there's something I don't understand about the environment in which python needs to execute things. It would be great if one of you can explain all of this to me, or even just point me to where I can read up on this... Y'all have a great weekend, and enjoy the holidays! Merry x-mas, cputter
-- http://mail.python.org/mailman/listinfo/python-list