1 Index: Source/Modules/python.cxx
2 ===================================================================
3 RCS file: /cvsroot/SWIG/Source/Modules/python.cxx,v
4 retrieving revision 1.25
5 diff -u -r1.25 python.cxx
6 --- Source/Modules/python.cxx 23 Sep 2003 21:26:04 -0000 1.25
7 +++ Source/Modules/python.cxx 30 Oct 2003 02:39:33 -0000
9 static String *shadow_indent = 0;
10 static int in_class = 0;
11 static int classic = 0;
12 +static int modern = 0;
13 +static int apply = 0;
14 +static int new_repr = 0;
16 /* C++ Support + Shadow Classes */
19 -interface <lib>- Set the lib name to <lib>\n\
20 -keyword - Use keyword arguments\n\
21 -classic - Use classic classes only\n\
22 + -modern - Use modern python features only, without compatibility code\n\
23 + -apply - Use apply() in proxy classes\n\
24 + -new_repr - Use more informative version of __repr__ in proxy classes\n\
25 -noexcept - No automatic exception handling\n\
26 -noproxy - Don't generate proxy classes \n\n";
29 } else if ((strcmp(argv[i],"-shadow") == 0) || ((strcmp(argv[i],"-proxy") == 0))) {
32 + } else if (strcmp(argv[i],"-apply") == 0) {
35 + } else if (strcmp(argv[i],"-new_repr") == 0) {
38 } else if ((strcmp(argv[i],"-noproxy") == 0)) {
42 } else if (strcmp(argv[i],"-classic") == 0) {
45 + } else if (strcmp(argv[i],"-modern") == 0) {
49 } else if (strcmp(argv[i],"-help") == 0) {
51 } else if (strcmp (argv[i], "-ldflags") == 0) {
54 "# This file was created automatically by SWIG.\n",
55 "# Don't modify this file, modify the SWIG interface instead.\n",
56 - "# This file is compatible with both classic and new-style classes.\n",
59 - Printf(f_shadow,"import %s\n", module);
61 - // Python-2.2 object hack
64 - "def _swig_setattr(self,class_type,name,value):\n",
65 - tab4, "if (name == \"this\"):\n",
66 - tab4, tab4, "if isinstance(value, class_type):\n",
67 - tab4, tab8, "self.__dict__[name] = value.this\n",
68 - tab4, tab8, "if hasattr(value,\"thisown\"): self.__dict__[\"thisown\"] = value.thisown\n",
69 - tab4, tab8, "del value.thisown\n",
70 - tab4, tab8, "return\n",
71 - // tab8, "if (name == \"this\") or (name == \"thisown\"): self.__dict__[name] = value; return\n",
72 - tab4, "method = class_type.__swig_setmethods__.get(name,None)\n",
73 - tab4, "if method: return method(self,value)\n",
74 - tab4, "self.__dict__[name] = value\n\n",
78 + "# This file is compatible with both classic and new-style classes.\n",
82 + Printf(f_shadow,"\nimport %s\n\n", module);
85 - "def _swig_getattr(self,class_type,name):\n",
86 - tab4, "method = class_type.__swig_getmethods__.get(name,None)\n",
87 - tab4, "if method: return method(self)\n",
88 - tab4, "raise AttributeError,name\n\n",
91 + // Python-2.2 object hack
93 + "def _swig_setattr(self,class_type,name,value):\n",
94 + tab4, "if (name == \"this\"):\n",
95 + tab4, tab4, "if isinstance(value, class_type):\n",
96 + tab4, tab8, "self.__dict__[name] = value.this\n",
97 + tab4, tab8, "if hasattr(value,\"thisown\"): self.__dict__[\"thisown\"] = value.thisown\n",
98 + tab4, tab8, "del value.thisown\n",
99 + tab4, tab8, "return\n",
100 + // tab8, "if (name == \"this\") or (name == \"thisown\"): self.__dict__[name] = value; return\n",
101 + tab4, "method = class_type.__swig_setmethods__.get(name,None)\n",
102 + tab4, "if method: return method(self,value)\n",
103 + tab4, "self.__dict__[name] = value\n\n",
110 - " _object = types.ObjectType\n",
111 - " _newclass = 1\n",
112 - "except AttributeError:\n",
113 - " class _object : pass\n",
114 - " _newclass = 0\n",
118 + "def _swig_getattr(self,class_type,name):\n",
119 + tab4, "method = class_type.__swig_getmethods__.get(name,None)\n",
120 + tab4, "if method: return method(self)\n",
121 + tab4, "raise AttributeError,name\n\n",
128 + " _object = types.ObjectType\n",
129 + " _newclass = 1\n",
130 + "except AttributeError:\n",
131 + " class _object : pass\n",
132 + " _newclass = 0\n",
140 if (directorsEnabled()) {
141 // Try loading weakref.proxy, which is only available in Python 2.1 and higher
144 return Language::importDirective(n);
148 + /* ------------------------------------------------------------
149 + * emitFuncCallHelper()
150 + * Write the shadow code to call a function in the extension
151 + * module. Takes into account the -apply flag and whether
152 + * to use keyword args or not.
153 + * ------------------------------------------------------------ */
155 + String *funcCallHelper(String *name, int kw) {
158 + str = NewString("");
160 + Printv(str, "apply(", module, ".", name, ", args", (kw ? ", kwargs" : ""), ")", NIL);
162 + Printv(str, module, ".", name, "(*args", (kw ? ", **kwargs" : ""), ")", NIL);
168 + /* ------------------------------------------------------------
169 + * emitFunctionShadowHelper()
170 + * Refactoring some common code out of functionWrapper and
171 + * dispatchFunction that writes the proxy code for non-member
173 + * ------------------------------------------------------------ */
175 + void emitFunctionShadowHelper(Node *n, File *f_dest, String *name, int kw) {
176 + if ( ! have_addtofunc(n) ) {
177 + /* If there is no addtofunc directive then just assign from the extension module */
178 + Printv(f_dest, "\n", name, " = ", module, ".", name, "\n", NIL);
180 + /* Otherwise make a wrapper function to insert the code into */
181 + Printv(f_dest, "\ndef ", name, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
182 + Printv(f_dest, tab4, "val = ", funcCallHelper(name, kw), "\n", NIL);
183 + Printv(f_dest, tab4, addtofunc(n), "\n", NIL);
184 + Printv(f_dest, tab4, "return val\n", NIL);
189 + /* ------------------------------------------------------------
191 + * check if using kwargs is allowed for this Node
192 + * ------------------------------------------------------------ */
194 + int check_kwargs(Node *n) {
195 + return ((use_kw || Getattr(n,"feature:kwargs")) && !Getattr(n, "feature:nokwargs")) ? 1 : 0;
199 + /* ------------------------------------------------------------
201 + * Check if there is a %addtofunc directive and it has text
202 + * ------------------------------------------------------------ */
204 + bool have_addtofunc(Node *n) {
205 + String* str = Getattr(n, "feature:addtofunc");
206 + return (str != NULL && Len(str) > 0);
209 + /* ------------------------------------------------------------
211 + * Get the %addtofunc code, stripping off {} if neccessary
212 + * ------------------------------------------------------------ */
214 + String *addtofunc(Node *n) {
215 + String* str = Getattr(n, "feature:addtofunc");
216 + char* t = Char(str);
219 + Delitem(str,DOH_END);
224 /* ------------------------------------------------------------
226 * ------------------------------------------------------------ */
231 - int allow_kwargs = (use_kw || Getattr(n,"feature:kwargs")) ? 1 : 0;
232 + int allow_kwargs = check_kwargs(n);
234 /* member of a director class? */
235 String *nodeType = Getattr(n, "nodeType");
236 @@ -1007,11 +1107,7 @@
238 /* Create a shadow for this function (if enabled and not in a member function) */
239 if ((shadow) && (!(shadow & PYSHADOW_MEMBER))) {
241 - Printv(f_shadow_stubs,iname, " = ", module, ".", iname, "\n\n", NIL);
243 - Printv(f_shadow,iname, " = ", module, ".", iname, "\n\n", NIL);
245 + emitFunctionShadowHelper(n, in_class ? f_shadow_stubs : f_shadow, iname, allow_kwargs);
248 if (!Getattr(n,"sym:nextSibling")) {
249 @@ -1068,7 +1164,7 @@
251 /* Create a shadow for this function (if enabled and not in a member function) */
252 if ((shadow) && (!(shadow & PYSHADOW_MEMBER))) {
253 - Printv(f_shadow_stubs,symname, " = ", module, ".", symname, "\n\n", NIL);
254 + emitFunctionShadowHelper(n, f_shadow_stubs, symname, 0);
258 @@ -1754,6 +1850,7 @@
260 virtual int classHandler(Node *n) {
261 int oldclassic = classic;
262 + int oldmodern = modern;
266 @@ -1763,8 +1860,16 @@
268 if (Getattr(n,"cplus:exceptionclass")) {
272 + if (Getattr(n,"feature:classic")) {
276 + if (Getattr(n,"feature:modern")) {
280 - if (Getattr(n,"feature:classic")) classic = 1;
282 shadow_indent = (String *) tab4;
284 @@ -1798,30 +1903,32 @@
285 Printf(f_shadow,"(%s)", base_class);
288 - Printf(f_shadow,"(_object)");
289 + Printf(f_shadow, modern ? "(object)" : "(_object)");
292 Printf(f_shadow,":\n");
294 - Printv(f_shadow,tab4,"__swig_setmethods__ = {}\n",NIL);
295 - if (Len(base_class)) {
296 - Printf(f_shadow,"%sfor _s in [%s]: __swig_setmethods__.update(_s.__swig_setmethods__)\n",tab4,base_class);
299 + Printv(f_shadow,tab4,"__swig_setmethods__ = {}\n",NIL);
300 + if (Len(base_class)) {
301 + Printf(f_shadow,"%sfor _s in [%s]: __swig_setmethods__.update(_s.__swig_setmethods__)\n",tab4,base_class);
305 - tab4, "__setattr__ = lambda self, name, value: _swig_setattr(self, ", class_name, ", name, value)\n",
308 + tab4, "__setattr__ = lambda self, name, value: _swig_setattr(self, ", class_name, ", name, value)\n",
311 - Printv(f_shadow,tab4,"__swig_getmethods__ = {}\n",NIL);
312 - if (Len(base_class)) {
313 - Printf(f_shadow,"%sfor _s in [%s]: __swig_getmethods__.update(_s.__swig_getmethods__)\n",tab4,base_class);
315 + Printv(f_shadow,tab4,"__swig_getmethods__ = {}\n",NIL);
316 + if (Len(base_class)) {
317 + Printf(f_shadow,"%sfor _s in [%s]: __swig_getmethods__.update(_s.__swig_getmethods__)\n",tab4,base_class);
321 - tab4, "__getattr__ = lambda self, name: _swig_getattr(self, ", class_name, ", name)\n",
324 + tab4, "__getattr__ = lambda self, name: _swig_getattr(self, ", class_name, ", name)\n",
330 /* Emit all of the members */
333 @@ -1853,21 +1960,37 @@
336 /* Supply a repr method for this class */
338 - tab4, "def __repr__(self):\n",
339 - tab8, "return \"<C ", class_name," instance at %s>\" % (self.this,)\n",
343 + tab4, "def __repr__(self):\n",
344 + tab8, "return \"<%s.%s; proxy of ", CPlusPlus ? "C++ " : "C ", real_classname," instance at %s>\" % (self.__class__.__module__, self.__class__.__name__, self.this,)\n",
349 + tab4, "def __repr__(self):\n",
350 + tab8, "return \"<C ", real_classname," instance at %s>\" % (self.this,)\n",
354 - /* Now build the real class with a normal constructor */
355 + /* Now the Ptr class */
357 "\nclass ", class_name, "Ptr(", class_name, "):\n",
358 - tab4, "def __init__(self,this):\n",
359 - tab8, "_swig_setattr(self, ", class_name, ", 'this', this)\n",
360 - tab8, "if not hasattr(self,\"thisown\"): _swig_setattr(self, ", class_name, ", 'thisown', 0)\n",
361 + tab4, "def __init__(self, this):\n", NIL);
364 + tab8, "_swig_setattr(self, ", class_name, ", 'this', this)\n",
365 + tab8, "if not hasattr(self,\"thisown\"): _swig_setattr(self, ", class_name, ", 'thisown', 0)\n",
366 + tab8, "_swig_setattr(self, ", class_name, ",self.__class__,", class_name, ")\n",
370 + tab8, "self.this = this\n",
371 + tab8, "if not hasattr(self,\"thisown\"): self.thisown = 0\n",
372 + tab8, "self.__class__ = ", class_name, "\n", NIL);
373 // tab8,"try: self.this = this.this; self.thisown = getattr(this,'thisown',0); this.thisown=0\n",
374 // tab8,"except AttributeError: self.this = this\n"
375 - tab8, "_swig_setattr(self, ", class_name, ",self.__class__,", class_name, ")\n",
379 Printf(f_shadow,"%s.%s_swigregister(%sPtr)\n", module, class_name, class_name,0);
381 @@ -1875,6 +1998,7 @@
382 Clear(f_shadow_stubs);
384 classic = oldclassic;
385 + modern = oldmodern;
389 @@ -1894,7 +2018,7 @@
391 if (!Getattr(n,"sym:nextSibling")) {
393 - int allow_kwargs = (use_kw || Getattr(n,"feature:kwargs")) ? 1 : 0;
394 + int allow_kwargs = (check_kwargs(n) && !Getattr(n,"sym:overloaded")) ? 1 : 0;
395 if (Strcmp(symname,"__repr__") == 0)
398 @@ -1902,14 +2026,18 @@
399 String *pycode = pythoncode(Getattr(n,"feature:shadow"),tab4);
400 Printv(f_shadow,pycode,"\n",NIL);
402 - if (allow_kwargs && !Getattr(n,"sym:overloaded")) {
403 - Printv(f_shadow,tab4, "def ", symname, "(*args, **kwargs): ", NIL);
404 - Printv(f_shadow, "return apply(", module, ".", Swig_name_member(class_name,symname), ",args, kwargs)\n", NIL);
406 - Printv(f_shadow, tab4, "def ", symname, "(*args): ", NIL);
407 - Printv(f_shadow, "return apply(", module, ".", Swig_name_member(class_name,symname), ",args)\n",NIL);
411 + Printv(f_shadow, tab4, "def ", symname, "(*args", (allow_kwargs ? ", **kwargs" : ""), "): ", NIL);
412 + if ( have_addtofunc(n) ) {
413 + Printv(f_shadow, "\n", NIL);
414 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
415 + Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
416 + Printv(f_shadow, tab8, "return val\n", NIL);
418 + Printv(f_shadow, "return ", funcCallHelper(Swig_name_member(class_name,symname), allow_kwargs), "\n", NIL);
425 @@ -1923,10 +2051,28 @@
426 String *symname = Getattr(n,"sym:name");
427 Language::staticmemberfunctionHandler(n);
429 - Printv(f_shadow, tab4, "__swig_getmethods__[\"", symname, "\"] = lambda x: ", module, ".", Swig_name_member(class_name, symname), "\n", NIL);
431 - Printv(f_shadow, tab4, "if _newclass:", symname, " = staticmethod(", module, ".",
432 - Swig_name_member(class_name, symname), ")\n", NIL);
433 + if ( !classic && have_addtofunc(n) ) {
434 + int kw = (check_kwargs(n) && !Getattr(n,"sym:overloaded")) ? 1 : 0;
435 + Printv(f_shadow, tab4, "def ", symname, "(*args", (kw ? ", **kwargs" : ""), "):\n", NIL);
436 + Printv(f_shadow, tab8, "val = ", funcCallHelper(Swig_name_member(class_name, symname), kw), "\n", NIL);
437 + Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
438 + Printv(f_shadow, tab8, "return val\n", NIL);
439 + Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname,
440 + " = staticmethod(", symname, ")\n", NIL);
443 + Printv(f_shadow, tab4, "__swig_getmethods__[\"", symname, "\"] = lambda x: ", symname, "\n", NIL);
448 + Printv(f_shadow, tab4, "__swig_getmethods__[\"", symname, "\"] = lambda x: ", module, ".", Swig_name_member(class_name, symname), "\n", NIL);
451 + Printv(f_shadow, tab4, modern ? "" : "if _newclass:", symname,
452 + " = staticmethod(", module, ".",
453 + Swig_name_member(class_name, symname), ")\n", NIL);
458 @@ -1972,7 +2118,7 @@
460 if (!Getattr(n,"sym:nextSibling")) {
462 - int allow_kwargs = (use_kw || Getattr(n,"feature:kwargs")) ? 1 : 0;
463 + int allow_kwargs = (check_kwargs(n) && (!Getattr(n,"sym:overloaded"))) ? 1 : 0;
464 if (!have_constructor) {
465 if (Getattr(n,"feature:shadow")) {
466 String *pycode = pythoncode(Getattr(n,"feature:shadow"),tab4);
467 @@ -1991,18 +2137,25 @@
468 tab8, tab4, "args = (self,) + args\n",
471 - if ((allow_kwargs) && (!Getattr(n,"sym:overloaded"))) {
472 - Printv(f_shadow, tab4, "def __init__(self,*args,**kwargs):\n", NIL);
473 - Printv(f_shadow, pass_self, NIL);
474 - Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', apply(", module, ".", Swig_name_construct(symname), ",args, kwargs))\n", NIL);
476 - Printv(f_shadow, tab4, "def __init__(self,*args):\n",NIL);
477 - Printv(f_shadow, pass_self, NIL);
478 - Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', apply(", module, ".", Swig_name_construct(symname), ",args))\n", NIL);
481 - tab8, "_swig_setattr(self, ", rclassname, ", 'thisown', 1)\n",
484 + Printv(f_shadow, tab4, "def __init__(self, *args",
485 + (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
486 + Printv(f_shadow, pass_self, NIL);
488 + Printv(f_shadow, tab8, "_swig_setattr(self, ", rclassname, ", 'this', ",
489 + funcCallHelper(Swig_name_construct(symname), allow_kwargs), ")\n", NIL);
491 + tab8, "_swig_setattr(self, ", rclassname, ", 'thisown', 1)\n",
494 + Printv(f_shadow, tab8, "newobj = ",
495 + funcCallHelper(Swig_name_construct(symname), allow_kwargs), "\n", NIL);
496 + Printv(f_shadow, tab8, "self.this = newobj.this\n", NIL);
497 + Printv(f_shadow, tab8, "self.thisown = 1\n", NIL);
498 + Printv(f_shadow, tab8, "del newobj.thisown\n", NIL);
500 + if ( have_addtofunc(n) )
501 + Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
504 have_constructor = 1;
505 @@ -2014,19 +2167,16 @@
506 String *pycode = pythoncode(Getattr(n,"feature:shadow"),"");
507 Printv(f_shadow_stubs,pycode,"\n",NIL);
509 - if ((allow_kwargs) && (!Getattr(n,"sym:overloaded")))
510 - Printv(f_shadow_stubs, "def ", symname, "(*args,**kwargs):\n", NIL);
512 - Printv(f_shadow_stubs, "def ", symname, "(*args):\n", NIL);
514 - Printv(f_shadow_stubs, tab4, "val = apply(", NIL);
515 - if ((allow_kwargs) && (!Getattr(n,"sym:overloaded")))
516 - Printv(f_shadow_stubs, module, ".", Swig_name_construct(symname), ",args,kwargs)\n", NIL);
518 - Printv(f_shadow_stubs, module, ".", Swig_name_construct(symname), ",args)\n", NIL);
519 - Printv(f_shadow_stubs,tab4, "val.thisown = 1\n",
520 - tab4, "return val\n\n", NIL);
523 + Printv(f_shadow_stubs, "\ndef ", symname, "(*args",
524 + (allow_kwargs ? ", **kwargs" : ""), "):\n", NIL);
525 + Printv(f_shadow_stubs, tab4, "val = ",
526 + funcCallHelper(Swig_name_construct(symname), allow_kwargs), "\n", NIL);
527 + Printv(f_shadow_stubs, tab4, "val.thisown = 1\n", NIL);
528 + if ( have_addtofunc(n) )
529 + Printv(f_shadow_stubs, tab4, addtofunc(n), "\n", NIL);
530 + Printv(f_shadow_stubs, tab4, "return val\n", NIL);
535 @@ -2045,7 +2195,9 @@
536 Language::destructorHandler(n);
539 - Printv(f_shadow, tab4, "def __del__(self, destroy= ", module, ".", Swig_name_destroy(symname), "):\n", NIL);
540 + Printv(f_shadow, tab4, "def __del__(self, destroy=", module, ".", Swig_name_destroy(symname), "):\n", NIL);
541 + if ( have_addtofunc(n) )
542 + Printv(f_shadow, tab8, addtofunc(n), "\n", NIL);
543 Printv(f_shadow, tab8, "try:\n", NIL);
544 Printv(f_shadow, tab4, tab8, "if self.thisown: destroy(self)\n", NIL);
545 Printv(f_shadow, tab8, "except: pass\n", NIL);
546 @@ -2066,21 +2218,22 @@
551 - if (!Getattr(n,"feature:immutable")) {
552 - Printv(f_shadow, tab4, "__swig_setmethods__[\"", symname, "\"] = ", module, ".", Swig_name_set(Swig_name_member(class_name,symname)), "\n", NIL);
555 + int immutable = (Getattr(n,"feature:immutable") != NULL);
558 + Printv(f_shadow, tab4, "__swig_setmethods__[\"", symname, "\"] = ", module, ".", Swig_name_set(Swig_name_member(class_name,symname)), "\n", NIL);
560 + Printv(f_shadow, tab4, "__swig_getmethods__[\"", symname, "\"] = ", module, ".", Swig_name_get(Swig_name_member(class_name,symname)),"\n", NIL);
562 - Printv(f_shadow, tab4, "__swig_getmethods__[\"", symname, "\"] = ", module, ".", Swig_name_get(Swig_name_member(class_name,symname)),"\n", NIL);
566 - Printv(f_shadow,tab4,"if _newclass:", symname," = property(", module, ".",
567 + Printv(f_shadow,tab4, modern ? "" : "if _newclass:",
568 + symname," = property(", module, ".",
569 Swig_name_get(Swig_name_member(class_name,symname)),")\n", NIL);
571 - Printv(f_shadow,tab4,"if _newclass:", symname," = property(",
572 - module, ".", Swig_name_get(Swig_name_member(class_name,symname)),",",
573 + Printv(f_shadow,tab4, modern ? "" : "if _newclass:",
574 + symname," = property(",
575 + module, ".", Swig_name_get(Swig_name_member(class_name,symname)),", ",
576 module, ".", Swig_name_set(Swig_name_member(class_name,symname)),")\n", NIL);
579 @@ -2142,8 +2295,9 @@
581 Delitem(temp,DOH_END);
584 /* Split the input text into lines */
585 - List *clist = DohSplit(temp,'\n',-1);
586 + List *clist = DohSplitLines(temp);
590 @@ -2194,7 +2348,7 @@
591 if ((!ImportMode) && ((Cmp(section,"python") == 0) || (Cmp(section,"shadow") == 0))) {
593 String *pycode = pythoncode(code,shadow_indent);
594 - Printv(f_shadow, pycode, "\n", NIL);
595 + Printv(f_shadow, pycode, NIL);