Modified the WrapperFunction class. Fixed various bugs

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@555 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-07-10 16:46:18 +00:00
commit 64e2755215
18 changed files with 524 additions and 492 deletions

View file

@ -496,7 +496,7 @@ void PYTHON::close_cmodule(void)
// ----------------------------------------------------------------------
// PYTHON::get_pointer(char *iname, char *srcname, char *src, char *target,
// DataType *t, WrapperFunction &f, char *ret)
// DataType *t, DOHString *f, char *ret)
//
// Emits code to get a pointer and do type checking.
// iname = name of the function/method (used for error messages)
@ -537,20 +537,20 @@ PYTHON::get_pointer(char *iname, char *srcname, char *src, char *dest,
//
// Return the code to be used as a function header
// ----------------------------------------------------------------------
void PYTHON::emit_function_header(WrapperFunction &emit_to, char *wname)
void PYTHON::emit_function_header(Wrapper *emit_to, char *wname)
{
if (!use_kw) {
Printv(emit_to.def,
Printv(emit_to->def,
"static PyObject *", wname,
"(PyObject *self, PyObject *args) {",
0);
} else {
Printv(emit_to.def,
Printv(emit_to->def,
"static PyObject *", wname,
"(PyObject *self, PyObject *args, PyObject *kwargs) {",
0);
}
Printf(emit_to.code," self = self;\n");
Printf(emit_to->code," self = self;\n");
}
// ----------------------------------------------------------------------
@ -562,7 +562,7 @@ void PYTHON::emit_function_header(WrapperFunction &emit_to, char *wname)
//
// Returns the name of the variable to use as the self pointer
// ----------------------------------------------------------------------
char *PYTHON::convert_self(WrapperFunction &)
char *PYTHON::convert_self(Wrapper *f)
{
// Default behaviour is no translation
return (char*)"";
@ -619,7 +619,7 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
char wname[256], self_name[256], call_name[256];
char source[64], target[64], temp[256], argnum[20];
char *usage = 0;
WrapperFunction f;
Wrapper *f;
DOHString *parse_args;
DOHString *arglist;
DOHString *get_pointers;
@ -631,6 +631,7 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
char *tm;
int numopt = 0;
f = NewWrapper();
parse_args = NewString("");
arglist = NewString("");
get_pointers = NewString("");
@ -652,7 +653,7 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
emit_function_header(f, wname);
f.add_local((char*)"PyObject *",(char*)"_resultobj");
Wrapper_add_local(f,(char*)"PyObject *",(char*)"_resultobj",0);
// Get the function usage string for later use
@ -709,9 +710,9 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
// Look for input typemap
if ((tm = typemap_lookup((char*)"in",(char*)"python",p->t,p->name,source,target,&f))) {
if ((tm = typemap_lookup((char*)"in",(char*)"python",p->t,p->name,source,target,f))) {
Putc('O',parse_args);
f.add_local((char*)"PyObject *",source,(char*)"0");
Wrapper_add_local(f, (char*)"PyObject *",source,(char*)"0");
Printf(arglist,"&%s",source);
if (i >= (pcount-numopt))
Printv(get_pointers, tab4, "if (", source, ")\n",
@ -764,9 +765,9 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
sprintf(tempb,"tempbool%d",i);
Putc('i',parse_args);
if (!p->defvalue)
f.add_local((char*)"int",tempb);
Wrapper_add_local(f,(char*)"int",tempb,0);
else
f.add_local((char*)"int",tempb,tempval);
Wrapper_add_local(f,(char*)"int",tempb,tempval);
Printv(get_pointers, tab4, target, " = ", p->t->print_cast(), " ", tempb, ";\n", 0);
Printf(arglist,"&%s",tempb);
}
@ -812,7 +813,7 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
sprintf(target,"_arg%d", i);
sprintf(temp,"argument %d",i+1);
f.add_local((char*)"PyObject *", source,(char*)"0");
Wrapper_add_local(f,(char*)"PyObject *", source,(char*)"0");
Printf(arglist,"&%s",source);
get_pointer(iname, temp, source, target, p->t, get_pointers, (char*)"NULL");
}
@ -843,7 +844,7 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
Printf(kwargs," NULL }");
if (use_kw) {
Printv(f.locals,tab4, "char *_kwnames[] = ", kwargs, ";\n", 0);
Printv(f->locals,tab4, "char *_kwnames[] = ", kwargs, ";\n", 0);
}
Printf(parse_args,":%s\"", iname);
@ -855,7 +856,7 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
strcpy(self_name,convert_self(f));
/* Now slap the whole first part of the wrapper function together */
Printv(f.code, parse_args, get_pointers, check, 0);
Printv(f->code, parse_args, get_pointers, check, 0);
// This function emits code to call the real function. Assuming you read
// the parameters in correctly, this will work.
@ -869,7 +870,7 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
if ((tm = typemap_lookup((char*)"out",(char*)"python",d,iname,(char*)"_result",(char*)"_resultobj"))) {
// Yep. Use it instead of the default
Printf(f.code,"%s\n", tm);
Printf(f->code,"%s\n", tm);
} else {
if ((d->type != T_VOID) || (d->is_pointer)) {
@ -884,38 +885,38 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
// Return an integer type
case T_INT: case T_SINT: case T_UINT: case T_BOOL:
Printf(f.code," _resultobj = Py_BuildValue(\"i\",_result);\n");
Printf(f->code," _resultobj = Py_BuildValue(\"i\",_result);\n");
break;
case T_SHORT: case T_SSHORT: case T_USHORT:
Printf(f.code," _resultobj = Py_BuildValue(\"h\",_result);\n");
Printf(f->code," _resultobj = Py_BuildValue(\"h\",_result);\n");
break;
case T_LONG : case T_SLONG : case T_ULONG:
Printf(f.code," _resultobj = Py_BuildValue(\"l\",_result);\n");
Printf(f->code," _resultobj = Py_BuildValue(\"l\",_result);\n");
break;
case T_SCHAR: case T_UCHAR :
Printf(f.code," _resultobj = Py_BuildValue(\"b\",_result);\n");
Printf(f->code," _resultobj = Py_BuildValue(\"b\",_result);\n");
break;
// Return a floating point value
case T_DOUBLE :
Printf(f.code," _resultobj = Py_BuildValue(\"d\",_result);\n");
Printf(f->code," _resultobj = Py_BuildValue(\"d\",_result);\n");
break;
case T_FLOAT :
Printf(f.code," _resultobj = Py_BuildValue(\"f\",_result);\n");
Printf(f->code," _resultobj = Py_BuildValue(\"f\",_result);\n");
break;
// Return a single ASCII value. Usually we need to convert
// it to a NULL-terminate string and return that instead.
case T_CHAR :
Printf(f.code," _resultobj = Py_BuildValue(\"c\",_result);\n");
Printf(f->code," _resultobj = Py_BuildValue(\"c\",_result);\n");
break;
case T_USER :
d->is_pointer++;
d->remember();
Printv(f.code,tab4, "_resultobj = SWIG_NewPointerObj((void *)_result, SWIGTYPE", d->print_mangle(), ");\n",0);
Printv(f->code,tab4, "_resultobj = SWIG_NewPointerObj((void *)_result, SWIGTYPE", d->print_mangle(), ");\n",0);
d->is_pointer--;
break;
default :
@ -931,36 +932,36 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
if ((d->type == T_CHAR) && (d->is_pointer == 1)) {
// Return a character string
Printf(f.code," _resultobj = Py_BuildValue(\"s\", _result);\n");
Printf(f->code," _resultobj = Py_BuildValue(\"s\", _result);\n");
// If declared as a new object, free the result
} else {
// Build a SWIG pointer.
d->remember();
Printv(f.code, tab4, "_resultobj = SWIG_NewPointerObj((void *) _result, SWIGTYPE", d->print_mangle(), ");\n", 0);
Printv(f->code, tab4, "_resultobj = SWIG_NewPointerObj((void *) _result, SWIGTYPE", d->print_mangle(), ");\n", 0);
}
}
} else {
Printf(f.code," Py_INCREF(Py_None);\n");
Printf(f.code," _resultobj = Py_None;\n");
Printf(f->code," Py_INCREF(Py_None);\n");
Printf(f->code," _resultobj = Py_None;\n");
}
}
// Check to see if there were any output arguments, if so we're going to
// create a Python list object out of the current result
Printv(f.code,outarg,0);
Printv(f->code,outarg,0);
// If there was any other cleanup needed, do that
Printv(f.code,cleanup,0);
Printv(f->code,cleanup,0);
// Look to see if there is any newfree cleanup code
if (NewObject) {
if ((tm = typemap_lookup((char*)"newfree",(char*)"python",d,iname,(char*)"_result",(char*)""))) {
Printf(f.code,"%s\n",tm);
Printf(f->code,"%s\n",tm);
}
}
@ -968,19 +969,19 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
if ((tm = typemap_lookup((char*)"ret",(char*)"python",d,iname,(char*)"_result",(char*)""))) {
// Yep. Use it instead of the default
Printf(f.code,"%s\n",tm);
Printf(f->code,"%s\n",tm);
}
Printf(f.code," return _resultobj;\n}\n");
Printf(f->code," return _resultobj;\n}\n");
// Substitute the cleanup code
Replace(f.code,"$cleanup",cleanup, DOH_REPLACE_ANY);
Replace(f->code,"$cleanup",cleanup, DOH_REPLACE_ANY);
// Substitute the function name
Replace(f.code,"$name",iname, DOH_REPLACE_ANY);
Replace(f->code,"$name",iname, DOH_REPLACE_ANY);
// Dump the function out
f.print(f_wrappers);
Wrapper_print(f,f_wrappers);
// Now register the function with the interpreter.
@ -1049,6 +1050,7 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l)
Delete(outarg);
Delete(check);
Delete(kwargs);
DelWrapper(f);
}
// -----------------------------------------------------------------------
@ -1070,7 +1072,10 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
static int have_globals = 0;
char *tm;
WrapperFunction getf, setf;
Wrapper *getf, *setf;
getf = NewWrapper();
setf = NewWrapper();
// If this is our first call, add the globals variable to the
// Python dictionary.
@ -1091,11 +1096,11 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
// Create a function for setting the value of the variable
// ---------------------------------------------------------------------
Printf(setf.def,"static int %s_set(PyObject *val) {", wname);
Printf(setf->def,"static int %s_set(PyObject *val) {", wname);
if (!(Status & STAT_READONLY)) {
if ((tm = typemap_lookup((char*)"varin",(char*)"python",t,name,(char*)"val",name))) {
Printf(setf.code,"%s\n",tm);
Replace(setf.code,"$name",iname, DOH_REPLACE_ANY);
Printf(setf->code,"%s\n",tm);
Replace(setf->code,"$name",iname, DOH_REPLACE_ANY);
} else {
if ((t->type != T_VOID) || (t->is_pointer)) {
if (!t->is_pointer) {
@ -1108,8 +1113,8 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
case T_SINT: case T_SSHORT: case T_SLONG:
case T_SCHAR: case T_UCHAR: case T_BOOL:
// Get an integer value
setf.add_local(t->print_type(), (char*)"tval");
Printv(setf.code,
Wrapper_add_local(setf,t->print_type(), (char*)"tval",0);
Printv(setf->code,
tab4, "tval = ", t->print_cast(), "PyInt_AsLong(val);\n",
tab4, "if (PyErr_Occurred()) {\n",
tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '",
@ -1122,8 +1127,8 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
case T_FLOAT: case T_DOUBLE:
// Get a floating point value
setf.add_local(t->print_type(), (char*)"tval");
Printv(setf.code,
Wrapper_add_local(setf,t->print_type(), (char*)"tval",0);
Printv(setf->code,
tab4, "tval = ", t->print_cast(), "PyFloat_AsDouble(val);\n",
tab4, "if (PyErr_Occurred()) {\n",
tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '",
@ -1137,8 +1142,8 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
// A single ascii character
case T_CHAR:
setf.add_local((char*)"char *", (char*)"tval");
Printv(setf.code,
Wrapper_add_local(setf,(char*)"char *", (char*)"tval",0);
Printv(setf->code,
tab4, "tval = (char *) PyString_AsString(val);\n",
tab4, "if (PyErr_Occurred()) {\n",
tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '",
@ -1150,9 +1155,9 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
break;
case T_USER:
t->is_pointer++;
setf.add_local(t->print_type(),(char*)"temp");
get_pointer(iname,(char*)"value",(char*)"val",(char*)"temp",t,setf.code,(char*)"1");
Printv(setf.code, tab4, name, " = *temp;\n", 0);
Wrapper_add_local(setf,t->print_type(),(char*)"temp",0);
get_pointer(iname,(char*)"value",(char*)"val",(char*)"temp",t,setf->code,(char*)"1");
Printv(setf->code, tab4, name, " = *temp;\n", 0);
t->is_pointer--;
break;
default:
@ -1163,8 +1168,8 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
// Parse a pointer value
if ((t->type == T_CHAR) && (t->is_pointer == 1)) {
setf.add_local((char*)"char *", (char*)"tval");
Printv(setf.code,
Wrapper_add_local(setf,(char*)"char *", (char*)"tval",0);
Printv(setf->code,
tab4, "tval = (char *) PyString_AsString(val);\n",
tab4, "if (PyErr_Occurred()) {\n",
tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '",
@ -1174,13 +1179,13 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
0);
if (CPlusPlus) {
Printv(setf.code,
Printv(setf->code,
tab4, "if (", name, ") delete [] ", name, ";\n",
tab4, name, " = new char[strlen(tval)+1];\n",
tab4, "strcpy((char *)", name, ",tval);\n",
0);
} else {
Printv(setf.code,
Printv(setf->code,
tab4, "if (", name, ") free(", name, ");\n",
tab4, name, " = (char *) malloc(strlen(tval)+1);\n",
tab4, "strcpy((char *)", name, ",tval);\n",
@ -1190,41 +1195,41 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
// Is a generic pointer value.
setf.add_local(t->print_type(),(char*)"temp");
get_pointer(iname,(char*)"value",(char*)"val",(char*)"temp",t,setf.code,(char*)"1");
Printv(setf.code,tab4, name, " = temp;\n", 0);
Wrapper_add_local(setf,t->print_type(),(char*)"temp",0);
get_pointer(iname,(char*)"value",(char*)"val",(char*)"temp",t,setf->code,(char*)"1");
Printv(setf->code,tab4, name, " = temp;\n", 0);
}
}
}
}
Printf(setf.code," return 0;\n");
Printf(setf->code," return 0;\n");
} else {
// Is a readonly variable. Issue an error
Printv(setf.code,
Printv(setf->code,
tab4, "PyErr_SetString(PyExc_TypeError,\"Variable ", iname,
" is read-only.\");\n",
tab4, "return 1;\n",
0);
}
Printf(setf.code,"}\n");
Printf(setf->code,"}\n");
// Dump out function for setting value
setf.print(f_wrappers);
Wrapper_print(setf,f_wrappers);
// ----------------------------------------------------------------
// Create a function for getting the value of a variable
// ----------------------------------------------------------------
Printf(getf.def,"static PyObject *%s_get() {", wname);
getf.add_local((char*)"PyObject *",(char*)"pyobj");
Printf(getf->def,"static PyObject *%s_get() {", wname);
Wrapper_add_local(getf,(char*)"PyObject *",(char*)"pyobj",0);
if ((tm = typemap_lookup((char*)"varout",(char*)"python",t,name,name,(char*)"pyobj"))) {
Printf(getf.code,"%s\n",tm);
Replace(getf.code,"$name",iname, DOH_REPLACE_ANY);
Printf(getf->code,"%s\n",tm);
Replace(getf->code,"$name",iname, DOH_REPLACE_ANY);
} else if ((tm = typemap_lookup((char*)"out",(char*)"python",t,name,name,(char*)"pyobj"))) {
Printf(getf.code,"%s\n",tm);
Replace(getf.code,"$name",iname, DOH_REPLACE_ANY);
Printf(getf->code,"%s\n",tm);
Replace(getf->code,"$name",iname, DOH_REPLACE_ANY);
} else {
if ((t->type != T_VOID) || (t->is_pointer)) {
if (!t->is_pointer) {
@ -1235,14 +1240,14 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
case T_SHORT: case T_SSHORT: case T_USHORT:
case T_LONG: case T_SLONG: case T_ULONG:
case T_SCHAR: case T_UCHAR: case T_BOOL:
Printv(getf.code, tab4, "pyobj = PyInt_FromLong((long) ", name, ");\n", 0);
Printv(getf->code, tab4, "pyobj = PyInt_FromLong((long) ", name, ");\n", 0);
break;
case T_FLOAT: case T_DOUBLE:
Printv(getf.code, tab4, "pyobj = PyFloat_FromDouble((double) ", name, ");\n", 0);
Printv(getf->code, tab4, "pyobj = PyFloat_FromDouble((double) ", name, ");\n", 0);
break;
case T_CHAR:
getf.add_local((char*)"char",(char*)"ptemp[2]");
Printv(getf.code,
Wrapper_add_local(getf,(char*)"char",(char*)"ptemp[2]",0);
Printv(getf->code,
tab4, "ptemp[0] = ", name, ";\n",
tab4, "ptemp[1] = 0;\n",
tab4, "pyobj = PyString_FromString(ptemp);\n",
@ -1252,7 +1257,7 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
// Hack this into a pointer
t->is_pointer++;
t->remember();
Printv(getf.code,
Printv(getf->code,
tab4, "pyobj = SWIG_NewPointerObj((void *) &", name ,
", SWIGTYPE", t->print_mangle(), ");\n",
0);
@ -1266,14 +1271,14 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
// Is some sort of pointer value
if ((t->type == T_CHAR) && (t->is_pointer == 1)) {
Printv(getf.code,
Printv(getf->code,
tab4, "if (", name, ")\n",
tab8, "pyobj = PyString_FromString(", name, ");\n",
tab4, "else pyobj = PyString_FromString(\"(NULL)\");\n",
0);
} else {
t->remember();
Printv(getf.code,
Printv(getf->code,
tab4, "pyobj = SWIG_NewPointerObj((void *)", name,
", SWIGTYPE", t->print_mangle(), ");\n",
0);
@ -1282,9 +1287,9 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
}
}
Printf(getf.code," return pyobj;\n}\n");
Printf(getf->code," return pyobj;\n}\n");
getf.print(f_wrappers);
Wrapper_print(getf,f_wrappers);
// Now add this to the variable linking mechanism
@ -1301,6 +1306,8 @@ void PYTHON::link_variable(char *name, char *iname, DataType *t) {
0);
}
}
DelWrapper(setf);
DelWrapper(getf);
}
// -----------------------------------------------------------------------