Numerous autodoc fixes for Python

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12735 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2011-06-10 19:33:40 +00:00
commit 3243cbaad4
7 changed files with 471 additions and 161 deletions

View file

@ -1122,9 +1122,9 @@ public:
initial++;
c++;
}
if (*c && !isspace(*c))
if (*c && !isspace(*c)) {
break;
else {
} else {
initial = 0;
}
}
@ -1267,35 +1267,29 @@ public:
}
/* -----------------------------------------------------------------------------
* makeParameterName()
* Note: the generated name should consist with that in kwnames[]
* addMissingParameterNames()
* For functions that have not had nameless parameters set in the Language class.
*
* Inputs:
* n - Node
* p - parameter node
* arg_num - parameter argument number
* Return:
* arg - a unique parameter name
* plist - entire parameter list
* arg_offset - argument number for first parameter
* Side effects:
* The "lname" attribute in each parameter in plist will be contain a parameter name
* ----------------------------------------------------------------------------- */
String *makeParameterName(ParmList *plist, Parm *p, int arg_num) {
String *arg = 0;
String *pn = Swig_name_make(p, 0, Getattr(p, "name"), 0, 0);
// Use C parameter name unless it is a duplicate or an empty parameter name
int count = 0;
if (SwigType_isvarargs(Getattr(p, "type"))) {
return NewString("*args");
void addMissingParameterNames(ParmList *plist, int arg_offset) {
Parm *p = plist;
int i = arg_offset;
while (p) {
if (!Getattr(p, "lname")) {
String *pname = Swig_cparm_name(p, i);
Delete(pname);
}
i++;
p = nextSibling(p);
}
while (plist) {
if ((Cmp(pn, Getattr(plist, "name")) == 0))
count++;
plist = nextSibling(plist);
}
arg = (!pn || !Len(pn) || (count > 1)) ? NewStringf("arg%d", arg_num) : Copy(pn);
return arg;
}
/* ------------------------------------------------------------
* make_autodocParmList()
* Generate the documentation for the function parameters
@ -1305,25 +1299,21 @@ public:
String *make_autodocParmList(Node *n, bool showTypes, bool calling = false, bool func_annotation = false) {
String *doc = NewString("");
String *pdocs = Copy(Getattr(n, "feature:pdocs"));
String *pdocs = 0;
ParmList *plist = CopyParmList(Getattr(n, "parms"));
Parm *p;
Parm *pnext;
Node *lookup;
int lines = 0;
int arg_num = 0;
const int maxwidth = 50;
int start_arg_num = is_wrapping_class() ? 1 : 0;
const int maxwidth = 80;
if (calling)
func_annotation = false;
if (pdocs)
Append(pdocs, "\n");
addMissingParameterNames(plist, start_arg_num); // for $1_name substitutions done in Swig_typemap_attach_parms
Swig_typemap_attach_parms("in", plist, 0);
Swig_typemap_attach_parms("doc", plist, 0);
@ -1354,18 +1344,15 @@ public:
value = Getattr(p, "tmap:doc:value");
}
// Note: the generated name should be consistent with that in kwnames[]
name = name ? name : Getattr(p, "name");
name = name ? name : Getattr(p, "lname");
type = type ? type : Getattr(p, "type");
value = value ? value : Getattr(p, "value");
name = makeParameterName(plist, p, arg_num);
// Reset it for convinient in further use. (mainly for makeParameterName())
// Since the plist is created by CopyParmList,
// we can hope that the set would have no side effect
Setattr(p, "name", name);
arg_num++;
if (SwigType_isvarargs(type))
break;
if (Len(doc)) {
// add a comma to the previous one if any
@ -1378,39 +1365,35 @@ public:
}
}
type = SwigType_base(type);
lookup = Swig_symbol_clookup(type, 0);
if (lookup)
type = Getattr(lookup, "sym:name");
// Do the param type too?
Node *nn = classLookup(Getattr(p, "type"));
String *type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0);
if (showTypes)
Printf(doc, "%s ", type);
Printf(doc, "%s ", type_str);
Append(doc, name);
if (pdoc) {
if (!pdocs)
pdocs = NewString("Parameters:\n");
Printf(pdocs, " %s\n", pdoc);
pdocs = NewString("\nParameters:\n");
Printf(pdocs, " %s\n", pdoc);
}
// Write the function annoation
// Write the function annotation
if (func_annotation)
Printf(doc, " : '%s'", type);
Printf(doc, " : '%s'", type_str);
// Write default value
if (value && !calling) {
String *pv = pyvalue(value, Getattr(p, "type"));
if (pv)
if (pv) {
value = pv;
else {
lookup = Swig_symbol_clookup(value, 0);
if (lookup) {
} else {
Node *lookup = Swig_symbol_clookup(value, 0);
if (lookup)
value = Getattr(lookup, "sym:name");
}
}
Printf(doc, " = %s", value);
Printf(doc, "=%s", value);
}
Delete(type_str);
}
if (pdocs)
Setattr(n, "feature:pdocs", pdocs);
@ -1430,7 +1413,7 @@ public:
String *make_autodoc(Node *n, autodoc_t ad_type) {
int extended = 0;
// If the function is overloaded then this funciton is called
// If the function is overloaded then this function is called
// for the last one. Rewind to the first so the docstrings are
// in order.
while (Getattr(n, "sym:previousSibling"))
@ -1468,15 +1451,14 @@ public:
if (!skipAuto) {
String *symname = Getattr(n, "sym:name");
SwigType *type = Getattr(n, "type");
String *type_str = NULL;
if (type) {
if (Strcmp(type, "void") == 0)
type = NULL;
else {
type = SwigType_base(type);
Node *lookup = Swig_symbol_clookup(type, 0);
if (lookup)
type = Getattr(lookup, "sym:name");
if (Strcmp(type, "void") == 0) {
type_str = NULL;
} else {
Node *nn = classLookup(type);
type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0);
}
}
@ -1497,40 +1479,50 @@ public:
case AUTODOC_CTOR:
if (Strcmp(class_name, symname) == 0) {
String *paramList = make_autodocParmList(n, showTypes);
Printf(doc, "__init__(");
if (showTypes)
Printf(doc, "%s ", getClassName());
if (Len(paramList))
Printf(doc, "__init__(self, %s) -> %s", paramList, class_name);
Printf(doc, "self, %s) -> %s", paramList, class_name);
else
Printf(doc, "__init__(self) -> %s", class_name);
Printf(doc, "self) -> %s", class_name);
} else
Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name);
break;
case AUTODOC_DTOR:
Append(doc, "__del__(self)");
if (showTypes)
Printf(doc, "__del__(%s self)", getClassName());
else
Printf(doc, "__del__(self)");
break;
case AUTODOC_STATICFUNC:
Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
if (type)
Printf(doc, " -> %s", type);
if (type_str)
Printf(doc, " -> %s", type_str);
break;
case AUTODOC_FUNC:
Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
if (type)
Printf(doc, " -> %s", type);
if (type_str)
Printf(doc, " -> %s", type_str);
break;
case AUTODOC_METHOD:
String *paramList = make_autodocParmList(n, showTypes);
Printf(doc, "%s(", symname);
if (showTypes)
Printf(doc, "%s ", class_name);
if (Len(paramList))
Printf(doc, "%s(self, %s)", symname, paramList);
Printf(doc, "self, %s)", paramList);
else
Printf(doc, "%s(self)", symname);
if (type)
Printf(doc, " -> %s", type);
Printf(doc, "self)");
if (type_str)
Printf(doc, " -> %s", type_str);
break;
}
Delete(type_str);
}
if (extended) {
String *pdocs = Getattr(n, "feature:pdocs");
@ -1935,7 +1927,7 @@ public:
/* Create a shadow for this function (if enabled and not in a member function) */
if (!builtin && (shadow) && (!(shadow & PYSHADOW_MEMBER))) {
emitFunctionShadowHelper(n, f_shadow_stubs, symname, 0);
emitFunctionShadowHelper(n, in_class ? f_shadow_stubs : f_shadow, symname, 0);
}
DelWrapper(f);
Delete(dispatch);
@ -3914,7 +3906,7 @@ public:
// Can't use checkAttribute(n, "access", "public") because
// "access" attr isn't set on %extend methods
if (!checkAttribute(n, "access", "private") && strncmp(Char(symname), "operator ", 9) && !Getattr(class_members, symname)) {
String *fullname = Swig_name_member(NULL, class_name, symname);
String *fullname = Swig_name_member(NSPACE_TODO, class_name, symname);
String *wname = Swig_name_wrapper(fullname);
Setattr(class_members, symname, n);
int argcount = Getattr(n, "python:argcount") ? atoi(Char(Getattr(n, "python:argcount"))) : 2;
@ -3940,12 +3932,13 @@ public:
if (!Getattr(n, "sym:nextSibling")) {
if (shadow && !builtin) {
int fproxy = fastproxy;
String *fullname = Swig_name_member(NSPACE_TODO, class_name, symname);
if (Strcmp(symname, "__repr__") == 0) {
have_repr = 1;
}
if (Getattr(n, "feature:shadow")) {
String *pycode = pythoncode(Getattr(n, "feature:shadow"), tab4);
String *pyaction = NewStringf("%s.%s", module, Swig_name_member(NSPACE_TODO, class_name, symname));
String *pyaction = NewStringf("%s.%s", module, fullname);
Replaceall(pycode, "$action", pyaction);
Delete(pyaction);
Printv(f_shadow, pycode, "\n", NIL);
@ -3958,7 +3951,7 @@ public:
if (!have_addtofunc(n)) {
if (!fastproxy || olddefs) {
Printv(f_shadow, tab4, "def ", symname, "(", parms, ")", returnTypeAnnotation(n), ":", NIL);
Printv(f_shadow, " return ", funcCall(Swig_name_member(NSPACE_TODO, class_name, symname), callParms), "\n", NIL);
Printv(f_shadow, " return ", funcCall(fullname, callParms), "\n", NIL);
}
} else {
Printv(f_shadow, tab4, "def ", symname, "(", parms, ")", returnTypeAnnotation(n), ":", NIL);
@ -3971,11 +3964,11 @@ public:
}
if (have_pythonappend(n)) {
fproxy = 0;
Printv(f_shadow, tab8, "val = ", funcCall(Swig_name_member(NSPACE_TODO, class_name, symname), callParms), "\n", NIL);
Printv(f_shadow, tab8, "val = ", funcCall(fullname, callParms), "\n", NIL);
Printv(f_shadow, pythoncode(pythonappend(n), tab8), "\n", NIL);
Printv(f_shadow, tab8, "return val\n\n", NIL);
} else {
Printv(f_shadow, tab8, "return ", funcCall(Swig_name_member(NSPACE_TODO, class_name, symname), callParms), "\n\n", NIL);
Printv(f_shadow, tab8, "return ", funcCall(fullname, callParms), "\n\n", NIL);
}
}
}
@ -3988,6 +3981,7 @@ public:
}
Append(shadow_list, symname);
}
Delete(fullname);
}
}
return SWIG_OK;
@ -4012,7 +4006,7 @@ public:
if (builtin && in_class) {
if ((GetFlagAttr(n, "feature:extend") || checkAttribute(n, "access", "public"))
&& !Getattr(class_members, symname)) {
String *fullname = Swig_name_member(NULL, class_name, symname);
String *fullname = Swig_name_member(NSPACE_TODO, class_name, symname);
String *wname = Swig_name_wrapper(fullname);
Setattr(class_members, symname, n);
int funpack = modernargs && fastunpack && !Getattr(n, "sym:overloaded");