Commit 4d3bc03f authored by David Gobbi's avatar David Gobbi
Browse files

The Py3K wrapping broke init args for subclasses.

The tp_new slot was unconditionally checking the constructor args,
looking for a swig-style pointer.  This meant that if a vtk class
was subclassed in Python, the subclass constructor args were checked
in the same way regardless of whether it defined its own __init__
method.
parent 8aead085
......@@ -18,9 +18,11 @@ import vtk
from vtk.test import Testing
class vtkCustomObject(vtk.vtkObject):
def __init__(self):
def __init__(self, extra=None):
"""Initialize all attributes."""
self._ExtraObject = vtk.vtkObject()
if extra is None:
extra = vtk.vtkObject()
self._ExtraObject = extra
def GetClassName(self):
"""Get the class name."""
......@@ -53,6 +55,13 @@ class TestSubclass(Testing.vtkTest):
o = vtkCustomObject()
self.assertEqual(o.GetClassName(), "vtkCustomObject")
def testConstructorArgs(self):
"""Test the use of constructor arguments."""
extra = vtk.vtkObject()
o = vtkCustomObject(extra)
self.assertEqual(o.GetClassName(), "vtkCustomObject")
self.assertEqual(id(o.GetExtraObject()), id(extra))
def testCallUnboundMethods(self):
"""Test calling an unbound method in an overridded method"""
o = vtkCustomObject()
......
......@@ -173,24 +173,29 @@ int PyVTKObject_Traverse(PyObject *o, visitproc visit, void *arg)
//--------------------------------------------------------------------
PyObject *PyVTKObject_New(PyTypeObject *tp, PyObject *args, PyObject *kwds)
{
if (kwds != NULL && PyDict_Size(kwds))
// If type was sublassed within python, then skip arg checks and
// simply create a new object.
if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) == 0)
{
PyErr_SetString(PyExc_TypeError,
"this function takes no keyword arguments");
return NULL;
}
if (kwds != NULL && PyDict_Size(kwds))
{
PyErr_SetString(PyExc_TypeError,
"this function takes no keyword arguments");
return NULL;
}
PyObject *o = 0;
if (!PyArg_UnpackTuple(args, tp->tp_name, 0, 1, &o))
{
return NULL;
}
PyObject *o = 0;
if (!PyArg_UnpackTuple(args, tp->tp_name, 0, 1, &o))
{
return NULL;
}
if (o)
{
// used to create a VTK object from a SWIG pointer
return vtkPythonUtil::GetObjectFromObject(
o, vtkPythonUtil::StripModule(tp->tp_name));
if (o)
{
// used to create a VTK object from a SWIG pointer
return vtkPythonUtil::GetObjectFromObject(
o, vtkPythonUtil::StripModule(tp->tp_name));
}
}
// if PyVTKObject_FromPointer gets NULL, it creates a new object.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment