There is no reference counting in C because C does not do automatic garbage collection, you have to do that yourself when you write C. If you don't free memory you allocated you just get a memory leak it won't automatically notice because it has no way of knowing whether or not that memory is actual going to be used. Python's reference counting lets it figure out when to free objects. If there are no references left, nothing can access that object or its memory and it gets destroyed. This is implemented via the incref, decref macros. Remember python objects are literally just C structs, they have a fileld that is just an int that counts the number of other objects or places in code that have an active pointer to the object. Any time you get a pointer to a python object you increment its reference count. As long as the reference count is not 0, it won't be freed by the garbage collector and you can be sure the pointer will be valid. Once your done you decref. If something else still has a reference to it, then the reference count will be positve and it won't be freed. If you were the only person with a reference than the reference count becomes zero and the object is destroyed by the gargage collector. If you directly write python C/api code you have to keep track of this yourself. If this is done incorrectly you will objects never getting freed or being free prematurely (segmentation faults or memory leaks).
Josh On Feb 21, 4:39 pm, "Robert Miller" <[EMAIL PROTECTED]> wrote: > FYI this is a completely hypothetical situation here. I am not doing > any developing in the Python/C API, and may never do so. But I'm > experimenting with different levels of architecture, mainly just to > see how things work. Right now the situation is, suppose I have some > function I want to call from Python, but in the end I want to > implement it in basically raw C. One approach I thought of was, okay, > I'll Pyrex a really simple function, compile it and go into the c > file, then write most of what I need from there. My main thought about > reference counting: > > Python objects seem to need heavy babysitting in terms of reference > counting, especially if you look at some Pyrex autogen code. But it > seems like the C compiler will keep track of reference counts for C > types. Just supposing (again, completely hypothetical) I decide to > write something in the P/C API completely without Pyrex or SageX, but > I want a Python function or two to get called at the top, and I want C > to do the rest. My impression is that I need to keep track of > reference counts for PyObjs but not C variables. Is this true? I have > never had to worry about reference counting when writing pure C, so > why do PyObjects need that extra help? > > On Feb 21, 10:57 am, Robert Bradshaw <[EMAIL PROTECTED]> > wrote: > > > I'll jump in and answer you questions. > > > On Feb 21, 2007, at 10:18 AM, Robert Miller wrote: > > > > David, > > > > I was wondering if you could help get me started here. I've been > > > reading the Python/C API, and I've been browsing both sides of a lot > > > of Pyrex: for argument's sake, let's say the Complex double matrices. > > > First, I'm wondering how they're cdef-ing classes that they're then > > > calling from Python. > > > cdef has two uses. The first is to declare a class as an extension > > type (so it can contain cdef'd property and functions), and the > > second is to declare a property/function as a c type. Cdef'd > > extension types are accessible from python, just not their cdef'd > > properties/functions. > > > > Also, what do .so files in site-packages have to > > > do with specific classes in the repository? > > > > ( > > > sage: > > > sage.matrix.matrix_complex_double_dense.Matrix_complex_double_dense?? > > > Type: type > > > Base Class: <type 'type'> > > > String Form: <type > > > 'sage.matrix.matrix_complex_double_dense.Matrix_complex_double_dense'> > > > Namespace: Interactive > > > File: > > > /Volumes/DATA/sage/local/lib/python2.5/site-packages/sage/matrix/ > > > matrix_complex_double_dense.so > > > ) > > > Each .pyx file has an associated shared object library containing all > > the classes/methods/etc. needed. > > > > I have some questions also about reference counting, but let's > > > start here..... > > > You shouldn't have to worry about it too much. Basically, if you > > malloc something manually, you have to worry about freeing it. If you > > call the C/Python API directly (which you probably won't need to do), > > you have to worry if the returned PyObject* is a borrowed or new > > copy. But otherwise pyrex will take care of it all for you. > > > > -- > > > Robert L. Miller > > >http://www.robertlmiller.com/ --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/ -~----------~----~----~----~------~----~------~--~---