source: vendor/current/lib/talloc/pytalloc_guide.txt

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 7.6 KB
Line 
1Using talloc in Samba4
2======================
3
4.. contents::
5
6Jelmer Vernooij
7August 2013
8
9The most current version of this document is available at
10 http://samba.org/ftp/unpacked/talloc/pytalloc_guide.txt
11
12pytalloc is a small library that provides glue for wrapping
13talloc-allocated objects from C in Python objects.
14
15What is pytalloc, and what is it not?
16-------------------------------------
17
18pytalloc is merely a helper library - it provides a convenient base type object
19for objects that wrap talloc-maintained memory in C. It won't write your
20bindings for you but it will make it easier to write C bindings that involve
21talloc, and take away some of the boiler plate.
22
23Python 3
24--------
25
26pytalloc can be used with Python 3. Usage from Python extension remains
27the same, but for the C utilities, the library to link to is tagged with
28Python's PEP3149 ABI tag, for example "pytalloc.cpython34m".
29To make a build for Python 3, configure with PYTHON=/usr/bin/python3.
30.
31=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
32pytalloc_Object / pytalloc_BaseObject
33
34This is the new base class that all Python objects that wrap talloc pointers
35derive from. It is itself a subclass of the "Object" type that all objects
36in Python derive from.
37
38Note that you will almost never create objects of the pytalloc_Object type
39itself, as they are just opaque pointers that can not be accessed from
40Python. A common pattern is other objects that subclass pytalloc_Object and
41rely on it for their memory management.
42
43Each `pytalloc_Object` wraps two core of information - a talloc context
44and a pointer. The pointer is the actual data that is wrapped. The talloc
45context is used for memory management purposes only; when the wrapping Python object
46goes away, it unlinks the talloc context. The talloc context pointer and the ptr
47can (and often do) have the same value.
48
49Each pytalloc_Object has a custom __repr__ implementation that
50describes that it is a talloc object and the location of the
51pointer it is wrapping. it also has a custom __cmp__/__eq__/__neq__ method that
52compares the pointers the object is wrapping rather than the objects
53themselves (since there can be multiple objects that wrap the same talloc
54pointer).
55
56It is preferred to use pytalloc_BaseObject as this implementation
57exposes less in the C ABI and correctly supports pointers in C arrays
58in the way needed by PIDL.
59
60=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
61PyTypeObject *pytalloc_GetObjectType(void)
62
63Obtain a pointer to the PyTypeObject for `pytalloc_Object`. The
64reference counter for the object will be NOT incremented, so the
65caller MUST NOT decrement it when it no longer needs it (eg by using
66`Py_DECREF`).
67
68=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
69PyTypeObject *pytalloc_GetBaseObjectType(void)
70
71Obtain a pointer to the PyTypeObject for `pytalloc_BaseObject`. The
72reference counter for the object will be NOT incremented, so the
73caller MUST NOT decrement it when it no longer needs it (eg by using
74`Py_DECREF`).
75
76=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
77int pytalloc_BaseObject_PyType_Ready(PyTypeObject *type);
78
79Wrapper for PyType_Ready() that will set the correct values into
80the PyTypeObject to create a BaseObject
81
82=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-
83int pytalloc_Check(PyObject *)
84
85Check whether a specific object is a talloc Object. Returns non-zero if it is
86a pytalloc_Object and zero otherwise.
87
88=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-
89int pytalloc_BaseObject_Check(PyObject *)
90
91Check whether a specific object is a talloc BaseObject. Returns non-zero if it is
92a pytalloc_BaseObject and zero otherwise.
93
94=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
95type *pytalloc_get_type(PyObject *py_obj, type)
96
97Retrieve the pointer from a `pytalloc_Object` py_obj. type should be a
98C type, similar to a type passed to `talloc_get_type`.
99
100=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
101pytalloc_get_ptr(PyObject *py_obj)
102
103Retrieve the pointer from a `pytalloc_Object` or `pytalloc_BaseObject`
104py_obj. There is no type checking - use `pytalloc_get_type` if
105possible.
106
107=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
108TALLOC_CTX *pytalloc_get_mem_ctx(PyObject *py_obj)
109
110Retrieve the talloc context associated with a pytalloc_Object or pytalloc_BaseObject.
111
112=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
113PyObject *pytalloc_steal_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr)
114
115Create a new Python wrapping object for a talloc pointer and context, with
116py_type as associated Python sub type object.
117
118This will *not* increment the reference counter for the talloc context,
119so the caller should make sure such an increment has happened. When the Python
120object goes away, it will unreference the talloc context.
121
122=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
123PyObject *pytalloc_steal(PyTypeObject *py_type, void *ptr)
124
125Create a new Python wrapping object for a talloc pointer and context, with
126py_type as associated Python sub type object.
127
128This will *not* increment the reference counter for the talloc context,
129so the caller should make sure such an increment has happened. When the Python
130object goes away, it will unreference the talloc context.
131
132=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
133PyObject *pytalloc_reference_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr)
134
135Create a new Python wrapping object for a talloc pointer and context, with
136py_type as associated Python sub type object.
137
138This will increment the reference counter for the talloc context.
139
140=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
141PyObject *pytalloc_reference(PyTypeObject *py_type, void *talloc_ptr)
142
143Create a new Python wrapping object for a talloc pointer, with
144py_type as associated Python sub type object. The pointer will also be used
145as the talloc context.
146
147This will increment the reference counter for the talloc context.
148
149=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
150PyObject *pytalloc_new(type, PyTypeObject *typeobj)
151
152Create a new, empty pytalloc_Object with the specified Python type object. type
153should be a C type, similar to talloc_new().
154
155=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
156PyObject *pytalloc_CObject_FromTallocPtr(void *);
157
158Create a new pytalloc_Object for an abitrary talloc-maintained C pointer. This will
159use a generic VoidPtr Python type, which just provides an opaque object in
160Python. The caller is responsible for incrementing the talloc reference count before calling
161this function - it will dereference the talloc pointer when it is garbage collected.
162
163This function is only available on Python 2.
164
165Debug function for talloc in Python
166-----------------------------------
167
168The "talloc" module in Python provides a couple of functions that can be used
169to debug issues with objects wrapped by pytalloc.
170
171=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
172report_full(obj?)
173
174Print a full report on a specific object or on all allocated objects by Python.
175Same behaviour as the `talloc_report_full()` function that is provided by
176C talloc.
177
178=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
179enable_null_tracking()
180
181This enables tracking of the NULL memory context without enabling leak
182reporting on exit. Useful for when you want to do your own leak
183reporting call via talloc_report_null_full().
184
185=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
186pytalloc_total_blocks(obj?)
187
188Return the talloc block count for all allocated objects or a specific object if
189specified.
Note: See TracBrowser for help on using the repository browser.