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
9 -noexcept - No automatic exception handling\n\
10 -noproxy - Don't generate proxy classes \n\n";
13 +/* flags for the make_autodoc function */
23 class PYTHON : public Language {
27 * ------------------------------------------------------------ */
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);
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);
47 + Printv(f_dest, tab4, "return ", funcCallHelper(name, kw), "\n", NIL);
55 /* ------------------------------------------------------------
57 + * Check if there is a docstring directive and it has text,
58 + * or there is an autodoc flag set
59 + * ------------------------------------------------------------ */
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"));
67 + /* ------------------------------------------------------------
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 + * ------------------------------------------------------------ */
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 = "\"\"\"";
83 + char* t = Char(str);
86 + Delitem(str,DOH_END);
91 + autodoc = make_autodoc(n, ad_type);
92 + have_auto = (autodoc != NULL && Len(autodoc) > 0);
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);
100 + doc = NewStringf("%s%s%s", triple_double, autodoc, triple_double);
102 + Setattr(n, "python:docstring", doc);
103 + Setattr(n, "python:autodoc", autodoc);
108 + /* ------------------------------------------------------------
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 + * ------------------------------------------------------------ */
118 + String* make_autodoc(Node *n, autodoc_t ad_type) {
120 + if (ad_type == AUTODOC_CLASS)
121 + return NULL; // No function call to document in this case
123 + // If the function is overloaded then this funciton is called
124 + // for the last one. Rewind to the first so the docstrings are
126 + while ( Getattr(n, "sym:previousSibling") )
127 + n = Getattr(n, "sym:previousSibling");
129 + String* doc = NewString("");
131 + bool showTypes = false;
132 + bool skipAuto = false;
134 + // check how should the parameters be rendered?
135 + String* autodoc = Getattr(n, "feature:autodoc");
136 + if (Strcmp(autodoc, "0") == 0)
138 + else if (Strcmp(autodoc, "1") == 0)
141 + // if not "0" or "1" then autodoc is already the string that should be used
142 + Printf(doc, "%s", autodoc);
147 + String* symname = Getattr(n, "sym:name");
148 + SwigType* type = Getattr(n, "type");
151 + if (Strcmp(type, "void") == 0)
154 + type = SwigType_base(type);
155 + Node* lookup = Swig_symbol_clookup(type, 0);
157 + type = Getattr(lookup, "sym:name");
161 + switch ( ad_type ) {
163 + if ( Strcmp(class_name, symname) == 0)
164 + Printf(doc, "__init__(%s) -> %s", make_autodocParmList(n, showTypes), class_name);
166 + Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name);
170 + Printf(doc, "__del__()");
173 + case AUTODOC_STATICFUNC:
174 + Printf(doc, "%s.%s(%s)", class_name, symname, make_autodocParmList(n, showTypes));
175 + if (type) Printf(doc, " -> %s", type);
179 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
180 + if (type) Printf(doc, " -> %s", type);
185 + // if it's overloaded then get the next decl and loop around again
186 + n = Getattr(n, "sym:nextSibling");
195 + String* make_autodocParmList(Node* n, bool showTypes) {
196 + String* doc = NewString("");
197 + ParmList* plist = Getattr(n,"parms");
201 + const int maxwidth = 50;
204 + for (p = plist; p; p = nextSibling(p)) {
205 + String* name = Getattr(p, "name");
206 + String* value = Getattr(p, "value");
209 + // add a comma to the previous one if any
212 + // Do we need to wrap a long line?
213 + if ((Len(doc) - lines*maxwidth) > maxwidth) {
214 + Printf(doc, "\n ");
219 + // Do the param type too?
221 + SwigType* type = SwigType_base(Getattr(p, "type"));
222 + lookup = Swig_symbol_clookup(type, 0);
224 + type = Getattr(lookup, "sym:name");
225 + Printf(doc, "%s ", type);
229 + Printf(doc, "%s", name);
234 + if (Strcmp(value, "NULL") == 0)
235 + value = NewString("None");
237 + lookup = Swig_symbol_clookup(value, 0);
239 + value = Getattr(lookup, "sym:name");
241 + Printf(doc, "=%s", value);
249 + /* ------------------------------------------------------------
251 * Check if there is a %addtofunc directive and it has text
252 * ------------------------------------------------------------ */
253 @@ -1661,7 +1872,9 @@
256 Printf(f_shadow,":\n");
258 + if ( have_docstring(n) )
259 + Printv(f_shadow, tab4, docstring(n, AUTODOC_CLASS), "\n", NIL);
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);
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);
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);
287 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n\n", NIL);
292 @@ -1820,12 +2039,18 @@
293 String *symname = Getattr(n,"sym:name");
294 Language::staticmemberfunctionHandler(n);
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);
310 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n\n", NIL);
312 Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname,
313 " = staticmethod(", symname, ")\n", NIL);
315 @@ -1912,6 +2137,8 @@
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);
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);
327 if ( have_addtofunc(n) )
328 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
329 + Printv(f_shadow, tab8, addtofunc(n), "\n\n", NIL);
332 have_constructor = 1;
333 @@ -1945,6 +2172,8 @@
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);
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);