Blame pytalloc_guide.txt

rpm-build 95f51c
Using talloc in Samba4
rpm-build 95f51c
======================
rpm-build 95f51c
rpm-build 95f51c
.. contents::
rpm-build 95f51c
rpm-build 95f51c
Jelmer Vernooij
rpm-build 95f51c
August 2013
rpm-build 95f51c
rpm-build 95f51c
The most current version of this document is available at
rpm-build 95f51c
   http://samba.org/ftp/unpacked/talloc/pytalloc_guide.txt
rpm-build 95f51c
rpm-build 95f51c
pytalloc is a small library that provides glue for wrapping
rpm-build 95f51c
talloc-allocated objects from C in Python objects.
rpm-build 95f51c
rpm-build 95f51c
What is pytalloc, and what is it not?
rpm-build 95f51c
-------------------------------------
rpm-build 95f51c
rpm-build 95f51c
pytalloc is merely a helper library - it provides a convenient base type object
rpm-build 95f51c
for objects that wrap talloc-maintained memory in C. It won't write your
rpm-build 95f51c
bindings for you but it will make it easier to write C bindings that involve
rpm-build 95f51c
talloc, and take away some of the boiler plate.
rpm-build 95f51c
rpm-build 95f51c
Python 3
rpm-build 95f51c
--------
rpm-build 95f51c
rpm-build 95f51c
pytalloc can be used with Python 3. Usage from Python extension remains
rpm-build 95f51c
the same, but for the C utilities, the library to link to is tagged with
rpm-build 95f51c
Python's PEP3149 ABI tag, for example "pytalloc.cpython34m".
rpm-build 95f51c
To make a build for Python 3, configure with PYTHON=/usr/bin/python3.
rpm-build 95f51c
.
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
pytalloc_Object / pytalloc_BaseObject
rpm-build 95f51c
rpm-build 95f51c
This is the new base class that all Python objects that wrap talloc pointers
rpm-build 95f51c
derive from. It is itself a subclass of the "Object" type that all objects
rpm-build 95f51c
in Python derive from.
rpm-build 95f51c
rpm-build 95f51c
Note that you will almost never create objects of the pytalloc_Object type
rpm-build 95f51c
itself, as they are just opaque pointers that can not be accessed from
rpm-build 95f51c
Python. A common pattern is other objects that subclass pytalloc_Object and
rpm-build 95f51c
rely on it for their memory management.
rpm-build 95f51c
rpm-build 95f51c
Each `pytalloc_Object` wraps two core of information - a talloc context
rpm-build 95f51c
and a pointer. The pointer is the actual data that is wrapped. The talloc
rpm-build 95f51c
context is used for memory management purposes only; when the wrapping Python object
rpm-build 95f51c
goes away, it unlinks the talloc context. The talloc context pointer and the ptr
rpm-build 95f51c
can (and often do) have the same value.
rpm-build 95f51c
rpm-build 95f51c
Each pytalloc_Object has a custom __repr__ implementation that
rpm-build 95f51c
describes that it is a talloc object and the location of the
rpm-build 95f51c
pointer it is wrapping. it also has a custom __cmp__/__eq__/__neq__ method that
rpm-build 95f51c
compares the pointers the object is wrapping rather than the objects
rpm-build 95f51c
themselves (since there can be multiple objects that wrap the same talloc
rpm-build 95f51c
pointer).
rpm-build 95f51c
rpm-build 95f51c
It is preferred to use pytalloc_BaseObject as this implementation
rpm-build 95f51c
exposes less in the C ABI and correctly supports pointers in C arrays
rpm-build 95f51c
in the way needed by PIDL.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyTypeObject *pytalloc_GetObjectType(void)
rpm-build 95f51c
rpm-build 95f51c
Obtain a pointer to the PyTypeObject for `pytalloc_Object`. The
rpm-build 95f51c
reference counter for the object will be NOT incremented, so the
rpm-build 95f51c
caller MUST NOT decrement it when it no longer needs it (eg by using
rpm-build 95f51c
`Py_DECREF`).
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyTypeObject *pytalloc_GetBaseObjectType(void)
rpm-build 95f51c
rpm-build 95f51c
Obtain a pointer to the PyTypeObject for `pytalloc_BaseObject`. The
rpm-build 95f51c
reference counter for the object will be NOT incremented, so the
rpm-build 95f51c
caller MUST NOT decrement it when it no longer needs it (eg by using
rpm-build 95f51c
`Py_DECREF`).
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
int pytalloc_BaseObject_PyType_Ready(PyTypeObject *type);
rpm-build 95f51c
rpm-build 95f51c
Wrapper for PyType_Ready() that will set the correct values into
rpm-build 95f51c
the PyTypeObject to create a BaseObject
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
int pytalloc_Check(PyObject *)
rpm-build 95f51c
rpm-build 95f51c
Check whether a specific object is a talloc Object. Returns non-zero if it is
rpm-build 95f51c
a pytalloc_Object and zero otherwise.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
int pytalloc_BaseObject_Check(PyObject *)
rpm-build 95f51c
rpm-build 95f51c
Check whether a specific object is a talloc BaseObject. Returns non-zero if it is
rpm-build 95f51c
a pytalloc_BaseObject and zero otherwise.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
int pytalloc_check_type(PyObject *py_obj, type)
rpm-build 95f51c
rpm-build 95f51c
Check if the object based on `pytalloc_*Object` py_obj. type should be a
rpm-build 95f51c
C type, similar to a type passed to `talloc_get_type`.
rpm-build 95f51c
This can be used as a check before using pytalloc_get_type()
rpm-build 95f51c
or an alternative codepath. Returns non-zero if it is
rpm-build 95f51c
an object of the expected type and zero otherwise.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
type *pytalloc_get_type(PyObject *py_obj, type)
rpm-build 95f51c
rpm-build 95f51c
Retrieve the pointer from a `pytalloc_Object` py_obj. type should be a
rpm-build 95f51c
C type, similar to a type passed to `talloc_get_type`.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
pytalloc_get_ptr(PyObject *py_obj)
rpm-build 95f51c
rpm-build 95f51c
Retrieve the pointer from a `pytalloc_Object` or `pytalloc_BaseObject`
rpm-build 95f51c
py_obj. There is no type checking - use `pytalloc_get_type` if
rpm-build 95f51c
possible.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
TALLOC_CTX *pytalloc_get_mem_ctx(PyObject *py_obj)
rpm-build 95f51c
rpm-build 95f51c
Retrieve the talloc context associated with a pytalloc_Object or pytalloc_BaseObject.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyObject *pytalloc_steal_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr)
rpm-build 95f51c
rpm-build 95f51c
Create a new Python wrapping object for a talloc pointer and context, with
rpm-build 95f51c
py_type as associated Python sub type object. This typically used
rpm-build 95f51c
when `mem_ctx` and `ptr` differ, e.g. a pointer to an array element.
rpm-build 95f51c
`pytalloc_get_ptr()` can be used to get the pointer out of the object again.
rpm-build 95f51c
rpm-build 95f51c
This will *not* increment the reference counter for the talloc context,
rpm-build 95f51c
so the caller should make sure such an increment has happened. When the Python
rpm-build 95f51c
object goes away, it will unreference the talloc context.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyObject *pytalloc_steal(PyTypeObject *py_type, void *ptr)
rpm-build 95f51c
rpm-build 95f51c
Create a new Python wrapping object for a talloc pointer and context, with
rpm-build 95f51c
py_type as associated Python sub type object. The pointer will also be used
rpm-build 95f51c
as the talloc context. `pytalloc_get_type()` can be used to get
rpm-build 95f51c
the pointer out of the object again.
rpm-build 95f51c
rpm-build 95f51c
This will *not* increment the reference counter for the talloc context,
rpm-build 95f51c
so the caller should make sure such an increment has happened. When the Python
rpm-build 95f51c
object goes away, it will unreference the talloc context.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyObject *pytalloc_reference_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr)
rpm-build 95f51c
rpm-build 95f51c
Create a new Python wrapping object for a talloc pointer and context, with
rpm-build 95f51c
py_type as associated Python sub type object. This typically used
rpm-build 95f51c
when `mem_ctx` and `ptr` differ, e.g. a pointer to an array element.
rpm-build 95f51c
`pytalloc_get_ptr()` can be used to get the pointer out of the object again.
rpm-build 95f51c
rpm-build 95f51c
This will increment the reference counter for the talloc context.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyObject *pytalloc_reference(PyTypeObject *py_type, void *talloc_ptr)
rpm-build 95f51c
rpm-build 95f51c
Create a new Python wrapping object for a talloc pointer, with
rpm-build 95f51c
py_type as associated Python sub type object. The pointer will also be used
rpm-build 95f51c
as the talloc context. `pytalloc_get_type()` can be used to get
rpm-build 95f51c
the pointer out of the object again.
rpm-build 95f51c
rpm-build 95f51c
This will increment the reference counter for the talloc context.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyObject *pytalloc_new(type, PyTypeObject *typeobj)
rpm-build 95f51c
rpm-build 95f51c
Create a new, empty pytalloc_Object with the specified Python type object. type
rpm-build 95f51c
should be a C type, similar to talloc_new().
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyObject *pytalloc_GenericObject_steal_ex(void *ptr)
rpm-build 95f51c
rpm-build 95f51c
Create a new Python wrapping object for a generic talloc pointer,
rpm-build 95f51c
as sub type of `pytalloc_BaseObject`. This typically used
rpm-build 95f51c
when `mem_ctx` and `ptr` differ, e.g. a pointer to an array element.
rpm-build 95f51c
`pytalloc_get_ptr()` can be used to get the pointer out of the object again.
rpm-build 95f51c
rpm-build 95f51c
This will *not* increment the reference counter for the talloc context,
rpm-build 95f51c
so the caller should make sure such an increment has happened. When the Python
rpm-build 95f51c
object goes away, it will unreference the talloc context.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyObject *pytalloc_GenericObject_steal(void *ptr)
rpm-build 95f51c
rpm-build 95f51c
Create a new Python wrapping object for a generic talloc pointer,
rpm-build 95f51c
as sub type of `pytalloc_BaseObject`. The pointer will also be used
rpm-build 95f51c
as the talloc context. `pytalloc_get_type()` can be used to get
rpm-build 95f51c
the pointer out of the object again.
rpm-build 95f51c
rpm-build 95f51c
This will *not* increment the reference counter for the talloc context,
rpm-build 95f51c
so the caller should make sure such an increment has happened. When the Python
rpm-build 95f51c
object goes away, it will unreference the talloc context.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyObject *pytalloc_GenericObject_reference_ex(void *ptr)
rpm-build 95f51c
rpm-build 95f51c
Create a new Python wrapping object for a generic talloc pointer,
rpm-build 95f51c
as sub type of `pytalloc_BaseObject`. This typically used
rpm-build 95f51c
when `mem_ctx` and `ptr` differ, e.g. a pointer to an array element.
rpm-build 95f51c
`pytalloc_get_ptr()` can be used to get the pointer out of the object again.
rpm-build 95f51c
rpm-build 95f51c
This will increment the reference counter for the talloc context.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
PyObject *pytalloc_GenericObject_reference(void *ptr)
rpm-build 95f51c
rpm-build 95f51c
Create a new Python wrapping object for a generic talloc pointer,
rpm-build 95f51c
as sub type of `pytalloc_BaseObject`. The pointer will also be used
rpm-build 95f51c
as the talloc context. `pytalloc_get_type()` can be used to get
rpm-build 95f51c
the pointer out of the object again.
rpm-build 95f51c
rpm-build 95f51c
This will increment the reference counter for the talloc context.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
DEPRECATED! PyObject *pytalloc_CObject_FromTallocPtr(void *);
rpm-build 95f51c
rpm-build 95f51c
Create a new pytalloc_Object for an abitrary talloc-maintained C pointer. This will
rpm-build 95f51c
use a generic VoidPtr Python type, which just provides an opaque object in
rpm-build 95f51c
Python. The caller is responsible for incrementing the talloc reference count before calling
rpm-build 95f51c
this function - it will dereference the talloc pointer when it is garbage collected.
rpm-build 95f51c
rpm-build 95f51c
This function is deprecated and only available on Python 2.
rpm-build 95f51c
Use pytalloc_GenericObject_{reference,steal}[_ex]() instead.
rpm-build 95f51c
rpm-build 95f51c
Debug function for talloc in Python
rpm-build 95f51c
-----------------------------------
rpm-build 95f51c
rpm-build 95f51c
The "talloc" module in Python provides a couple of functions that can be used
rpm-build 95f51c
to debug issues with objects wrapped by pytalloc.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
report_full(obj?)
rpm-build 95f51c
rpm-build 95f51c
Print a full report on a specific object or on all allocated objects by Python.
rpm-build 95f51c
Same behaviour as the `talloc_report_full()` function that is provided by
rpm-build 95f51c
C talloc.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
enable_null_tracking()
rpm-build 95f51c
rpm-build 95f51c
This enables tracking of the NULL memory context without enabling leak
rpm-build 95f51c
reporting on exit. Useful for when you want to do your own leak
rpm-build 95f51c
reporting call via talloc_report_null_full().
rpm-build 95f51c
rpm-build 95f51c
This must be done in the top level script, not an imported module.
rpm-build 95f51c
rpm-build 95f51c
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
rpm-build 95f51c
pytalloc_total_blocks(obj?)
rpm-build 95f51c
rpm-build 95f51c
Return the talloc block count for all allocated objects or a specific object if
rpm-build 95f51c
specified.