Mailing List Archive

API question -- decref after adding to dictionary?
I'm converting my own associative array into a python dictionary so I can
callout to some python code. I think the snippet below should be clear
enough.

PyDictObject* newdict = PyDict_New();
for (CKeyValList::iterator i(kvl); i; ++i) {
PyString* v = Pystring_FromString(i.value());
PyDict__SetItemString(p, i.key(), v);
Py_DECREF(v);
}

Is the DECREF necessary? Reading the source of dictobject.c makes me think
it is, but the comments in object.h say otherwise.

Replies to me will be summarized to the list. Thanks.
/r$
API question -- decref after adding to dictionary? [ In reply to ]
Rich Salz wrote:
>
> I'm converting my own associative array into a python dictionary so I can
> callout to some python code. I think the snippet below should be clear
> enough.
>
> PyDictObject* newdict = PyDict_New();
> for (CKeyValList::iterator i(kvl); i; ++i) {
> PyString* v = Pystring_FromString(i.value());
> PyDict__SetItemString(p, i.key(), v);
> Py_DECREF(v);
> }
>
> Is the DECREF necessary? Reading the source of dictobject.c makes me think
> it is, but the comments in object.h say otherwise.

Yes it is needed. (Trust the code in dictobject.c, after all, that's
what gets executed !)

--
Marc-Andre Lemburg
______________________________________________________________________
Y2000: 156 days left
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/
Rich Salz wrote:
API question -- decref after adding to dictionary? [ In reply to ]
>Yes it is needed. (Trust the code in dictobject.c, after all, that's
>what gets executed !)


Thanks. I wasn't sure I was reading the code correctly.

The API documentation doesn't seem to say what the functions do to
object references. Am I missing it, or do I really have to read the
implementation to figure it out? If the latter, my interest in Python just
dropped a few points. I mean, refcounts are about the most important
part of writing extensions... (And yes, I understand that this constrains
future implementations. So be it.)
/r$
API question -- decref after adding to dictionary? [ In reply to ]
Rich Salz writes:
> The API documentation doesn't seem to say what the functions do to
> object references. Am I missing it, or do I really have to read the

Rich,
This is a matter of completeness; the API docs just aren't "there"
yet in this regard, and the time to devote to adding the information
hasn't been available.
In the meanwhile, I'd certainly be glad to receive patches which
added the information to the API reference.
I do hope that this isn't a deciding factor as to whether you use
Python. The discussions of reference counting in the API and
Extending & Embedding manuals do try to explain the approaches taken
in Python and reduce the pain significantly. I've written a number of
C extensions myself, including one very early in my experience with
Python, and haven't had a lot of difficulty with it. (Note: I did
*not* say I haven't had any difficulties!)


-Fred

--
Fred L. Drake, Jr. <fdrake@acm.org>
Corporation for National Research Initiatives
API question -- decref after adding to dictionary? [ In reply to ]
Fred L. Drake <fdrake@cnri.reston.va.us> wrote:

: Rich Salz writes:
: > The API documentation doesn't seem to say what the functions do to
: > object references. Am I missing it, or do I really have to read the

: Rich,
: This is a matter of completeness; the API docs just aren't "there"
: yet in this regard, and the time to devote to adding the information
: hasn't been available.
: In the meanwhile, I'd certainly be glad to receive patches which
: added the information to the API reference.
: I do hope that this isn't a deciding factor as to whether you use
: Python. The discussions of reference counting in the API and
: Extending & Embedding manuals do try to explain the approaches taken
: in Python and reduce the pain significantly. I've written a number of
: C extensions myself, including one very early in my experience with
: Python, and haven't had a lot of difficulty with it. (Note: I did
: *not* say I haven't had any difficulties!)

Fred, I haven't given up on my own extention writing section, just gotten
delayed researching the C++ side of it. Should I just drop it?

-Arcege
API question -- decref after adding to dictionary? [ In reply to ]
Michael P. Reilly writes:
> Fred, I haven't given up on my own extention writing section, just gotten
> delayed researching the C++ side of it. Should I just drop it?

Sorry for the delay in responding; I tried to actually get work done
today! ;-)
I think these cover different topics; the upshot of my discussion
with Rich is that Skip is creating a database of reference count
behaviors, which I (or others) can use to improve the API reference,
not the extending & embedding.
I still welcome your contribution!


-Fred

--
Fred L. Drake, Jr. <fdrake@acm.org>
Corporation for National Research Initiatives