File: PyVTKObject.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 (805 lines) | stat: -rw-r--r-- 24,031 bytes parent folder | download | duplicates (4)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
/*-----------------------------------------------------------------------
  The PyVTKObject was created in Oct 2000 by David Gobbi for VTK 3.2.
  Support for weakref added in July 2005 by Prabhu Ramachandran.
  Buffer interface for vtk arrays added in Feb 2008 by Berk Geveci.

  A PyVTKObject is a python object that represents a VTK object.
  The methods are stored in the __dict__ of the associated type objects.
  Each PyVTKObject also has a __dict__ of its own that can be used to
  store arbitrary attributes.

  Memory management is done as follows. Each PyVTKObject has
  an entry along with a smart pointer to its vtkObjectBase in
  the vtkPythonUtil::ObjectMap.  When a PyVTKObject is destructed,
  it is removed along with the smart pointer from the ObjectMap.
-----------------------------------------------------------------------*/

#include "PyVTKObject.h"
#include "PyVTKMethodDescriptor.h"
#include "vtkABINamespace.h"
#include "vtkDataArray.h"
#include "vtkObjectBase.h"
#include "vtkPythonCommand.h"
#include "vtkPythonUtil.h"

#include <cstddef>
#include <dictobject.h>
#include <sstream>

// This will be set to the python type struct for vtkObjectBase
static PyTypeObject* PyVTKObject_Type = nullptr;

VTK_ABI_NAMESPACE_BEGIN
//------------------------------------------------------------------------------
PyVTKClass::PyVTKClass(
  PyTypeObject* typeobj, PyMethodDef* methods, const char* classname, vtknewfunc constructor)
{
  this->py_type = typeobj;
  this->py_methods = methods;
  this->vtk_name = classname;
  this->vtk_new = constructor;
}
VTK_ABI_NAMESPACE_END

//------------------------------------------------------------------------------
// Create a Python "override" method
// See the help string below this function for details.
static PyObject* PyVTKClass_override(PyObject* cls, PyObject* type)
{
  PyTypeObject* typeobj = (PyTypeObject*)cls;
  std::string clsName = vtkPythonUtil::StripModuleFromType(typeobj);

  if (PyObject_TypeCheck(type, &PyType_Type))
  {
    PyTypeObject* newtypeobj = (PyTypeObject*)type;
    if (PyType_IsSubtype(newtypeobj, typeobj))
    {
      // Make sure "type" and intermediate classes aren't wrapped classes
      for (PyTypeObject* tp = newtypeobj; tp && tp != typeobj;
           tp =
#if PY_VERSION_HEX >= 0x030A0000
             (PyTypeObject*)PyType_GetSlot(tp, Py_tp_base)
#else
             tp->tp_base
#endif
      )
      {
        PyVTKClass* c = vtkPythonUtil::FindClass(vtkPythonUtil::StripModuleFromType(tp));
        if (c && tp == c->py_type)
        {
          std::string str("method requires overriding with a pure python subclass of ");
          str += clsName;
          str += ", subclassing from VTK C++ subclasses is not allowed.";
          PyErr_SetString(PyExc_TypeError, str.c_str());
          return nullptr;
        }
      }

      // Set the override
      PyVTKClass* thecls = vtkPythonUtil::FindClass(clsName.c_str());
      thecls->py_type = newtypeobj;
      // Store override in dict of old type, to keep a reference to it
      PyDict_SetItemString(typeobj->tp_dict, "__override__", type);
    }
    else
    {
      std::string str("method requires a subtype of ");
      str += clsName;
      PyErr_SetString(PyExc_TypeError, str.c_str());
      return nullptr;
    }
  }
  else if (type == Py_None)
  {
    // Clear the override
    PyVTKClass* thecls = vtkPythonUtil::FindClass(clsName.c_str());
    thecls->py_type = typeobj;
    // Delete the __override__ attribute if it exists
    if (PyDict_DelItemString(typeobj->tp_dict, "__override__") == -1)
    {
      // Clear the KeyError that occurs if __override__ doesn't exist
      PyErr_Clear();
    }
  }
  else
  {
    PyErr_SetString(PyExc_TypeError, "method requires a type object or None.");
    return nullptr;
  }

  Py_INCREF(type);
  return type;
}

static PyMethodDef PyVTKClass_override_def = { "override", PyVTKClass_override, METH_CLASS | METH_O,
  "This method can be used to override a VTK class with a Python subclass.\n"
  "The class type passed to override will afterwards be instantiated\n"
  "instead of the type override is called on.\n"
  "For example,\n"
  "\n"
  "class foo(vtk.vtkPoints):\n"
  "  pass\n"
  "vtk.vtkPoints.override(foo)\n"
  "\n"
  "will lead to foo being instantied every time vtkPoints() is called.\n"
  "The main objective of this functionality is to enable developers to\n"
  "extend VTK classes with more pythonic subclasses that contain\n"
  "convenience functionality.\n" };

//------------------------------------------------------------------------------
// Add a class, add methods and members to its type object.  A return
// value of nullptr signifies that the class was already added.
PyTypeObject* PyVTKClass_Add(
  PyTypeObject* pytype, PyMethodDef* methods, const char* classname, vtknewfunc constructor)
{
  // Check whether the type is already in the map (use classname as key),
  // and return it if so.  If not, then add it to the map.
  pytype = vtkPythonUtil::AddClassToMap(pytype, methods, classname, constructor);

  // Cache the type object for vtkObjectBase for quick access
  if (PyVTKObject_Type == nullptr && strcmp(classname, "vtkObjectBase") == 0)
  {
    PyVTKObject_Type = pytype;
  }

  // If type object already has a dict, we're done
  if (pytype->tp_dict)
  {
    return pytype;
  }

  // Create the dict
  pytype->tp_dict = PyDict_New();

  // Add special attribute __vtkname__
  PyObject* s = PyUnicode_FromString(classname);
  PyDict_SetItemString(pytype->tp_dict, "__vtkname__", s);
  Py_DECREF(s);

  // Add all of the methods
  for (PyMethodDef* meth = methods; meth && meth->ml_name; meth++)
  {
    PyObject* func = PyVTKMethodDescriptor_New(pytype, meth);
    PyDict_SetItemString(pytype->tp_dict, meth->ml_name, func);
    Py_DECREF(func);
  }

  // Add the override method
  if (strcmp(classname, "vtkObjectBase") == 0)
  {
    PyObject* func = PyDescr_NewClassMethod(pytype, &PyVTKClass_override_def);
    PyDict_SetItemString(pytype->tp_dict, PyVTKClass_override_def.ml_name, func);
    Py_DECREF(func);
  }
  return pytype;
}

void PyVTKClass_AddCombinedGetSetDefinitions(PyTypeObject* pytype, PyGetSetDef* getsets)
{
  // Add all of the getsets
  for (PyGetSetDef* getset = getsets; getset && getset->name; getset++)
  {
    if (getset->get == nullptr)
    {
      // find a getter in superclass
      if (pytype->tp_base != nullptr)
      {
        auto key = PyUnicode_FromString(getset->name);
        if (auto superGetSet = vtkPythonUtil::FindGetSetDescriptor(pytype->tp_base, key))
        {
          getset->get = superGetSet->get;
        }
        Py_DECREF(key);
      }
    }
    else if (getset->set == nullptr)
    {
      // find a setter in superclass
      if (pytype->tp_base != nullptr)
      {
        auto key = PyUnicode_FromString(getset->name);
        if (auto superGetSet = vtkPythonUtil::FindGetSetDescriptor(pytype->tp_base, key))
        {
          getset->set = superGetSet->set;
        }
        Py_DECREF(key);
      }
    }
    PyObject* descr = PyDescr_NewGetSet(pytype, getset);
    PyDict_SetItemString(pytype->tp_dict, getset->name, descr);
    Py_DECREF(descr);
  }
}

//------------------------------------------------------------------------------
int PyVTKObject_Check(PyObject* op)
{
  return PyObject_TypeCheck(op, PyVTKObject_Type);
}

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

//------------------------------------------------------------------------------
PyObject* PyVTKObject_String(PyObject* op)
{
  std::ostringstream vtkmsg_with_warning_C4701;
  ((PyVTKObject*)op)->vtk_ptr->Print(vtkmsg_with_warning_C4701);
  vtkmsg_with_warning_C4701.put('\0');
  PyObject* res = PyUnicode_FromString(vtkmsg_with_warning_C4701.str().c_str());
  return res;
}

//------------------------------------------------------------------------------
PyObject* PyVTKObject_Repr(PyObject* op)
{
  PyVTKObject* obj = (PyVTKObject*)op;
  return PyUnicode_FromFormat("<%s(%p) at %p>", vtkPythonUtil::GetTypeNameForObject(op),
    static_cast<void*>(obj->vtk_ptr), static_cast<void*>(obj));
}

//------------------------------------------------------------------------------
int PyVTKObject_Traverse(PyObject* o, visitproc visit, void* arg)
{
  PyVTKObject* self = (PyVTKObject*)o;
  int err = 0;

  if (self->vtk_observers != nullptr)
  {
    unsigned long* olist = self->vtk_observers;
    while (err == 0 && *olist != 0)
    {
      vtkObject* op = static_cast<vtkObject*>(self->vtk_ptr);
      vtkCommand* c = op->GetCommand(*olist);
      if (c == nullptr)
      {
        // observer is gone, remove from list
        unsigned long* tmp = olist;
        do
        {
          tmp++;
        } while (*tmp != 0);
        *olist = *--tmp;
        *tmp = 0;
      }
      else
      {
        // visit the observer
        vtkPythonCommand* cbc = static_cast<vtkPythonCommand*>(c);
        err = visit(cbc->obj, arg);
        olist++;
      }
    }
  }

  return err;
}

//------------------------------------------------------------------------------
PyObject* PyVTKObject_New(PyTypeObject* tp, PyObject* args, PyObject* /*kwds*/)
{
  // XXX(python3-abi3): all types will be heap types in abi3
  // If type was subclassed within python, then skip arg checks and
  // simply create a new object.
  PyObject* o = nullptr;
  if ((PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) == 0)
  {
    if (!PyArg_UnpackTuple(args, vtkPythonUtil::GetTypeName(tp), 0, 1, &o))
    {
      return nullptr;
    }

    if (o)
    {
      // used to create a VTK object from a SWIG pointer
      return vtkPythonUtil::GetObjectFromObject(o, vtkPythonUtil::StripModuleFromType(tp));
    }
  }

  // if PyVTKObject_FromPointer gets nullptr, it creates a new object.
  o = PyVTKObject_FromPointer(tp, nullptr, nullptr);

  return o;
}

//------------------------------------------------------------------------------
int PyVTKObject_Init(PyObject* obj, PyObject* /*args*/, PyObject* kwds)
{
  bool success = true;
  if (kwds != nullptr && PyDict_Size(kwds))
  {
    PyObject *key, *value;
    Py_ssize_t ppos = 0;
    // Walks through every keyword argument and sets the property on the object.
    // Loosely equivalent to this python code with error handling omitted.
    // for key, value in kwargs.items():
    //     setattr(obj, key, value)
    PyTypeObject* tp = Py_TYPE(obj);
    while (PyDict_Next(kwds, &ppos, &key, &value) && success)
    {
      // Check if the key is an existing property.
      if (vtkPythonUtil::FindGetSetDescriptor(tp, key) != nullptr)
      {
        // If the setter failed, break.
        if (PyObject_SetAttr(obj, key, value) == -1)
        {
          // Don't try to raise an exception here because it's already raised by PyObject_SetAttr.
          success = false;
        }
      }
      else
      {
        PyObject* utf8StringObj = PyUnicode_AsUTF8String(key);
        std::string keyName = utf8StringObj ? PyBytes_AsString(utf8StringObj) : "(null)";
        Py_XDECREF(utf8StringObj);
        // Raise TypeError for unexpected keyword argument.
        PyErr_SetString(PyExc_TypeError,
          ("Unexpected keyword argument \'" + keyName + "\' for \'" +
            vtkPythonUtil::GetTypeName(tp) + "\' constructor")
            .c_str());
        success = false;
      }
    }
  }
  return success ? 0 : -1;
}

//------------------------------------------------------------------------------
void PyVTKObject_Delete(PyObject* op)
{
  PyVTKObject* self = (PyVTKObject*)op;

  PyObject_GC_UnTrack(op);

  if (self->vtk_weakreflist != nullptr)
  {
    PyObject_ClearWeakRefs(op);
  }

  // A python object owning a VTK object reference is getting
  // destroyed.  Remove the python object's VTK object reference.
  vtkPythonUtil::RemoveObjectFromMap(op);

  Py_DECREF(self->vtk_dict);
  delete[] self->vtk_observers;
  delete[] self->vtk_buffer;

  PyObject_GC_Del(op);
}

//------------------------------------------------------------------------------
// This defines any special attributes of wrapped VTK objects.

static PyObject* PyVTKObject_GetDict(PyObject* op, void*)
{
  PyVTKObject* self = (PyVTKObject*)op;
  Py_INCREF(self->vtk_dict);
  return self->vtk_dict;
}

static PyObject* PyVTKObject_GetThis(PyObject* op, void*)
{
  PyVTKObject* self = (PyVTKObject*)op;
  const char* classname = self->vtk_ptr->GetClassName();
  const char* cp = classname;
  char buf[1024];
  // check to see if classname is a valid python identifier
  if (isalpha(*cp) || *cp == '_')
  {
    do
    {
      cp++;
    } while (isalnum(*cp) || *cp == '_');
  }
  // otherwise, use the pythonic form of the class name
  if (*cp != '\0')
  {
    classname = vtkPythonUtil::StripModuleFromObject(op);
  }
  snprintf(buf, sizeof(buf), "p_%.500s", classname);
  return PyUnicode_FromString(vtkPythonUtil::ManglePointer(self->vtk_ptr, buf));
}

#if PY_VERSION_HEX >= 0x03070000
#define pystr(x) x
#else
#define pystr(x) const_cast<char*>(x)
#endif

PyGetSetDef PyVTKObject_GetSet[] = { { pystr("__dict__"), PyVTKObject_GetDict, nullptr,
                                       pystr("Dictionary of attributes set by user."), nullptr },
  { pystr("__this__"), PyVTKObject_GetThis, nullptr, pystr("Pointer to the C++ object."), nullptr },
  { nullptr, nullptr, nullptr, nullptr, nullptr } };

//------------------------------------------------------------------------------
// The following methods and struct define the "buffer" protocol
// for PyVTKObject, so that python can read from a vtkDataArray.
// This is particularly useful for NumPy.

//------------------------------------------------------------------------------
// Convert a VTK type to a python type char (struct module)
static const char* pythonTypeFormat(int t)
{
  const char* b = nullptr;

  switch (t)
  {
    case VTK_CHAR:
      b = "c";
      break;
    case VTK_SIGNED_CHAR:
      b = "b";
      break;
    case VTK_UNSIGNED_CHAR:
      b = "B";
      break;
    case VTK_SHORT:
      b = "h";
      break;
    case VTK_UNSIGNED_SHORT:
      b = "H";
      break;
    case VTK_INT:
      b = "i";
      break;
    case VTK_UNSIGNED_INT:
      b = "I";
      break;
    case VTK_LONG:
      b = "l";
      break;
    case VTK_UNSIGNED_LONG:
      b = "L";
      break;
    case VTK_LONG_LONG:
      b = "q";
      break;
    case VTK_UNSIGNED_LONG_LONG:
      b = "Q";
      break;
    case VTK_FLOAT:
      b = "f";
      break;
    case VTK_DOUBLE:
      b = "d";
      break;
#ifndef VTK_USE_64BIT_IDS
    case VTK_ID_TYPE:
      b = "i";
      break;
#else
    case VTK_ID_TYPE:
      b = "q";
      break;
#endif
  }

  return b;
}

//------------------------------------------------------------------------------
static int PyVTKObject_AsBuffer_GetBuffer(PyObject* obj, Py_buffer* view, int flags)
{
  PyVTKObject* self = (PyVTKObject*)obj;
  vtkDataArray* da = vtkDataArray::SafeDownCast(self->vtk_ptr);
  if (da)
  {
    void* ptr = da->GetVoidPointer(0);
    Py_ssize_t ntuples = da->GetNumberOfTuples();
    int ncomp = da->GetNumberOfComponents();
    int dsize = da->GetDataTypeSize();
    const char* format = pythonTypeFormat(da->GetDataType());
    Py_ssize_t size = ntuples * ncomp * dsize;

    if (da->GetDataType() == VTK_BIT)
    {
      size = (ntuples * ncomp + 7) / 8;
    }

    // start by building a basic "unsigned char" buffer
    if (PyBuffer_FillInfo(view, obj, ptr, size, 0, flags) == -1)
    {
      return -1;
    }
    // check if a dimensioned array was requested
    if (format != nullptr && (flags & PyBUF_ND) != 0)
    {
      // first, build a simple 1D array
      view->itemsize = dsize;
      view->ndim = (ncomp > 1 ? 2 : 1);
      view->format = const_cast<char*>(format);

      {
        if (self->vtk_buffer && self->vtk_buffer[0] != view->ndim)
        {
          delete[] self->vtk_buffer;
          self->vtk_buffer = nullptr;
        }
        if (self->vtk_buffer == nullptr)
        {
          self->vtk_buffer = new Py_ssize_t[2 * view->ndim + 1];
          self->vtk_buffer[0] = view->ndim;
        }
        view->shape = &self->vtk_buffer[1];
        view->strides = &self->vtk_buffer[view->ndim + 1];
      }

      if (view->ndim == 1)
      {
        // simple one-dimensional array
        view->shape[0] = ntuples * ncomp;
        view->strides[0] = view->itemsize;
      }
      else
      {
        // use native C dimension ordering by default
        char order = 'C';
        if ((flags & PyBUF_ANY_CONTIGUOUS) == PyBUF_F_CONTIGUOUS)
        {
          // use fortran ordering only if explicitly requested
          order = 'F';
        }
        // need to allocate space for the strides and shape
        view->shape[0] = ntuples;
        view->shape[1] = ncomp;
        if (order == 'F')
        {
          view->shape[0] = ncomp;
          view->shape[1] = ntuples;
        }
        PyBuffer_FillContiguousStrides(view->ndim, view->shape, view->strides, dsize, order);
      }
    }
    return 0;
  }

  PyErr_Format(
    PyExc_ValueError, "Cannot get a buffer from %s.", vtkPythonUtil::GetTypeNameForObject(obj));
  return -1;
}

//------------------------------------------------------------------------------
static void PyVTKObject_AsBuffer_ReleaseBuffer(PyObject* obj, Py_buffer* view)
{
  // nothing to do, the caller will decref the obj
  (void)obj;
  (void)view;
}

//------------------------------------------------------------------------------
PyBufferProcs PyVTKObject_AsBuffer = {
  PyVTKObject_AsBuffer_GetBuffer,    // bf_getbuffer
  PyVTKObject_AsBuffer_ReleaseBuffer // bf_releasebuffer
};

//------------------------------------------------------------------------------
PyObject* PyVTKObject_FromPointer(PyTypeObject* pytype, PyObject* ghostdict, vtkObjectBase* ptr)
{
  // This will be set if we create a new C++ object
  bool created = false;
  std::string classname = vtkPythonUtil::StripModuleFromType(pytype);
  PyVTKClass* cls = nullptr;

  if (ptr)
  {
    // If constructing from an existing C++ object, use its actual class
    classname = ptr->GetClassName();
    cls = vtkPythonUtil::FindClass(classname.c_str());
  }

  if (cls == nullptr)
  {
    // Use the vtkname of the supplied class type
    PyObject* s = PyObject_GetAttrString((PyObject*)pytype, "__vtkname__");
    if (s)
    {
      PyObject* tmp = PyUnicode_AsUTF8String(s);
      if (tmp)
      {
        Py_DECREF(s);
        s = tmp;
      }
      const char* vtkname_classname = PyBytes_AsString(s);
      if (vtkname_classname == nullptr)
      {
        Py_DECREF(s);
        return nullptr;
      }
      classname = vtkname_classname;
      Py_DECREF(s);
    }
    cls = vtkPythonUtil::FindClass(classname.c_str());
    if (cls == nullptr)
    {
      PyErr_Format(PyExc_ValueError, "internal error, unknown VTK class %.200s", classname.c_str());
      return nullptr;
    }
  }

  if (!ptr)
  {
    // Create a new instance of this class since we were not given one.
    if (cls->vtk_new)
    {
      ptr = cls->vtk_new();
      if (!ptr)
      {
        // The vtk_new() method returns null when a factory class has no
        // implementation (i.e. cannot provide a concrete class instance.)
        // NotImplementedError indicates a pure virtual method call.
        PyErr_SetString(
          PyExc_NotImplementedError, "no concrete implementation exists for this class");
        return nullptr;
      }

      // Check if the VTK object already has a Python object
      // (e.g. vtk_new() might return a singleton instance)
      PyObject* obj = vtkPythonUtil::FindObject(ptr);
      if (obj)
      {
        ptr->Delete();
        return obj;
      }

      // flag to indicate that the VTK object is a new instance
      created = true;

      // Check the type of the newly-created object
      const char* newclassname = ptr->GetClassName();
      if (std::string(newclassname) != classname)
      {
        PyVTKClass* newclass = vtkPythonUtil::FindClass(newclassname);
        if (newclass)
        {
          classname = newclassname;
          cls = newclass;
        }
      }
    }
    else
    {
      PyErr_SetString(PyExc_TypeError, "this is an abstract class and cannot be instantiated");
      return nullptr;
    }
  }

  if ((PyType_GetFlags(pytype) & Py_TPFLAGS_HEAPTYPE) != 0)
  {
    // Incref if class was declared in python (see PyType_GenericAlloc).
    Py_INCREF(pytype);
  }
  else
  {
    // To support factory New methods, use the object's actual class
    pytype = cls->py_type;
  }

  // Create a new dict unless object is being resurrected from a ghost
  PyObject* pydict = ghostdict;
  if (pydict)
  {
    Py_INCREF(pydict);
  }
  else
  {
    pydict = PyDict_New();
  }

  PyVTKObject* self = PyObject_GC_New(PyVTKObject, pytype);

  self->vtk_ptr = ptr;
  self->vtk_flags = 0;
  self->vtk_class = cls;
  self->vtk_dict = pydict;
  self->vtk_buffer = nullptr;
  self->vtk_observers = nullptr;
  self->vtk_weakreflist = nullptr;

  PyObject_GC_Track((PyObject*)self);

  // A python object owning a VTK object reference is getting
  // created.  Add the python object's VTK object reference.
  vtkPythonUtil::AddObjectToMap((PyObject*)self, ptr);

  // The hash now owns a reference so we can free ours.
  if (created)
  {
    ptr->Delete();
  }
  else if (ghostdict == nullptr && pytype->tp_init != nullptr)
  {
    // For checking if Python __init__ call modifies the C++ object
    vtkObject* checkptr = vtkObject::SafeDownCast(ptr);
    vtkMTimeType checktime = 0;
    if (checkptr)
    {
      checktime = checkptr->vtkObject::GetMTime();
    }
    // Call __init__(self)
    PyObject* arglist = Py_BuildValue("()");
    int res = pytype->tp_init((PyObject*)self, arglist, nullptr);
    Py_DECREF(arglist);
    if (res < 0)
    {
      Py_DECREF(self);
      self = nullptr;
    }
    else if (checkptr && checktime < checkptr->vtkObject::GetMTime())
    {
      // If the C++ object already existed within VTK, and the Python object
      // is being created right now, then we don't want the creation of the
      // Python object to cause changes to object attributes that were already
      // set by the C++ VTK code.  For example, if we're getting the output
      // dataset from a C++ VTK filter, we don't want any C++ attributes of
      // the dataset to change when the Python part of the dataset object is
      // created and initialized.
      std::string message = "Python method ";
      message += pytype->tp_name;
      message += ".__init__() ";
      message += "unexpectedly modified pre-existing C++ base object ";
      message += checkptr->GetObjectDescription();
      message += ".";
      PyErr_WarnEx(PyExc_RuntimeWarning, message.c_str(), 1);
    }
  }

  return (PyObject*)self;
}

vtkObjectBase* PyVTKObject_GetObject(PyObject* obj)
{
  return ((PyVTKObject*)obj)->vtk_ptr;
}

void PyVTKObject_AddObserver(PyObject* obj, unsigned long id)
{
  unsigned long* olist = ((PyVTKObject*)obj)->vtk_observers;
  unsigned long n = 0;
  if (olist == nullptr)
  {
    olist = new unsigned long[8];
    ((PyVTKObject*)obj)->vtk_observers = olist;
  }
  else
  {
    // count the number of items
    while (olist[n] != 0)
    {
      n++;
    }
    // check if n+1 is a power of two (base allocation is 8)
    unsigned long m = n + 1;
    if (m >= 8 && (n & m) == 0)
    {
      unsigned long* tmp = olist;
      olist = new unsigned long[2 * m];
      for (unsigned long i = 0; i < n; i++)
      {
        olist[i] = tmp[i];
      }
      delete[] tmp;
      ((PyVTKObject*)obj)->vtk_observers = olist;
    }
  }
  olist[n++] = id;
  olist[n] = 0;
}

unsigned int PyVTKObject_GetFlags(PyObject* obj)
{
  return ((PyVTKObject*)obj)->vtk_flags;
}

void PyVTKObject_SetFlag(PyObject* obj, unsigned int flag, int val)
{
  if (val)
  {
    ((PyVTKObject*)obj)->vtk_flags |= flag;
  }
  else
  {
    ((PyVTKObject*)obj)->vtk_flags &= ~flag;
  }
}