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
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 do document in this case
123 + // check how should the parameters be rendered?
124 + String* autodoc = Getattr(n, "feature:autodoc");
126 + if (Strcmp(autodoc, "0") == 0)
128 + else if (Strcmp(autodoc, "1") == 0)
133 + // If the function is overloaded then this funciton is called
134 + // for the last one. Rewind to the first so the docstrings are
136 + while ( Getattr(n, "sym:previousSibling") )
137 + n = Getattr(n, "sym:previousSibling");
139 + String* doc = NewString("");
141 + String* symname = Getattr(n, "sym:name");
142 + SwigType* type = Getattr(n, "type");
145 + if (Strcmp(type, "void") == 0)
148 + type = SwigType_base(type);
149 + Node* lookup = Swig_symbol_clookup(type, 0);
151 + type = Getattr(lookup, "sym:name");
155 + switch ( ad_type ) {
157 + if ( Strcmp(class_name, symname) == 0)
158 + Printf(doc, "__init__(%s) -> %s", make_autodocParmList(n, showTypes), class_name);
160 + Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name);
164 + Printf(doc, "__del__()");
167 + case AUTODOC_STATICFUNC:
168 + Printf(doc, "%s.%s(%s)", class_name, symname, make_autodocParmList(n, showTypes));
169 + if (type) Printf(doc, " -> %s", type);
173 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
174 + if (type) Printf(doc, " -> %s", type);
178 + // if it's overloaded then get the next decl and loop around again
179 + n = Getattr(n, "sym:nextSibling");
188 + String* make_autodocParmList(Node* n, bool showTypes) {
189 + String* doc = NewString("");
190 + ParmList* plist = Getattr(n,"parms");
194 + const int maxwidth = 50;
197 + for (p = plist; p; p = nextSibling(p)) {
198 + String* name = Getattr(p, "name");
199 + String* value = Getattr(p, "value");
202 + // add a comma to the previous one if any
205 + // Do we need to wrap a long line?
206 + if ((Len(doc) - lines*maxwidth) > maxwidth) {
207 + Printf(doc, "\n ");
212 + // Do the param type too?
214 + SwigType* type = SwigType_base(Getattr(p, "type"));
215 + lookup = Swig_symbol_clookup(type, 0);
217 + type = Getattr(lookup, "sym:name");
218 + Printf(doc, "%s ", type);
222 + Printf(doc, "%s", name);
227 + if (Strcmp(value, "NULL") == 0)
228 + value = NewString("None");
230 + lookup = Swig_symbol_clookup(value, 0);
232 + value = Getattr(lookup, "sym:name");
234 + Printf(doc, "=%s", value);
242 + /* ------------------------------------------------------------
244 * Check if there is a %addtofunc directive and it has text
245 * ------------------------------------------------------------ */
246 @@ -1661,7 +1865,9 @@
249 Printf(f_shadow,":\n");
251 + if ( have_docstring(n) )
252 + Printv(f_shadow, tab4, docstring(n, AUTODOC_CLASS), "\n", NIL);
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);
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);
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);
280 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n\n", NIL);
285 @@ -1820,12 +2032,18 @@
286 String *symname = Getattr(n,"sym:name");
287 Language::staticmemberfunctionHandler(n);
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);
303 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n\n", NIL);
305 Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname,
306 " = staticmethod(", symname, ")\n", NIL);
308 @@ -1912,6 +2130,8 @@
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);
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);
320 if ( have_addtofunc(n) )
321 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
322 + Printv(f_shadow, tab8, addtofunc(n), "\n\n", NIL);
325 have_constructor = 1;
326 @@ -1945,6 +2165,8 @@
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);
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);