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