1 Index: Source/Modules/python.cxx
2 ===================================================================
3 RCS file: /cvsroot/swig/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 12 May 2004 19:45:20 -0000
9 static int have_constructor;
11 static String *real_classname;
13 +/* flags for the make_autodoc function */
23 static const char *usage = (char *)"\
24 Python Options (available with -python)\n\
25 -ldflags - Print runtime libraries to link with\n\
26 -globals <name> - Set <name> used to access C global variable [default: 'cvar']\n\
29 * use %module(directors="1") modulename at the start of the
30 * interface file to enable director generation.
32 + String* mod_docstring = NULL;
34 Node *module = Getattr(n, "module");
36 Node *options = Getattr(module, "options");
39 if (Getattr(options, "dirprot")) {
42 + mod_docstring = Getattr(options, "docstring");
49 "# This file is compatible with both classic and new-style classes.\n",
53 + if (mod_docstring && Len(mod_docstring)) {
54 + Printv(f_shadow, "\n\"\"\"\n", mod_docstring, "\n\"\"\"\n", NIL);
55 + Delete(mod_docstring); mod_docstring = NULL;
58 Printf(f_shadow,"\nimport %s\n\n", module);
63 * ------------------------------------------------------------ */
65 void emitFunctionShadowHelper(Node *n, File *f_dest, String *name, int kw) {
66 - if ( ! have_addtofunc(n) ) {
67 - /* If there is no addtofunc directive then just assign from the extension module */
68 + if ( !have_pythonprepend(n) && !have_pythonappend(n) && !have_docstring(n) ) {
69 + /* If there is no pythonappend or docstring directive then just assign from the extension module */
70 Printv(f_dest, "\n", name, " = ", module, ".", name, "\n", NIL);
72 /* Otherwise make a wrapper function to insert the code into */
73 Printv(f_dest, "\ndef ", name, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
74 - Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
75 - Printv(f_dest, tab4, addtofunc(n), "\n", NIL);
76 - Printv(f_dest, tab4, "return val\n", NIL);
77 + if ( have_docstring(n) )
78 + Printv(f_dest, tab4, docstring(n, AUTODOC_FUNC, tab4), "\n", NIL);
79 + if ( have_pythonprepend(n) )
80 + Printv(f_dest, tab4, pythonprepend(n), "\n", NIL);
81 + if ( have_pythonappend(n) ) {
82 + Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
83 + Printv(f_dest, tab4, pythonappend(n), "\n", NIL);
84 + Printv(f_dest, tab4, "return val\n", NIL);
86 + Printv(f_dest, tab4, "return ", funcCallHelper(name, kw), "\n", NIL);
92 @@ -441,24 +466,303 @@
96 /* ------------------------------------------------------------
98 - * Check if there is a %addtofunc directive and it has text
100 + * Check if there is a docstring directive and it has text,
101 + * or there is an autodoc flag set
102 + * ------------------------------------------------------------ */
104 + bool have_docstring(Node *n) {
105 + String* str = Getattr(n, "feature:docstring");
106 + return (str != NULL && Len(str) > 0) ||
107 + (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc"));
110 + /* ------------------------------------------------------------
112 + * Get the docstring text, stripping off {} if neccessary,
113 + * and enclose in triple double quotes. If autodoc is also
114 + * set then it will build a combined docstring.
115 + * ------------------------------------------------------------ */
117 + String *docstring(Node *n, autodoc_t ad_type, const String* indent) {
118 + String* str = Getattr(n, "feature:docstring");
119 + bool have_ds = (str != NULL && Len(str) > 0);
120 + bool have_auto = (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc"));
121 + char* triple_double = "\"\"\"";
122 + String* autodoc = NULL;
123 + String* doc = NULL;
126 + char* t = Char(str);
129 + Delitem(str,DOH_END);
134 + autodoc = make_autodoc(n, ad_type);
135 + have_auto = (autodoc != NULL && Len(autodoc) > 0);
138 + // If there is more than one line then make docstrings like this:
142 + // And here is line2 followed by the rest of them
145 + // otherwise, put it all on a single line
147 + if ( have_auto && have_ds ) { // Both autodoc and docstring are present
148 + doc = NewString("");
149 + Printv(doc, triple_double, "\n",
150 + pythoncode(autodoc, indent), "\n",
151 + pythoncode(str, indent),
152 + indent, triple_double, NIL);
154 + else if ( !have_auto && have_ds ) { // only docstring
155 + if (Strchr(str, '\n') == NULL) {
156 + doc = NewStringf("%s%s%s", triple_double, str, triple_double);
159 + doc = NewString("");
160 + Printv(doc, triple_double, "\n",
161 + pythoncode(str, indent),
162 + indent, triple_double, NIL);
165 + else if ( have_auto && !have_ds ) { // only autodoc
166 + if (Strchr(autodoc, '\n') == NULL) {
167 + doc = NewStringf("%s%s%s", triple_double, autodoc, triple_double);
170 + doc = NewString("");
171 + Printv(doc, triple_double, "\n",
172 + pythoncode(autodoc, indent),
173 + indent, triple_double, NIL);
177 + doc = NewString("");
179 + // Save the generated strings in the parse tree in case they are used later
180 + // by post processing tools
181 + Setattr(n, "python:docstring", doc);
182 + Setattr(n, "python:autodoc", autodoc);
187 + /* ------------------------------------------------------------
189 + * Build a docstring for the node, using parameter and other
190 + * info in the parse tree. If the value of the autodoc
191 + * attribute is "0" then do not include parameter types, if
192 + * it is "1" (the default) then do. If it has some other
193 + * value then assume it is supplied by the extension writer
194 + * and use it directly.
195 + * ------------------------------------------------------------ */
197 + String* make_autodoc(Node *n, autodoc_t ad_type) {
199 + if (ad_type == AUTODOC_CLASS)
200 + return NULL; // No function call to document in this case
202 + // If the function is overloaded then this funciton is called
203 + // for the last one. Rewind to the first so the docstrings are
205 + while ( Getattr(n, "sym:previousSibling") )
206 + n = Getattr(n, "sym:previousSibling");
208 + String* doc = NewString("");
210 + bool showTypes = false;
211 + bool skipAuto = false;
213 + // check how should the parameters be rendered?
214 + String* autodoc = Getattr(n, "feature:autodoc");
215 + if (Strcmp(autodoc, "0") == 0)
217 + else if (Strcmp(autodoc, "1") == 0)
220 + // if not "0" or "1" then autodoc is already the string that should be used
221 + Printf(doc, "%s", autodoc);
226 + String* symname = Getattr(n, "sym:name");
227 + SwigType* type = Getattr(n, "type");
230 + if (Strcmp(type, "void") == 0)
233 + SwigType* qt = SwigType_typedef_resolve_all(type);
234 + if (SwigType_isenum(qt))
235 + type = NewString("int");
237 + type = SwigType_base(type);
238 + Node* lookup = Swig_symbol_clookup(type, 0);
240 + type = Getattr(lookup, "sym:name");
245 + switch ( ad_type ) {
247 + if ( Strcmp(class_name, symname) == 0) {
248 + String* paramList = make_autodocParmList(n, showTypes);
249 + if (Len(paramList))
250 + Printf(doc, "__init__(self, %s) -> %s", paramList, class_name);
252 + Printf(doc, "__init__(self) -> %s", class_name);
255 + Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name);
259 + Printf(doc, "__del__(self)");
262 + case AUTODOC_STATICFUNC:
263 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
264 + if (type) Printf(doc, " -> %s", type);
268 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
269 + if (type) Printf(doc, " -> %s", type);
272 + case AUTODOC_METHOD:
273 + String* paramList = make_autodocParmList(n, showTypes);
274 + if (Len(paramList))
275 + Printf(doc, "%s(self, %s)", symname, paramList);
277 + Printf(doc, "%s(self)", symname);
278 + if (type) Printf(doc, " -> %s", type);
283 + // if it's overloaded then get the next decl and loop around again
284 + n = Getattr(n, "sym:nextSibling");
293 + String* make_autodocParmList(Node* n, bool showTypes) {
294 + String* doc = NewString("");
295 + ParmList* plist = Getattr(n,"parms");
299 + const int maxwidth = 50;
302 + for (p = plist; p; p = nextSibling(p)) {
303 + String* name = Getattr(p, "name");
304 + String* value = Getattr(p, "value");
307 + // add a comma to the previous one if any
310 + // Do we need to wrap a long line?
311 + if ((Len(doc) - lines*maxwidth) > maxwidth) {
312 + Printf(doc, "\n%s", tab4);
317 + // Do the param type too?
319 + SwigType* type = SwigType_base(Getattr(p, "type"));
320 + SwigType* qt = SwigType_typedef_resolve_all(type);
321 + if (SwigType_isenum(qt))
322 + type = NewString("int");
324 + lookup = Swig_symbol_clookup(type, 0);
326 + type = Getattr(lookup, "sym:name");
328 + Printf(doc, "%s ", type);
332 + Printf(doc, "%s", name);
337 + if (Strcmp(value, "NULL") == 0)
338 + value = NewString("None");
340 + lookup = Swig_symbol_clookup(value, 0);
342 + value = Getattr(lookup, "sym:name");
344 + Printf(doc, "=%s", value);
352 + /* ------------------------------------------------------------
353 + * have_pythonprepend()
354 + * Check if there is a %pythonprepend directive and it has text
355 + * ------------------------------------------------------------ */
357 + bool have_pythonprepend(Node *n) {
358 + String* str = Getattr(n, "feature:pythonprepend");
359 + return (str != NULL && Len(str) > 0);
362 + /* ------------------------------------------------------------
364 + * Get the %pythonprepend code, stripping off {} if neccessary
365 + * ------------------------------------------------------------ */
367 + String *pythonprepend(Node *n) {
368 + String* str = Getattr(n, "feature:pythonprepend");
369 + char* t = Char(str);
372 + Delitem(str,DOH_END);
377 + /* ------------------------------------------------------------
378 + * have_pythonappend()
379 + * Check if there is a %pythonappend directive and it has text
380 * ------------------------------------------------------------ */
382 - bool have_addtofunc(Node *n) {
383 - String* str = Getattr(n, "feature:addtofunc");
384 + bool have_pythonappend(Node *n) {
385 + String* str = Getattr(n, "feature:pythonappend");
386 return (str != NULL && Len(str) > 0);
389 /* ------------------------------------------------------------
391 - * Get the %addtofunc code, stripping off {} if neccessary
393 + * Get the %pythonappend code, stripping off {} if neccessary
394 * ------------------------------------------------------------ */
396 - String *addtofunc(Node *n) {
397 - String* str = Getattr(n, "feature:addtofunc");
398 + String *pythonappend(Node *n) {
399 + String* str = Getattr(n, "feature:pythonappend");
403 Delitem(str,DOH_END);
404 @@ -1731,9 +2035,11 @@
405 Printf(f_shadow, modern ? "(object)" : "(_object)");
408 Printf(f_shadow,":\n");
410 + if ( Getattr(n, "feature:docstring") ) // don't use have_docstring in this case because autodoc doesn't apply
411 + Printv(f_shadow, tab4, docstring(n, AUTODOC_CLASS, tab4), "\n", NIL);
414 Printv(f_shadow,tab4,"__swig_setmethods__ = {}\n",NIL);
415 if (Len(base_class)) {
416 Printf(f_shadow,"%sfor _s in [%s]: __swig_setmethods__.update(_s.__swig_setmethods__)\n",tab4,base_class);
417 @@ -1866,16 +2172,24 @@
419 Printv(f_shadow,pycode,"\n",NIL);
422 - Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "): ", NIL);
423 - if ( have_addtofunc(n) ) {
424 - Printv(f_shadow, "\n", NIL);
425 - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
426 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
427 - Printv(f_shadow, tab8, "return val\n", NIL);
428 + Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "):", NIL);
429 + if ( !have_pythonprepend(n) && !have_pythonappend(n) && !have_docstring(n)) {
430 + Printv(f_shadow, " return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
432 - Printv(f_shadow, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
433 + Printv(f_shadow, "\n", NIL);
434 + if ( have_docstring(n) )
435 + Printv(f_shadow, tab8, docstring(n, AUTODOC_METHOD, tab8), "\n", NIL);
436 + if ( have_pythonprepend(n) )
437 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
438 + if ( have_pythonappend(n) ) {
439 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
440 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
441 + Printv(f_shadow, tab8, "return val\n\n", NIL);
443 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n\n", NIL);
449 @@ -1890,14 +2204,22 @@
450 virtual int staticmemberfunctionHandler(Node *n) {
451 String *symname = Getattr(n,"sym:name");
452 Language::staticmemberfunctionHandler(n);
454 - if ( !classic && have_addtofunc(n) ) {
455 + if ( !classic && (have_pythonprepend(n) || have_pythonappend(n) || have_docstring(n)) ) {
456 int kw = (check_kwargs(n) && !Getattr(n,"sym:overloaded")) ? 1 : 0;
457 Printv(f_shadow, tab4, "def ", symname, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
458 - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
459 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
460 - Printv(f_shadow, tab8, "return val\n", NIL);
461 + if ( have_docstring(n) )
462 + Printv(f_shadow, tab8, docstring(n, AUTODOC_STATICFUNC, tab8), "\n", NIL);
463 + if ( have_pythonprepend(n) )
464 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
465 + if ( have_pythonappend(n) ) {
466 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
467 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
468 + Printv(f_shadow, tab8, "return val\n\n", NIL);
470 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n\n", NIL);
472 Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname,
473 " = staticmethod(", symname, ")\n", NIL);
476 @@ -1982,8 +2304,12 @@
479 Printv(f_shadow, tab4, "def __init__(self, *args",
480 (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
481 + if ( have_docstring(n) )
482 + Printv(f_shadow, tab8, docstring(n, AUTODOC_CTOR, tab8), "\n", NIL);
483 + if ( have_pythonprepend(n) )
484 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
485 Printv(f_shadow, pass_self, NIL);
487 Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', ",
488 funcCallHelper(Swig_name_construct(symname), allow_kwargs), ")\n", NIL);
489 @@ -1996,10 +2322,10 @@
490 Printv(f_shadow, tab8, "self.this = newobj.this\n", NIL);
491 Printv(f_shadow, tab8, "self.thisown = 1\n", NIL);
492 Printv(f_shadow, tab8, "del newobj.thisown\n", NIL);
494 - if ( have_addtofunc(n) )
495 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
496 + if ( have_pythonappend(n) )
497 + Printv(f_shadow, tab8, pythonappend(n), "\n\n", NIL);
500 have_constructor = 1;
502 @@ -2015,13 +2341,17 @@
505 Printv(f_shadow_stubs, "\ndef ", symname, "(*args",
506 (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
507 + if ( have_docstring(n) )
508 + Printv(f_shadow_stubs, tab4, docstring(n, AUTODOC_CTOR, tab4), "\n", NIL);
509 + if ( have_pythonprepend(n) )
510 + Printv(f_shadow_stubs, tab4, pythonprepend(n), "\n", NIL);
511 Printv(f_shadow_stubs, tab4, "val = ",
512 funcCallHelper(Swig_name_construct(symname), allow_kwargs), "\n", NIL);
513 Printv(f_shadow_stubs, tab4, "val.thisown = 1\n", NIL);
514 - if ( have_addtofunc(n) )
515 - Printv(f_shadow_stubs, tab4, addtofunc(n), "\n", NIL);
516 + if ( have_pythonappend(n) )
517 + Printv(f_shadow_stubs, tab4, pythonappend(n), "\n", NIL);
518 Printv(f_shadow_stubs, tab4, "return val\n", NIL);
522 @@ -2048,13 +2378,18 @@
524 Printv(f_shadow,pycode,"\n", NIL);
526 Printv(f_shadow, tab4, "def __del__(self, destroy=", module, ".", Swig_name_destroy(symname), "):\n", NIL);
527 - if ( have_addtofunc(n) )
528 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
529 + if ( have_docstring(n) )
530 + Printv(f_shadow, tab8, docstring(n, AUTODOC_DTOR, tab8), "\n", NIL);
531 + if ( have_pythonprepend(n) )
532 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
533 Printv(f_shadow, tab8, "try:\n", NIL);
534 - Printv(f_shadow, tab4, tab8, "if self.thisown: destroy(self)\n", NIL);
535 + Printv(f_shadow, tab8, tab4, "if self.thisown: destroy(self)\n", NIL);
536 Printv(f_shadow, tab8, "except: pass\n", NIL);
537 + if ( have_pythonappend(n) )
538 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
539 + Printv(f_shadow, "\n", NIL);