FAQ
I'm sure what I'm needing is simple, I must just be missing it somewhere.
There seems to be some fairly basic yet critical functions missing for
manipulating Dictionaries and Lists through the Python 'C' libraries. The
two needs I have are:

(1) Replacing the contents of one dictionary with the contents of another.
This is the case when I pass a dictionary from Python to my C code, using
the dictionary as a "keyed" structure. Through other subsequent calls I
create a new dictionary with matching entries, and want to return this back
to the caller. Right now I'm having to retrieve the (key,value) list, then
loop through each entry in the passed dictionary calling SetItem(key,value).
Seems a bit of overkill when in Python itself I can just say dict1 = dict2.

(2) Removing all entries from a list. Actually my "real" need is to
completely override the default search path for Python. Right now I'm
inserting two additional paths to the head of the Python 'path' list, but
what I would rather do is remove all existing entries from the list then add
my two entries. I can see no way to remove list entries from the 'C' side.
But, if anyone can tell me a better way to override the Python path without
mucking with this variable, that would be great too!

Thanks for any help,
Rich

Search Discussions

  • Bernhard Herzog at Aug 24, 2000 at 2:31 pm

    "Richard Harvey" <tririch at connect.net> writes:

    (1) Replacing the contents of one dictionary with the contents of another.
    This is the case when I pass a dictionary from Python to my C code, using
    the dictionary as a "keyed" structure. Through other subsequent calls I
    create a new dictionary with matching entries, and want to return this back
    to the caller. Right now I'm having to retrieve the (key,value) list, then
    loop through each entry in the passed dictionary calling SetItem(key,value).
    You could also use PyDict_Next. Unfortunately this function appears to
    be undocumented.
    Seems a bit of overkill when in Python itself I can just say dict1 = dict2.
    But dict1 = dict2 doesn't copy the dictionary. It just rebinds the dict1
    variable. In C that would be

    Py_DECREF(dict1);
    dict1 = dict2;
    Py_INCREF(dict1);

    The refcounting may have to be different in your code, of course.
    (2) Removing all entries from a list.
    PySequence_DelSlice(list, 0, PySequence_Length(list))

    should do it, I think.

    --
    Bernhard Herzog | Sketch, a drawing program for Unix
    herzog at online.de | http://sketch.sourceforge.net/
  • Alex at Aug 24, 2000 at 2:48 pm

    Right now I'm having to retrieve the (key,value) list, then loop
    through each entry in the passed dictionary calling
    SetItem(key,value). Seems a bit of overkill when in Python itself I
    can just say dict1 = dict2.
    dict1 = dict2 just makes a new reference to the old dictionary, which is
    not what you're describing with your loop.

    I think the PyDict_Copy function does what you want. Something like

    PyObject *NewDictionary;
    NewDictionary = PyDict_Copy(OldDictionary);
    if (!NewDictionary) {return NULL};
    Right now I'm inserting two additional paths to the head of the Python
    'path' list, but what I would rather do is remove all existing entries
    from the list then add my two entries. I can see no way to remove
    list entries from the 'C' side. But, if anyone can tell me a better
    way to override the Python path without mucking with this variable,
    that would be great too!
    Why do you want to do this on the C side? Wouldn't

    sys.path = [my_two_entries]

    be much easier?

    Alex.

    --
    To succeed in the world it is not enough to be stupid; you must also be
    well-mannered. -- Voltaire

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 24, '00 at 1:12p
activeAug 24, '00 at 2:48p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase