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 -4 -r1.28 python.cxx
6 --- Source/Modules/python.cxx 18 Nov 2003 20:19:15 -0000 1.28
7 +++ Source/Modules/python.cxx 27 Nov 2003 00:52:28 -0000
9 -new_repr - Use more informative version of __repr__ in proxy classes\n\
10 -noexcept - No automatic exception handling\n\
11 -noproxy - Don't generate proxy classes \n\n";
14 +/* flags for the make_autodoc function */
24 class PYTHON : public Language {
27 /* ------------------------------------------------------------
30 * ------------------------------------------------------------ */
32 void emitFunctionShadowHelper(Node *n, File *f_dest, String *name, int kw) {
33 - if ( ! have_addtofunc(n) ) {
34 - /* If there is no addtofunc directive then just assign from the extension module */
35 + if ( ! have_addtofunc(n) && ! have_docstring(n) ) {
36 + /* If there is no addtofunc or docstring directive then just assign from the extension module */
37 Printv(f_dest, "\n", name, " = ", module, ".", name, "\n", NIL);
39 /* Otherwise make a wrapper function to insert the code into */
40 Printv(f_dest, "\ndef ", name, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
41 - Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
42 - Printv(f_dest, tab4, addtofunc(n), "\n", NIL);
43 - Printv(f_dest, tab4, "return val\n", NIL);
44 + if ( have_docstring(n) )
45 + Printv(f_dest, tab4, docstring(n, AUTODOC_FUNC, tab4), "\n", NIL);
46 + if ( have_addtofunc(n) ) {
47 + Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
48 + Printv(f_dest, tab4, addtofunc(n), "\n", NIL);
49 + Printv(f_dest, tab4, "return val\n", NIL);
51 + Printv(f_dest, tab4, "return ", funcCallHelper(name, kw), "\n", NIL);
61 /* ------------------------------------------------------------
63 + * Check if there is a docstring directive and it has text,
64 + * or there is an autodoc flag set
65 + * ------------------------------------------------------------ */
67 + bool have_docstring(Node *n) {
68 + String* str = Getattr(n, "feature:docstring");
69 + return (str != NULL && Len(str) > 0) ||
70 + (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc"));
73 + /* ------------------------------------------------------------
75 + * Get the docstring text, stripping off {} if neccessary,
76 + * and enclose in triple double quotes. If autodoc is also
77 + * set then it will build a combined docstring.
78 + * ------------------------------------------------------------ */
80 + String *docstring(Node *n, autodoc_t ad_type, const String* indent) {
81 + String* str = Getattr(n, "feature:docstring");
82 + bool have_ds = (str != NULL && Len(str) > 0);
83 + bool have_auto = (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc"));
84 + char* triple_double = "\"\"\"";
85 + String* autodoc = NULL;
89 + char* t = Char(str);
92 + Delitem(str,DOH_END);
97 + autodoc = make_autodoc(n, ad_type);
98 + have_auto = (autodoc != NULL && Len(autodoc) > 0);
101 +// if ( have_auto && have_ds )
102 +// doc = NewStringf("%s%s\n\n%s%s", triple_double, autodoc, str, triple_double);
103 +// else if ( !have_auto && have_ds )
104 +// doc = NewStringf("%s%s%s", triple_double, str, triple_double);
106 +// doc = NewStringf("%s%s%s", triple_double, autodoc, triple_double);
109 + // If there is more than one line then make docstrings like this:
113 + // And here is line2 followed by the rest of them
116 + // otherwise, put it all on a single line
118 + if ( have_auto && have_ds ) { // Both autodoc and docstring are present
119 + doc = NewString("");
120 + Printv(doc, triple_double, "\n",
121 + pythoncode(autodoc, indent), "\n",
122 + pythoncode(str, indent),
123 + indent, triple_double, NIL);
125 + else if ( !have_auto && have_ds ) { // only docstring
126 + if (Strchr(str, '\n') == NULL) {
127 + doc = NewStringf("%s%s%s", triple_double, str, triple_double);
130 + doc = NewString("");
131 + Printv(doc, triple_double, "\n",
132 + pythoncode(str, indent),
133 + indent, triple_double, NIL);
136 + else if ( have_auto && !have_ds ) { // only autodoc
137 + if (Strchr(autodoc, '\n') == NULL) {
138 + doc = NewStringf("%s%s%s", triple_double, autodoc, triple_double);
141 + doc = NewString("");
142 + Printv(doc, triple_double, "\n",
143 + pythoncode(autodoc, indent),
144 + indent, triple_double, NIL);
148 + doc = NewString("");
150 + // Save the generated strings in the parse tree in case they are used later
151 + // by post processing tools
152 + Setattr(n, "python:docstring", doc);
153 + Setattr(n, "python:autodoc", autodoc);
158 + /* ------------------------------------------------------------
160 + * Build a docstring for the node, using parameter and other
161 + * info in the parse tree. If the value of the autodoc
162 + * attribute is "0" then do not include parameter types, if
163 + * it is "1" (the default) then do. If it has some other
164 + * value then assume it is supplied by the extension writer
165 + * and use it directly.
166 + * ------------------------------------------------------------ */
168 + String* make_autodoc(Node *n, autodoc_t ad_type) {
170 + if (ad_type == AUTODOC_CLASS)
171 + return NULL; // No function call to document in this case
173 + // If the function is overloaded then this funciton is called
174 + // for the last one. Rewind to the first so the docstrings are
176 + while ( Getattr(n, "sym:previousSibling") )
177 + n = Getattr(n, "sym:previousSibling");
179 + String* doc = NewString("");
181 + bool showTypes = false;
182 + bool skipAuto = false;
184 + // check how should the parameters be rendered?
185 + String* autodoc = Getattr(n, "feature:autodoc");
186 + if (Strcmp(autodoc, "0") == 0)
188 + else if (Strcmp(autodoc, "1") == 0)
191 + // if not "0" or "1" then autodoc is already the string that should be used
192 + Printf(doc, "%s", autodoc);
197 + String* symname = Getattr(n, "sym:name");
198 + SwigType* type = Getattr(n, "type");
201 + if (Strcmp(type, "void") == 0)
204 + type = SwigType_base(type);
205 + Node* lookup = Swig_symbol_clookup(type, 0);
207 + type = Getattr(lookup, "sym:name");
211 + switch ( ad_type ) {
213 + if ( Strcmp(class_name, symname) == 0)
214 + Printf(doc, "__init__(%s) -> %s", make_autodocParmList(n, showTypes), class_name);
216 + Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name);
220 + Printf(doc, "__del__()");
223 + case AUTODOC_STATICFUNC:
224 + Printf(doc, "%s.%s(%s)", class_name, symname, make_autodocParmList(n, showTypes));
225 + if (type) Printf(doc, " -> %s", type);
229 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
230 + if (type) Printf(doc, " -> %s", type);
235 + // if it's overloaded then get the next decl and loop around again
236 + n = Getattr(n, "sym:nextSibling");
245 + String* make_autodocParmList(Node* n, bool showTypes) {
246 + String* doc = NewString("");
247 + ParmList* plist = Getattr(n,"parms");
251 + const int maxwidth = 50;
254 + for (p = plist; p; p = nextSibling(p)) {
255 + String* name = Getattr(p, "name");
256 + String* value = Getattr(p, "value");
259 + // add a comma to the previous one if any
262 + // Do we need to wrap a long line?
263 + if ((Len(doc) - lines*maxwidth) > maxwidth) {
264 + Printf(doc, "\n%s", tab4);
269 + // Do the param type too?
271 + SwigType* type = SwigType_base(Getattr(p, "type"));
272 + lookup = Swig_symbol_clookup(type, 0);
274 + type = Getattr(lookup, "sym:name");
275 + Printf(doc, "%s ", type);
279 + Printf(doc, "%s", name);
284 + if (Strcmp(value, "NULL") == 0)
285 + value = NewString("None");
287 + lookup = Swig_symbol_clookup(value, 0);
289 + value = Getattr(lookup, "sym:name");
291 + Printf(doc, "=%s", value);
299 + /* ------------------------------------------------------------
301 * Check if there is a %addtofunc directive and it has text
302 * ------------------------------------------------------------ */
304 @@ -1660,9 +1915,11 @@
305 Printf(f_shadow, modern ? "(object)" : "(_object)");
308 Printf(f_shadow,":\n");
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);
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 @@ -1795,16 +2052,22 @@
319 Printv(f_shadow,pycode,"\n",NIL);
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);
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);
341 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n\n", NIL);
347 @@ -1819,14 +2082,20 @@
348 virtual int staticmemberfunctionHandler(Node *n) {
349 String *symname = Getattr(n,"sym:name");
350 Language::staticmemberfunctionHandler(n);
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);
366 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n\n", NIL);
368 Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname,
369 " = staticmethod(", symname, ")\n", NIL);
372 @@ -1911,8 +2180,10 @@
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);
381 Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', ",
382 funcCallHelper(Swig_name_construct(symname), allow_kwargs), ")\n", NIL);
383 @@ -1926,9 +2197,9 @@
384 Printv(f_shadow, tab8, "self.thisown = 1\n", NIL);
385 Printv(f_shadow, tab8, "del newobj.thisown\n", NIL);
387 if ( have_addtofunc(n) )
388 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
389 + Printv(f_shadow, tab8, addtofunc(n), "\n\n", NIL);
392 have_constructor = 1;
394 @@ -1944,8 +2215,10 @@
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 @@ -1977,13 +2250,15 @@
407 Printv(f_shadow,pycode,"\n", NIL);
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);