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

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Sep 3 19:04:42 CEST 2009


Hi, I'm not sure this is relevant (sorry, no time to look closely), but a couple weeks ago
I fixed a bug in exec.cpp:

------------------------------------------------------------------------
r55639 | rwgk | 2009-08-17 17:24:54 -0700 (Mon, 17 Aug 2009) | 8 lines

libs/python/src/exec.cpp: bug fixes
Remark:
  operator!() for boost::python::object invokes PyObject_IsTrue()
  and is therefore not equivalent to "is None".
  In this particular case !global or !local returns true for an
  empty dict.
(Changes to libs/python/test/exec.cpp just helped in debugging.)

------------------------------------------------------------------------

This made it into the 1.40.0 release. I recommend you work with the current trunk or
the latest release.

Ralf




----- Original Message ----
From: Constant Dupuis <constant.dupuis at gmail.com>
To: cplusplus-sig at python.org
Sent: Thursday, September 3, 2009 12:12:07 AM
Subject: [C++-sig] Embending : Expose and Extract a dictionary object To/From C++

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



More information about the Cplusplus-sig mailing list