Commit 74de9b8c authored by David Gobbi's avatar David Gobbi
Browse files

Add the module to the tp_name member of every type.

This is done according to the documentation for PyTypeObject. It
allows python to correctly generate the __module__ attribute for
extension types, which is needed by some python packaging systems.
parent 45abed45
......@@ -152,7 +152,7 @@ static PyMemberDef PyVTKMethodDescriptor_Members[] = {
PyTypeObject PyVTKMethodDescriptor_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"vtk.method_descriptor", // tp_name
"vtkCommonCorePython.method_descriptor", // tp_name
sizeof(PyMethodDescrObject), // tp_basicsize
0, // tp_itemsize
PyVTKMethodDescriptor_Delete, // tp_dealloc
......
......@@ -760,7 +760,7 @@ static PyObject *PyVTKMutableObject_New(
PyTypeObject PyVTKMutableObject_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"vtk.mutable", // tp_name
"vtkCommonCorePython.mutable", // tp_name
sizeof(PyVTKMutableObject), // tp_basicsize
0, // tp_itemsize
PyVTKMutableObject_Delete, // tp_dealloc
......
......@@ -47,7 +47,7 @@ static void PyVTKNamespace_Delete(PyObject *op)
PyTypeObject PyVTKNamespace_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"vtk.namespace", // tp_name
"vtkCommonCorePython.namespace", // tp_name
0, // tp_basicsize
0, // tp_itemsize
PyVTKNamespace_Delete, // tp_dealloc
......
......@@ -199,7 +199,8 @@ PyObject *PyVTKObject_New(PyTypeObject *tp, PyObject *args, PyObject *kwds)
if (o)
{
// should use vtk_mangle instead of tp_name, to match __this__
return vtkPythonUtil::GetObjectFromObject(o, tp->tp_name);
return vtkPythonUtil::GetObjectFromObject(
o, vtkPythonUtil::StripModule(tp->tp_name));
}
// if PyVTKObject_FromPointer gets NULL, it creates a new object.
......@@ -347,7 +348,7 @@ PyObject *PyVTKObject_FromPointer(
{
// This will be set if we create a new C++ object
bool created = false;
const char *classname = pytype->tp_name;
const char *classname = vtkPythonUtil::StripModule(pytype->tp_name);
PyVTKClass *cls = 0;
if (ptr)
......
......@@ -189,7 +189,8 @@ PyVTKTemplate_GetItem(PyObject *ob, PyObject *key)
if (PyType_Check(o))
{
const char *cp = ((PyTypeObject *)o)->tp_name;
const char *cp =
vtkPythonUtil::StripModule(((PyTypeObject *)o)->tp_name);
size_t n = strlen(cp);
if (n == 5 && strcmp(cp, "float") == 0)
{
......@@ -298,7 +299,7 @@ static PyObject *PyVTKTemplate_Call(PyObject *, PyObject *, PyObject *)
PyTypeObject PyVTKTemplate_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"vtk.template", // tp_name
"vtkCommonCorePython.template", // tp_name
sizeof(PyVTKTemplate), // tp_basicsize
0, // tp_itemsize
PyVTKTemplate_Delete, // tp_dealloc
......
......@@ -301,7 +301,7 @@ PyVTKSpecialType *vtkPythonUtil::AddSpecialTypeToMap(
PyTypeObject *pytype, PyMethodDef *methods, PyMethodDef *constructors,
vtkcopyfunc copyfunc)
{
const char *classname = pytype->tp_name;
const char *classname = vtkPythonUtil::StripModule(pytype->tp_name);
vtkPythonUtilCreateIfNeeded();
#ifdef VTKPYTHONDEBUG
......@@ -526,6 +526,21 @@ const char *vtkPythonUtil::PythonicClassName(const char *classname)
return classname;
}
//--------------------------------------------------------------------
const char *vtkPythonUtil::StripModule(const char *tpname)
{
const char *cp = tpname;
while (*cp != '.' && *cp != '\0')
{
cp++;
}
if (*cp == '.')
{
return ++cp;
}
return tpname;
}
//--------------------------------------------------------------------
PyVTKClass *vtkPythonUtil::AddClassToMap(
PyTypeObject *pytype, PyMethodDef *methods,
......@@ -762,7 +777,8 @@ PyObject *vtkPythonUtil::GetObjectFromObject(
void *vtkPythonUtil::GetPointerFromSpecialObject(
PyObject *obj, const char *result_type, PyObject **newobj)
{
const char *object_type = obj->ob_type->tp_name;
const char *object_type =
vtkPythonUtil::StripModule(obj->ob_type->tp_name);
// do a lookup on the desired type
vtkPythonSpecialTypeMap::iterator it =
......@@ -908,11 +924,12 @@ void vtkPythonUtil::AddEnumToMap(PyTypeObject *enumtype)
}
// Only add to map if it isn't already there
const char *enumname = vtkPythonUtil::StripModule(enumtype->tp_name);
vtkPythonEnumMap::iterator i =
vtkPythonMap->EnumMap->find(enumtype->tp_name);
vtkPythonMap->EnumMap->find(StripModule(enumname));
if (i == vtkPythonMap->EnumMap->end())
{
(*vtkPythonMap->EnumMap)[enumtype->tp_name] = enumtype;
(*vtkPythonMap->EnumMap)[enumname] = enumtype;
}
}
......
......@@ -47,6 +47,10 @@ public:
// a python-printable name.
static const char *PythonicClassName(const char *classname);
// Description:
// Given a qualified python name "module.name", remove "module.".
static const char *StripModule(const char *tpname);
// Description:
// Add a PyVTKClass to the type lookup table, this allows us to later
// create object given only the class name.
......
......@@ -276,14 +276,14 @@ static void vtkWrapPython_GenerateSpecialHeaders(
/* -------------------------------------------------------------------- */
/* import any wrapped enum types that are used by this file */
static void vtkWrapPython_ImportExportEnumTypes(
FILE *fp, FileInfo *file_info, HierarchyInfo *hinfo)
FILE *fp, const char *thisModule,
FileInfo *file_info, HierarchyInfo *hinfo)
{
const char **types;
int numTypes = 0;
FunctionInfo *currentFunction;
const char *thisModule = 0;
int i, j, k, n, m, ii, nn;
ClassInfo *data;
int i, j, k, n, m, ii, nn;
ValueInfo *val;
types = (const char **)malloc(1000*sizeof(const char *));
......@@ -343,17 +343,6 @@ static void vtkWrapPython_ImportExportEnumTypes(
}
}
/* get the module that is being wrapped */
data = file_info->MainClass;
if (!data && file_info->Contents->NumberOfClasses > 0)
{
data = file_info->Contents->Classes[0];
}
if (data)
{
thisModule = vtkWrapPython_ClassModule(hinfo, data->Name);
}
/* for each unique enum type found in the file */
for (i = 0; i < numTypes; i++)
{
......@@ -415,6 +404,7 @@ int main(int argc, char *argv[])
HierarchyInfo *hinfo = NULL;
FileInfo *file_info;
FILE *fp;
const char *module = "vtkCommonCore";
const char *name;
char *name_from_file = NULL;
int numberOfWrappedClasses = 0;
......@@ -532,13 +522,24 @@ int main(int argc, char *argv[])
"extern \"C\" { %s void PyVTKAddFile_%s(PyObject *, const char *); }\n",
"VTK_PYTHON_EXPORT", name);
/* get the module that is being wrapped */
data = file_info->MainClass;
if (!data && file_info->Contents->NumberOfClasses > 0)
{
data = file_info->Contents->Classes[0];
}
if (data && hinfo)
{
module = vtkWrapPython_ClassModule(hinfo, data->Name);
}
/* do the imports of any enum types that are used by methods */
vtkWrapPython_ImportExportEnumTypes(fp, file_info, hinfo);
vtkWrapPython_ImportExportEnumTypes(fp, module, file_info, hinfo);
/* Wrap any enum types defined in the global namespace */
for (i = 0; i < contents->NumberOfEnums; i++)
{
vtkWrapPython_GenerateEnumType(fp, NULL, contents->Enums[i]);
vtkWrapPython_GenerateEnumType(fp, module, NULL, contents->Enums[i]);
}
/* Wrap any namespaces */
......@@ -546,7 +547,7 @@ int main(int argc, char *argv[])
{
if (contents->Namespaces[i]->NumberOfConstants > 0)
{
vtkWrapPython_WrapNamespace(fp, contents->Namespaces[i]);
vtkWrapPython_WrapNamespace(fp, module, contents->Namespaces[i]);
numberOfWrappedNamespaces++;
}
}
......@@ -592,7 +593,7 @@ int main(int argc, char *argv[])
if (hinfo || data == file_info->MainClass)
{
if (vtkWrapPython_WrapOneClass(
fp, data->Name, data, file_info, hinfo, is_vtkobject))
fp, module, data->Name, data, file_info, hinfo, is_vtkobject))
{
/* re-index wrapAsVTKObject for wrapped classes */
wrapAsVTKObject[numberOfWrappedClasses] = (is_vtkobject ? 1 : 0);
......@@ -607,12 +608,11 @@ int main(int argc, char *argv[])
contents->NumberOfConstants);
fprintf(fp,
"void PyVTKAddFile_%s(\n"
" PyObject *%s, const char *%s)\n"
" PyObject *%s, const char *)\n"
"{\n"
"%s",
name,
(wrapped_anything ? "dict" : ""),
(numberOfWrappedClasses ? "modulename" : ""),
(wrapped_anything ? " PyObject *o;\n" : ""));
/* Add all of the namespaces */
......@@ -642,7 +642,7 @@ int main(int argc, char *argv[])
{
/* Template generator */
fprintf(fp,
" o = Py%s_TemplateNew(modulename);\n"
" o = Py%s_TemplateNew();\n"
"\n",
data->Name);
......@@ -677,7 +677,7 @@ int main(int argc, char *argv[])
{
/* Class is derived from vtkObjectBase */
fprintf(fp,
" o = Py%s_ClassNew(modulename);\n"
" o = Py%s_ClassNew();\n"
"\n",
data->Name);
}
......@@ -685,7 +685,7 @@ int main(int argc, char *argv[])
{
/* Classes that are not derived from vtkObjectBase */
fprintf(fp,
" o = Py%s_TypeNew(modulename);\n"
" o = Py%s_TypeNew();\n"
"\n",
data->Name);
}
......
......@@ -311,7 +311,7 @@ static void vtkWrapPython_ExportVTKClass(
/* for vtkObjectBase objects: export New method for use by subclasses */
fprintf(fp,
"extern \"C\" { %s PyObject *Py%s_ClassNew(const char *); }\n"
"extern \"C\" { %s PyObject *Py%s_ClassNew(); }\n"
"\n",
"VTK_PYTHON_EXPORT", classname);
......@@ -322,7 +322,7 @@ static void vtkWrapPython_ExportVTKClass(
vtkWrapPython_PythonicName(supername, classname);
fprintf(fp,
"#ifndef DECLARED_Py%s_ClassNew\n"
"extern \"C\" { PyObject *Py%s_ClassNew(const char *); }\n"
"extern \"C\" { PyObject *Py%s_ClassNew(); }\n"
"#define DECLARED_Py%s_ClassNew\n"
"#endif\n",
classname, classname, classname);
......@@ -352,7 +352,7 @@ static void vtkWrapPython_GenerateObjectNew(
}
fprintf(fp,
"PyObject *Py%s_ClassNew(const char *)\n"
"PyObject *Py%s_ClassNew()\n"
"{\n"
" PyVTKClass_Add(\n"
" &Py%s_Type, Py%s_Methods,\n",
......@@ -403,7 +403,7 @@ static void vtkWrapPython_GenerateObjectNew(
{
vtkWrapPython_PythonicName(name, superclassname);
fprintf(fp,
" pytype->tp_base = (PyTypeObject *)Py%s_ClassNew(0);\n\n",
" pytype->tp_base = (PyTypeObject *)Py%s_ClassNew();\n\n",
superclassname);
}
......@@ -439,14 +439,15 @@ static void vtkWrapPython_GenerateObjectNew(
/* -------------------------------------------------------------------- */
/* write out the type object */
void vtkWrapPython_GenerateObjectType(FILE *fp, const char *classname)
void vtkWrapPython_GenerateObjectType(
FILE *fp, const char *module, const char *classname)
{
/* Generate the TypeObject */
fprintf(fp,
"static PyTypeObject Py%s_Type = {\n"
" PyObject_HEAD_INIT(&PyType_Type)\n"
" 0,\n"
" \"%s\", // tp_name\n"
" \"%sPython.%s\", // tp_name\n"
" sizeof(PyVTKObject), // tp_basicsize\n"
" 0, // tp_itemsize\n"
" PyVTKObject_Delete, // tp_dealloc\n"
......@@ -455,7 +456,7 @@ void vtkWrapPython_GenerateObjectType(FILE *fp, const char *classname)
" 0, // tp_setattr\n"
" 0, // tp_compare\n"
" PyVTKObject_Repr, // tp_repr\n",
classname, classname);
classname, module, classname);
fprintf(fp,
" 0, // tp_as_number\n"
......@@ -510,8 +511,9 @@ void vtkWrapPython_GenerateObjectType(FILE *fp, const char *classname)
/* -------------------------------------------------------------------- */
/* Wrap one class */
int vtkWrapPython_WrapOneClass(
FILE *fp, const char *classname, ClassInfo *data,
FileInfo *finfo, HierarchyInfo *hinfo, int is_vtkobject)
FILE *fp, const char *module, const char *classname,
ClassInfo *data, FileInfo *finfo, HierarchyInfo *hinfo,
int is_vtkobject)
{
int class_has_new = 0;
int i;
......@@ -559,7 +561,7 @@ int vtkWrapPython_WrapOneClass(
if (data->Enums[i]->Access == VTK_ACCESS_PUBLIC)
{
vtkWrapPython_GenerateEnumType(
fp, classname, data->Enums[i]);
fp, module, classname, data->Enums[i]);
}
}
......@@ -574,7 +576,7 @@ int vtkWrapPython_WrapOneClass(
if (is_vtkobject)
{
vtkWrapPython_GenerateObjectType(
fp, classname);
fp, module, classname);
vtkWrapPython_GenerateObjectNew(
fp, classname, data, hinfo, class_has_new);
}
......@@ -583,7 +585,7 @@ int vtkWrapPython_WrapOneClass(
else if (!data->IsAbstract)
{
vtkWrapPython_GenerateSpecialType(
fp, classname, data, finfo, hinfo);
fp, module, classname, data, finfo, hinfo);
}
/* the docstring for the class, as a static var ending in "Doc" */
......
......@@ -22,8 +22,9 @@
/* Wrap one class, returns zero if not wrappable */
int vtkWrapPython_WrapOneClass(
FILE *fp, const char *classname, ClassInfo *data,
FileInfo *file_info, HierarchyInfo *hinfo, int is_vtkobject);
FILE *fp, const char *module, const char *classname,
ClassInfo *data, FileInfo *file_info, HierarchyInfo *hinfo,
int is_vtkobject);
/* get the true superclass */
const char *vtkWrapPython_GetSuperClass(
......
......@@ -72,7 +72,7 @@ void vtkWrapPython_AddEnumType(
/* -------------------------------------------------------------------- */
/* write out an enum type object */
void vtkWrapPython_GenerateEnumType(
FILE *fp, const char *classname, EnumInfo *data)
FILE *fp, const char *module, const char *classname, EnumInfo *data)
{
char enumname[512];
char tpname[512];
......@@ -105,7 +105,7 @@ void vtkWrapPython_GenerateEnumType(
"PyTypeObject Py%s_Type = {\n"
" PyObject_HEAD_INIT(&PyType_Type)\n"
" 0,\n"
" \"%s\", // tp_name\n"
" \"%sPython.%s\", // tp_name\n"
" sizeof(PyIntObject), // tp_basicsize\n"
" 0, // tp_itemsize\n"
" 0, // tp_dealloc\n"
......@@ -114,7 +114,7 @@ void vtkWrapPython_GenerateEnumType(
" 0, // tp_setattr\n"
" 0, // tp_compare\n"
" 0, // tp_repr\n",
enumname, tpname);
enumname, module, tpname);
fprintf(fp,
" 0, // tp_as_number\n"
......@@ -174,8 +174,7 @@ void vtkWrapPython_GenerateEnumType(
" return (PyObject *)self;\n"
"}\n"
"\n",
enumname,
enumname);
enumname, enumname);
}
/* generate code that adds all public enum types to a python dict */
......
......@@ -26,7 +26,7 @@ int vtkWrapPython_IsEnumWrapped(
/* write out an enum type wrapped in python */
void vtkWrapPython_GenerateEnumType(
FILE *fp, const char *classname, EnumInfo *data);
FILE *fp, const char *module, const char *classname, EnumInfo *data);
/* generate code that adds an enum type to a python dict */
void vtkWrapPython_AddEnumType(
......
......@@ -27,7 +27,8 @@
/* -------------------------------------------------------------------- */
/* Wrap the namespace */
int vtkWrapPython_WrapNamespace(FILE *fp, NamespaceInfo *data)
int vtkWrapPython_WrapNamespace(
FILE *fp, const char *module, NamespaceInfo *data)
{
int i;
......@@ -35,7 +36,7 @@ int vtkWrapPython_WrapNamespace(FILE *fp, NamespaceInfo *data)
for (i = 0; i < data->NumberOfEnums; i++)
{
vtkWrapPython_GenerateEnumType(
fp, data->Name, data->Enums[i]);
fp, module, data->Name, data->Enums[i]);
}
fprintf(fp,
......
......@@ -21,6 +21,7 @@
#include "vtkParseHierarchy.h"
/* Wrap one class, returns zero if not wrappable */
int vtkWrapPython_WrapNamespace(FILE *fp, NamespaceInfo *data);
int vtkWrapPython_WrapNamespace(
FILE *fp, const char *module, NamespaceInfo *data);
#endif /* VTK_WRAP_PYTHON_NAMESPACE_H */
......@@ -427,7 +427,7 @@ int vtkWrapPython_WrapTemplatedClass(
vtkWrapPython_PythonicName(instantiations[k], classname);
vtkWrapPython_WrapOneClass(
fp, classname, sdata, file_info, hinfo, is_vtkobject);
fp, modulename, classname, sdata, file_info, hinfo, is_vtkobject);
vtkParse_FreeClass(sdata);
vtkParse_FreeTemplateDecomposition(name, nargs, args);
......@@ -457,14 +457,14 @@ int vtkWrapPython_WrapTemplatedClass(
"\n");
fprintf(fp,
"PyObject *Py%s_TemplateNew(const char *modulename)\n"
"PyObject *Py%s_TemplateNew()\n"
"{\n"
" PyObject *o;\n"
"\n"
" PyObject *temp = PyVTKTemplate_New(\"%s\", modulename,\n"
" PyObject *temp = PyVTKTemplate_New(\"%s\", \"%s\",\n"
" Py%s_Doc);\n"
"\n",
data->Name, data->Name, data->Name);
data->Name, data->Name, modulename, data->Name);
for (k = 0; k < ninstantiations; k++)
{
......@@ -475,13 +475,13 @@ int vtkWrapPython_WrapTemplatedClass(
hinfo, entry, instantiations[k], "vtkObjectBase", NULL))
{
fprintf(fp,
" o = Py%s_ClassNew(modulename);\n",
" o = Py%s_ClassNew();\n",
classname);
}
else
{
fprintf(fp,
" o = Py%s_TypeNew(modulename);\n",
" o = Py%s_TypeNew();\n",
classname);
}
......
......@@ -611,8 +611,8 @@ int vtkWrapPython_IsSpecialTypeWrappable(ClassInfo *data)
/* -------------------------------------------------------------------- */
/* write out a special type object */
void vtkWrapPython_GenerateSpecialType(
FILE *fp, const char *classname, ClassInfo *data,
FileInfo *finfo, HierarchyInfo *hinfo)
FILE *fp, const char *module, const char *classname,
ClassInfo *data, FileInfo *finfo, HierarchyInfo *hinfo)
{
char supername[1024];
const char *name;
......@@ -662,7 +662,7 @@ void vtkWrapPython_GenerateSpecialType(
"static PyTypeObject Py%s_Type = {\n"
" PyObject_HEAD_INIT(&PyType_Type)\n"
" 0,\n"
" \"%s\", // tp_name\n"
" \"%sPython.%s\", // tp_name\n"
" sizeof(PyVTKSpecialObject), // tp_basicsize\n"
" 0, // tp_itemsize\n"
" Py%s_Delete, // tp_dealloc\n"
......@@ -671,7 +671,7 @@ void vtkWrapPython_GenerateSpecialType(
" 0, // tp_setattr\n"
" 0, // tp_compare\n"
" PyVTKSpecialObject_Repr, // tp_repr\n",
classname, classname, classname);
classname, module, classname, classname);
fprintf(fp,
" 0, // tp_as_number\n");
......@@ -791,7 +791,7 @@ void vtkWrapPython_GenerateSpecialType(
/* export New method for use by subclasses */
fprintf(fp,
"extern \"C\" { %s PyObject *Py%s_TypeNew(const char *); }\n\n",
"extern \"C\" { %s PyObject *Py%s_TypeNew(); }\n\n",
"VTK_PYTHON_EXPORT", classname);
/* import New method of the superclass */
......@@ -799,7 +799,7 @@ void vtkWrapPython_GenerateSpecialType(
{
fprintf(fp,
"#ifndef DECLARED_Py%s_TypeNew\n"
"extern \"C\" { PyObject *Py%s_TypeNew(const char *); }\n"
"extern \"C\" { PyObject *Py%s_TypeNew(); }\n"
"#define DECLARED_Py%s_TypeNew\n"
"#endif\n",
supername, supername, supername);
......@@ -808,7 +808,7 @@ void vtkWrapPython_GenerateSpecialType(
/* the method for adding the VTK extras to the type,
* the unused "const char *" arg is the module name */
fprintf(fp,
"PyObject *Py%s_TypeNew(const char *)\n"
"PyObject *Py%s_TypeNew()\n"
"{\n"
" PyVTKSpecialType_Add(\n"
" &Py%s_Type,\n"
......@@ -835,7 +835,7 @@ void vtkWrapPython_GenerateSpecialType(
if (has_superclass)
{
fprintf(fp,
" pytype->tp_base = (PyTypeObject *)Py%s_TypeNew(0);\n\n",
" pytype->tp_base = (PyTypeObject *)Py%s_TypeNew();\n\n",
supername);
}
......
......@@ -25,7 +25,7 @@ int vtkWrapPython_IsSpecialTypeWrappable(ClassInfo *data);
/* write out a python type object */
void vtkWrapPython_GenerateSpecialType(
FILE *fp, const char *classname, ClassInfo *data,
FileInfo *finfo, HierarchyInfo *hinfo);
FILE *fp, const char *module, const char *classname,
ClassInfo *data, FileInfo *finfo, HierarchyInfo *hinfo);
#endif /* VTK_WRAP_PYTHON_TYPE_H */
Supports Markdown
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