Note that there is a mailing list dedicated to the Python/C API,
http://mail.python.org/mailman/listinfo/capi-sig

[EMAIL PROTECTED] writes:
> issue #2 I'm in a situation when i don't really need to extend
> python with any classes of my own but i do have extra luggage for
> the python data structures such as tuples, lists, dictionaries, etc
> on the c++ side. I see no place in PyObject_HEAD where i can stick a
> void* to my extra data.

Adding additional luggage is a valid definition of extending.  :-)

Simply inherit from the desired data structure, and add the pointers
you need to the extended structure.  If you must add extra data to
existing Python objects created by someone else, then you can use a
weak dictionary:

import weakref
_map = {}  # can't use WeakKeyDictionary because we need
           # mapping by identity

def set_extra_data(obj, data):
    # use id(obj) as key to map by object identity
    ident = id(obj)
    ref = weakref.ref(obj, lambda x: _map.pop(ident))
    _map[ident] = ref, data

def extra_data(obj):
    if id(obj) not in _map:
        raise TypeError("object has no extra data")
    ref, data = _map[id(obj)]
    assert ref() is obj
    return data
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to