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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
/*-----------------------------------------------------------------------
The PyVTKNamespace was created in Nov 2014 by David Gobbi.
This is a PyModule subclass for wrapping C++ namespaces.
-----------------------------------------------------------------------*/
#include "PyVTKNamespace.h"
#include "vtkABINamespace.h"
#include "vtkPythonUtil.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
//------------------------------------------------------------------------------
static const char* PyVTKNamespace_Doc = "A python module that wraps a C++ namespace.\n";
//------------------------------------------------------------------------------
static void PyVTKNamespace_Delete(PyObject* op)
{
// remove from the map so that there is no dangling reference
vtkPythonUtil::RemoveNamespaceFromMap(op);
// call the superclass destructor
#if PY_VERSION_HEX >= 0x030A0000
PyTypeObject* type = Py_TYPE(op);
PyTypeObject* base = (PyTypeObject*)PyType_GetSlot(type, Py_tp_base);
if (base)
{
destructor dtor = (destructor)PyType_GetSlot(base, Py_tp_dealloc);
dtor(op);
}
#else
PyVTKNamespace_Type.tp_base->tp_dealloc(op);
#endif
}
#ifdef VTK_PYTHON_NEEDS_DEPRECATION_WARNING_SUPPRESSION
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
//------------------------------------------------------------------------------
// clang-format off
PyTypeObject PyVTKNamespace_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"vtkmodules.vtkCommonCore.namespace", // tp_name
0, // tp_basicsize
0, // tp_itemsize
PyVTKNamespace_Delete, // tp_dealloc
#if PY_VERSION_HEX >= 0x03080000
0, // tp_vectorcall_offset
#else
nullptr, // tp_print
#endif
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_compare
nullptr, // tp_repr
nullptr, // tp_as_number
nullptr, // tp_as_sequence
nullptr, // tp_as_mapping
nullptr, // tp_hash
nullptr, // tp_call
nullptr, // tp_string
nullptr, // tp_getattro
nullptr, // tp_setattro
nullptr, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
PyVTKNamespace_Doc, // tp_doc
nullptr, // tp_traverse
nullptr, // tp_clear
nullptr, // tp_richcompare
0, // tp_weaklistoffset
nullptr, // tp_iter
nullptr, // tp_iternext
nullptr, // tp_methods
nullptr, // tp_members
nullptr, // tp_getset
&PyModule_Type, // tp_base
nullptr, // tp_dict
nullptr, // tp_descr_get
nullptr, // tp_descr_set
0, // tp_dictoffset
nullptr, // tp_init
nullptr, // tp_alloc
nullptr, // tp_new
nullptr, // tp_free
nullptr, // tp_is_gc
nullptr, // tp_bases
nullptr, // tp_mro
nullptr, // tp_cache
nullptr, // tp_subclasses
nullptr, // tp_weaklist
VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED };
// clang-format on
//------------------------------------------------------------------------------
PyObject* PyVTKNamespace_New(const char* name)
{
// first check to see if this namespace exists
PyObject* self = vtkPythonUtil::FindNamespace(name);
if (self)
{
Py_INCREF(self);
}
else
{
// make sure python has readied the type object
PyType_Ready(&PyVTKNamespace_Type);
// call the superclass new function
PyObject* empty = PyTuple_New(0);
self = PyVTKNamespace_Type.tp_base->tp_new(&PyVTKNamespace_Type, empty, nullptr);
Py_DECREF(empty);
// call the superclass init function
PyObject* pyname = PyUnicode_FromString(name);
PyObject* args = PyTuple_Pack(1, pyname);
Py_DECREF(pyname);
PyVTKNamespace_Type.tp_base->tp_init(self, args, nullptr);
Py_DECREF(args);
// remember the object for later reference
vtkPythonUtil::AddNamespaceToMap(self);
}
return self;
}
//------------------------------------------------------------------------------
PyObject* PyVTKNamespace_GetDict(PyObject* self)
{
return PyModule_GetDict(self);
}
//------------------------------------------------------------------------------
const char* PyVTKNamespace_GetName(PyObject* self)
{
return PyModule_GetName(self);
}
|