]> git.saurik.com Git - wxWidgets.git/blob - wxPython/SWIG/swig.python-docstring.patch
Updated docstring patch for new SWIG CVS
[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.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 19 Nov 2003 17:34:55 -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,193 @@
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 do document in this case
122 +
123 + // check how should the parameters be rendered?
124 + String* autodoc = Getattr(n, "feature:autodoc");
125 + bool showTypes;
126 + if (Strcmp(autodoc, "0") == 0)
127 + showTypes = false;
128 + else if (Strcmp(autodoc, "1") == 0)
129 + showTypes = true;
130 + else
131 + return autodoc;
132 +
133 + // If the function is overloaded then this funciton is called
134 + // for the last one. Rewind to the first so the docstrings are
135 + // in order.
136 + while ( Getattr(n, "sym:previousSibling") )
137 + n = Getattr(n, "sym:previousSibling");
138 +
139 + String* doc = NewString("");
140 + while (n) {
141 + String* symname = Getattr(n, "sym:name");
142 + SwigType* type = Getattr(n, "type");
143 +
144 + if (type) {
145 + if (Strcmp(type, "void") == 0)
146 + type = NULL;
147 + else {
148 + type = SwigType_base(type);
149 + Node* lookup = Swig_symbol_clookup(type, 0);
150 + if (lookup)
151 + type = Getattr(lookup, "sym:name");
152 + }
153 + }
154 +
155 + switch ( ad_type ) {
156 + case AUTODOC_CTOR:
157 + if ( Strcmp(class_name, symname) == 0)
158 + Printf(doc, "__init__(%s) -> %s", make_autodocParmList(n, showTypes), class_name);
159 + else
160 + Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name);
161 + break;
162 +
163 + case AUTODOC_DTOR:
164 + Printf(doc, "__del__()");
165 + break;
166 +
167 + case AUTODOC_STATICFUNC:
168 + Printf(doc, "%s.%s(%s)", class_name, symname, make_autodocParmList(n, showTypes));
169 + if (type) Printf(doc, " -> %s", type);
170 + break;
171 +
172 + case AUTODOC_FUNC:
173 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
174 + if (type) Printf(doc, " -> %s", type);
175 + break;
176 + }
177 +
178 + // if it's overloaded then get the next decl and loop around again
179 + n = Getattr(n, "sym:nextSibling");
180 + if (n)
181 + Printf(doc, "\n");
182 + }
183 +
184 + return doc;
185 + }
186 +
187 +
188 + String* make_autodocParmList(Node* n, bool showTypes) {
189 + String* doc = NewString("");
190 + ParmList* plist = Getattr(n,"parms");
191 + Parm* p;
192 + Node* lookup;
193 + int lines = 0;
194 + const int maxwidth = 50;
195 +
196 +
197 + for (p = plist; p; p = nextSibling(p)) {
198 + String* name = Getattr(p, "name");
199 + String* value = Getattr(p, "value");
200 +
201 + if ( Len(doc) ) {
202 + // add a comma to the previous one if any
203 + Printf(doc, ", ");
204 +
205 + // Do we need to wrap a long line?
206 + if ((Len(doc) - lines*maxwidth) > maxwidth) {
207 + Printf(doc, "\n ");
208 + lines += 1;
209 + }
210 + }
211 +
212 + // Do the param type too?
213 + if (showTypes) {
214 + SwigType* type = SwigType_base(Getattr(p, "type"));
215 + lookup = Swig_symbol_clookup(type, 0);
216 + if (lookup)
217 + type = Getattr(lookup, "sym:name");
218 + Printf(doc, "%s ", type);
219 + }
220 +
221 + if (name)
222 + Printf(doc, "%s", name);
223 + else
224 + Printf(doc, "??");
225 +
226 + if (value) {
227 + if (Strcmp(value, "NULL") == 0)
228 + value = NewString("None");
229 + else {
230 + lookup = Swig_symbol_clookup(value, 0);
231 + if (lookup)
232 + value = Getattr(lookup, "sym:name");
233 + }
234 + Printf(doc, "=%s", value);
235 + }
236 + }
237 +
238 + return doc;
239 + }
240 +
241 +
242 + /* ------------------------------------------------------------
243 * have_addtofunc()
244 * Check if there is a %addtofunc directive and it has text
245 * ------------------------------------------------------------ */
246 @@ -1661,7 +1865,9 @@
247 }
248 }
249 Printf(f_shadow,":\n");
250 -
251 + if ( have_docstring(n) )
252 + Printv(f_shadow, tab4, docstring(n, AUTODOC_CLASS), "\n", NIL);
253 +
254 if (!modern) {
255 Printv(f_shadow,tab4,"__swig_setmethods__ = {}\n",NIL);
256 if (Len(base_class)) {
257 @@ -1796,14 +2002,20 @@
258 Printv(f_shadow,pycode,"\n",NIL);
259 } else {
260
261 - Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "): ", NIL);
262 - if ( have_addtofunc(n) ) {
263 - Printv(f_shadow, "\n", NIL);
264 - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
265 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
266 - Printv(f_shadow, tab8, "return val\n", NIL);
267 + Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "):", NIL);
268 + if ( ! have_addtofunc(n) && ! have_docstring(n)) {
269 + Printv(f_shadow, " return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
270 } else {
271 - Printv(f_shadow, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
272 + Printv(f_shadow, "\n", NIL);
273 + if ( have_docstring(n) )
274 + Printv(f_shadow, tab8, docstring(n, AUTODOC_FUNC), "\n", NIL);
275 + if ( have_addtofunc(n) ) {
276 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
277 + Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
278 + Printv(f_shadow, tab8, "return val\n\n", NIL);
279 + } else {
280 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n\n", NIL);
281 + }
282 }
283 }
284
285 @@ -1820,12 +2032,18 @@
286 String *symname = Getattr(n,"sym:name");
287 Language::staticmemberfunctionHandler(n);
288 if (shadow) {
289 - if ( !classic && have_addtofunc(n) ) {
290 + if ( !classic && (have_addtofunc(n) || have_docstring(n)) ) {
291 int kw = (check_kwargs(n) && !Getattr(n,"sym:overloaded")) ? 1 : 0;
292 Printv(f_shadow, tab4, "def ", symname, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
293 - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
294 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
295 - Printv(f_shadow, tab8, "return val\n", NIL);
296 + if ( have_docstring(n) )
297 + Printv(f_shadow, tab8, docstring(n, AUTODOC_STATICFUNC), "\n", NIL);
298 + if ( have_addtofunc(n) ) {
299 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
300 + Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
301 + Printv(f_shadow, tab8, "return val\n\n", NIL);
302 + } else {
303 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n\n", NIL);
304 + }
305 Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname,
306 " = staticmethod(", symname, ")\n", NIL);
307
308 @@ -1912,6 +2130,8 @@
309
310 Printv(f_shadow, tab4, "def __init__(self, *args",
311 (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
312 + if ( have_docstring(n) )
313 + Printv(f_shadow, tab8, docstring(n, AUTODOC_CTOR), "\n", NIL);
314 Printv(f_shadow, pass_self, NIL);
315 if (!modern) {
316 Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', ",
317 @@ -1927,7 +2147,7 @@
318 Printv(f_shadow, tab8, "del newobj.thisown\n", NIL);
319 }
320 if ( have_addtofunc(n) )
321 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
322 + Printv(f_shadow, tab8, addtofunc(n), "\n\n", NIL);
323 Delete(pass_self);
324 }
325 have_constructor = 1;
326 @@ -1945,6 +2165,8 @@
327
328 Printv(f_shadow_stubs, "\ndef ", symname, "(*args",
329 (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
330 + if ( have_docstring(n) )
331 + Printv(f_shadow_stubs, tab4, docstring(n, AUTODOC_CTOR), "\n", NIL);
332 Printv(f_shadow_stubs, tab4, "val = ",
333 funcCallHelper(Swig_name_construct(symname), allow_kwargs), "\n", NIL);
334 Printv(f_shadow_stubs, tab4, "val.thisown = 1\n", NIL);
335 @@ -1978,11 +2200,13 @@
336 Printv(f_shadow,pycode,"\n", NIL);
337 } else {
338 Printv(f_shadow, tab4, "def __del__(self, destroy=", module, ".", Swig_name_destroy(symname), "):\n", NIL);
339 + if ( have_docstring(n) )
340 + Printv(f_shadow, tab8, docstring(n, AUTODOC_DTOR), "\n", NIL);
341 if ( have_addtofunc(n) )
342 Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
343 Printv(f_shadow, tab8, "try:\n", NIL);
344 Printv(f_shadow, tab4, tab8, "if self.thisown: destroy(self)\n", NIL);
345 - Printv(f_shadow, tab8, "except: pass\n", NIL);
346 + Printv(f_shadow, tab8, "except: pass\n\n", NIL);
347 }
348 }
349 return SWIG_OK;