Question about embedding Python

Greg Ewing (using news.cis.dfn.de) me at privacy.net
Mon Mar 17 22:40:05 EST 2003


Del Iablo wrote:
> In order to do this I've tried encapsulating the Python variables inside a
> script variable structure in the C++ code, called ScriptVariable. This
> struct contains a PyObj pointer to the variable in the dictionary. I can
> then do runtime conversion (e.g., PyInt_AsLong) to get the value.

You haven't got a pointer to the variable, you've got a pointer
to the current *value* of the variable. More precisely, you've
got a reference to the object currently associated with a particular
key in the dictionary.

When you "change the value of the variable", you're associating that
key with a different object. But your C pointer is still referring to
the object which was previously associated with that key.

> If I do not re-get the pointers to the variables before attempting to cast
> them, the program crashes

As has been pointed out, this could be due to your not incrementing
the reference count of the object. But even if you fix that, the
above problem will remain.

> I'd like to avoid doing the dictionary lookup every time if possible. Am I
> just stuck doing it every time?

Yes. The only way to find out what object is currently associated
with a key in a dictionary is to look it up. There's no getting
around that.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg





More information about the Python-list mailing list