Commit 43768391 authored by David Gobbi's avatar David Gobbi Committed by Marcus D. Hanwell
Browse files

ENH: Add string support to vtkParse and vtkWrapPython.c

parent aa30d197
......@@ -1522,7 +1522,10 @@ int PyVTKCheckArg(
case 'z':
if (format[1] == '#') // memory buffer
{
penalty |= VTK_PYTHON_GOOD_MATCH;
if (penalty != VTK_PYTHON_INCOMPATIBLE)
{
penalty = VTK_PYTHON_GOOD_MATCH;
}
// make sure that arg can act as a buffer
if (arg != Py_None && arg->ob_type->tp_as_buffer == 0)
{
......@@ -1569,6 +1572,42 @@ int PyVTKCheckArg(
}
}
// string functions
else if (name[0] == 's' && strcmp(classname, "string") == 0)
{
// this makes "char *" preferable to "string"
penalty = VTK_PYTHON_GOOD_MATCH;
if (!PyString_Check(arg))
{
#ifdef PY_USING_UNICODE
penalty++;
if (!PyUnicode_Check(arg))
{
penalty = VTK_PYTHON_INCOMPATIBLE;
}
#else
penalty = VTK_PYTHON_INCOMPATIBLE;
#endif
}
}
#ifdef PY_USING_UNICODE
// unicode string functions
else if (name[0] == 'u' && strcmp(classname, "unicode") == 0)
{
if (!PyUnicode_Check(arg))
{
penalty = VTK_PYTHON_GOOD_MATCH;
penalty++;
if (!PyString_Check(arg))
{
penalty = VTK_PYTHON_INCOMPATIBLE;
}
penalty = VTK_PYTHON_INCOMPATIBLE;
}
}
#endif
// callback functions
else if (name[0] == 'f' && strcmp(classname, "func") == 0)
{
......
This diff is collapsed.
......@@ -49,8 +49,8 @@ Modify lex.yy.c:
"/*" { int c1 = 0, c2 = input();
for (;;)
{
if (c2 == 0)
{
if (c2 == 0)
{
fprintf(yyout,"Cannot find closing comment.\n");
break;
......@@ -60,11 +60,11 @@ Modify lex.yy.c:
}
};
^[\t ]*"//BTX".*$ {
^[\t ]*"//BTX".*$ {
int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = input();
for (;;)
{
if (c5 == 0)
{
if (c5 == 0)
{
fprintf(yyout,"Cannot find matching //ETX.\n");
break;
......@@ -77,14 +77,14 @@ Modify lex.yy.c:
if (c5 == 0) break;
if (c5 == '\n') break;
c5 = input();
}
}
};
^[\t ]*"// .NAME".* {
^[\t ]*"// .NAME".* {
int pos = 1;
while (yytext[pos-1] != 'M' || yytext[pos] != 'E')
{
pos++;
pos++;
}
yylval.str = (char *)malloc(yyleng + 1 - pos - 1);
memcpy(yylval.str,yytext+ pos + 1,yyleng - pos - 1);
......@@ -92,44 +92,44 @@ Modify lex.yy.c:
data.NameComment = vtkstrdup(yylval.str);
};
^[\t ]*"// .SECTION Description".* {
^[\t ]*"// .SECTION Description".* {
CommentState = 1;
data.Description = (char*)malloc(100000);
data.Description[0] = '\0';
};
^[\t ]*"// .SECTION See Also".* {
^[\t ]*"// .SECTION See Also".* {
CommentState = 3;
data.SeeAlso = (char*)malloc(100000);
data.SeeAlso[0] = '\0';
};
^[\t ]*"// .SECTION see also".* {
^[\t ]*"// .SECTION see also".* {
CommentState = 3;
data.SeeAlso = (char*)malloc(100000);
data.SeeAlso[0] = '\0';
};
^[\t ]*"// .SECTION Caveats".* {
^[\t ]*"// .SECTION Caveats".* {
CommentState = 4;
data.Caveats = (char*)malloc(100000);
data.Caveats[0] = '\0';
};
^[\t ]*"// Description:".* {
^[\t ]*"// Description:".* {
CommentState = 2;
HaveComment = 1;
CommentText[0] = '\0';
};
^[\t ]*[\r\n] {
HaveComment = 0;
CommentState = 0;
^[\t ]*[\r\n] {
HaveComment = 0;
CommentState = 0;
};
^[\t ]*"//".* {
^[\t ]*"//".* {
int pos = 1;
while (yytext[pos-1] != '/' || yytext[pos] != '/') pos++;
while (yytext[pos-1] != '/' || yytext[pos] != '/') pos++;
yylval.str = (char *)malloc(yyleng + 1 - pos - 1 + 1);
memcpy(yylval.str,yytext+ pos + 1,yyleng - pos - 1);
yylval.str[yyleng - pos - 1] = '\n';
......@@ -152,7 +152,7 @@ Modify lex.yy.c:
"["[\t\n\r\ ]*[1-9][0-9]*[\t\n\r ]*"]" {
sscanf(yytext+1,"%d",&yylval.integer); return(ARRAY_NUM);}
"struct "[\t\n\r ]*"vtk"[a-zA-Z0-9_]*[\t\n\r ]*";" return(CLASS_REF);
"class "[\t\n\r ]*"vtk"[a-zA-Z0-9_]*[\t\n\r ]*";" return(CLASS_REF);
"void"[\t\n\r ]*"("[\t\n\r ]*"*"[a-zA-Z0-9_]*[\t\n\r ]*")"[\t\n\r ]*"("[\t\n\r ]*"void"[\t\n\r ]*"*"[\n\t ]*")" return(VAR_FUNCTION);
......@@ -170,18 +170,18 @@ Modify lex.yy.c:
"signed"[\t\n\r ]*"char" return(SIGNED_CHAR);
"class" return(CLASS);
"public" return(PUBLIC);
"private" return(PRIVATE);
"protected" return(PROTECTED);
"int" return(INT);
"float" return(FLOAT);
"short" return(SHORT);
"long" return(LONG);
"double" return(DOUBLE);
"void" return(VOID);
"char" return(CHAR);
"bool" return(BOOL);
"class" return(CLASS);
"public" return(PUBLIC);
"private" return(PRIVATE);
"protected" return(PROTECTED);
"int" return(INT);
"float" return(FLOAT);
"short" return(SHORT);
"long" return(LONG);
"double" return(DOUBLE);
"void" return(VOID);
"char" return(CHAR);
"bool" return(BOOL);
"virtual" return(VIRTUAL);
"const" return(CONST);
"operator" return(OPERATOR);
......@@ -192,13 +192,13 @@ Modify lex.yy.c:
"vtkFloatingPointType" return(DOUBLE);
"vtkIdType" return(IdType);
"vtkStdString" return(StdString);
"vtkUnicodeString" return(UnicodeString);
"vtkSetMacro" return(SetMacro);
"vtkGetMacro" return(GetMacro);
"vtkSetStringMacro" return(SetStringMacro);
"vtkGetStringMacro" return(GetStringMacro);
"vtkSetClampMacro" return(SetClampMacro);
"vtkSetObjectMacro" return(SetObjectMacro);
"vtkSetReferenceCountedObjectMacro" return(SetReferenceCountedObjectMacro);
"vtkGetObjectMacro" return(GetObjectMacro);
"vtkBooleanMacro" return(BooleanMacro);
"vtkSetVector2Macro" return(SetVector2Macro);
......@@ -231,13 +231,13 @@ Modify lex.yy.c:
"vtkTypeFloat64" return(TypeFloat64);
[0-9]+ { sscanf(yytext,"%d",&yylval.integer); return(NUM);}
[0-9]+ { sscanf(yytext,"%d",&yylval.integer); return(NUM);}
\"[^\"]+\" {
yylval.str = vtkstrdup(yytext + 1);
yylval.str[strlen(yytext)-2] = '\0';
return(STRING);
}
\"[^\"]+\" {
yylval.str = vtkstrdup(yytext + 1);
yylval.str[strlen(yytext)-2] = '\0';
return(STRING);
}
^"#"[^\n]*$ ;
......@@ -257,12 +257,12 @@ Modify lex.yy.c:
[\t\n\r ] ;
"[" return(yytext[0]);
"]" return(yytext[0]);
"[" return(yytext[0]);
"]" return(yytext[0]);
[~{}():;*=,&.\-] { return(yytext[0]); }
[~{}():;*=,&.\-] { return(yytext[0]); }
. return(OTHER);
. return(OTHER);
%%
This diff is collapsed.
This diff is collapsed.
......@@ -35,7 +35,7 @@
* Mask for removing everything but the base type
*/
#define VTK_PARSE_BASE_TYPE 0x001F
#define VTK_PARSE_BASE_TYPE 0x00FF
/*
* Mask for checking signed/unsigned
......@@ -105,6 +105,8 @@
#define VTK_PARSE_UNSIGNED___INT64 0x1C
#define VTK_PARSE_SIGNED_CHAR 0x0D
#define VTK_PARSE_BOOL 0x0E
#define VTK_PARSE_STRING 0x21
#define VTK_PARSE_UNICODE_STRING 0x22
/*
* Basic pointer types
......@@ -131,6 +133,8 @@
#define VTK_PARSE_UNSIGNED___INT64_PTR 0x31C
#define VTK_PARSE_SIGNED_CHAR_PTR 0x30D
#define VTK_PARSE_BOOL_PTR 0x30E
#define VTK_PARSE_STRING_PTR 0x321
#define VTK_PARSE_UNICODE_STRING_PTR 0x322
/*
* Basic reference types
......@@ -157,5 +161,7 @@
#define VTK_PARSE_UNSIGNED___INT64_REF 0x11C
#define VTK_PARSE_SIGNED_CHAR_REF 0x10D
#define VTK_PARSE_BOOL_REF 0x10E
#define VTK_PARSE_STRING_REF 0x121
#define VTK_PARSE_UNICODE_STRING_REF 0x122
#endif
......@@ -239,6 +239,8 @@ static void vtkWrapPython_MakeTempVariable(
case VTK_PARSE___INT64: fprintf(fp,"__int64 "); break;
case VTK_PARSE_SIGNED_CHAR: fprintf(fp,"signed char "); break;
case VTK_PARSE_BOOL: fprintf(fp,"bool "); break;
case VTK_PARSE_STRING: fprintf(fp,"vtkStdString "); break;
case VTK_PARSE_UNICODE_STRING: fprintf(fp,"vtkUnicodeString "); break;
case VTK_PARSE_UNKNOWN: return;
}
......@@ -316,6 +318,16 @@ static void vtkWrapPython_MakeTempVariable(
" PyObject *tempB%d = 0;\n",
i);
}
/* ditto for string */
if ((i != MAX_ARGS) &&
(((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_UNICODE_STRING) ||
((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_STRING)))
{
fprintf(fp,
" PyObject *tempS%d = 0;\n",
i);
}
}
......@@ -588,6 +600,14 @@ static void vtkWrapPython_ReturnValue(
MAX_ARGS, MAX_ARGS, MAX_ARGS, MAX_ARGS, MAX_ARGS);
break;
}
/* return a string */
case VTK_PARSE_STRING:
{
fprintf(fp,
" result = PyString_FromString(temp%i);\n",
MAX_ARGS);
}
}
}
......@@ -743,6 +763,12 @@ static char *vtkWrapPython_FormatString(FunctionInfo *currentFunction)
case VTK_PARSE_BOOL:
result[currPos++] = 'O';
break;
case VTK_PARSE_STRING:
result[currPos++] = 'O';
break;
case VTK_PARSE_UNICODE_STRING:
result[currPos++] = 'O';
break;
}
}
......@@ -790,6 +816,18 @@ static char *vtkWrapPython_ArgCheckString(
currPos += 5;
}
if (argtype == VTK_PARSE_STRING)
{
strcpy(&result[currPos], " string");
currPos += 7;
}
if (argtype == VTK_PARSE_UNICODE_STRING)
{
strcpy(&result[currPos], " unicode");
currPos += 8;
}
if (argtype == VTK_PARSE_VTK_OBJECT_REF ||
argtype == VTK_PARSE_VTK_OBJECT_PTR ||
argtype == VTK_PARSE_VTK_OBJECT)
......@@ -955,6 +993,12 @@ static void vtkWrapPython_Signature(FunctionInfo *currentFunction)
case VTK_PARSE_BOOL:
vtkWrapPython_AddToSignature(result,"bool",&currPos);
break;
case VTK_PARSE_STRING:
vtkWrapPython_AddToSignature(result,"string",&currPos);
break;
case VTK_PARSE_UNICODE_STRING:
vtkWrapPython_AddToSignature(result,"unicode",&currPos);
break;
}
}
......@@ -1058,6 +1102,13 @@ static void vtkWrapPython_Signature(FunctionInfo *currentFunction)
case VTK_PARSE_BOOL:
vtkWrapPython_AddToSignature(result,"bool",&currPos);
break;
case VTK_PARSE_STRING:
vtkWrapPython_AddToSignature(result,"string",&currPos);
break;
case VTK_PARSE_UNICODE_STRING:
vtkWrapPython_AddToSignature(result,"unicode",&currPos);
break;
}
}
......@@ -1269,6 +1320,21 @@ void vtkWrapPython_RemovePreceededMethods(
{
if (!vote1) { vote2 = 1; }
}
/* a "char *" method preceeds a string method */
else if ((baseType1 == VTK_PARSE_CHAR) &&
(indirect1 == VTK_PARSE_POINTER) &&
(baseType2 == VTK_PARSE_STRING) &&
((indirect2 == VTK_PARSE_REF) || (indirect2 == 0)))
{
if (!vote2) { vote1 = 1; }
}
else if ((baseType2 == VTK_PARSE_CHAR) &&
(indirect2 == VTK_PARSE_POINTER) &&
(baseType1 == VTK_PARSE_STRING) &&
((indirect1 == VTK_PARSE_REF) || (indirect1 == 0)))
{
if (!vote1) { vote2 = 1; }
}
/* mismatch: both methods are allowed to live */
else if ((baseType1 != baseType2) ||
(unsigned1 != unsigned2) ||
......@@ -1525,6 +1591,11 @@ static void vtkWrapPython_GenerateMethods(
{
fprintf(fp,", &tempB%d",i);
}
else if ((argType == VTK_PARSE_STRING) ||
(argType == VTK_PARSE_UNICODE_STRING))
{
fprintf(fp,", &tempS%d",i);
}
else if (argType == VTK_PARSE_VOID_PTR)
{
fprintf(fp,", &temp%d, &size%d",i,i);
......@@ -1604,6 +1675,16 @@ static void vtkWrapPython_GenerateMethods(
" }\n",
i, i, on_error);
}
else if (argType == VTK_PARSE_STRING)
{
fprintf(fp,
" temp%d = PyString_AsString(tempS%d);\n"
" if (PyErr_Occurred())\n"
" {\n"
" %s;\n"
" }\n",
i, i, on_error);
}
}
/* make sure passed method is callable for VAR functions */
if (theSignature->NumberOfArguments == 1 &&
......@@ -2082,6 +2163,12 @@ static int vtkWrapPython_MethodCheck(
if (argType == VTK_PARSE_LONG_LONG) args_ok = 0;
if (argType == VTK_PARSE_UNSIGNED_LONG_LONG) args_ok = 0;
#endif
if (argType == VTK_PARSE_STRING_PTR) args_ok = 0;
if (argType == VTK_PARSE_UNICODE_STRING_PTR) args_ok = 0;
/* disable UNICODE for now */
if ((argType & VTK_PARSE_BASE_TYPE) ==
VTK_PARSE_UNICODE_STRING) args_ok = 0;
}
/* make sure we have all the info we need for array arguments */
......@@ -2117,14 +2204,21 @@ static int vtkWrapPython_MethodCheck(
/* eliminate types that aren't supported by the compiler */
#ifndef VTK_SIZEOF___INT64
if (returnType == VTK_PARSE___INT64) args_ok = 0;
if (returnType == VTK_PARSE_UNSIGNED___INT64) args_ok = 0;
if (returnType == VTK_PARSE___INT64) args_ok = 0;
if (returnType == VTK_PARSE_UNSIGNED___INT64) args_ok = 0;
#endif
#ifndef VTK_SIZEOF_LONG_LONG
if (returnType == VTK_PARSE_LONG_LONG) args_ok = 0;
if (returnType == VTK_PARSE_UNSIGNED_LONG_LONG) args_ok = 0;
if (returnType == VTK_PARSE_LONG_LONG) args_ok = 0;
if (returnType == VTK_PARSE_UNSIGNED_LONG_LONG) args_ok = 0;
#endif
if (returnType == VTK_PARSE_STRING_PTR) args_ok = 0;
if (returnType == VTK_PARSE_UNICODE_STRING_PTR) args_ok = 0;
/* disable UNICODE for now */
if ((returnType & VTK_PARSE_BASE_TYPE) ==
VTK_PARSE_UNICODE_STRING) args_ok = 0;
/* if we need a return type hint make sure we have one */
switch (returnType)
{
......
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