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 2 Jun 2004 01:38:46 -0000
10 static String *const_code = 0;
11 static String *shadow_methods = 0;
12 static String *module = 0;
13 +static String *package = 0;
14 static String *mainmodule = 0;
15 static String *interface = 0;
16 static String *global_name = 0;
17 static int shadow = 1;
19 static int have_constructor;
21 static String *real_classname;
23 +/* flags for the make_autodoc function */
33 static const char *usage = (char *)"\
34 Python Options (available with -python)\n\
35 -ldflags - Print runtime libraries to link with\n\
36 -globals <name> - Set <name> used to access C global variable [default: 'cvar']\n\
39 * use %module(directors="1") modulename at the start of the
40 * interface file to enable director generation.
42 + String* mod_docstring = NULL;
44 - Node *module = Getattr(n, "module");
46 - Node *options = Getattr(module, "options");
47 + Node *mod = Getattr(n, "module");
49 + Node *options = Getattr(mod, "options");
51 if (Getattr(options, "directors")) {
54 if (Getattr(options, "dirprot")) {
57 + mod_docstring = Getattr(options, "docstring");
58 + package = Getattr(options, "package");
65 "# This file is compatible with both classic and new-style classes.\n",
69 + if (mod_docstring && Len(mod_docstring)) {
70 + Printv(f_shadow, "\n\"\"\"\n", mod_docstring, "\n\"\"\"\n", NIL);
71 + Delete(mod_docstring); mod_docstring = NULL;
74 Printf(f_shadow,"\nimport %s\n\n", module);
78 virtual int importDirective(Node *n) {
80 String *modname = Getattr(n,"module");
82 - Printf(f_shadow,"import %s\n", modname);
83 + Printf(f_shadow,"import ");
85 + // Find the module node for this imported module. It should be the
86 + // first child but search just in case.
87 + Node* mod = firstChild(n);
88 + while (mod && Strcmp(nodeType(mod), "module") != 0)
89 + mod = nextSibling(mod);
91 + // Is the imported module in another package? (IOW, does it use the
92 + // %module(package="name") option and it's different than the package
94 + Node *options = Getattr(mod, "options");
95 + if (options && Getattr(options, "package")) {
96 + String* pkg = Getattr(options, "package");
97 + if (!package || Strcmp(pkg, package) != 0)
98 + Printf(f_shadow, "%s.", Getattr(options, "package"));
101 + // finally, output the name of the imported module
102 + Printf(f_shadow, "%s\n", modname);
105 return Language::importDirective(n);
107 @@ -417,17 +455,25 @@
109 * ------------------------------------------------------------ */
111 void emitFunctionShadowHelper(Node *n, File *f_dest, String *name, int kw) {
112 - if ( ! have_addtofunc(n) ) {
113 - /* If there is no addtofunc directive then just assign from the extension module */
114 + if ( !have_pythonprepend(n) && !have_pythonappend(n) && !have_docstring(n) ) {
115 + /* If there is no pythonappend or docstring directive then just assign from the extension module */
116 Printv(f_dest, "\n", name, " = ", module, ".", name, "\n", NIL);
118 /* Otherwise make a wrapper function to insert the code into */
119 Printv(f_dest, "\ndef ", name, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
120 - Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
121 - Printv(f_dest, tab4, addtofunc(n), "\n", NIL);
122 - Printv(f_dest, tab4, "return val\n", NIL);
123 + if ( have_docstring(n) )
124 + Printv(f_dest, tab4, docstring(n, AUTODOC_FUNC, tab4), "\n", NIL);
125 + if ( have_pythonprepend(n) )
126 + Printv(f_dest, tab4, pythonprepend(n), "\n", NIL);
127 + if ( have_pythonappend(n) ) {
128 + Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
129 + Printv(f_dest, tab4, pythonappend(n), "\n", NIL);
130 + Printv(f_dest, tab4, "return val\n", NIL);
132 + Printv(f_dest, tab4, "return ", funcCallHelper(name, kw), "\n", NIL);
138 @@ -441,24 +487,303 @@
142 /* ------------------------------------------------------------
144 - * Check if there is a %addtofunc directive and it has text
146 + * Check if there is a docstring directive and it has text,
147 + * or there is an autodoc flag set
148 + * ------------------------------------------------------------ */
150 + bool have_docstring(Node *n) {
151 + String* str = Getattr(n, "feature:docstring");
152 + return (str != NULL && Len(str) > 0) ||
153 + (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc"));
156 + /* ------------------------------------------------------------
158 + * Get the docstring text, stripping off {} if neccessary,
159 + * and enclose in triple double quotes. If autodoc is also
160 + * set then it will build a combined docstring.
161 + * ------------------------------------------------------------ */
163 + String *docstring(Node *n, autodoc_t ad_type, const String* indent) {
164 + String* str = Getattr(n, "feature:docstring");
165 + bool have_ds = (str != NULL && Len(str) > 0);
166 + bool have_auto = (Getattr(n,"feature:autodoc") && !Getattr(n, "feature:noautodoc"));
167 + char* triple_double = "\"\"\"";
168 + String* autodoc = NULL;
169 + String* doc = NULL;
172 + char* t = Char(str);
175 + Delitem(str,DOH_END);
180 + autodoc = make_autodoc(n, ad_type);
181 + have_auto = (autodoc != NULL && Len(autodoc) > 0);
184 + // If there is more than one line then make docstrings like this:
188 + // And here is line2 followed by the rest of them
191 + // otherwise, put it all on a single line
193 + if ( have_auto && have_ds ) { // Both autodoc and docstring are present
194 + doc = NewString("");
195 + Printv(doc, triple_double, "\n",
196 + pythoncode(autodoc, indent), "\n",
197 + pythoncode(str, indent),
198 + indent, triple_double, NIL);
200 + else if ( !have_auto && have_ds ) { // only docstring
201 + if (Strchr(str, '\n') == NULL) {
202 + doc = NewStringf("%s%s%s", triple_double, str, triple_double);
205 + doc = NewString("");
206 + Printv(doc, triple_double, "\n",
207 + pythoncode(str, indent),
208 + indent, triple_double, NIL);
211 + else if ( have_auto && !have_ds ) { // only autodoc
212 + if (Strchr(autodoc, '\n') == NULL) {
213 + doc = NewStringf("%s%s%s", triple_double, autodoc, triple_double);
216 + doc = NewString("");
217 + Printv(doc, triple_double, "\n",
218 + pythoncode(autodoc, indent),
219 + indent, triple_double, NIL);
223 + doc = NewString("");
225 + // Save the generated strings in the parse tree in case they are used later
226 + // by post processing tools
227 + Setattr(n, "python:docstring", doc);
228 + Setattr(n, "python:autodoc", autodoc);
233 + /* ------------------------------------------------------------
235 + * Build a docstring for the node, using parameter and other
236 + * info in the parse tree. If the value of the autodoc
237 + * attribute is "0" then do not include parameter types, if
238 + * it is "1" (the default) then do. If it has some other
239 + * value then assume it is supplied by the extension writer
240 + * and use it directly.
241 + * ------------------------------------------------------------ */
243 + String* make_autodoc(Node *n, autodoc_t ad_type) {
245 + if (ad_type == AUTODOC_CLASS)
246 + return NULL; // No function call to document in this case
248 + // If the function is overloaded then this funciton is called
249 + // for the last one. Rewind to the first so the docstrings are
251 + while ( Getattr(n, "sym:previousSibling") )
252 + n = Getattr(n, "sym:previousSibling");
254 + String* doc = NewString("");
256 + bool showTypes = false;
257 + bool skipAuto = false;
259 + // check how should the parameters be rendered?
260 + String* autodoc = Getattr(n, "feature:autodoc");
261 + if (Strcmp(autodoc, "0") == 0)
263 + else if (Strcmp(autodoc, "1") == 0)
266 + // if not "0" or "1" then autodoc is already the string that should be used
267 + Printf(doc, "%s", autodoc);
272 + String* symname = Getattr(n, "sym:name");
273 + SwigType* type = Getattr(n, "type");
276 + if (Strcmp(type, "void") == 0)
279 + SwigType* qt = SwigType_typedef_resolve_all(type);
280 + if (SwigType_isenum(qt))
281 + type = NewString("int");
283 + type = SwigType_base(type);
284 + Node* lookup = Swig_symbol_clookup(type, 0);
286 + type = Getattr(lookup, "sym:name");
291 + switch ( ad_type ) {
293 + if ( Strcmp(class_name, symname) == 0) {
294 + String* paramList = make_autodocParmList(n, showTypes);
295 + if (Len(paramList))
296 + Printf(doc, "__init__(self, %s) -> %s", paramList, class_name);
298 + Printf(doc, "__init__(self) -> %s", class_name);
301 + Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name);
305 + Printf(doc, "__del__(self)");
308 + case AUTODOC_STATICFUNC:
309 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
310 + if (type) Printf(doc, " -> %s", type);
314 + Printf(doc, "%s(%s)", symname, make_autodocParmList(n, showTypes));
315 + if (type) Printf(doc, " -> %s", type);
318 + case AUTODOC_METHOD:
319 + String* paramList = make_autodocParmList(n, showTypes);
320 + if (Len(paramList))
321 + Printf(doc, "%s(self, %s)", symname, paramList);
323 + Printf(doc, "%s(self)", symname);
324 + if (type) Printf(doc, " -> %s", type);
329 + // if it's overloaded then get the next decl and loop around again
330 + n = Getattr(n, "sym:nextSibling");
339 + String* make_autodocParmList(Node* n, bool showTypes) {
340 + String* doc = NewString("");
341 + ParmList* plist = Getattr(n,"parms");
345 + const int maxwidth = 50;
348 + for (p = plist; p; p = nextSibling(p)) {
349 + String* name = Getattr(p, "name");
350 + String* value = Getattr(p, "value");
353 + // add a comma to the previous one if any
356 + // Do we need to wrap a long line?
357 + if ((Len(doc) - lines*maxwidth) > maxwidth) {
358 + Printf(doc, "\n%s", tab4);
363 + // Do the param type too?
365 + SwigType* type = SwigType_base(Getattr(p, "type"));
366 + SwigType* qt = SwigType_typedef_resolve_all(type);
367 + if (SwigType_isenum(qt))
368 + type = NewString("int");
370 + lookup = Swig_symbol_clookup(type, 0);
372 + type = Getattr(lookup, "sym:name");
374 + Printf(doc, "%s ", type);
378 + Printf(doc, "%s", name);
383 + if (Strcmp(value, "NULL") == 0)
384 + value = NewString("None");
386 + lookup = Swig_symbol_clookup(value, 0);
388 + value = Getattr(lookup, "sym:name");
390 + Printf(doc, "=%s", value);
398 + /* ------------------------------------------------------------
399 + * have_pythonprepend()
400 + * Check if there is a %pythonprepend directive and it has text
401 + * ------------------------------------------------------------ */
403 + bool have_pythonprepend(Node *n) {
404 + String* str = Getattr(n, "feature:pythonprepend");
405 + return (str != NULL && Len(str) > 0);
408 + /* ------------------------------------------------------------
410 + * Get the %pythonprepend code, stripping off {} if neccessary
411 + * ------------------------------------------------------------ */
413 + String *pythonprepend(Node *n) {
414 + String* str = Getattr(n, "feature:pythonprepend");
415 + char* t = Char(str);
418 + Delitem(str,DOH_END);
423 + /* ------------------------------------------------------------
424 + * have_pythonappend()
425 + * Check if there is a %pythonappend directive and it has text
426 * ------------------------------------------------------------ */
428 - bool have_addtofunc(Node *n) {
429 - String* str = Getattr(n, "feature:addtofunc");
430 + bool have_pythonappend(Node *n) {
431 + String* str = Getattr(n, "feature:pythonappend");
432 return (str != NULL && Len(str) > 0);
435 /* ------------------------------------------------------------
437 - * Get the %addtofunc code, stripping off {} if neccessary
439 + * Get the %pythonappend code, stripping off {} if neccessary
440 * ------------------------------------------------------------ */
442 - String *addtofunc(Node *n) {
443 - String* str = Getattr(n, "feature:addtofunc");
444 + String *pythonappend(Node *n) {
445 + String* str = Getattr(n, "feature:pythonappend");
449 Delitem(str,DOH_END);
450 @@ -1657,9 +1982,18 @@
451 mod = Getattr(n,"module");
453 String *modname = Getattr(mod,"name");
454 if (Strcmp(modname,mainmodule) != 0) {
455 - importname = NewStringf("%s.%s", modname, Getattr(n,"sym:name"));
456 + // check if the module has a package option
457 + String* pkg = NULL;
458 + Node *options = Getattr(mod, "options");
459 + if (options && Getattr(options, "package"))
460 + pkg = Getattr(options, "package");
462 + if (!package || Strcmp(pkg, package) != 0)
463 + importname = NewStringf("%s.%s.%s", pkg, modname, Getattr(n,"sym:name"));
465 + importname = NewStringf("%s.%s", modname, Getattr(n,"sym:name"));
467 importname = NewString(Getattr(n,"sym:name"));
469 Setattr(n,"python:proxy",importname);
470 @@ -1731,9 +2065,11 @@
471 Printf(f_shadow, modern ? "(object)" : "(_object)");
474 Printf(f_shadow,":\n");
476 + if ( Getattr(n, "feature:docstring") ) // don't use have_docstring in this case because autodoc doesn't apply
477 + Printv(f_shadow, tab4, docstring(n, AUTODOC_CLASS, tab4), "\n", NIL);
480 Printv(f_shadow,tab4,"__swig_setmethods__ = {}\n",NIL);
481 if (Len(base_class)) {
482 Printf(f_shadow,"%sfor _s in [%s]: __swig_setmethods__.update(_s.__swig_setmethods__)\n",tab4,base_class);
483 @@ -1866,16 +2202,24 @@
485 Printv(f_shadow,pycode,"\n",NIL);
488 - Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "): ", NIL);
489 - if ( have_addtofunc(n) ) {
490 - Printv(f_shadow, "\n", NIL);
491 - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
492 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
493 - Printv(f_shadow, tab8, "return val\n", NIL);
494 + Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "):", NIL);
495 + if ( !have_pythonprepend(n) && !have_pythonappend(n) && !have_docstring(n)) {
496 + Printv(f_shadow, " return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
498 - Printv(f_shadow, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
499 + Printv(f_shadow, "\n", NIL);
500 + if ( have_docstring(n) )
501 + Printv(f_shadow, tab8, docstring(n, AUTODOC_METHOD, tab8), "\n", NIL);
502 + if ( have_pythonprepend(n) )
503 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
504 + if ( have_pythonappend(n) ) {
505 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
506 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
507 + Printv(f_shadow, tab8, "return val\n\n", NIL);
509 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n\n", NIL);
515 @@ -1890,14 +2234,22 @@
516 virtual int staticmemberfunctionHandler(Node *n) {
517 String *symname = Getattr(n,"sym:name");
518 Language::staticmemberfunctionHandler(n);
520 - if ( !classic && have_addtofunc(n) ) {
521 + if ( !classic && (have_pythonprepend(n) || have_pythonappend(n) || have_docstring(n)) ) {
522 int kw = (check_kwargs(n) && !Getattr(n,"sym:overloaded")) ? 1 : 0;
523 Printv(f_shadow, tab4, "def ", symname, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
524 - Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
525 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
526 - Printv(f_shadow, tab8, "return val\n", NIL);
527 + if ( have_docstring(n) )
528 + Printv(f_shadow, tab8, docstring(n, AUTODOC_STATICFUNC, tab8), "\n", NIL);
529 + if ( have_pythonprepend(n) )
530 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
531 + if ( have_pythonappend(n) ) {
532 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
533 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
534 + Printv(f_shadow, tab8, "return val\n\n", NIL);
536 + Printv(f_shadow, tab8, "return ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n\n", NIL);
538 Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname,
539 " = staticmethod(", symname, ")\n", NIL);
542 @@ -1982,8 +2334,12 @@
545 Printv(f_shadow, tab4, "def __init__(self, *args",
546 (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
547 + if ( have_docstring(n) )
548 + Printv(f_shadow, tab8, docstring(n, AUTODOC_CTOR, tab8), "\n", NIL);
549 + if ( have_pythonprepend(n) )
550 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
551 Printv(f_shadow, pass_self, NIL);
553 Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', ",
554 funcCallHelper(Swig_name_construct(symname), allow_kwargs), ")\n", NIL);
555 @@ -1996,10 +2352,10 @@
556 Printv(f_shadow, tab8, "self.this = newobj.this\n", NIL);
557 Printv(f_shadow, tab8, "self.thisown = 1\n", NIL);
558 Printv(f_shadow, tab8, "del newobj.thisown\n", NIL);
560 - if ( have_addtofunc(n) )
561 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
562 + if ( have_pythonappend(n) )
563 + Printv(f_shadow, tab8, pythonappend(n), "\n\n", NIL);
566 have_constructor = 1;
568 @@ -2015,13 +2371,17 @@
571 Printv(f_shadow_stubs, "\ndef ", symname, "(*args",
572 (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
573 + if ( have_docstring(n) )
574 + Printv(f_shadow_stubs, tab4, docstring(n, AUTODOC_CTOR, tab4), "\n", NIL);
575 + if ( have_pythonprepend(n) )
576 + Printv(f_shadow_stubs, tab4, pythonprepend(n), "\n", NIL);
577 Printv(f_shadow_stubs, tab4, "val = ",
578 funcCallHelper(Swig_name_construct(symname), allow_kwargs), "\n", NIL);
579 Printv(f_shadow_stubs, tab4, "val.thisown = 1\n", NIL);
580 - if ( have_addtofunc(n) )
581 - Printv(f_shadow_stubs, tab4, addtofunc(n), "\n", NIL);
582 + if ( have_pythonappend(n) )
583 + Printv(f_shadow_stubs, tab4, pythonappend(n), "\n", NIL);
584 Printv(f_shadow_stubs, tab4, "return val\n", NIL);
588 @@ -2048,13 +2408,18 @@
590 Printv(f_shadow,pycode,"\n", NIL);
592 Printv(f_shadow, tab4, "def __del__(self, destroy=", module, ".", Swig_name_destroy(symname), "):\n", NIL);
593 - if ( have_addtofunc(n) )
594 - Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
595 + if ( have_docstring(n) )
596 + Printv(f_shadow, tab8, docstring(n, AUTODOC_DTOR, tab8), "\n", NIL);
597 + if ( have_pythonprepend(n) )
598 + Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL);
599 Printv(f_shadow, tab8, "try:\n", NIL);
600 - Printv(f_shadow, tab4, tab8, "if self.thisown: destroy(self)\n", NIL);
601 + Printv(f_shadow, tab8, tab4, "if self.thisown: destroy(self)\n", NIL);
602 Printv(f_shadow, tab8, "except: pass\n", NIL);
603 + if ( have_pythonappend(n) )
604 + Printv(f_shadow, tab8, pythonappend(n), "\n", NIL);
605 + Printv(f_shadow, "\n", NIL);