[C++-sig] Re: Accessing C++ values from within Python

Jeffrey Holle jeff.holle at verizon.net
Tue Jul 6 07:29:02 CEST 2004


Sounds like you have not placed your testScript.py file in a "python 
public" directory.

The easiest way to make the python interpreter look in an additional 
place is to add the directory to your PYTHONPATH environement variable.

I understand that its also possible to do this by using the approbriate 
Python C API calls before calling Py_Initialize(), but do not know the 
details yet.

Don't know what to make of the garbage collection stuff.  Sounds like 
your python interpreter isn't healthy....

xin wrote:
> I finally got around to trying to get this to work, as I am unable to 
> get bjam to operate correctly I tried to implement it in my existing code.
> Unfortunately I get problems with the getPythonFunction() function. When 
> I ran it, the application would abort.
> 
> After playing around a bit I ended up with :
> PyObject *getPythonFunction(const char *moduleName, const char 
> *functionName)
> {
>    PyObject *pModule = PyImport_ImportModule((char *)moduleName);
> }
> And I got the error when I ran it:
> Exception exceptions.ImportError: 'No module named testScript.py' in 
> 'garbage collection' ignored
> Fatal Python error: unexpected exception during garbage collection Aborted
> 
> Any comments/suggestions?
> 
> Jeffrey Holle wrote:
> 
>> I'm sending the source of a simple project that does what I believe 
>> you are trying to do.
>>
>> In Main.cpp, note the use of call, and the use of ref within it.
>> The former does all the "magic" of converting C++ objects to/from python.
>> The later causes the C++ object to be passed by reference, allowing 
>> the python script to alter C++ object attributes.
>>
>>
>> xin wrote:
>>
>>> Hi.
>>> I have a setup such as the following:
>>>
>>> class object {
>>> public:
>>>     void doSomthingElse();
>>>     float x;
>>>     float *y;
>>>     Somthing SomePyScript;
>>> };
>>>
>>> int main()
>>> {
>>>     object *array = new object[10]; // size of array is only an example
>>>     for (int i = 0; i < 10; i++)
>>>     {
>>>         array[i].y = new float[array[i]]
>>>         RunPythonScript(array[i]);
>>>     }
>>>     return 1;
>>> }
>>>
>>> Where:
>>>  - " Somting SomePyScript; " Is some reference to a python script 
>>> that is affiliated with this instance of the class (varies from 
>>> instance to instance)
>>>  - " RunPythonScript(array[i]); " Is some method of running the 
>>> python script that is affiliated with the instance array[i].
>>>
>>> My intention is that the python scripts change values within the 
>>> class 'object'
>>> e.g. The python code sets 'x' to equal 0.9. And then sets each value 
>>> of y to equal a muliple of 9. (y[0] = 0, y[1] = 9, y[2] = 18, ... 
>>> ,y[100] = 900 etc...)
>>>
>>> Is this at all possible? Is it possible without moving all the memory 
>>> from one place, to another area accessable by both python and then 
>>> back again? If so how would I go about doing it?
>>>
>>> I've tried all sorts of methods of attempting this, but have had no 
>>> luck. Any help would be appreciated.
>>>
>>> Thanks.
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> C++-sig mailing list
>> C++-sig at python.org
>> http://mail.python.org/mailman/listinfo/c++-sig




More information about the Cplusplus-sig mailing list