]> git.saurik.com Git - wxWidgets.git/blob - wxPython/SWIG/swig.python-docstring.patch
Some docstring additions, reformats and epydoc markup.
[wxWidgets.git] / wxPython / SWIG / swig.python-docstring.patch
1 Index: Source/Modules/python.cxx
2 ===================================================================
3 RCS file: /cvsroot/swig/SWIG/Source/Modules/python.cxx,v
4 retrieving revision 1.40
5 diff -u -4 -r1.40 python.cxx
6 --- Source/Modules/python.cxx 24 Jan 2004 00:25:31 -0000 1.40
7 +++ Source/Modules/python.cxx 28 Apr 2004 22:20:03 -0000
8 @@ -8,9 +8,9 @@
9 * Copyright (C) 1999-2000. The University of Chicago
10 * See the file LICENSE for information on usage and redistribution.
11 * ----------------------------------------------------------------------------- */
12
13 -char cvsroot_python_cxx[] = "$Header$";
14 +char cvsroot_python_cxx[] = "$Header$";
15
16 #include "swigmod.h"
17
18 #include <ctype.h>
19 @@ -50,8 +50,18 @@
20 static int have_constructor;
21 static int have_repr;
22 static String *real_classname;
23
24 +/* flags for the make_autodoc function */
25 +enum autodoc_t {
26 + AUTODOC_CLASS,
27 + AUTODOC_CTOR,
28 + AUTODOC_DTOR,
29 + AUTODOC_STATICFUNC,
30 + AUTODOC_FUNC,
31 + AUTODOC_METHOD
32 +};
33 +
34 static const char *usage = (char *)"\
35 Python Options (available with -python)\n\
36 -ldflags - Print runtime libraries to link with\n\
37 -globals <name> - Set <name> used to access C global variable [default: 'cvar']\n\
38 @@ -417,17 +427,25 @@
39 * functions.
40 * ------------------------------------------------------------ */
41
42 void emitFunctionShadowHelper(Node *n, File *f_dest, String *name, int kw) {
43 - if ( ! have_addtofunc(n) ) {
44 - /* If there is no addtofunc directive then just assign from the extension module */
45 + if ( !have_pythonprepend(n) && !have_pythonappend(n) && !have_docstring(n) ) {
46 + /* If there is no pythonappend or docstring directive then just assign from the extension module */
47 Printv(f_dest, "\n", name, " = ", module, ".", name, "\n", NIL);
48 } else {
49 /* Otherwise make a wrapper function to insert the code into */
50 Printv(f_dest, "\ndef ", name, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
51 - Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
52 - Printv(f_dest, tab4, addtofunc(n), "\n", NIL);
53 - Printv(f_dest, tab4, "return val\n", NIL);
54 + if ( have_docstring(n) )
55 + Printv(f_dest, tab4, docstring(n, AUTODOC_FUNC, tab4), "\n", NIL);
56 + if ( have_pythonprepend(n) )
57 + Printv(f_dest, tab4, pythonprepend(n), "\n", NIL);
58 + if ( have_pythonappend(n) ) {
59 + Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
60 + Printv(f_dest, tab4, pythonappend(n), "\n", NIL);
61 + Printv(f_dest, tab4, "return val\n", NIL);
62 + } else {
63 + Printv(f_dest, tab4, "return ", funcCallHelper(name, kw), "\n", NIL);
64 + }
65 }
66 }
67
68
69 @@ -441,24 +459,303 @@
70 }
71
72
73 /* ------------------------------------------------------------
74 - * have_addtofunc()
75 - * Check if there is a %addtofunc directive and it has text
76 + * have_docstring()
77 + * Check if there is a docstring directive and it has text,
78 + * or there is an autodoc flag set
79 + * ------------------------------------------------------------ */
80 +
81 + bool have_docstring(Node *n) {
82 + String* str = Getattr(n, "feature:docstring");
83 + return (str != NULL && Len(str) > 0) ||
84 + (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc"));
85 + }
86 +
87 + /* ------------------------------------------------------------
88 + * docstring()
89 + * Get the docstring text, stripping off {} if neccessary,
90 + * and enclose in triple double quotes. If autodoc is also
91 + * set then it will build a combined docstring.
92 + * ------------------------------------------------------------ */
93 +
94 + String *docstring(Node *n, autodoc_t ad_type, const String* indent) {
95 + String* str = Getattr(n, "feature:docstring");
96 + bool have_ds = (str != NULL && Len(str) > 0);
97 + bool have_auto = (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc"));
98 + char* triple_double = "\"\"\"";
99 + String* autodoc = NULL;
100 + String* doc = NULL;
101 +
102 + if ( have_ds ) {
103 + char* t = Char(str);
104 + if (*t == '{') {
105 + Delitem(str ,0);
106 + Delitem(str,DOH_END);
107 + }
108 + }
109 +
110 + if ( have_auto ) {
111 + autodoc = make_autodoc(n, ad_type);
112 + have_auto = (autodoc != NULL && Len(autodoc) > 0);
113 + }
114 +
115 + // If there is more than one line then make docstrings like this:
116 + //
117 + // """
118 + // This is line1
119 + // And here is line2 followed by the rest of them
120 + // """
121 + //
122 + // otherwise, put it all on a single line
123 + //
124 + if ( have_auto && have_ds ) { // Both autodoc and docstring are present
125 + doc = NewString("");
126 + Printv(doc, triple_double, "\n",
127 + pythoncode(autodoc, indent), "\n",
128 + pythoncode(str, indent),
129 + indent, triple_double, NIL);
130 + }
131 + else if ( !have_auto && have_ds ) { // only docstring
132 + if (Strchr(str, '\n') == NULL) {
133 + doc = NewStringf("%s%s%s", triple_double, str, triple_double);
134 + }
135 + else {
136 + doc = NewString("");
137 + Printv(doc, triple_double, "\n",
138 + pythoncode(str, indent),
139 + indent, triple_double, NIL);
140 + }
141 + }
142 + else if ( have_auto && !have_ds ) { // only autodoc
143 + if (Strchr(autodoc, '\n') == NULL) {
144 + doc = NewStringf("%s%s%s", triple_double, autodoc, triple_double);
145 + }
146 + else {
147 + doc = NewString("");
148 + Printv(doc, triple_double, "\n",
149 + pythoncode(autodoc, indent),
150 + indent, triple_double, NIL);
151 + }
152 + }
153 + else
154 + doc = NewString("");
155 +
156 + // Save the generated strings in the parse tree in case they are used later
157 + // by post processing tools
158 + Setattr(n, "python:docstring", doc);
159 + Setattr(n, "python:autodoc", autodoc);
160 + return doc;
161 + }
162 +
163 +
164 + /* ------------------------------------------------------------
165 + * make_autodoc()
166 + * Build a docstring for the node, using parameter and other
167 + * info in the parse tree. If the value of the autodoc
168 + * attribute is "0" then do not include parameter types, if
169 + * it is "1" (the default) then do. If it has some other
170 + * value then assume it is supplied by the extension writer
171 + * and use it directly.
172 * ------------------------------------------------------------ */
173
174 - bool have_addtofunc(Node *n) {
175 - String* str = Getattr(n, "feature:addtofunc");
176 + String* make_autodoc(Node *n, autodoc_t ad_type) {
177 +
178 + if (ad_type == AUTODOC_CLASS)
179 + return NULL; // No function call to document in this case
180 +
181 + // If the function is overloaded then this funciton is called
182 + // for the last one. Rewind to the first so the docstrings are
183 + // in order.
184 + while ( Getattr(n, "sym:previousSibling") )
185 + n = Getattr(n, "sym:previousSibling");
186 +
187 + String* doc = NewString("");
188 + while (n) {
189 + bool showTypes = false;
190 + bool skipAuto = false;
191 +
192 + // check how should the parameters be rendered?
193 + String* autodoc = Getattr(n, "feature:autodoc");
194 + if (Strcmp(autodoc, "0") == 0)
195 + showTypes = false;
196 + else if (Strcmp(autodoc, "1") == 0)
197 + showTypes = true;
198 + else {
199 + // if not "0" or "1" then autodoc is already the string that should be used
200 + Printf(doc, "%s", autodoc);
201 + skipAuto = true;
202 + }
203 +
204 + if (!skipAuto) {
205 + String* symname = Getattr(n, "sym:name");
206 + SwigType* type = Getattr(n, "type");
207 +
208 + if (type) {
209 + if (Strcmp(type, "void") == 0)
210 + type = NULL;
211 + else {
212 + SwigType* qt = SwigType_typedef_resolve_all(type);
213 + if (SwigType_isenum(qt))
214 + type = NewString("int");
215 + else {
216 + type = SwigType_base(type);
217 + Node* lookup = Swig_symbol_clookup(type, 0);
218 + if (lookup)
219 + type = Getattr(lookup, "sym:name");
220 + }
221 + }
222 + }
223 +
224 + switch ( ad_type ) {
225 + case AUTODOC_CTOR:
226 + if ( Strcmp(class_name, symname) == 0) {
227 + String* paramList = make_autodocParmList(n, showTypes);
228 + if (Len(paramList))
229 + Printf(doc, "__init__(self, %s) -> %s", paramList, class_name);
230 + else
231 + Printf(doc, "__init__(self) -> %s", class_name);
232 + }
233 + else
234 + Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name);
235 + break;
236 +
237 + case AUTODOC_DTOR:
238 + Printf(doc, "__del__(self)");
239 + break;
240 +
241 + case AUTODOC_STATICFUNC:
242 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
243 + if (type) Printf(doc, " -> %s", type);
244 + break;
245 +
246 + case AUTODOC_FUNC:
247 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
248 + if (type) Printf(doc, " -> %s", type);
249 + break;
250 +
251 + case AUTODOC_METHOD:
252 + String* paramList = make_autodocParmList(n, showTypes);
253 + if (Len(paramList))
254 + Printf(doc, "%s(self, %s)", symname, paramList);
255 + else
256 + Printf(doc, "%s(self)", symname);
257 + if (type) Printf(doc, " -> %s", type);
258 + break;
259 + }
260 + }
261 +
262 + // if it's overloaded then get the next decl and loop around again
263 + n = Getattr(n, "sym:nextSibling");
264 + if (n)
265 + Printf(doc, "\n");
266 + }
267 +
268 + return doc;
269 + }
270 +
271 +
272 + String* make_autodocParmList(Node* n, bool showTypes) {
273 + String* doc = NewString("");
274 + ParmList* plist = Getattr(n,"parms");
275 + Parm* p;
276 + Node* lookup;
277 + int lines = 0;
278 + const int maxwidth = 50;
279 +
280 +
281 + for (p = plist; p; p = nextSibling(p)) {
282 + String* name = Getattr(p, "name");
283 + String* value = Getattr(p, "value");
284 +
285 + if ( Len(doc) ) {
286 + // add a comma to the previous one if any
287 + Printf(doc, ", ");
288 +
289 + // Do we need to wrap a long line?
290 + if ((Len(doc) - lines*maxwidth) > maxwidth) {
291 + Printf(doc, "\n%s", tab4);
292 + lines += 1;
293 + }
294 + }
295 +
296 + // Do the param type too?
297 + if (showTypes) {
298 + SwigType* type = SwigType_base(Getattr(p, "type"));
299 + SwigType* qt = SwigType_typedef_resolve_all(type);
300 + if (SwigType_isenum(qt))
301 + type = NewString("int");
302 + else {
303 + lookup = Swig_symbol_clookup(type, 0);
304 + if (lookup)
305 + type = Getattr(lookup, "sym:name");
306 + }
307 + Printf(doc, "%s ", type);
308 + }
309 +
310 + if (name)
311 + Printf(doc, "%s", name);
312 + else
313 + Printf(doc, "??");
314 +
315 + if (value) {
316 + if (Strcmp(value, "NULL") == 0)
317 + value = NewString("None");
318 + else {
319 + lookup = Swig_symbol_clookup(value, 0);
320 + if (lookup)
321 + value = Getattr(lookup, "sym:name");
322 + }
323 + Printf(doc, "=%s", value);
324 + }
325 + }
326 +
327 + return doc;
328 + }
329 +
330 +
331 + /* ------------------------------------------------------------
332 + * have_pythonprepend()
333 + * Check if there is a %pythonprepend directive and it has text
334 + * ------------------------------------------------------------ */
335 +
336 + bool have_pythonprepend(Node *n) {
337 + String* str = Getattr(n, "feature:pythonprepend");
338 + return (str != NULL && Len(str) > 0);
339 + }
340 +
341 + /* ------------------------------------------------------------
342 + * pythonprepend()
343 + * Get the %pythonprepend code, stripping off {} if neccessary
344 + * ------------------------------------------------------------ */
345 +
346 + String *pythonprepend(Node *n) {
347 + String* str = Getattr(n, "feature:pythonprepend");
348 + char* t = Char(str);
349 + if (*t == '{') {
350 + Delitem(str ,0);
351 + Delitem(str,DOH_END);
352 + }
353 + return str;
354 + }
355 +
356 + /* ------------------------------------------------------------
357 + * have_pythonappend()
358 + * Check if there is a %pythonappend directive and it has text
359 + * ------------------------------------------------------------ */
360 +
361 + bool have_pythonappend(Node *n) {
362 + String* str = Getattr(n, "feature:pythonappend");
363 return (str != NULL && Len(str) > 0);
364 }
365
366 /* ------------------------------------------------------------
367 - * addtofunc()
368 - * Get the %addtofunc code, stripping off {} if neccessary
369 + * pythonappend()
370 + * Get the %pythonappend code, stripping off {} if neccessary
371 * ------------------------------------------------------------ */
372
373 - String *addtofunc(Node *n) {
374 - String* str = Getattr(n, "feature:addtofunc");
375 + String *pythonappend(Node *n) {
376 + String* str = Getattr(n, "feature:pythonappend");
377 char* t = Char(str);
378 if (*t == '{') {
379 Delitem(str ,0);
380 Delitem(str,DOH_END);
381 @@ -1731,9 +2028,11 @@
382 Printf(f_shadow, modern ? "(object)" : "(_object)");
383 }
384 }
385 Printf(f_shadow,":\n");
386 -
387 + if ( Getattr(n, "feature:docstring") ) // don't use have_docstring in this case because autodoc doesn't apply
388 + Printv(f_shadow, tab4, docstring(n, AUTODOC_CLASS, tab4), "\n", NIL);
389 +
390 if (!modern) {
391 Printv(f_shadow,tab4,"__swig_setmethods__ = {}\n",NIL);
392 if (Len(base_class)) {
393 Printf(f_shadow,"%sfor _s in [%s]: __swig_setmethods__.update(_s.__swig_setmethods__)\n",tab4,base_class);
394 @@ -1866,16 +2165,24 @@
395 Delete(pyaction);
396 Printv(f_shadow,pycode,"\n",NIL);
397 } else {
398
399 - Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "): ", NIL);
400 - if ( have_addtofunc(n) ) {
401 - Printv(f_shadow, "\n", NIL);
402 - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
403 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
404 - Printv(f_shadow, tab8, "return val\n", NIL);
405 + Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "):", NIL);
406 + if ( !have_pythonprepend(n) && !have_pythonappend(n) && !have_docstring(n)) {
407 + Printv(f_shadow, " return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
408 } else {
409 - Printv(f_shadow, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
410 + Printv(f_shadow, "\n", NIL);
411 + if ( have_docstring(n) )
412 + Printv(f_shadow, tab8, docstring(n, AUTODOC_METHOD, tab8), "\n", NIL);
413 + if ( have_pythonprepend(n) )
414 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
415 + if ( have_pythonappend(n) ) {
416 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
417 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
418 + Printv(f_shadow, tab8, "return val\n\n", NIL);
419 + } else {
420 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n\n", NIL);
421 + }
422 }
423 }
424
425 }
426 @@ -1890,14 +2197,22 @@
427 virtual int staticmemberfunctionHandler(Node *n) {
428 String *symname = Getattr(n,"sym:name");
429 Language::staticmemberfunctionHandler(n);
430 if (shadow) {
431 - if ( !classic && have_addtofunc(n) ) {
432 + if ( !classic && (have_pythonprepend(n) || have_pythonappend(n) || have_docstring(n)) ) {
433 int kw = (check_kwargs(n) && !Getattr(n,"sym:overloaded")) ? 1 : 0;
434 Printv(f_shadow, tab4, "def ", symname, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
435 - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
436 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
437 - Printv(f_shadow, tab8, "return val\n", NIL);
438 + if ( have_docstring(n) )
439 + Printv(f_shadow, tab8, docstring(n, AUTODOC_STATICFUNC, tab8), "\n", NIL);
440 + if ( have_pythonprepend(n) )
441 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
442 + if ( have_pythonappend(n) ) {
443 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
444 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
445 + Printv(f_shadow, tab8, "return val\n\n", NIL);
446 + } else {
447 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n\n", NIL);
448 + }
449 Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname,
450 " = staticmethod(", symname, ")\n", NIL);
451
452 if (!modern) {
453 @@ -1982,8 +2297,12 @@
454 }
455
456 Printv(f_shadow, tab4, "def __init__(self, *args",
457 (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
458 + if ( have_docstring(n) )
459 + Printv(f_shadow, tab8, docstring(n, AUTODOC_CTOR, tab8), "\n", NIL);
460 + if ( have_pythonprepend(n) )
461 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
462 Printv(f_shadow, pass_self, NIL);
463 if (!modern) {
464 Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', ",
465 funcCallHelper(Swig_name_construct(symname), allow_kwargs), ")\n", NIL);
466 @@ -1996,10 +2315,10 @@
467 Printv(f_shadow, tab8, "self.this = newobj.this\n", NIL);
468 Printv(f_shadow, tab8, "self.thisown = 1\n", NIL);
469 Printv(f_shadow, tab8, "del newobj.thisown\n", NIL);
470 }
471 - if ( have_addtofunc(n) )
472 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
473 + if ( have_pythonappend(n) )
474 + Printv(f_shadow, tab8, pythonappend(n), "\n\n", NIL);
475 Delete(pass_self);
476 }
477 have_constructor = 1;
478 } else {
479 @@ -2015,13 +2334,17 @@
480 } else {
481
482 Printv(f_shadow_stubs, "\ndef ", symname, "(*args",
483 (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
484 + if ( have_docstring(n) )
485 + Printv(f_shadow_stubs, tab4, docstring(n, AUTODOC_CTOR, tab4), "\n", NIL);
486 + if ( have_pythonprepend(n) )
487 + Printv(f_shadow_stubs, tab4, pythonprepend(n), "\n", NIL);
488 Printv(f_shadow_stubs, tab4, "val = ",
489 funcCallHelper(Swig_name_construct(symname), allow_kwargs), "\n", NIL);
490 Printv(f_shadow_stubs, tab4, "val.thisown = 1\n", NIL);
491 - if ( have_addtofunc(n) )
492 - Printv(f_shadow_stubs, tab4, addtofunc(n), "\n", NIL);
493 + if ( have_pythonappend(n) )
494 + Printv(f_shadow_stubs, tab4, pythonappend(n), "\n", NIL);
495 Printv(f_shadow_stubs, tab4, "return val\n", NIL);
496 }
497 }
498 }
499 @@ -2048,13 +2371,18 @@
500 Delete(pyaction);
501 Printv(f_shadow,pycode,"\n", NIL);
502 } else {
503 Printv(f_shadow, tab4, "def __del__(self, destroy=", module, ".", Swig_name_destroy(symname), "):\n", NIL);
504 - if ( have_addtofunc(n) )
505 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
506 + if ( have_docstring(n) )
507 + Printv(f_shadow, tab8, docstring(n, AUTODOC_DTOR, tab8), "\n", NIL);
508 + if ( have_pythonprepend(n) )
509 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
510 Printv(f_shadow, tab8, "try:\n", NIL);
511 - Printv(f_shadow, tab4, tab8, "if self.thisown: destroy(self)\n", NIL);
512 + Printv(f_shadow, tab8, tab4, "if self.thisown: destroy(self)\n", NIL);
513 Printv(f_shadow, tab8, "except: pass\n", NIL);
514 + if ( have_pythonappend(n) )
515 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
516 + Printv(f_shadow, "\n", NIL);
517 }
518 }
519 return SWIG_OK;
520 }