File: PyVTKExtras.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 205,916 kB
  • sloc: cpp: 2,336,565; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 178; javascript: 165; objc: 153; tcl: 59
file content (120 lines) | stat: -rw-r--r-- 3,692 bytes parent folder | download | duplicates (6)
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

#include "PyVTKExtras.h"
#include "PyVTKReference.h"
#include "vtkABINamespace.h"
#include "vtkPythonCompatibility.h"

// 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

//------------------------------------------------------------------------------
// Helper function for the buffer_shared() check: get the pointer and
// size (in bytes) of the buffer of the provided object.  A return
// value of zero indicates that an exception was raised.
static void* buffer_pointer_and_size(PyObject* o, Py_ssize_t* size)
{
  void* ptr = nullptr;

  // New buffer protocol
  Py_buffer view = { nullptr, nullptr, 0, 0, 0, 0, nullptr, nullptr, nullptr, nullptr, nullptr };
  if (PyObject_CheckBuffer(o))
  {
    // Check for a simple buffer
    if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == -1)
    {
      // Check for a C or Fortran contiguous buffer
      PyErr_Clear();
      if (PyObject_GetBuffer(o, &view, PyBUF_STRIDES) == -1)
      {
        return nullptr;
      }
    }

    ptr = view.buf;
    *size = view.len;

    PyBuffer_Release(&view);

    if (ptr)
    {
      return ptr;
    }
  }

  PyErr_SetString(PyExc_TypeError, "object does not have a readable buffer");

  return nullptr;
}

//------------------------------------------------------------------------------
static PyObject* PyVTKExtras_buffer_shared(PyObject*, PyObject* args)
{
  PyObject* ob[2] = { nullptr, nullptr };
  if (PyArg_UnpackTuple(args, "buffer_shared", 2, 2, &ob[0], &ob[1]))
  {
    void* ptr[2] = { nullptr, nullptr };
    Py_ssize_t size[2] = { 0, 0 };
    for (int i = 0; i < 2; i++)
    {
      ptr[i] = buffer_pointer_and_size(ob[i], &size[i]);
      if (!ptr[i])
      {
        break;
      }
    }
    // check if the pointers and memory size are equal
    if (ptr[0] && ptr[1])
    {
      if (ptr[0] == ptr[1] && size[0] == size[1])
      {
        Py_RETURN_TRUE;
      }
      else
      {
        Py_RETURN_FALSE;
      }
    }
  }
  return nullptr;
}

//------------------------------------------------------------------------------
static PyMethodDef PyVTKExtras_Methods[] = {
  { "buffer_shared", PyVTKExtras_buffer_shared, METH_VARARGS,
    "Check if two objects share the same buffer, meaning that they"
    " point to the same block of memory.  An TypeError exception will"
    " be raised if either of the objects does not provide a buffer." },
  { nullptr, nullptr, 0, nullptr }
};

//------------------------------------------------------------------------------
// Exported method called by vtkCommonCorePythonInit
void PyVTKAddFile_PyVTKExtras(PyObject* dict)
{
  // It is necessary to call PyType_Ready() on all subclasses
  PyType_Ready(&PyVTKNumberReference_Type);
  PyType_Ready(&PyVTKStringReference_Type);
  PyType_Ready(&PyVTKTupleReference_Type);

  // Add the "mutable" object (used for C++ pass-by-reference)
  PyObject* o = (PyObject*)&PyVTKReference_Type;
  PyDict_SetItemString(dict, "reference", o); // new name (as of VTK 8.1)
  PyDict_SetItemString(dict, "mutable", o);   // old name

  for (PyMethodDef* meth = PyVTKExtras_Methods; meth->ml_name != nullptr; meth++)
  {
    // Third argument would be the module object, but all we have is
    // the module's dict, and it's safe to set it to nullptr.
    o = PyCFunction_NewEx(meth, nullptr, nullptr);
    if (o && PyDict_SetItemString(dict, meth->ml_name, o) != 0)
    {
      Py_DECREF(o);
    }
  }
}