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 22 Apr 2004 16:38:43 -0000
9 * Copyright (C) 1999-2000. The University of Chicago
10 * See the file LICENSE for information on usage and redistribution.
11 * ----------------------------------------------------------------------------- */
13 -char cvsroot_python_cxx[] = "$Header$";
14 +char cvsroot_python_cxx[] = "$Header$";
20 static int have_constructor;
22 static String *real_classname;
24 +/* flags for the make_autodoc function */
34 static const char *usage = (char *)"\
35 Python Options (available with -python)\n\
36 -ldflags - Print runtime libraries to link with\n\
37 -globals <name> - Set <name> used to access C global variable [default: 'cvar']\n\
40 * ------------------------------------------------------------ */
42 void emitFunctionShadowHelper(Node *n, File *f_dest, String *name, int kw) {
43 - if ( ! have_addtofunc(n) ) {
44 - /* If there is no addtofunc directive then just assign from the extension module */
45 + if ( !have_pythonprepend(n) && !have_pythonappend(n) && !have_docstring(n) ) {
46 + /* If there is no pythonappend or docstring directive then just assign from the extension module */
47 Printv(f_dest, "\n", name, " = ", module, ".", name, "\n", NIL);
49 /* Otherwise make a wrapper function to insert the code into */
50 Printv(f_dest, "\ndef ", name, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
51 - Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
52 - Printv(f_dest, tab4, addtofunc(n), "\n", NIL);
53 - Printv(f_dest, tab4, "return val\n", NIL);
54 + if ( have_docstring(n) )
55 + Printv(f_dest, tab4, docstring(n, AUTODOC_FUNC, tab4), "\n", NIL);
56 + if ( have_pythonprepend(n) )
57 + Printv(f_dest, tab4, pythonprepend(n), "\n", NIL);
58 + if ( have_pythonappend(n) ) {
59 + Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
60 + Printv(f_dest, tab4, pythonappend(n), "\n", NIL);
61 + Printv(f_dest, tab4, "return val\n", NIL);
63 + Printv(f_dest, tab4, "return ", funcCallHelper(name, kw), "\n", NIL);
69 @@ -441,24 +459,303 @@
73 /* ------------------------------------------------------------
75 - * Check if there is a %addtofunc directive and it has text
77 + * Check if there is a docstring directive and it has text,
78 + * or there is an autodoc flag set
79 + * ------------------------------------------------------------ */
81 + bool have_docstring(Node *n) {
82 + String* str = Getattr(n, "feature:docstring");
83 + return (str != NULL && Len(str) > 0) ||
84 + (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc"));
87 + /* ------------------------------------------------------------
89 + * Get the docstring text, stripping off {} if neccessary,
90 + * and enclose in triple double quotes. If autodoc is also
91 + * set then it will build a combined docstring.
92 + * ------------------------------------------------------------ */
94 + String *docstring(Node *n, autodoc_t ad_type, const String* indent) {
95 + String* str = Getattr(n, "feature:docstring");
96 + bool have_ds = (str != NULL && Len(str) > 0);
97 + bool have_auto = (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc"));
98 + char* triple_double = "\"\"\"";
99 + String* autodoc = NULL;
100 + String* doc = NULL;
103 + char* t = Char(str);
106 + Delitem(str,DOH_END);
111 + autodoc = make_autodoc(n, ad_type);
112 + have_auto = (autodoc != NULL && Len(autodoc) > 0);
115 + // If there is more than one line then make docstrings like this:
119 + // And here is line2 followed by the rest of them
122 + // otherwise, put it all on a single line
124 + if ( have_auto && have_ds ) { // Both autodoc and docstring are present
125 + doc = NewString("");
126 + Printv(doc, triple_double, "\n",
127 + pythoncode(autodoc, indent), "\n",
128 + pythoncode(str, indent),
129 + indent, triple_double, NIL);
131 + else if ( !have_auto && have_ds ) { // only docstring
132 + if (Strchr(str, '\n') == NULL) {
133 + doc = NewStringf("%s%s%s", triple_double, str, triple_double);
136 + doc = NewString("");
137 + Printv(doc, triple_double, "\n",
138 + pythoncode(str, indent),
139 + indent, triple_double, NIL);
142 + else if ( have_auto && !have_ds ) { // only autodoc
143 + if (Strchr(autodoc, '\n') == NULL) {
144 + doc = NewStringf("%s%s%s", triple_double, autodoc, triple_double);
147 + doc = NewString("");
148 + Printv(doc, triple_double, "\n",
149 + pythoncode(autodoc, indent),
150 + indent, triple_double, NIL);
154 + doc = NewString("");
156 + // Save the generated strings in the parse tree in case they are used later
157 + // by post processing tools
158 + Setattr(n, "python:docstring", doc);
159 + Setattr(n, "python:autodoc", autodoc);
164 + /* ------------------------------------------------------------
166 + * Build a docstring for the node, using parameter and other
167 + * info in the parse tree. If the value of the autodoc
168 + * attribute is "0" then do not include parameter types, if
169 + * it is "1" (the default) then do. If it has some other
170 + * value then assume it is supplied by the extension writer
171 + * and use it directly.
172 * ------------------------------------------------------------ */
174 - bool have_addtofunc(Node *n) {
175 - String* str = Getattr(n, "feature:addtofunc");
176 + String* make_autodoc(Node *n, autodoc_t ad_type) {
178 + if (ad_type == AUTODOC_CLASS)
179 + return NULL; // No function call to document in this case
181 + // If the function is overloaded then this funciton is called
182 + // for the last one. Rewind to the first so the docstrings are
184 + while ( Getattr(n, "sym:previousSibling") )
185 + n = Getattr(n, "sym:previousSibling");
187 + String* doc = NewString("");
189 + bool showTypes = false;
190 + bool skipAuto = false;
192 + // check how should the parameters be rendered?
193 + String* autodoc = Getattr(n, "feature:autodoc");
194 + if (Strcmp(autodoc, "0") == 0)
196 + else if (Strcmp(autodoc, "1") == 0)
199 + // if not "0" or "1" then autodoc is already the string that should be used
200 + Printf(doc, "%s", autodoc);
205 + String* symname = Getattr(n, "sym:name");
206 + SwigType* type = Getattr(n, "type");
209 + if (Strcmp(type, "void") == 0)
212 + SwigType* qt = SwigType_typedef_resolve_all(type);
213 + if (SwigType_isenum(qt))
214 + type = NewString("int");
216 + type = SwigType_base(type);
217 + Node* lookup = Swig_symbol_clookup(type, 0);
219 + type = Getattr(lookup, "sym:name");
224 + switch ( ad_type ) {
226 + if ( Strcmp(class_name, symname) == 0) {
227 + String* paramList = make_autodocParmList(n, showTypes);
228 + if (Len(paramList))
229 + Printf(doc, "__init__(self, %s) -> %s", paramList, class_name);
231 + Printf(doc, "__init__(self) -> %s", class_name);
234 + Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name);
238 + Printf(doc, "__del__(self)");
241 + case AUTODOC_STATICFUNC:
242 + Printf(doc, "%s.%s(%s)", class_name, symname, make_autodocParmList(n, showTypes));
243 + if (type) Printf(doc, " -> %s", type);
247 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
248 + if (type) Printf(doc, " -> %s", type);
251 + case AUTODOC_METHOD:
252 + String* paramList = make_autodocParmList(n, showTypes);
253 + if (Len(paramList))
254 + Printf(doc, "%s(self, %s)", symname, paramList);
256 + Printf(doc, "%s(self)", symname);
257 + if (type) Printf(doc, " -> %s", type);
262 + // if it's overloaded then get the next decl and loop around again
263 + n = Getattr(n, "sym:nextSibling");
272 + String* make_autodocParmList(Node* n, bool showTypes) {
273 + String* doc = NewString("");
274 + ParmList* plist = Getattr(n,"parms");
278 + const int maxwidth = 50;
281 + for (p = plist; p; p = nextSibling(p)) {
282 + String* name = Getattr(p, "name");
283 + String* value = Getattr(p, "value");
286 + // add a comma to the previous one if any
289 + // Do we need to wrap a long line?
290 + if ((Len(doc) - lines*maxwidth) > maxwidth) {
291 + Printf(doc, "\n%s", tab4);
296 + // Do the param type too?
298 + SwigType* type = SwigType_base(Getattr(p, "type"));
299 + SwigType* qt = SwigType_typedef_resolve_all(type);
300 + if (SwigType_isenum(qt))
301 + type = NewString("int");
303 + lookup = Swig_symbol_clookup(type, 0);
305 + type = Getattr(lookup, "sym:name");
307 + Printf(doc, "%s ", type);
311 + Printf(doc, "%s", name);
316 + if (Strcmp(value, "NULL") == 0)
317 + value = NewString("None");
319 + lookup = Swig_symbol_clookup(value, 0);
321 + value = Getattr(lookup, "sym:name");
323 + Printf(doc, "=%s", value);
331 + /* ------------------------------------------------------------
332 + * have_pythonprepend()
333 + * Check if there is a %pythonprepend directive and it has text
334 + * ------------------------------------------------------------ */
336 + bool have_pythonprepend(Node *n) {
337 + String* str = Getattr(n, "feature:pythonprepend");
338 + return (str != NULL && Len(str) > 0);
341 + /* ------------------------------------------------------------
343 + * Get the %pythonprepend code, stripping off {} if neccessary
344 + * ------------------------------------------------------------ */
346 + String *pythonprepend(Node *n) {
347 + String* str = Getattr(n, "feature:pythonprepend");
348 + char* t = Char(str);
351 + Delitem(str,DOH_END);
356 + /* ------------------------------------------------------------
357 + * have_pythonappend()
358 + * Check if there is a %pythonappend directive and it has text
359 + * ------------------------------------------------------------ */
361 + bool have_pythonappend(Node *n) {
362 + String* str = Getattr(n, "feature:pythonappend");
363 return (str != NULL && Len(str) > 0);
366 /* ------------------------------------------------------------
368 - * Get the %addtofunc code, stripping off {} if neccessary
370 + * Get the %pythonappend code, stripping off {} if neccessary
371 * ------------------------------------------------------------ */
373 - String *addtofunc(Node *n) {
374 - String* str = Getattr(n, "feature:addtofunc");
375 + String *pythonappend(Node *n) {
376 + String* str = Getattr(n, "feature:pythonappend");
380 Delitem(str,DOH_END);
381 @@ -1731,9 +2028,11 @@
382 Printf(f_shadow, modern ? "(object)" : "(_object)");
385 Printf(f_shadow,":\n");
387 + if ( Getattr(n, "feature:docstring") ) // don't use have_docstring in this case because autodoc doesn't apply
388 + Printv(f_shadow, tab4, docstring(n, AUTODOC_CLASS, tab4), "\n", NIL);
391 Printv(f_shadow,tab4,"__swig_setmethods__ = {}\n",NIL);
392 if (Len(base_class)) {
393 Printf(f_shadow,"%sfor _s in [%s]: __swig_setmethods__.update(_s.__swig_setmethods__)\n",tab4,base_class);
394 @@ -1866,16 +2165,24 @@
396 Printv(f_shadow,pycode,"\n",NIL);
399 - Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "): ", NIL);
400 - if ( have_addtofunc(n) ) {
401 - Printv(f_shadow, "\n", NIL);
402 - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
403 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
404 - Printv(f_shadow, tab8, "return val\n", NIL);
405 + Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "):", NIL);
406 + if ( !have_pythonprepend(n) && !have_pythonappend(n) && !have_docstring(n)) {
407 + Printv(f_shadow, " return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
409 - Printv(f_shadow, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
410 + Printv(f_shadow, "\n", NIL);
411 + if ( have_docstring(n) )
412 + Printv(f_shadow, tab8, docstring(n, AUTODOC_METHOD, tab8), "\n", NIL);
413 + if ( have_pythonprepend(n) )
414 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
415 + if ( have_pythonappend(n) ) {
416 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
417 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
418 + Printv(f_shadow, tab8, "return val\n\n", NIL);
420 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n\n", NIL);
426 @@ -1890,14 +2197,22 @@
427 virtual int staticmemberfunctionHandler(Node *n) {
428 String *symname = Getattr(n,"sym:name");
429 Language::staticmemberfunctionHandler(n);
431 - if ( !classic && have_addtofunc(n) ) {
432 + if ( !classic && (have_pythonprepend(n) || have_pythonappend(n) || have_docstring(n)) ) {
433 int kw = (check_kwargs(n) && !Getattr(n,"sym:overloaded")) ? 1 : 0;
434 Printv(f_shadow, tab4, "def ", symname, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
435 - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
436 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
437 - Printv(f_shadow, tab8, "return val\n", NIL);
438 + if ( have_docstring(n) )
439 + Printv(f_shadow, tab8, docstring(n, AUTODOC_STATICFUNC, tab8), "\n", NIL);
440 + if ( have_pythonprepend(n) )
441 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
442 + if ( have_pythonappend(n) ) {
443 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
444 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
445 + Printv(f_shadow, tab8, "return val\n\n", NIL);
447 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n\n", NIL);
449 Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname,
450 " = staticmethod(", symname, ")\n", NIL);
453 @@ -1982,8 +2297,12 @@
456 Printv(f_shadow, tab4, "def __init__(self, *args",
457 (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
458 + if ( have_docstring(n) )
459 + Printv(f_shadow, tab8, docstring(n, AUTODOC_CTOR, tab8), "\n", NIL);
460 + if ( have_pythonprepend(n) )
461 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
462 Printv(f_shadow, pass_self, NIL);
464 Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', ",
465 funcCallHelper(Swig_name_construct(symname), allow_kwargs), ")\n", NIL);
466 @@ -1996,10 +2315,10 @@
467 Printv(f_shadow, tab8, "self.this = newobj.this\n", NIL);
468 Printv(f_shadow, tab8, "self.thisown = 1\n", NIL);
469 Printv(f_shadow, tab8, "del newobj.thisown\n", NIL);
471 - if ( have_addtofunc(n) )
472 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
473 + if ( have_pythonappend(n) )
474 + Printv(f_shadow, tab8, pythonappend(n), "\n\n", NIL);
477 have_constructor = 1;
479 @@ -2015,13 +2334,17 @@
482 Printv(f_shadow_stubs, "\ndef ", symname, "(*args",
483 (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
484 + if ( have_docstring(n) )
485 + Printv(f_shadow_stubs, tab4, docstring(n, AUTODOC_CTOR, tab4), "\n", NIL);
486 + if ( have_pythonprepend(n) )
487 + Printv(f_shadow_stubs, tab4, pythonprepend(n), "\n", NIL);
488 Printv(f_shadow_stubs, tab4, "val = ",
489 funcCallHelper(Swig_name_construct(symname), allow_kwargs), "\n", NIL);
490 Printv(f_shadow_stubs, tab4, "val.thisown = 1\n", NIL);
491 - if ( have_addtofunc(n) )
492 - Printv(f_shadow_stubs, tab4, addtofunc(n), "\n", NIL);
493 + if ( have_pythonappend(n) )
494 + Printv(f_shadow_stubs, tab4, pythonappend(n), "\n", NIL);
495 Printv(f_shadow_stubs, tab4, "return val\n", NIL);
499 @@ -2048,13 +2371,18 @@
501 Printv(f_shadow,pycode,"\n", NIL);
503 Printv(f_shadow, tab4, "def __del__(self, destroy=", module, ".", Swig_name_destroy(symname), "):\n", NIL);
504 - if ( have_addtofunc(n) )
505 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
506 + if ( have_docstring(n) )
507 + Printv(f_shadow, tab8, docstring(n, AUTODOC_DTOR, tab8), "\n", NIL);
508 + if ( have_pythonprepend(n) )
509 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
510 Printv(f_shadow, tab8, "try:\n", NIL);
511 - Printv(f_shadow, tab4, tab8, "if self.thisown: destroy(self)\n", NIL);
512 + Printv(f_shadow, tab8, tab4, "if self.thisown: destroy(self)\n", NIL);
513 Printv(f_shadow, tab8, "except: pass\n", NIL);
514 + if ( have_pythonappend(n) )
515 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
516 + Printv(f_shadow, "\n", NIL);