Re: [C++-sig] [Boost.Python] imports and callbacks
-----Original Message----- From: c++-sig-bounces+ngoodspeed=solidworks.com@python.org [mailto:c++- sig-bounces+ngoodspeed=solidworks.com@python.org] On Behalf Of Tanguy Fautre Sent: Thursday, November 16, 2006 11:49 AM To: c++-sig@python.org Subject: [C++-sig] [Boost.Python] imports and callbacks
The interpreter however complains that math.cos is unknown (i.e. ignoring the "import math;" at the beginning of the python code).
If I uncomment #import math in the event_callback definition, everything works fine.
[Nat] When you invoke Python from C++, are you passing the same dict for locals and globals? Whatever you're passing for globals and locals when you evaluate the class definition, are you passing the same two dicts when you re-enter the registered handler? I had a similar issue in which I simply wanted to run a customization script. I prepopulated one dict (I think globals) and passed an empty dict for locals. My inline script code worked fine -- but when I started trying to define functions and classes in the script, the nested code stopped being able to reference my global symbols. I fixed it by passing the same dict for both.
Nat Goodspeed wrote:
-----Original Message----- From: c++-sig-bounces+ngoodspeed=solidworks.com@python.org [mailto:c++- sig-bounces+ngoodspeed=solidworks.com@python.org] On Behalf Of Tanguy Fautre Sent: Thursday, November 16, 2006 11:49 AM To: c++-sig@python.org Subject: [C++-sig] [Boost.Python] imports and callbacks
The interpreter however complains that math.cos is unknown (i.e. ignoring the "import math;" at the beginning of the python code).
If I uncomment #import math in the event_callback definition, everything works fine.
[Nat] When you invoke Python from C++, are you passing the same dict for locals and globals?
Whatever you're passing for globals and locals when you evaluate the class definition, are you passing the same two dicts when you re-enter the registered handler?
I had a similar issue in which I simply wanted to run a customization script. I prepopulated one dict (I think globals) and passed an empty dict for locals. My inline script code worked fine -- but when I started trying to define functions and classes in the script, the nested code stopped being able to reference my global symbols.
I fixed it by passing the same dict for both.
Yes, I'm using one global dict for all the scripts, and separate (empty) local dicts for each scripts. I'll try your approach of using the global dict in place of the local one. For that, I'll need to create separate global dicts for each scripts then. Tanguy
Tanguy Fautre wrote:
Nat Goodspeed wrote:
I fixed it by passing the same dict for both.
Yes, I'm using one global dict for all the scripts, and separate (empty) local dicts for each scripts.
I'll try your approach of using the global dict in place of the local one. For that, I'll need to create separate global dicts for each scripts then.
This does the trick. Thanks. Also, by having a separate global dictionary for each script, it avoids one script polluting the namespace of another script. One little catch though, if you read Boost.Python doc (cf. URL below), you would think that python::dict a; python::dict b(a); creates two separate dictionaries, while in fact b is still a reference to a. To do it correctly, I had to do python::dict a; python::dict b(a.copy()); This maybe needs to be clarified in Boost.Python documentation ? http://boost.org/libs/python/doc/tutorial/doc/html/python/object.html Tanguy
Tanguy Fautre <tanguy.fautre@spaceapplications.com> writes:
One little catch though, if you read Boost.Python doc (cf. URL below), you would think that
python::dict a; python::dict b(a);
creates two separate dictionaries, while in fact b is still a reference to a. To do it correctly, I had to do
python::dict a; python::dict b(a.copy());
I think python::dict b(object(a)); should also work. I *think* the copy ctor is different from all the other ctors. Of course it must make such a copy in order to preserve reference semantics when passing these objects around.
This maybe needs to be clarified in Boost.Python documentation ?
Probably. Joel?
http://boost.org/libs/python/doc/tutorial/doc/html/python/object.html
-- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (3)
-
David Abrahams -
Nat Goodspeed -
Tanguy Fautre