| 1 | Index: Source/Modules/python.cxx |
| 2 | =================================================================== |
| 3 | RCS file: /cvsroot/SWIG/Source/Modules/python.cxx,v |
| 4 | retrieving revision 1.28 |
| 5 | diff -u -r1.28 python.cxx |
| 6 | --- Source/Modules/python.cxx 18 Nov 2003 20:19:15 -0000 1.28 |
| 7 | +++ Source/Modules/python.cxx 21 Nov 2003 16:56:31 -0000 |
| 8 | @@ -68,6 +68,17 @@ |
| 9 | -noexcept - No automatic exception handling\n\ |
| 10 | -noproxy - Don't generate proxy classes \n\n"; |
| 11 | |
| 12 | + |
| 13 | +/* flags for the make_autodoc function */ |
| 14 | +enum autodoc_t { |
| 15 | + AUTODOC_CLASS, |
| 16 | + AUTODOC_CTOR, |
| 17 | + AUTODOC_DTOR, |
| 18 | + AUTODOC_STATICFUNC, |
| 19 | + AUTODOC_FUNC |
| 20 | +}; |
| 21 | + |
| 22 | + |
| 23 | class PYTHON : public Language { |
| 24 | public: |
| 25 | |
| 26 | @@ -415,15 +426,21 @@ |
| 27 | * ------------------------------------------------------------ */ |
| 28 | |
| 29 | void emitFunctionShadowHelper(Node *n, File *f_dest, String *name, int kw) { |
| 30 | - if ( ! have_addtofunc(n) ) { |
| 31 | + if ( ! have_addtofunc(n) && ! have_docstring(n) ) { |
| 32 | /* If there is no addtofunc directive then just assign from the extension module */ |
| 33 | Printv(f_dest, "\n", name, " = ", module, ".", name, "\n", NIL); |
| 34 | } else { |
| 35 | /* Otherwise make a wrapper function to insert the code into */ |
| 36 | Printv(f_dest, "\ndef ", name, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL); |
| 37 | - Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL); |
| 38 | - Printv(f_dest, tab4, addtofunc(n), "\n", NIL); |
| 39 | - Printv(f_dest, tab4, "return val\n", NIL); |
| 40 | + if ( have_docstring(n) ) |
| 41 | + Printv(f_dest, tab4, docstring(n, AUTODOC_FUNC), "\n", NIL); |
| 42 | + if ( have_addtofunc(n) ) { |
| 43 | + Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL); |
| 44 | + Printv(f_dest, tab4, addtofunc(n), "\n", NIL); |
| 45 | + Printv(f_dest, tab4, "return val\n", NIL); |
| 46 | + } else { |
| 47 | + Printv(f_dest, tab4, "return ", funcCallHelper(name, kw), "\n", NIL); |
| 48 | + } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | @@ -439,6 +456,200 @@ |
| 53 | |
| 54 | |
| 55 | /* ------------------------------------------------------------ |
| 56 | + * have_docstring() |
| 57 | + * Check if there is a docstring directive and it has text, |
| 58 | + * or there is an autodoc flag set |
| 59 | + * ------------------------------------------------------------ */ |
| 60 | + |
| 61 | + bool have_docstring(Node *n) { |
| 62 | + String* str = Getattr(n, "feature:docstring"); |
| 63 | + return (str != NULL && Len(str) > 0) || |
| 64 | + (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc")); |
| 65 | + } |
| 66 | + |
| 67 | + /* ------------------------------------------------------------ |
| 68 | + * docstring() |
| 69 | + * Get the docstring text, stripping off {} if neccessary, |
| 70 | + * and enclose in triple-double quotes. If autodoc is also |
| 71 | + * set then it will build a combined docstring. |
| 72 | + * ------------------------------------------------------------ */ |
| 73 | + |
| 74 | + String *docstring(Node *n, autodoc_t ad_type) { |
| 75 | + String* str = Getattr(n, "feature:docstring"); |
| 76 | + bool have_ds = (str != NULL && Len(str) > 0); |
| 77 | + bool have_auto = (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc")); |
| 78 | + char* triple_double = "\"\"\""; |
| 79 | + String* autodoc; |
| 80 | + String* doc; |
| 81 | + |
| 82 | + if ( have_ds ) { |
| 83 | + char* t = Char(str); |
| 84 | + if (*t == '{') { |
| 85 | + Delitem(str ,0); |
| 86 | + Delitem(str,DOH_END); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if ( have_auto ) { |
| 91 | + autodoc = make_autodoc(n, ad_type); |
| 92 | + have_auto = (autodoc != NULL && Len(autodoc) > 0); |
| 93 | + } |
| 94 | + |
| 95 | + if ( have_auto && have_ds ) |
| 96 | + doc = NewStringf("%s%s\n\n%s%s", triple_double, autodoc, str, triple_double); |
| 97 | + else if ( !have_auto && have_ds ) |
| 98 | + doc = NewStringf("%s%s%s", triple_double, str, triple_double); |
| 99 | + else |
| 100 | + doc = NewStringf("%s%s%s", triple_double, autodoc, triple_double); |
| 101 | + |
| 102 | + Setattr(n, "python:docstring", doc); |
| 103 | + Setattr(n, "python:autodoc", autodoc); |
| 104 | + return doc; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /* ------------------------------------------------------------ |
| 109 | + * make_autodoc() |
| 110 | + * Build a docstring for the node, using parameter and other |
| 111 | + * info in the parse tree. If the value of the autodoc |
| 112 | + * attribute is "0" then do not include parameter types, if |
| 113 | + * it is "1" (the default) then do. If it has some other |
| 114 | + * value then assume it is supplied by the extension writer |
| 115 | + * and use it directly. |
| 116 | + * ------------------------------------------------------------ */ |
| 117 | + |
| 118 | + String* make_autodoc(Node *n, autodoc_t ad_type) { |
| 119 | + |
| 120 | + if (ad_type == AUTODOC_CLASS) |
| 121 | + return NULL; // No function call to document in this case |
| 122 | + |
| 123 | + // If the function is overloaded then this funciton is called |
| 124 | + // for the last one. Rewind to the first so the docstrings are |
| 125 | + // in order. |
| 126 | + while ( Getattr(n, "sym:previousSibling") ) |
| 127 | + n = Getattr(n, "sym:previousSibling"); |
| 128 | + |
| 129 | + String* doc = NewString(""); |
| 130 | + while (n) { |
| 131 | + bool showTypes = false; |
| 132 | + bool skipAuto = false; |
| 133 | + |
| 134 | + // check how should the parameters be rendered? |
| 135 | + String* autodoc = Getattr(n, "feature:autodoc"); |
| 136 | + if (Strcmp(autodoc, "0") == 0) |
| 137 | + showTypes = false; |
| 138 | + else if (Strcmp(autodoc, "1") == 0) |
| 139 | + showTypes = true; |
| 140 | + else { |
| 141 | + // if not "0" or "1" then autodoc is already the string that should be used |
| 142 | + Printf(doc, "%s", autodoc); |
| 143 | + skipAuto = true; |
| 144 | + } |
| 145 | + |
| 146 | + if (!skipAuto) { |
| 147 | + String* symname = Getattr(n, "sym:name"); |
| 148 | + SwigType* type = Getattr(n, "type"); |
| 149 | + |
| 150 | + if (type) { |
| 151 | + if (Strcmp(type, "void") == 0) |
| 152 | + type = NULL; |
| 153 | + else { |
| 154 | + type = SwigType_base(type); |
| 155 | + Node* lookup = Swig_symbol_clookup(type, 0); |
| 156 | + if (lookup) |
| 157 | + type = Getattr(lookup, "sym:name"); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + switch ( ad_type ) { |
| 162 | + case AUTODOC_CTOR: |
| 163 | + if ( Strcmp(class_name, symname) == 0) |
| 164 | + Printf(doc, "__init__(%s) -> %s", make_autodocParmList(n, showTypes), class_name); |
| 165 | + else |
| 166 | + Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name); |
| 167 | + break; |
| 168 | + |
| 169 | + case AUTODOC_DTOR: |
| 170 | + Printf(doc, "__del__()"); |
| 171 | + break; |
| 172 | + |
| 173 | + case AUTODOC_STATICFUNC: |
| 174 | + Printf(doc, "%s.%s(%s)", class_name, symname, make_autodocParmList(n, showTypes)); |
| 175 | + if (type) Printf(doc, " -> %s", type); |
| 176 | + break; |
| 177 | + |
| 178 | + case AUTODOC_FUNC: |
| 179 | + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes)); |
| 180 | + if (type) Printf(doc, " -> %s", type); |
| 181 | + break; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // if it's overloaded then get the next decl and loop around again |
| 186 | + n = Getattr(n, "sym:nextSibling"); |
| 187 | + if (n) |
| 188 | + Printf(doc, "\n"); |
| 189 | + } |
| 190 | + |
| 191 | + return doc; |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | + String* make_autodocParmList(Node* n, bool showTypes) { |
| 196 | + String* doc = NewString(""); |
| 197 | + ParmList* plist = Getattr(n,"parms"); |
| 198 | + Parm* p; |
| 199 | + Node* lookup; |
| 200 | + int lines = 0; |
| 201 | + const int maxwidth = 50; |
| 202 | + |
| 203 | + |
| 204 | + for (p = plist; p; p = nextSibling(p)) { |
| 205 | + String* name = Getattr(p, "name"); |
| 206 | + String* value = Getattr(p, "value"); |
| 207 | + |
| 208 | + if ( Len(doc) ) { |
| 209 | + // add a comma to the previous one if any |
| 210 | + Printf(doc, ", "); |
| 211 | + |
| 212 | + // Do we need to wrap a long line? |
| 213 | + if ((Len(doc) - lines*maxwidth) > maxwidth) { |
| 214 | + Printf(doc, "\n "); |
| 215 | + lines += 1; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + // Do the param type too? |
| 220 | + if (showTypes) { |
| 221 | + SwigType* type = SwigType_base(Getattr(p, "type")); |
| 222 | + lookup = Swig_symbol_clookup(type, 0); |
| 223 | + if (lookup) |
| 224 | + type = Getattr(lookup, "sym:name"); |
| 225 | + Printf(doc, "%s ", type); |
| 226 | + } |
| 227 | + |
| 228 | + if (name) |
| 229 | + Printf(doc, "%s", name); |
| 230 | + else |
| 231 | + Printf(doc, "??"); |
| 232 | + |
| 233 | + if (value) { |
| 234 | + if (Strcmp(value, "NULL") == 0) |
| 235 | + value = NewString("None"); |
| 236 | + else { |
| 237 | + lookup = Swig_symbol_clookup(value, 0); |
| 238 | + if (lookup) |
| 239 | + value = Getattr(lookup, "sym:name"); |
| 240 | + } |
| 241 | + Printf(doc, "=%s", value); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + return doc; |
| 246 | + } |
| 247 | + |
| 248 | + |
| 249 | + /* ------------------------------------------------------------ |
| 250 | * have_addtofunc() |
| 251 | * Check if there is a %addtofunc directive and it has text |
| 252 | * ------------------------------------------------------------ */ |
| 253 | @@ -1661,7 +1872,9 @@ |
| 254 | } |
| 255 | } |
| 256 | Printf(f_shadow,":\n"); |
| 257 | - |
| 258 | + if ( have_docstring(n) ) |
| 259 | + Printv(f_shadow, tab4, docstring(n, AUTODOC_CLASS), "\n", NIL); |
| 260 | + |
| 261 | if (!modern) { |
| 262 | Printv(f_shadow,tab4,"__swig_setmethods__ = {}\n",NIL); |
| 263 | if (Len(base_class)) { |
| 264 | @@ -1796,14 +2009,20 @@ |
| 265 | Printv(f_shadow,pycode,"\n",NIL); |
| 266 | } else { |
| 267 | |
| 268 | - Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "): ", NIL); |
| 269 | - if ( have_addtofunc(n) ) { |
| 270 | - Printv(f_shadow, "\n", NIL); |
| 271 | - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL); |
| 272 | - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL); |
| 273 | - Printv(f_shadow, tab8, "return val\n", NIL); |
| 274 | + Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "):", NIL); |
| 275 | + if ( ! have_addtofunc(n) && ! have_docstring(n)) { |
| 276 | + Printv(f_shadow, " return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL); |
| 277 | } else { |
| 278 | - Printv(f_shadow, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL); |
| 279 | + Printv(f_shadow, "\n", NIL); |
| 280 | + if ( have_docstring(n) ) |
| 281 | + Printv(f_shadow, tab8, docstring(n, AUTODOC_FUNC), "\n", NIL); |
| 282 | + if ( have_addtofunc(n) ) { |
| 283 | + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL); |
| 284 | + Printv(f_shadow, tab8, addtofunc(n), "\n", NIL); |
| 285 | + Printv(f_shadow, tab8, "return val\n\n", NIL); |
| 286 | + } else { |
| 287 | + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n\n", NIL); |
| 288 | + } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | @@ -1820,12 +2039,18 @@ |
| 293 | String *symname = Getattr(n,"sym:name"); |
| 294 | Language::staticmemberfunctionHandler(n); |
| 295 | if (shadow) { |
| 296 | - if ( !classic && have_addtofunc(n) ) { |
| 297 | + if ( !classic && (have_addtofunc(n) || have_docstring(n)) ) { |
| 298 | int kw = (check_kwargs(n) && !Getattr(n,"sym:overloaded")) ? 1 : 0; |
| 299 | Printv(f_shadow, tab4, "def ", symname, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL); |
| 300 | - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL); |
| 301 | - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL); |
| 302 | - Printv(f_shadow, tab8, "return val\n", NIL); |
| 303 | + if ( have_docstring(n) ) |
| 304 | + Printv(f_shadow, tab8, docstring(n, AUTODOC_STATICFUNC), "\n", NIL); |
| 305 | + if ( have_addtofunc(n) ) { |
| 306 | + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL); |
| 307 | + Printv(f_shadow, tab8, addtofunc(n), "\n", NIL); |
| 308 | + Printv(f_shadow, tab8, "return val\n\n", NIL); |
| 309 | + } else { |
| 310 | + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n\n", NIL); |
| 311 | + } |
| 312 | Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname, |
| 313 | " = staticmethod(", symname, ")\n", NIL); |
| 314 | |
| 315 | @@ -1912,6 +2137,8 @@ |
| 316 | |
| 317 | Printv(f_shadow, tab4, "def __init__(self, *args", |
| 318 | (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL); |
| 319 | + if ( have_docstring(n) ) |
| 320 | + Printv(f_shadow, tab8, docstring(n, AUTODOC_CTOR), "\n", NIL); |
| 321 | Printv(f_shadow, pass_self, NIL); |
| 322 | if (!modern) { |
| 323 | Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', ", |
| 324 | @@ -1927,7 +2154,7 @@ |
| 325 | Printv(f_shadow, tab8, "del newobj.thisown\n", NIL); |
| 326 | } |
| 327 | if ( have_addtofunc(n) ) |
| 328 | - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL); |
| 329 | + Printv(f_shadow, tab8, addtofunc(n), "\n\n", NIL); |
| 330 | Delete(pass_self); |
| 331 | } |
| 332 | have_constructor = 1; |
| 333 | @@ -1945,6 +2172,8 @@ |
| 334 | |
| 335 | Printv(f_shadow_stubs, "\ndef ", symname, "(*args", |
| 336 | (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL); |
| 337 | + if ( have_docstring(n) ) |
| 338 | + Printv(f_shadow_stubs, tab4, docstring(n, AUTODOC_CTOR), "\n", NIL); |
| 339 | Printv(f_shadow_stubs, tab4, "val = ", |
| 340 | funcCallHelper(Swig_name_construct(symname), allow_kwargs), "\n", NIL); |
| 341 | Printv(f_shadow_stubs, tab4, "val.thisown = 1\n", NIL); |
| 342 | @@ -1978,11 +2207,13 @@ |
| 343 | Printv(f_shadow,pycode,"\n", NIL); |
| 344 | } else { |
| 345 | Printv(f_shadow, tab4, "def __del__(self, destroy=", module, ".", Swig_name_destroy(symname), "):\n", NIL); |
| 346 | + if ( have_docstring(n) ) |
| 347 | + Printv(f_shadow, tab8, docstring(n, AUTODOC_DTOR), "\n", NIL); |
| 348 | if ( have_addtofunc(n) ) |
| 349 | Printv(f_shadow, tab8, addtofunc(n), "\n", NIL); |
| 350 | Printv(f_shadow, tab8, "try:\n", NIL); |
| 351 | Printv(f_shadow, tab4, tab8, "if self.thisown: destroy(self)\n", NIL); |
| 352 | - Printv(f_shadow, tab8, "except: pass\n", NIL); |
| 353 | + Printv(f_shadow, tab8, "except: pass\n\n", NIL); |
| 354 | } |
| 355 | } |
| 356 | return SWIG_OK; |