merge
[cython.git] / Cython / Includes / cpython / __init__.pxd
1 #####################################################################
2 #
3 # These are the Cython pxd files for (most of) the Python/C API.
4 #
5 # REFERENCE COUNTING:
6 #
7 #   JUST TO SCARE YOU:
8 #   If you are going to use any of the Python/C API in your Cython
9 #   program, you might be responsible for doing reference counting.
10 #   Read http://docs.python.org/api/refcounts.html which is so
11 #   important I've copied it below.
12 #
13 # For all the declaration below, whenver the Py_ function returns
14 # a *new reference* to a PyObject*, the return type is "object".
15 # When the function returns a borrowed reference, the return
16 # type is PyObject*.  When Cython sees "object" as a return type
17 # it doesn't increment the reference count.  When it sees PyObject*
18 # in order to use the result you must explicitly cast to <object>,
19 # and when you do that Cython increments the reference count wether
20 # you want it to or not, forcing you to an explicit DECREF (or leak memory).
21 # To avoid this we make the above convention.  Note, you can
22 # always locally override this convention by putting something like
23 #
24 #     cdef extern from "Python.h":
25 #         PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)
26 #
27 # in your file after any .pxi includes.  Cython will use the latest
28 # declaration. 
29 #
30 # Cython takes care of this automatically for anything of type object.
31 ## More precisely, I think the correct convention for
32 ## using the Python/C API from Pyrex is as follows.
33 ##
34 ## (1) Declare all input arguments as type "object".  This way no explicit
35 ##    <PyObject*> casting is needed, and moreover Pyrex doesn't generate
36 ##    any funny reference counting.
37 ## (2) Declare output as object if a new reference is returned.
38 ## (3) Declare output as PyObject* if a borrowed reference is returned.
39 ##    
40 ## This way when you call objects, no cast is needed, and if the api
41 ## calls returns a new reference (which is about 95% of them), then
42 ## you can just assign to a variable of type object.  With borrowed
43 ## references if you do an explicit typecast to <object>, Pyrex generates an
44 ## INCREF and DECREF so you have to be careful.  However, you got a
45 ## borrowed reference in this case, so there's got to be another reference
46 ## to your object, so you're OK, as long as you relealize this
47 ## and use the result of an explicit cast to <object> as a borrowed
48 ## reference (and you can call Py_INCREF if you want to turn it
49 ## into another reference for some reason). 
50
51 # "The reference count is important because today's computers have
52 # a finite (and often severely limited) memory size; it counts how
53 # many different places there are that have a reference to an
54 # object. Such a place could be another object, or a global (or
55 # static) C variable, or a local variable in some C function. When
56 # an object's reference count becomes zero, the object is
57 # deallocated. If it contains references to other objects, their
58 # reference count is decremented. Those other objects may be
59 # deallocated in turn, if this decrement makes their reference
60 # count become zero, and so on. (There's an obvious problem with
61 # objects that reference each other here; for now, the solution is
62 # ``don't do that.'')
63 #
64 # Reference counts are always manipulated explicitly. The normal
65 # way is to use the macro Py_INCREF() to increment an object's
66 # reference count by one, and Py_DECREF() to decrement it by
67 # one. The Py_DECREF() macro is considerably more complex than the
68 # incref one, since it must check whether the reference count
69 # becomes zero and then cause the object's deallocator to be
70 # called. The deallocator is a function pointer contained in the
71 # object's type structure. The type-specific deallocator takes
72 # care of decrementing the reference counts for other objects
73 # contained in the object if this is a compound object type, such
74 # as a list, as well as performing any additional finalization
75 # that's needed. There's no chance that the reference count can
76 # overflow; at least as many bits are used to hold the reference
77 # count as there are distinct memory locations in virtual memory
78 # (assuming sizeof(long) >= sizeof(char*)). Thus, the reference
79 # count increment is a simple operation.
80
81 # It is not necessary to increment an object's reference count for
82 # every local variable that contains a pointer to an object. In
83 # theory, the object's reference count goes up by one when the
84 # variable is made to point to it and it goes down by one when the
85 # variable goes out of scope. However, these two cancel each other
86 # out, so at the end the reference count hasn't changed. The only
87 # real reason to use the reference count is to prevent the object
88 # from being deallocated as long as our variable is pointing to
89 # it. If we know that there is at least one other reference to the
90 # object that lives at least as long as our variable, there is no
91 # need to increment the reference count temporarily. An important
92 # situation where this arises is in objects that are passed as
93 # arguments to C functions in an extension module that are called
94 # from Python; the call mechanism guarantees to hold a reference
95 # to every argument for the duration of the call.
96 #
97 # However, a common pitfall is to extract an object from a list
98 # and hold on to it for a while without incrementing its reference
99 # count. Some other operation might conceivably remove the object
100 # from the list, decrementing its reference count and possible
101 # deallocating it. The real danger is that innocent-looking
102 # operations may invoke arbitrary Python code which could do this;
103 # there is a code path which allows control to flow back to the
104 # user from a Py_DECREF(), so almost any operation is potentially
105 # dangerous.
106 #
107 # A safe approach is to always use the generic operations
108 # (functions whose name begins with "PyObject_", "PyNumber_",
109 # "PySequence_" or "PyMapping_"). These operations always
110 # increment the reference count of the object they return. This
111 # leaves the caller with the responsibility to call Py_DECREF()
112 # when they are done with the result; this soon becomes second
113 # nature.
114 #
115 # Now you should read http://docs.python.org/api/refcountDetails.html
116 # just to be sure you understand what is going on.
117 #
118 #################################################################
119
120 from cpython.version cimport *
121 from cpython.ref cimport *
122 from cpython.exc cimport *
123 from cpython.module cimport *
124 from cpython.mem cimport *
125 from cpython.tuple cimport *
126 from cpython.list cimport *
127 from cpython.object cimport *
128 from cpython.sequence cimport *
129 from cpython.mapping cimport *
130 from cpython.iterator cimport *
131 from cpython.type cimport *
132 from cpython.number cimport *
133 from cpython.int cimport *
134 from cpython.bool cimport *
135 from cpython.long cimport *
136 from cpython.float cimport *
137 from cpython.complex cimport *
138 from cpython.string cimport *
139 from cpython.unicode cimport *
140 from cpython.dict cimport *
141 from cpython.instance cimport *
142 from cpython.function cimport *
143 from cpython.method cimport *
144 from cpython.weakref cimport *
145 from cpython.getargs cimport *
146
147 # Python <= 2.x
148 from cpython.cobject cimport *
149 from cpython.oldbuffer cimport *
150
151 # Python >= 2.4
152 from cpython.set cimport *
153
154 # Python >= 2.6
155 from cpython.buffer cimport *
156 from cpython.bytes cimport *
157
158 # Python >= 3.0
159 from cpython.pycapsule cimport *