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

Constant Dupuis constant.dupuis at gmail.com
Thu Sep 3 09:12:07 CEST 2009


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


More information about the Cplusplus-sig mailing list