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