[C++-sig] Embending : Expose and Extract a dictionary object To/From C++

Constant Dupuis constant.dupuis at gmail.com
Thu Sep 3 13:40:50 CEST 2009


Do you known an other way to do that ?

2009/9/3 Constant Dupuis <constant.dupuis at gmail.com>:
> I try with the sample given here
> http://wiki.python.org/moin/boost.python/EmbeddingPython
>
> With a C++ Class and this works, so I guess something "wrong" with the
> c++ dict object ....
>
>
>
> 2009/9/3 Constant Dupuis <constant.dupuis at gmail.com>:
>> nop, i got a Segmentation fault
>>
>> I forget to say that I'm on Mac, I don't knwo if this can change somethings.
>>
>>
>> 2009/9/3 Mark Chandler <admin at lodle.net>:
>>> Can you print iptc by it self?
>>>
>>>
>>>
>>> On 03/09/2009, at 3:41 PM, Constant Dupuis wrote:
>>>
>>>> Ok, thanks,
>>>>
>>>> Now I got a "Segmentation fault" error message when I try "print
>>>> iptc['Headline']".
>>>>
>>>> Is the dict object directly exposable ? Or do I have to create a
>>>> std::map base class ?
>>>>
>>>> Constant
>>>>
>>>> 2009/9/3 Mark Chandler <admin at lodle.net>:
>>>>>
>>>>> when you do the exec add the main_namespace twice
>>>>>
>>>>>> object ignored = exec("print('Headline')\n print(iptc['Headline'])\n",
>>>>>> main_namespace,main_namespace);
>>>>>
>>>>>
>>>>> On 03/09/2009, at 3:12 PM, Constant Dupuis wrote:
>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> I would like to expose a dictionary object instanciated in C++ to a
>>>>>> Python code, which manipulate the dictoinary.
>>>>>> And after extract the resulting modified dictionary back in C++.
>>>>>>
>>>>>> I try this :
>>>>>>
>>>>>> try {
>>>>>>
>>>>>>  Py_Initialize();
>>>>>>
>>>>>>       std::cout << "Hello, World!\n";
>>>>>>
>>>>>>       dict d;
>>>>>>       d["Headline"] = "Titre";
>>>>>>
>>>>>>       object main_module = import("__main__");
>>>>>>       object main_namespace = main_module.attr("__dict__");
>>>>>>
>>>>>>       main_namespace["iptc"] = ptr(&d);
>>>>>>
>>>>>>       object ignored = exec("print('Headline')\n"
>>>>>>
>>>>>>  "print(iptc['Headline'])\n",
>>>>>>                                               main_namespace);
>>>>>>
>>>>>>
>>>>>> } catch( error_already_set ) {
>>>>>>  PyErr_Print();
>>>>>> }
>>>>>>
>>>>>> And here is hte output :
>>>>>>
>>>>>> Hello, World!
>>>>>> Headline
>>>>>> Traceback (most recent call last):
>>>>>>  File "<string>", line 2, in <module>
>>>>>> TypeError: 'NoneType' object is unsubscriptable
>>>>>>
>>>>>> What did I missed ?
>>>>>>
>>>>>> Thanks en regards,
>>>>>>
>>>>>> Constant
>>>>>> _______________________________________________
>>>>>> Cplusplus-sig mailing list
>>>>>> Cplusplus-sig at python.org
>>>>>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>>>>
>>>>> _______________________________________________
>>>>> Cplusplus-sig mailing list
>>>>> Cplusplus-sig at python.org
>>>>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>>>>
>>>> _______________________________________________
>>>> Cplusplus-sig mailing list
>>>> Cplusplus-sig at python.org
>>>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>>
>>> _______________________________________________
>>> Cplusplus-sig mailing list
>>> Cplusplus-sig at python.org
>>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>>
>>
>


More information about the Cplusplus-sig mailing list