Ignore:
Timestamp:
Nov 14, 2012, 12:59:34 PM (13 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: update vendor to 3.6.0

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vendor/current/lib/talloc/pytalloc.c

    r414 r740  
    11/*
    22   Unix SMB/CIFS implementation.
    3    Python/Talloc glue
    4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
    5    
     3   Python Talloc Module
     4   Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
     5
    66   This program is free software; you can redistribute it and/or modify
    77   it under the terms of the GNU General Public License as published by
    88   the Free Software Foundation; either version 3 of the License, or
    99   (at your option) any later version.
    10    
     10
    1111   This program is distributed in the hope that it will be useful,
    1212   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1313   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1414   GNU General Public License for more details.
    15    
     15
    1616   You should have received a copy of the GNU General Public License
    1717   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1818*/
    1919
    20 #include "replace.h"
     20#include <Python.h>
    2121#include <talloc.h>
    2222#include <pytalloc.h>
    2323
    24 /**
    25  * Simple dealloc for talloc-wrapping PyObjects
    26  */
    27 void py_talloc_dealloc(PyObject* self)
     24void inittalloc(void);
     25
     26/* print a talloc tree report for a talloc python object */
     27static PyObject *py_talloc_report_full(PyObject *self, PyObject *args)
    2828{
    29         py_talloc_Object *obj = (py_talloc_Object *)self;
    30         talloc_free(obj->talloc_ctx);
    31         obj->talloc_ctx = NULL;
    32         self->ob_type->tp_free(self);
     29        PyObject *py_obj = Py_None;
     30        PyTypeObject *type;
     31
     32        if (!PyArg_ParseTuple(args, "|O", &py_obj))
     33                return NULL;
     34
     35        if (py_obj == Py_None) {
     36                talloc_report_full(NULL, stdout);
     37        } else {
     38                type = (PyTypeObject*)PyObject_Type(py_obj);
     39                talloc_report_full(py_talloc_get_mem_ctx(py_obj), stdout);
     40        }
     41        return Py_None;
    3342}
    3443
    35 /**
    36  * Import an existing talloc pointer into a Python object.
    37  */
    38 PyObject *py_talloc_steal_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx,
    39                                                    void *ptr)
     44/* enable null tracking */
     45static PyObject *py_talloc_enable_null_tracking(PyObject *self)
    4046{
    41         py_talloc_Object *ret = (py_talloc_Object *)py_type->tp_alloc(py_type, 0);
    42         ret->talloc_ctx = talloc_new(NULL);
    43         if (ret->talloc_ctx == NULL) {
    44                 return NULL;
    45         }
    46         if (talloc_steal(ret->talloc_ctx, mem_ctx) == NULL) {
    47                 return NULL;
    48         }
    49         ret->ptr = ptr;
    50         return (PyObject *)ret;
     47        talloc_enable_null_tracking();
     48        return Py_None;
    5149}
    5250
     51/* return the number of talloc blocks */
     52static PyObject *py_talloc_total_blocks(PyObject *self, PyObject *args)
     53{
     54        PyObject *py_obj = Py_None;
     55        PyTypeObject *type;
     56
     57        if (!PyArg_ParseTuple(args, "|O", &py_obj))
     58                return NULL;
     59
     60        if (py_obj == Py_None) {
     61                return PyLong_FromLong(talloc_total_blocks(NULL));
     62        }
     63
     64        type = (PyTypeObject*)PyObject_Type(py_obj);
     65
     66        return PyLong_FromLong(talloc_total_blocks(py_talloc_get_mem_ctx(py_obj)));
     67}
     68
     69static PyMethodDef talloc_methods[] = {
     70        { "report_full", (PyCFunction)py_talloc_report_full, METH_VARARGS,
     71                "show a talloc tree for an object"},
     72        { "enable_null_tracking", (PyCFunction)py_talloc_enable_null_tracking, METH_NOARGS,
     73                "enable tracking of the NULL object"},
     74        { "total_blocks", (PyCFunction)py_talloc_total_blocks, METH_VARARGS,
     75                "return talloc block count"},
     76        { NULL }
     77};
    5378
    5479/**
    55  * Import an existing talloc pointer into a Python object, leaving the
    56  * original parent, and creating a reference to the object in the python
    57  * object
     80 * Default (but only slightly more useful than the default) implementation of Repr().
    5881 */
    59 PyObject *py_talloc_reference_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr)
    60 {
    61         py_talloc_Object *ret = (py_talloc_Object *)py_type->tp_alloc(py_type, 0);
    62         ret->talloc_ctx = talloc_new(NULL);
    63         if (ret->talloc_ctx == NULL) {
    64                 return NULL;
    65         }
    66         if (talloc_reference(ret->talloc_ctx, mem_ctx) == NULL) {
    67                 return NULL;
    68         }
    69         ret->ptr = ptr;
    70         return (PyObject *)ret;
    71 }
    72 
    73 /**
    74  * Default (but slightly more useful than the default) implementation of Repr().
    75  */
    76 PyObject *py_talloc_default_repr(PyObject *obj)
     82static PyObject *py_talloc_default_repr(PyObject *obj)
    7783{
    7884        py_talloc_Object *talloc_obj = (py_talloc_Object *)obj;
     
    8389}
    8490
    85 static void py_cobject_talloc_free(void *ptr)
     91/**
     92 * Simple dealloc for talloc-wrapping PyObjects
     93 */
     94static void py_talloc_dealloc(PyObject* self)
    8695{
    87         talloc_free(ptr);
     96        py_talloc_Object *obj = (py_talloc_Object *)self;
     97        assert(talloc_unlink(NULL, obj->talloc_ctx) != -1);
     98        obj->talloc_ctx = NULL;
     99        self->ob_type->tp_free(self);
    88100}
    89101
    90 PyObject *PyCObject_FromTallocPtr(void *ptr)
     102/**
     103 * Default (but only slightly more useful than the default) implementation of cmp.
     104 */
     105static int py_talloc_default_cmp(PyObject *_obj1, PyObject *_obj2)
    91106{
    92         return PyCObject_FromVoidPtr(ptr, py_cobject_talloc_free);
     107        py_talloc_Object *obj1 = (py_talloc_Object *)_obj1,
     108                                         *obj2 = (py_talloc_Object *)_obj2;
     109        if (obj1->ob_type != obj2->ob_type)
     110                return (obj1->ob_type - obj2->ob_type);
     111
     112        return ((char *)py_talloc_get_ptr(obj1) - (char *)py_talloc_get_ptr(obj2));
    93113}
     114
     115static PyTypeObject TallocObject_Type = {
     116        .tp_name = "talloc.Object",
     117        .tp_doc = "Python wrapper for a talloc-maintained object.",
     118        .tp_basicsize = sizeof(py_talloc_Object),
     119        .tp_dealloc = (destructor)py_talloc_dealloc,
     120        .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
     121        .tp_repr = py_talloc_default_repr,
     122        .tp_compare = py_talloc_default_cmp,
     123};
     124
     125void inittalloc(void)
     126{
     127        PyObject *m;
     128
     129        if (PyType_Ready(&TallocObject_Type) < 0)
     130                return;
     131
     132        m = Py_InitModule3("talloc", talloc_methods,
     133                                           "Python wrapping of talloc-maintained objects.");
     134        if (m == NULL)
     135                return;
     136
     137        Py_INCREF(&TallocObject_Type);
     138        PyModule_AddObject(m, "Object", (PyObject *)&TallocObject_Type);
     139}
Note: See TracChangeset for help on using the changeset viewer.