File: PyVTKMethodDescriptor.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (217 lines) | stat: -rw-r--r-- 7,178 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    PyVTKMethodDescriptor.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "PyVTKMethodDescriptor.h"
#include "vtkPythonUtil.h"

#include <structmember.h> // a python header

// Silence warning like
// "dereferencing type-punned pointer will break strict-aliasing rules"
// it happens because this kind of expression: (long *)&ptr
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif

// Required for Python 2.5 through Python 2.7
#ifndef PyDescr_TYPE
#define PyDescr_TYPE(x) (((PyDescrObject *)(x))->d_type)
#define PyDescr_NAME(x) (((PyDescrObject *)(x))->d_name)
#endif

//--------------------------------------------------------------------
// C API

PyObject *PyVTKMethodDescriptor_New(PyTypeObject *pytype, PyMethodDef *meth)
{
  PyMethodDescrObject *descr = (PyMethodDescrObject *)
    PyType_GenericAlloc(&PyVTKMethodDescriptor_Type, 0);

  if (descr)
  {
    Py_XINCREF(pytype);
    PyDescr_TYPE(descr) = pytype;
    PyDescr_NAME(descr) = PyString_InternFromString(meth->ml_name);
    descr->d_method = meth;

    if (!PyDescr_NAME(descr))
    {
      Py_DECREF(descr);
      descr = 0;
    }
  }

  return (PyObject *)descr;
}

//--------------------------------------------------------------------
// Object protocol

static void PyVTKMethodDescriptor_Delete(PyObject *ob)
{
  PyMethodDescrObject *descr = (PyMethodDescrObject *)ob;
  PyObject_GC_UnTrack(descr);
  Py_XDECREF(PyDescr_TYPE(descr));
  Py_XDECREF(PyDescr_NAME(descr));
  PyObject_GC_Del(descr);
}

static PyObject *PyVTKMethodDescriptor_Repr(PyObject *ob)
{
  PyMethodDescrObject *descr = (PyMethodDescrObject *)ob;
#ifdef VTK_PY3K
  return PyUnicode_FromFormat("<method \'%U\' of \'%s\' objects>",
    PyDescr_NAME(descr), PyDescr_TYPE(descr)->tp_name);
#else
  return PyString_FromFormat("<method \'%s\' of \'%s\' objects>",
    PyString_AS_STRING(descr->d_name), descr->d_type->tp_name);
#endif
}

static int PyVTKMethodDescriptor_Traverse(
  PyObject *ob, visitproc visit, void *arg)
{
  PyMethodDescrObject *descr = (PyMethodDescrObject *)ob;
  Py_VISIT(PyDescr_TYPE(descr));
  return 0;
}

static PyObject *PyVTKMethodDescriptor_Call(
  PyObject *ob, PyObject *args, PyObject *kwds)
{
  PyMethodDescrObject *descr = (PyMethodDescrObject *)ob;
  PyObject *result = 0;
  PyObject *func = PyCFunction_New(
    descr->d_method, (PyObject *)PyDescr_TYPE(descr));

  if (func)
  {
    result = PyEval_CallObjectWithKeywords(func, args, kwds);
    Py_DECREF(func);
  }

  return result;
}

static PyObject *PyVTKMethodDescriptor_Get(
  PyObject *self, PyObject *obj, PyObject *)
{
  PyMethodDescrObject *descr = (PyMethodDescrObject *)self;

  if (obj == NULL)
  {
    // If no object to bind to, return the descriptor itself
    Py_INCREF(self);
    return self;
  }

  if (PyObject_TypeCheck(obj, PyDescr_TYPE(descr)))
  {
    // Bind the method to the object
    return PyCFunction_New(descr->d_method, obj);
  }

#ifdef VTK_PY3K
  PyErr_Format(
    PyExc_TypeError,
    "descriptor '%U' for '%s' objects doesn't apply to '%s' object",
    PyDescr_NAME(descr), PyDescr_TYPE(descr)->tp_name,
    Py_TYPE(obj)->tp_name);
#else
  PyErr_Format(
    PyExc_TypeError,
    "descriptor '%s' for '%s' objects doesn't apply to '%s' object",
    PyString_AS_STRING(PyDescr_NAME(descr)), PyDescr_TYPE(descr)->tp_name,
    Py_TYPE(obj)->tp_name);
#endif

  return NULL;
}

static PyObject *PyVTKMethodDescriptor_GetDoc(PyObject *ob, void *)
{
  PyMethodDescrObject *descr = (PyMethodDescrObject *)ob;

  if (descr->d_method->ml_doc == NULL)
  {
    Py_INCREF(Py_None);
    return Py_None;
  }

  return PyString_FromString(descr->d_method->ml_doc);
}

static PyGetSetDef PyVTKMethodDescriptor_GetSet[] = {
  { (char *)"__doc__", PyVTKMethodDescriptor_GetDoc, 0, 0, 0 },
  { 0, 0, 0, 0, 0 }
};

static PyMemberDef PyVTKMethodDescriptor_Members[] = {
  { (char *)"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type),
    READONLY, 0 },
  { (char *)"__name__", T_OBJECT, offsetof(PyDescrObject, d_name),
    READONLY, 0 },
  { 0, 0, 0, 0, 0 }
};

//--------------------------------------------------------------------
PyTypeObject PyVTKMethodDescriptor_Type = {
  PyVarObject_HEAD_INIT(&PyType_Type, 0)
  "vtkCommonCorePython.method_descriptor", // tp_name
  sizeof(PyMethodDescrObject),           // tp_basicsize
  0,                                     // tp_itemsize
  PyVTKMethodDescriptor_Delete,          // tp_dealloc
  0,                                     // tp_print
  0,                                     // tp_getattr
  0,                                     // tp_setattr
  0,                                     // tp_compare
  PyVTKMethodDescriptor_Repr,            // tp_repr
  0,                                     // tp_as_number
  0,                                     // tp_as_sequence
  0,                                     // tp_as_mapping
  0,                                     // tp_hash
  PyVTKMethodDescriptor_Call,            // tp_call
  0,                                     // tp_string
  PyObject_GenericGetAttr,               // tp_getattro
  0,                                     // tp_setattro
  0,                                     // tp_as_buffer
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
  0,                                     // tp_doc
  PyVTKMethodDescriptor_Traverse,        // tp_traverse
  0,                                     // tp_clear
  0,                                     // tp_richcompare
  0,                                     // tp_weaklistoffset
  0,                                     // tp_iter
  0,                                     // tp_iternext
  0,                                     // tp_methods
  PyVTKMethodDescriptor_Members,         // tp_members
  PyVTKMethodDescriptor_GetSet,          // tp_getset
  0,                                     // tp_base
  0,                                     // tp_dict
  PyVTKMethodDescriptor_Get,             // tp_descr_get
  0,                                     // tp_descr_set
  0,                                     // tp_dictoffset
  0,                                     // tp_init
  0,                                     // tp_alloc
  0,                                     // tp_new
  0,                                     // tp_free
  0,                                     // tp_is_gc
  0,                                     // tp_bases
  0,                                     // tp_mro
  0,                                     // tp_cache
  0,                                     // tp_subclasses
  0,                                     // tp_weaklist
  VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED
};