PageRenderTime 59ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/v-1-3-a5-release/SWIG/Source/Modules1.1/python.cxx

#
C++ | 1547 lines | 1213 code | 173 blank | 161 comment | 230 complexity | 36dd47d38357cdfd3b806fdd653da286 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * python.cxx
  3. *
  4. * Python module.
  5. *
  6. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  7. *
  8. * Copyright (C) 1999-2000. The University of Chicago
  9. * See the file LICENSE for information on usage and redistribution.
  10. * ----------------------------------------------------------------------------- */
  11. static char cvsroot[] = "$Header$";
  12. #include "mod11.h"
  13. #include "python.h"
  14. static String *const_code = 0;
  15. static String *shadow_methods = 0;
  16. static String *module = 0;
  17. static String *interface = 0;
  18. static String *global_name = 0;
  19. static int shadow = 0;
  20. static int have_defarg = 0;
  21. static int have_output;
  22. static int use_kw = 0;
  23. static int noopt = 1;
  24. static FILE *f_shadow;
  25. static Hash *hash;
  26. static Hash *symbols;
  27. static String *classes;
  28. static String *func;
  29. static String *vars;
  30. static String *pragma_include = 0;
  31. static String *import_file = 0;
  32. static String *methods;
  33. static char *class_name;
  34. static char *usage = (char *)"\
  35. Python Options (available with -python)\n\
  36. -globals name - Set name used to access C global variable ('cvar' by default).\n\
  37. -module name - Set module name\n\
  38. -interface name - Set the lib name\n\
  39. -keyword - Use keyword arguments\n\
  40. -noopt - No optimized shadows (pre 1.5.2)\n\
  41. -opt - Optimized shadow classes (1.5.2 or later)\n\
  42. -shadow - Generate shadow classes. \n\n";
  43. /* Test to see if a type corresponds to something wrapped with a shadow class */
  44. static DOH *is_shadow(SwigType *t) {
  45. DOH *r;
  46. SwigType *lt = Swig_clocal_type(t);
  47. r = Getattr(hash,lt);
  48. Delete(lt);
  49. return r;
  50. }
  51. /* -----------------------------------------------------------------------------
  52. * PYTHON::parse_args()
  53. * ----------------------------------------------------------------------------- */
  54. void
  55. PYTHON::parse_args(int argc, char *argv[]) {
  56. int i;
  57. strcpy(LibDir,"python");
  58. for (i = 1; i < argc; i++) {
  59. if (argv[i]) {
  60. if(strcmp(argv[i],"-module") == 0) {
  61. if (argv[i+1]) {
  62. module = NewString(argv[i+1]);
  63. Swig_mark_arg(i);
  64. Swig_mark_arg(i+1);
  65. i++;
  66. } else {
  67. Swig_arg_error();
  68. }
  69. /* Added edz@bsn.com */
  70. } else if(strcmp(argv[i],"-interface") == 0) {
  71. if (argv[i+1]) {
  72. interface = NewString(argv[i+1]);
  73. Swig_mark_arg(i);
  74. Swig_mark_arg(i+1);
  75. i++;
  76. } else {
  77. Swig_arg_error();
  78. }
  79. /* end added */
  80. } else if (strcmp(argv[i],"-globals") == 0) {
  81. if (argv[i+1]) {
  82. global_name = NewString(argv[i+1]);
  83. Swig_mark_arg(i);
  84. Swig_mark_arg(i+1);
  85. i++;
  86. } else {
  87. Swig_arg_error();
  88. }
  89. } else if (strcmp(argv[i],"-shadow") == 0) {
  90. shadow = 1;
  91. Swig_mark_arg(i);
  92. } else if (strcmp(argv[i],"-noopt") == 0) {
  93. noopt = 1;
  94. Swig_mark_arg(i);
  95. } else if (strcmp(argv[i],"-opt") == 0) {
  96. noopt = 0;
  97. Swig_mark_arg(i);
  98. } else if (strcmp(argv[i],"-keyword") == 0) {
  99. use_kw = 1;
  100. Swig_mark_arg(i);
  101. } else if (strcmp(argv[i],"-help") == 0) {
  102. fputs(usage,stderr);
  103. }
  104. }
  105. }
  106. if (!global_name) global_name = NewString("cvar");
  107. Preprocessor_define((void *) "SWIGPYTHON", 0);
  108. typemap_lang = (char*)"python";
  109. }
  110. /* -----------------------------------------------------------------------------
  111. * PYTHON::parse()
  112. * ----------------------------------------------------------------------------- */
  113. void
  114. PYTHON::parse() {
  115. hash = NewHash();
  116. symbols = NewHash();
  117. const_code = NewString("");
  118. shadow_methods = NewString("");
  119. classes = NewString("");
  120. func = NewString("");
  121. vars = NewString("");
  122. pragma_include = NewString("");
  123. methods = NewString("");
  124. Swig_banner(f_runtime);
  125. Printf(f_runtime,"#define SWIGPYTHON\n");
  126. if (NoInclude)
  127. Printf(f_runtime,"#define SWIG_NOINCLUDE\n");
  128. if (Swig_insert_file("common.swg", f_runtime) == -1) {
  129. Printf(stderr,"SWIG : Fatal error. Unable to locate common.swg. (Possible installation problem).\n");
  130. SWIG_exit (EXIT_FAILURE);
  131. }
  132. if (Swig_insert_file("python.swg", f_runtime) == -1) {
  133. Printf(stderr,"SWIG : Fatal error. Unable to locate python.swg. (Possible installation problem).\n");
  134. SWIG_exit (EXIT_FAILURE);
  135. }
  136. yyparse();
  137. }
  138. /* -----------------------------------------------------------------------------
  139. * PYTHON::set_module()
  140. * ----------------------------------------------------------------------------- */
  141. void
  142. PYTHON::set_module(char *mod_name) {
  143. /* If an "import" method has been set and we're in shadow class mode,
  144. output a python command to load the module */
  145. if (import_file) {
  146. if (!(strcmp(Char(import_file),input_file+strlen(input_file)-strlen(Char(import_file))))) {
  147. if (shadow) {
  148. Printf(f_shadow,"\nfrom %s import *\n", mod_name);
  149. }
  150. Delete(import_file);
  151. import_file = 0;
  152. }
  153. }
  154. if (module) return;
  155. module = NewString(mod_name);
  156. }
  157. /* -----------------------------------------------------------------------------
  158. * PYTHON::import(char *filename)
  159. * ----------------------------------------------------------------------------- */
  160. void
  161. PYTHON::import(char *filename) {
  162. if (import_file) Delete(import_file);
  163. import_file = NewString(filename);
  164. }
  165. /* -----------------------------------------------------------------------------
  166. * PYTHON::add_method()
  167. * ----------------------------------------------------------------------------- */
  168. void
  169. PYTHON::add_method(char *name, char *function, int kw) {
  170. if (!kw)
  171. Printf(methods,"\t { \"%s\", %s, METH_VARARGS },\n", name, function);
  172. else
  173. Printf(methods,"\t { \"%s\", (PyCFunction) %s, METH_VARARGS | METH_KEYWORDS },\n", name, function);
  174. }
  175. /* -----------------------------------------------------------------------------
  176. * PYTHON::initialize()
  177. * ----------------------------------------------------------------------------- */
  178. void
  179. PYTHON::initialize(void) {
  180. char filen[256];
  181. if (!module) {
  182. Printf(stderr,"*** Error. No module name specified.\n");
  183. SWIG_exit (EXIT_FAILURE);
  184. }
  185. /* If shadow classing is enabled, we're going to change the module name to "modulec" */
  186. if (shadow) {
  187. sprintf(filen,"%s%s.py", output_dir, Char(module));
  188. // If we don't have an interface then change the module name X to Xc
  189. if (! interface)
  190. Append(module,"c");
  191. if ((f_shadow = fopen(filen,"w")) == 0) {
  192. Printf(stderr,"Unable to open %s\n", filen);
  193. SWIG_exit (EXIT_FAILURE);
  194. }
  195. Printf(f_shadow,"# This file was created automatically by SWIG.\n");
  196. Printf(f_shadow,"import %s\n", interface ? interface : module);
  197. // Include some information in the code
  198. Printf(f_header,"\n/*-----------------------------------------------\n @(target):= %s.so\n\
  199. ------------------------------------------------*/\n", interface ? interface : module);
  200. if (!noopt)
  201. Printf(f_shadow,"import new\n");
  202. }
  203. Printf(f_header,"#define SWIG_init init%s\n\n", module);
  204. Printf(f_header,"#define SWIG_name \"%s\"\n", module);
  205. /* Output the start of the init function. */
  206. Printf(f_init,"static PyObject *SWIG_globals;\n");
  207. Printf(f_init,"#ifdef __cplusplus\n");
  208. Printf(f_init,"extern \"C\" \n");
  209. Printf(f_init,"#endif\n");
  210. Printf(f_init,"SWIGEXPORT(void) init%s(void) {\n",module);
  211. Printf(f_init,"PyObject *m, *d;\n");
  212. Printf(f_init,"int i;\n");
  213. Printf(f_init,"SWIG_globals = SWIG_newvarlink();\n");
  214. Printf(f_init,"m = Py_InitModule(\"%s\", %sMethods);\n", module, module);
  215. Printf(f_init,"d = PyModule_GetDict(m);\n");
  216. Printv(f_init,
  217. "for (i = 0; swig_types_initial[i]; i++) {\n",
  218. "swig_types[i] = SWIG_TypeRegister(swig_types_initial[i]);\n",
  219. "}\n",
  220. 0);
  221. Printf(f_wrappers,"#ifdef __cplusplus\n");
  222. Printf(f_wrappers,"extern \"C\" {\n");
  223. Printf(f_wrappers,"#endif\n");
  224. Printf(const_code,"static swig_const_info swig_const_table[] = {\n");
  225. Printf(methods,"static PyMethodDef %sMethods[] = {\n", module);
  226. }
  227. /* -----------------------------------------------------------------------------
  228. * PYTHON::close()
  229. * ----------------------------------------------------------------------------- */
  230. void
  231. PYTHON::close(void) {
  232. Printf(methods,"\t { NULL, NULL }\n");
  233. Printf(methods,"};\n");
  234. Printf(f_wrappers,"%s\n",methods);
  235. Printf(f_wrappers,"#ifdef __cplusplus\n");
  236. Printf(f_wrappers,"}\n");
  237. Printf(f_wrappers,"#endif\n");
  238. SwigType_emit_type_table(f_runtime,f_wrappers);
  239. Printf(const_code, "{0}};\n");
  240. Printf(f_wrappers,"%s\n",const_code);
  241. Printv(f_init, "SWIG_InstallConstants(d,swig_const_table);\n", 0);
  242. Printf(f_init,"}\n");
  243. if (shadow) {
  244. Printv(f_shadow,
  245. classes,
  246. "\n\n#-------------- FUNCTION WRAPPERS ------------------\n\n",
  247. func,
  248. "\n\n#-------------- VARIABLE WRAPPERS ------------------\n\n",
  249. vars,
  250. 0);
  251. if (Len(pragma_include) > 0) {
  252. Printv(f_shadow,
  253. "\n\n#-------------- USER INCLUDE -----------------------\n\n",
  254. pragma_include,
  255. 0);
  256. }
  257. fclose(f_shadow);
  258. }
  259. }
  260. /* -----------------------------------------------------------------------------
  261. * PYTHON::get_pointer()
  262. * ----------------------------------------------------------------------------- */
  263. void
  264. PYTHON::get_pointer(char *src, char *dest, SwigType *t, String *f, char *ret) {
  265. SwigType *lt;
  266. SwigType_remember(t);
  267. Printv(f,tab4, "if ((SWIG_ConvertPtr(", src, ",(void **) &", dest, ",", 0);
  268. lt = Swig_clocal_type(t);
  269. if (Cmp(lt,"p.void") == 0) {
  270. Printv(f, "0,1)) == -1) return ", ret, ";\n", 0);
  271. }
  272. /* if (SwigType_type(t) == T_VOID) Printv(f, "0,1)) == -1) return ", ret, ";\n", 0);*/
  273. else {
  274. Printv(f,"SWIGTYPE", SwigType_manglestr(t), ",1)) == -1) return ", ret, ";\n", 0);
  275. }
  276. Delete(lt);
  277. }
  278. /* -----------------------------------------------------------------------------
  279. * PYTHON::create_command()
  280. * ----------------------------------------------------------------------------- */
  281. void
  282. PYTHON::create_command(char *cname, char *iname) {
  283. add_method(iname, Char(Swig_name_wrapper(cname)), use_kw);
  284. }
  285. /* -----------------------------------------------------------------------------
  286. * PYTHON::create_function()
  287. * ----------------------------------------------------------------------------- */
  288. void
  289. PYTHON::create_function(char *name, char *iname, SwigType *d, ParmList *l) {
  290. Parm *p;
  291. int pcount,i,j;
  292. char wname[256];
  293. char source[64], target[64], argnum[20];
  294. char *usage = 0;
  295. Wrapper *f;
  296. String *parse_args;
  297. String *arglist;
  298. String *get_pointers;
  299. String *cleanup;
  300. String *outarg;
  301. String *check;
  302. String *kwargs;
  303. char *tm;
  304. int numopt = 0;
  305. f = NewWrapper();
  306. parse_args = NewString("");
  307. arglist = NewString("");
  308. get_pointers = NewString("");
  309. cleanup = NewString("");
  310. outarg = NewString("");
  311. check = NewString("");
  312. kwargs = NewString("");
  313. have_output = 0;
  314. strcpy(wname,Char(Swig_name_wrapper(iname)));
  315. if (!use_kw) {
  316. Printv(f->def,
  317. "static PyObject *", wname,
  318. "(PyObject *self, PyObject *args) {",
  319. 0);
  320. } else {
  321. Printv(f->def,
  322. "static PyObject *", wname,
  323. "(PyObject *self, PyObject *args, PyObject *kwargs) {",
  324. 0);
  325. }
  326. Wrapper_add_local(f,"resultobj", "PyObject *resultobj");
  327. /* Get the function usage string for later use */
  328. usage = usage_func(iname,d,l);
  329. /* Write code to extract function parameters. */
  330. pcount = emit_args(d, l, f);
  331. if (!use_kw) {
  332. Printf(parse_args," if(!PyArg_ParseTuple(args,\"");
  333. } else {
  334. Printf(parse_args," if(!PyArg_ParseTupleAndKeywords(args,kwargs,\"");
  335. Printf(arglist,",kwnames");
  336. }
  337. i = 0;
  338. j = 0;
  339. numopt = check_numopt(l);
  340. if (numopt) have_defarg = 1;
  341. p = l;
  342. Printf(kwargs,"{ ");
  343. while (p != 0) {
  344. SwigType *pt = Gettype(p);
  345. String *pn = Getname(p);
  346. String *pv = Getvalue(p);
  347. sprintf(source,"obj%d",i);
  348. sprintf(target,Char(Getlname(p)));
  349. sprintf(argnum,"%d",j+1);
  350. if (!Getignore(p)) {
  351. Putc(',',arglist);
  352. if (j == pcount-numopt) Putc('|',parse_args); /* Optional argument separator */
  353. if (Len(pn)) {
  354. Printf(kwargs,"\"%s\",", pn);
  355. } else {
  356. Printf(kwargs,"\"arg%d\",", j+1);
  357. }
  358. if ((tm = Swig_typemap_lookup((char*)"in",pt,pn,source,target,f))) {
  359. Putc('O',parse_args);
  360. Wrapper_add_localv(f, source, "PyObject *",source, " = 0", 0);
  361. Printf(arglist,"&%s",source);
  362. if (i >= (pcount-numopt))
  363. Printv(get_pointers, tab4, "if (", source, ")\n", 0);
  364. Printv(get_pointers,tm,"\n", 0);
  365. Replace(get_pointers,"$argnum", argnum, DOH_REPLACE_ANY);
  366. Replace(get_pointers,"$arg",source, DOH_REPLACE_ANY);
  367. } else {
  368. int noarg = 0;
  369. switch(SwigType_type(pt)) {
  370. case T_INT : case T_UINT:
  371. Putc('i',parse_args);
  372. break;
  373. case T_SHORT: case T_USHORT:
  374. Putc('h',parse_args);
  375. break;
  376. case T_LONG : case T_ULONG:
  377. Putc('l',parse_args);
  378. break;
  379. case T_SCHAR : case T_UCHAR :
  380. Putc('b',parse_args);
  381. break;
  382. case T_CHAR:
  383. Putc('c',parse_args);
  384. break;
  385. case T_FLOAT :
  386. Putc('f',parse_args);
  387. break;
  388. case T_DOUBLE:
  389. Putc('d',parse_args);
  390. break;
  391. case T_BOOL:
  392. {
  393. char tempb[128];
  394. char tempval[128];
  395. if (pv) {
  396. sprintf(tempval, "(int) %s", Char(pv));
  397. }
  398. sprintf(tempb,"tempbool%d",i);
  399. Putc('i',parse_args);
  400. if (!pv)
  401. Wrapper_add_localv(f,tempb,"int",tempb,0);
  402. else
  403. Wrapper_add_localv(f,tempb,"int",tempb, "=",tempval,0);
  404. Printv(get_pointers, tab4, target, " = (", SwigType_lstr(pt,0), ") ", tempb, ";\n", 0);
  405. Printf(arglist,"&%s",tempb);
  406. noarg = 1;
  407. }
  408. break;
  409. case T_VOID :
  410. noarg = 1;
  411. break;
  412. case T_USER:
  413. Putc('O',parse_args);
  414. sprintf(source,"argo%d", i);
  415. sprintf(target,Char(Getlname(p)));
  416. Wrapper_add_localv(f,source,"PyObject *",source,"=0",0);
  417. Printf(arglist,"&%s",source);
  418. SwigType_add_pointer(pt);
  419. get_pointer(source, target, pt, get_pointers, (char*)"NULL");
  420. SwigType_del_pointer(pt);
  421. noarg = 1;
  422. break;
  423. case T_STRING:
  424. Putc('s',parse_args);
  425. Printf(arglist,"&%s", Getlname(p));
  426. noarg = 1;
  427. break;
  428. case T_POINTER: case T_ARRAY: case T_REFERENCE:
  429. /* Have some sort of pointer variable. Create a temporary local
  430. variable for the string and read the pointer value into it. */
  431. Putc('O',parse_args);
  432. sprintf(source,"argo%d", i);
  433. sprintf(target,"%s",Char(Getlname(p)));
  434. Wrapper_add_localv(f,source,"PyObject *",source,"=0",0);
  435. Printf(arglist,"&%s",source);
  436. get_pointer(source, target, pt, get_pointers, (char*)"NULL");
  437. noarg = 1;
  438. break;
  439. default :
  440. Printf(stderr,"%s : Line %d. Unable to use type %s as a function argument.\n",input_file, line_number, SwigType_str(pt,0));
  441. break;
  442. }
  443. if (!noarg)
  444. Printf(arglist,"&%s",Getlname(p));
  445. }
  446. j++;
  447. }
  448. /* Check if there was any constraint code */
  449. if ((tm = Swig_typemap_lookup((char*)"check",pt,pn,source,target,0))) {
  450. Printf(check,"%s\n",tm);
  451. Replace(check,"$argnum", argnum, DOH_REPLACE_ANY);
  452. }
  453. /* Check if there was any cleanup code */
  454. if ((tm = Swig_typemap_lookup((char*)"freearg",pt,pn,target,source,0))) {
  455. Printf(cleanup,"%s\n",tm);
  456. Replace(cleanup,"$argnum", argnum, DOH_REPLACE_ANY);
  457. Replace(cleanup,"$arg",source, DOH_REPLACE_ANY);
  458. }
  459. /* Check for output arguments */
  460. if ((tm = Swig_typemap_lookup((char*)"argout",pt,pn,target,(char*)"resultobj",0))) {
  461. Printf(outarg,"%s\n", tm);
  462. Replace(outarg,"$argnum",argnum,DOH_REPLACE_ANY);
  463. Replace(outarg,"$arg",source, DOH_REPLACE_ANY);
  464. have_output++;
  465. }
  466. p = Getnext(p);
  467. i++;
  468. }
  469. Printf(kwargs," NULL }");
  470. if (use_kw) {
  471. Printv(f->locals,tab4, "char *kwnames[] = ", kwargs, ";\n", 0);
  472. }
  473. Printf(parse_args,":%s\"", iname);
  474. Printv(parse_args,
  475. arglist, ")) return NULL;\n",
  476. 0);
  477. /* Now slap the whole first part of the wrapper function together */
  478. Printv(f->code, parse_args, get_pointers, check, 0);
  479. /* Emit the function call */
  480. emit_func_call(name,d,l,f);
  481. /* Return the function value */
  482. if ((tm = Swig_typemap_lookup((char*)"out",d,iname,(char*)"result",(char*)"resultobj",0))) {
  483. Printf(f->code,"%s\n", tm);
  484. } else {
  485. switch(SwigType_type(d)) {
  486. case T_INT: case T_UINT: case T_BOOL:
  487. case T_SHORT: case T_USHORT:
  488. case T_LONG : case T_ULONG:
  489. case T_SCHAR: case T_UCHAR :
  490. Printf(f->code," resultobj = PyInt_FromLong((long)result);\n");
  491. break;
  492. case T_DOUBLE :
  493. case T_FLOAT :
  494. Printf(f->code," resultobj = PyFloat_FromDouble(result);\n");
  495. break;
  496. case T_CHAR :
  497. Printf(f->code," resultobj = Py_BuildValue(\"c\",result);\n");
  498. break;
  499. case T_USER :
  500. SwigType_add_pointer(d);
  501. SwigType_remember(d);
  502. Printv(f->code,tab4, "resultobj = SWIG_NewPointerObj((void *)result, SWIGTYPE", SwigType_manglestr(d), ");\n",0);
  503. SwigType_del_pointer(d);
  504. break;
  505. case T_STRING:
  506. Printf(f->code," resultobj = PyString_FromString(result);\n");
  507. break;
  508. case T_POINTER: case T_ARRAY: case T_REFERENCE:
  509. SwigType_remember(d);
  510. Printv(f->code, tab4, "resultobj = SWIG_NewPointerObj((void *) result, SWIGTYPE", SwigType_manglestr(d), ");\n", 0);
  511. break;
  512. case T_VOID:
  513. Printf(f->code," Py_INCREF(Py_None);\n");
  514. Printf(f->code," resultobj = Py_None;\n");
  515. break;
  516. default :
  517. Printf(stderr,"%s: Line %d. Unable to use return type %s in function %s.\n", input_file, line_number, SwigType_str(d,0), name);
  518. break;
  519. }
  520. }
  521. /* Output argument output code */
  522. Printv(f->code,outarg,0);
  523. /* Output cleanup code */
  524. Printv(f->code,cleanup,0);
  525. /* Look to see if there is any newfree cleanup code */
  526. if (NewObject) {
  527. if ((tm = Swig_typemap_lookup((char*)"newfree",d,iname,(char*)"result",(char*)"",0))) {
  528. Printf(f->code,"%s\n",tm);
  529. }
  530. }
  531. /* See if there is any return cleanup code */
  532. if ((tm = Swig_typemap_lookup((char*)"ret",d,iname,(char*)"result",(char*)"",0))) {
  533. Printf(f->code,"%s\n",tm);
  534. }
  535. Printf(f->code," return resultobj;\n}\n");
  536. /* Substitute the cleanup code */
  537. Replace(f->code,"$cleanup",cleanup, DOH_REPLACE_ANY);
  538. /* Substitute the function name */
  539. Replace(f->code,"$name",iname, DOH_REPLACE_ANY);
  540. /* Dump the function out */
  541. Wrapper_print(f,f_wrappers);
  542. /* Now register the function with the interpreter. */
  543. add_method(iname, wname, use_kw);
  544. /* Create a shadow for this function (if enabled and not in a member function) */
  545. if ((shadow) && (!(shadow & PYSHADOW_MEMBER))) {
  546. int need_wrapper = 0;
  547. int munge_return = 0;
  548. /* Check return code for modification */
  549. if (is_shadow(d)) {
  550. need_wrapper = 1;
  551. munge_return = 1;
  552. }
  553. /* If no modification is needed. We're just going to play some
  554. symbol table games instead */
  555. if (!need_wrapper) {
  556. Printv(func,iname, " = ", module, ".", iname, "\n\n", 0);
  557. } else {
  558. Printv(func,"def ", iname, "(*args, **kwargs):\n", 0);
  559. Printv(func, tab4, "val = apply(", module, ".", iname, ",args,kwargs)\n",0);
  560. if (munge_return) {
  561. /* If the output of this object has been remapped in any way, we're
  562. going to return it as a bare object */
  563. if (!Swig_typemap_search((char*)"out",d,iname)) {
  564. /* If there are output arguments, we are going to return the value
  565. unchanged. Otherwise, emit some shadow class conversion code. */
  566. if (!have_output) {
  567. Printv(func, tab4, "if val: val = ", is_shadow(d), "Ptr(val)", 0);
  568. if ((!SwigType_ispointer(d)) || NewObject)
  569. Printf(func, "; val.thisown = 1\n");
  570. else
  571. Printf(func,"\n");
  572. }
  573. }
  574. }
  575. Printv(func, tab4, "return val\n\n", 0);
  576. }
  577. }
  578. Delete(parse_args);
  579. Delete(arglist);
  580. Delete(get_pointers);
  581. Delete(cleanup);
  582. Delete(outarg);
  583. Delete(check);
  584. Delete(kwargs);
  585. DelWrapper(f);
  586. }
  587. /* -----------------------------------------------------------------------------
  588. * PYTHON::link_variable()
  589. * ----------------------------------------------------------------------------- */
  590. void
  591. PYTHON::link_variable(char *name, char *iname, SwigType *t) {
  592. char *wname;
  593. static int have_globals = 0;
  594. char *tm;
  595. Wrapper *getf, *setf;
  596. getf = NewWrapper();
  597. setf = NewWrapper();
  598. /* If this is our first call, add the globals variable to the
  599. Python dictionary. */
  600. if (!have_globals) {
  601. Printf(f_init,"\t PyDict_SetItemString(d,\"%s\", SWIG_globals);\n",global_name);
  602. have_globals=1;
  603. if ((shadow) && (!(shadow & PYSHADOW_MEMBER))) {
  604. Printv(vars, global_name, " = ", module, ".", global_name, "\n", 0);
  605. }
  606. }
  607. wname = Char(Swig_name_wrapper(name));
  608. /* Create a function for setting the value of the variable */
  609. Printf(setf->def,"static int %s_set(PyObject *val) {", wname);
  610. if (!(Status & STAT_READONLY)) {
  611. if ((tm = Swig_typemap_lookup((char*)"varin",t,name,(char*)"val",name,0))) {
  612. Printf(setf->code,"%s\n",tm);
  613. Replace(setf->code,"$name",iname, DOH_REPLACE_ANY);
  614. } else {
  615. switch(SwigType_type(t)) {
  616. case T_INT: case T_SHORT: case T_LONG :
  617. case T_UINT: case T_USHORT: case T_ULONG:
  618. case T_SCHAR: case T_UCHAR: case T_BOOL:
  619. Wrapper_add_localv(setf,"tval",SwigType_lstr(t,0),"tval",0);
  620. Printv(setf->code,
  621. tab4, "tval = (", SwigType_lstr(t,0), ") PyInt_AsLong(val);\n",
  622. tab4, "if (PyErr_Occurred()) {\n",
  623. tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '",
  624. iname, "'(", SwigType_str(t,0), ")\");\n",
  625. tab8, "return 1; \n",
  626. tab4, "}\n",
  627. tab4, name, " = tval;\n",
  628. 0);
  629. break;
  630. case T_FLOAT: case T_DOUBLE:
  631. Wrapper_add_localv(setf,"tval",SwigType_lstr(t,0), "tval",0);
  632. Printv(setf->code,
  633. tab4, "tval = (", SwigType_lstr(t,0), ") PyFloat_AsDouble(val);\n",
  634. tab4, "if (PyErr_Occurred()) {\n",
  635. tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '",
  636. iname, "'(", SwigType_str(t,0), ")\");\n",
  637. tab8, "return 1; \n",
  638. tab4, "}\n",
  639. tab4, name, " = tval;\n",
  640. 0);
  641. break;
  642. case T_CHAR:
  643. Wrapper_add_local(setf,"tval","char * tval");
  644. Printv(setf->code,
  645. tab4, "tval = (char *) PyString_AsString(val);\n",
  646. tab4, "if (PyErr_Occurred()) {\n",
  647. tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '",
  648. iname, "'(", SwigType_str(t,0), ")\");\n",
  649. tab8, "return 1; \n",
  650. tab4, "}\n",
  651. tab4, name, " = *tval;\n",
  652. 0);
  653. break;
  654. case T_USER:
  655. SwigType_add_pointer(t);
  656. Wrapper_add_localv(setf,"temp",SwigType_lstr(t,0),"temp",0);
  657. get_pointer((char*)"val",(char*)"temp",t,setf->code,(char*)"1");
  658. Printv(setf->code, tab4, name, " = *temp;\n", 0);
  659. SwigType_del_pointer(t);
  660. break;
  661. case T_STRING:
  662. Wrapper_add_local(setf,"tval","char * tval");
  663. Printv(setf->code,
  664. tab4, "tval = (char *) PyString_AsString(val);\n",
  665. tab4, "if (PyErr_Occurred()) {\n",
  666. tab8, "PyErr_SetString(PyExc_TypeError,\"C variable '",
  667. iname, "'(", SwigType_str(t,0), ")\");\n",
  668. tab8, "return 1; \n",
  669. tab4, "}\n",
  670. 0);
  671. if (CPlusPlus) {
  672. Printv(setf->code,
  673. tab4, "if (", name, ") delete [] ", name, ";\n",
  674. tab4, name, " = new char[strlen(tval)+1];\n",
  675. tab4, "strcpy((char *)", name, ",tval);\n",
  676. 0);
  677. } else {
  678. Printv(setf->code,
  679. tab4, "if (", name, ") free((char*)", name, ");\n",
  680. tab4, name, " = (char *) malloc(strlen(tval)+1);\n",
  681. tab4, "strcpy((char *)", name, ",tval);\n",
  682. 0);
  683. }
  684. break;
  685. case T_ARRAY:
  686. {
  687. int setable = 0;
  688. SwigType *aop;
  689. SwigType *ta = Copy(t);
  690. aop = SwigType_pop(ta);
  691. if (SwigType_type(ta) == T_CHAR) {
  692. String *dim = SwigType_array_getdim(aop,0);
  693. if (dim && Len(dim)) {
  694. Printf(setf->code, "strncpy(%s,PyString_AsString(val), %s);\n", name,dim);
  695. setable = 1;
  696. }
  697. }
  698. if (!setable) {
  699. Printv(setf->code,
  700. tab4, "PyErr_SetString(PyExc_TypeError,\"Variable ", iname,
  701. " is read-only.\");\n",
  702. tab4, "return 1;\n",
  703. 0);
  704. }
  705. Delete(ta);
  706. Delete(aop);
  707. }
  708. break;
  709. case T_POINTER: case T_REFERENCE:
  710. Wrapper_add_localv(setf,"temp", SwigType_lstr(t,0), "temp",0);
  711. get_pointer((char*)"val",(char*)"temp",t,setf->code,(char*)"1");
  712. Printv(setf->code,tab4, name, " = temp;\n", 0);
  713. break;
  714. default:
  715. Printf(stderr,"%s : Line %d. Unable to link with type %s.\n", input_file, line_number, SwigType_str(t,0));
  716. }
  717. }
  718. Printf(setf->code," return 0;\n");
  719. } else {
  720. /* Is a readonly variable. Issue an error */
  721. Printv(setf->code,
  722. tab4, "PyErr_SetString(PyExc_TypeError,\"Variable ", iname,
  723. " is read-only.\");\n",
  724. tab4, "return 1;\n",
  725. 0);
  726. }
  727. Printf(setf->code,"}\n");
  728. Wrapper_print(setf,f_wrappers);
  729. /* Create a function for getting the value of a variable */
  730. Printf(getf->def,"static PyObject *%s_get() {", wname);
  731. Wrapper_add_local(getf,"pyobj", "PyObject *pyobj");
  732. if ((tm = Swig_typemap_lookup((char*)"varout",t,name,name,(char*)"pyobj",0))) {
  733. Printf(getf->code,"%s\n",tm);
  734. Replace(getf->code,"$name",iname, DOH_REPLACE_ANY);
  735. } else if ((tm = Swig_typemap_lookup((char*)"out",t,name,name,(char*)"pyobj",0))) {
  736. Printf(getf->code,"%s\n",tm);
  737. Replace(getf->code,"$name",iname, DOH_REPLACE_ANY);
  738. } else {
  739. switch(SwigType_type(t)) {
  740. case T_INT: case T_UINT:
  741. case T_SHORT: case T_USHORT:
  742. case T_LONG: case T_ULONG:
  743. case T_SCHAR: case T_UCHAR: case T_BOOL:
  744. Printv(getf->code, tab4, "pyobj = PyInt_FromLong((long) ", name, ");\n", 0);
  745. break;
  746. case T_FLOAT: case T_DOUBLE:
  747. Printv(getf->code, tab4, "pyobj = PyFloat_FromDouble((double) ", name, ");\n", 0);
  748. break;
  749. case T_CHAR:
  750. Wrapper_add_local(getf,"ptemp","char ptemp[2]");
  751. Printv(getf->code,
  752. tab4, "ptemp[0] = ", name, ";\n",
  753. tab4, "ptemp[1] = 0;\n",
  754. tab4, "pyobj = PyString_FromString(ptemp);\n",
  755. 0);
  756. break;
  757. case T_USER:
  758. SwigType_add_pointer(t);
  759. SwigType_remember(t);
  760. Printv(getf->code,
  761. tab4, "pyobj = SWIG_NewPointerObj((void *) &", name ,
  762. ", SWIGTYPE", SwigType_manglestr(t), ");\n",
  763. 0);
  764. SwigType_del_pointer(t);
  765. break;
  766. case T_STRING:
  767. Printv(getf->code,
  768. tab4, "if (", name, ")\n",
  769. tab8, "pyobj = PyString_FromString(", name, ");\n",
  770. tab4, "else pyobj = PyString_FromString(\"(NULL)\");\n",
  771. 0);
  772. break;
  773. case T_POINTER: case T_ARRAY: case T_REFERENCE:
  774. SwigType_remember(t);
  775. Printv(getf->code,
  776. tab4, "pyobj = SWIG_NewPointerObj((void *)", name,
  777. ", SWIGTYPE", SwigType_manglestr(t), ");\n",
  778. 0);
  779. break;
  780. default:
  781. Printf(stderr,"Unable to link with type %s\n", SwigType_str(t,0));
  782. break;
  783. }
  784. }
  785. Printf(getf->code," return pyobj;\n}\n");
  786. Wrapper_print(getf,f_wrappers);
  787. /* Now add this to the variable linking mechanism */
  788. Printf(f_init,"\t SWIG_addvarlink(SWIG_globals,\"%s\",%s_get, %s_set);\n", iname, wname, wname);
  789. /* Output a shadow variable. (If applicable and possible) */
  790. if ((shadow) && (!(shadow & PYSHADOW_MEMBER))) {
  791. if (is_shadow(t)) {
  792. Printv(vars,
  793. iname, " = ", is_shadow(t), "Ptr(", module, ".", global_name,
  794. ".", iname, ")\n",
  795. 0);
  796. }
  797. }
  798. DelWrapper(setf);
  799. DelWrapper(getf);
  800. }
  801. /* -----------------------------------------------------------------------------
  802. * PYTHON::declare_const()
  803. * ----------------------------------------------------------------------------- */
  804. void
  805. PYTHON::declare_const(char *name, char *, SwigType *type, char *value) {
  806. char *tm;
  807. if ((tm = Swig_typemap_lookup((char*)"const",type,name,value,name,0))) {
  808. Printf(const_code,"%s\n", tm);
  809. } else {
  810. switch(SwigType_type(type)) {
  811. case T_INT: case T_UINT: case T_BOOL:
  812. case T_SHORT: case T_USHORT:
  813. case T_LONG: case T_ULONG:
  814. case T_SCHAR: case T_UCHAR:
  815. Printv(const_code, tab4, "{ SWIG_PY_INT, \"", name, "\", (long) ", value, ", 0, 0, 0},\n", 0);
  816. break;
  817. case T_DOUBLE:
  818. case T_FLOAT:
  819. Printv(const_code, tab4, "{ SWIG_PY_FLOAT, \"", name, "\", 0, (double) ", value, ", 0,0},\n", 0);
  820. break;
  821. case T_CHAR :
  822. Printf(const_code," { SWIG_PY_STRING, \"%s\", 0, 0, (void *) \"%s\", 0 }, \n", name, value);
  823. break;
  824. case T_STRING:
  825. Printf(const_code," { SWIG_PY_STRING, \"%s\", 0, 0, (void *) \"%s\", 0 }, \n", name, value);
  826. break;
  827. case T_POINTER: case T_ARRAY: case T_REFERENCE:
  828. SwigType_remember(type);
  829. Printv(const_code, tab4, "{ SWIG_PY_POINTER, \"", name, "\", 0, 0, (void *) ", value, ", &SWIGTYPE", SwigType_manglestr(type), "}, \n", 0);
  830. break;
  831. default:
  832. Printf(stderr,"%s : Line %d. Unsupported constant value.\n", input_file, line_number);
  833. return;
  834. break;
  835. }
  836. }
  837. if ((shadow) && (!(shadow & PYSHADOW_MEMBER))) {
  838. Printv(vars,name, " = ", module, ".", name, "\n", 0);
  839. }
  840. }
  841. /* -----------------------------------------------------------------------------
  842. * PYTHON::usage_func() - Make string showing how to call a function
  843. * ----------------------------------------------------------------------------- */
  844. char *
  845. PYTHON::usage_func(char *iname, SwigType *, ParmList *l) {
  846. static String *temp = 0;
  847. Parm *p;
  848. int i;
  849. if (!temp) temp = NewString("");
  850. Clear(temp);
  851. Printf(temp,"%s(", iname);
  852. i = 0;
  853. p = l;
  854. while (p != 0) {
  855. SwigType *pt = Gettype(p);
  856. String *pn = Getname(p);
  857. if (!Getignore(p)) {
  858. i++;
  859. /* If parameter has been named, use that. Otherwise, just print a type */
  860. if (SwigType_type(pt) != T_VOID) {
  861. if (Len(pn) > 0) {
  862. Printf(temp,"%s",pn);
  863. } else {
  864. Printf(temp,"%s", SwigType_str(pt,0));
  865. }
  866. }
  867. p = Getnext(p);
  868. if (p != 0) {
  869. if (!Getignore(p))
  870. Putc(',',temp);
  871. }
  872. } else {
  873. p = Getnext(p);
  874. if (p) {
  875. if ((!Getignore(p)) && (i > 0))
  876. Putc(',',temp);
  877. }
  878. }
  879. }
  880. Putc(')',temp);
  881. return Char(temp);
  882. }
  883. /* -----------------------------------------------------------------------------
  884. * PYTHON::add_native()
  885. * ----------------------------------------------------------------------------- */
  886. void
  887. PYTHON::add_native(char *name, char *funcname, SwigType *, ParmList *) {
  888. add_method(name, funcname,0);
  889. if (shadow) {
  890. Printv(func, name, " = ", module, ".", name, "\n\n", 0);
  891. }
  892. }
  893. /* -----------------------------------------------------------------------------
  894. * PYTHON::cpp_class_decl() - Register a class definition
  895. * ----------------------------------------------------------------------------- */
  896. void
  897. PYTHON::cpp_class_decl(char *name, char *rename, char *type) {
  898. String *stype;
  899. if (shadow) {
  900. stype = NewString(name);
  901. SwigType_add_pointer(stype);
  902. Setattr(hash,stype,rename);
  903. Delete(stype);
  904. /* Add full name of datatype to the hash table */
  905. if (strlen(type) > 0) {
  906. stype = NewStringf("%s %s", type, name);
  907. SwigType_add_pointer(stype);
  908. Setattr(hash,stype,rename);
  909. Delete(stype);
  910. }
  911. }
  912. }
  913. /* -----------------------------------------------------------------------------
  914. * PYTHON::pragma()
  915. * ----------------------------------------------------------------------------- */
  916. void
  917. PYTHON::pragma(char *lang, char *cmd, char *value) {
  918. if (strcmp(lang,(char*)"python") == 0) {
  919. if (strcmp(cmd,"CODE") == 0) {
  920. if (shadow) {
  921. Printf(f_shadow,"%s\n",value);
  922. }
  923. } else if (strcmp(cmd,"code") == 0) {
  924. if (shadow) {
  925. Printf(f_shadow,"%s\n",value);
  926. }
  927. } else if (strcmp(cmd,"include") == 0) {
  928. if (shadow) {
  929. if (value) {
  930. FILE *f = Swig_open(value);
  931. if (!f) {
  932. Printf(stderr,"%s : Line %d. Unable to locate file %s\n", input_file, line_number,value);
  933. } else {
  934. char buffer[4096];
  935. while (fgets(buffer,4095,f)) {
  936. Printv(pragma_include,buffer,0);
  937. }
  938. }
  939. }
  940. }
  941. } else {
  942. Printf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file, line_number);
  943. }
  944. }
  945. }
  946. struct PyPragma {
  947. String *m_method;
  948. String *m_text;
  949. PyPragma *next;
  950. PyPragma(char *method, char *text) {
  951. m_method = NewString(method);
  952. m_text = NewString(text);
  953. next = 0;
  954. }
  955. ~PyPragma() {
  956. Delete(m_method);
  957. Delete(m_text);
  958. if (next) delete next;
  959. }
  960. };
  961. static PyPragma *pragmas = 0;
  962. /* -----------------------------------------------------------------------------
  963. * PYTHON::cpp_pragma() - Handle C++ pragmas
  964. * ----------------------------------------------------------------------------- */
  965. void
  966. PYTHON::cpp_pragma(Pragma *plist) {
  967. PyPragma *pyp1 = 0, *pyp2 = 0;
  968. if (pragmas) {
  969. delete pragmas;
  970. pragmas = 0;
  971. }
  972. while (plist) {
  973. if (strcmp(Char(plist->lang),(char*)"python") == 0) {
  974. if (strcmp(Char(plist->name),"addtomethod") == 0) {
  975. /* parse value, expected to be in the form "methodName:line" */
  976. String *temp = NewString(plist->value);
  977. char* txtptr = strchr(Char(temp), ':');
  978. if (txtptr) {
  979. /* add name and line to a list in current_class */
  980. *txtptr = 0;
  981. txtptr++;
  982. pyp1 = new PyPragma(Char(temp),txtptr);
  983. pyp1->next = 0;
  984. if (pyp2) {
  985. pyp2->next = pyp1;
  986. pyp2 = pyp1;
  987. } else {
  988. pragmas = pyp1;
  989. pyp2 = pragmas;
  990. }
  991. } else {
  992. Printf(stderr,"%s : Line %d. Malformed addtomethod pragma. Should be \"methodName:text\"\n",
  993. Char(plist->filename),plist->lineno);
  994. }
  995. Delete(temp);
  996. } else if (strcmp(Char(plist->name), "addtoclass") == 0) {
  997. pyp1 = new PyPragma((char*)"__class__",Char(plist->value));
  998. pyp1->next = 0;
  999. if (pyp2) {
  1000. pyp2->next = pyp1;
  1001. pyp2 = pyp1;
  1002. } else {
  1003. pragmas = pyp1;
  1004. pyp2 = pragmas;
  1005. }
  1006. }
  1007. }
  1008. plist = plist->next;
  1009. }
  1010. }
  1011. /* -----------------------------------------------------------------------------
  1012. * PYTHON::emitAddPragmas()
  1013. *
  1014. * Search the current class pragma for any text belonging to name.
  1015. * Append the text properly spaced to the output string.
  1016. * ----------------------------------------------------------------------------- */
  1017. void
  1018. PYTHON::emitAddPragmas(String *output, char* name, char* spacing) {
  1019. PyPragma *p = pragmas;
  1020. while (p) {
  1021. if (strcmp(Char(p->m_method),name) == 0) {
  1022. Printv(output,spacing,p->m_text,"\n",0);
  1023. }
  1024. p = p->next;
  1025. }
  1026. }
  1027. /* C++ Support + Shadow Classes */
  1028. static String *setattr = 0;
  1029. static String *getattr = 0;
  1030. static String *csetattr = 0;
  1031. static String *cgetattr = 0;
  1032. static String *pyclass = 0;
  1033. static String *imethod = 0;
  1034. static String *construct = 0;
  1035. static String *cinit = 0;
  1036. static String *additional = 0;
  1037. static int have_constructor;
  1038. static int have_destructor;
  1039. static int have_getattr;
  1040. static int have_setattr;
  1041. static int have_repr;
  1042. static char *class_type;
  1043. static char *real_classname;
  1044. static String *base_class = 0;
  1045. static int class_renamed = 0;
  1046. /* -----------------------------------------------------------------------------
  1047. * PYTHON::cpp_open_class()
  1048. * ----------------------------------------------------------------------------- */
  1049. void
  1050. PYTHON::cpp_open_class(char *classname, char *rname, char *ctype, int strip) {
  1051. this->Language::cpp_open_class(classname, rname, ctype, strip);
  1052. if (shadow) {
  1053. /* Create new strings for building up a wrapper function */
  1054. setattr = NewString("");
  1055. getattr = NewString("");
  1056. csetattr = NewString("");
  1057. cgetattr = NewString("");
  1058. pyclass = NewString("");
  1059. imethod = NewString("");
  1060. construct = NewString("");
  1061. cinit = NewString("");
  1062. additional= NewString("");
  1063. base_class = 0;
  1064. have_constructor = 0;
  1065. have_destructor = 0;
  1066. have_getattr = 0;
  1067. have_setattr = 0;
  1068. have_repr = 0;
  1069. if (rname) {
  1070. class_name = Swig_copy_string(rname);
  1071. class_renamed = 1;
  1072. } else {
  1073. class_name = Swig_copy_string(classname);
  1074. class_renamed = 0;
  1075. }
  1076. }
  1077. real_classname = Swig_copy_string(classname);
  1078. class_type = Swig_copy_string(ctype);
  1079. cpp_class_decl(real_classname,class_name,class_type);
  1080. if (shadow) {
  1081. Printv(setattr,
  1082. tab4, "def __setattr__(self,name,value):\n",
  1083. tab8, "if (name == \"this\") or (name == \"thisown\"): self.__dict__[name] = value; return\n",
  1084. tab8, "method = ", class_name, ".__setmethods__.get(name,None)\n",
  1085. tab8, "if method: return method(self,value)\n",
  1086. 0);
  1087. Printv(getattr, tab4, "def __getattr__(self,name):\n", 0);
  1088. Printv(csetattr, tab4, "__setmethods__ = {\n", 0);
  1089. Printv(cgetattr, tab4, "__getmethods__ = {\n", 0);
  1090. }
  1091. }
  1092. /* -----------------------------------------------------------------------------
  1093. * PYTHON::cpp_member_func()
  1094. * ----------------------------------------------------------------------------- */
  1095. void
  1096. PYTHON::cpp_member_func(char *name, char *iname, SwigType *t, ParmList *l) {
  1097. char *realname;
  1098. int oldshadow;
  1099. char cname[1024];
  1100. /* Create the default member function */
  1101. oldshadow = shadow; /* Disable shadowing when wrapping member functions */
  1102. if (shadow) shadow = shadow | PYSHADOW_MEMBER;
  1103. this->Language::cpp_member_func(name,iname,t,l);
  1104. shadow = oldshadow;
  1105. if (shadow) {
  1106. realname = iname ? iname : name;
  1107. /* Check to see if we've already seen this */
  1108. sprintf(cname,"python:%s::%s",class_name,realname);
  1109. if (Getattr(symbols,cname)) return;
  1110. Setattr(symbols,cname,cname);
  1111. if (strcmp(realname,"__repr__") == 0)
  1112. have_repr = 1;
  1113. if (!is_shadow(t) && !noopt) {
  1114. Printv(imethod,
  1115. class_name, ".", realname, " = new.instancemethod(", module, ".", Swig_name_member(class_name,realname), ", None, ", class_name, ")\n",
  1116. 0);
  1117. } else {
  1118. if (use_kw)
  1119. Printv(pyclass,tab4, "def ", realname, "(*args, **kwargs):\n", 0);
  1120. else
  1121. Printv(pyclass, tab4, "def ", realname, "(*args):\n", 0);
  1122. if (use_kw)
  1123. Printv(pyclass, tab8, "val = apply(", module, ".", Swig_name_member(class_name,realname), ",args, kwargs)\n", 0);
  1124. else
  1125. Printv(pyclass, tab8, "val = apply(", module, ".", Swig_name_member(class_name,realname), ",args)\n",0);
  1126. /* Check to see if the return type is an object */
  1127. if (is_shadow(t)) {
  1128. if (!Swig_typemap_search((char*)"out",t,Swig_name_member(class_name,realname))) {
  1129. if (!have_output) {
  1130. Printv(pyclass, tab8, "if val: val = ", is_shadow(t), "Ptr(val) ", 0);
  1131. if ((!SwigType_ispointer(t) || NewObject)) {
  1132. Printf(pyclass, "; val.thisown = 1\n");
  1133. } else {
  1134. Printf(pyclass,"\n");
  1135. }
  1136. }
  1137. }
  1138. }
  1139. Printv(pyclass, tab8, "return val\n", 0);
  1140. }
  1141. /* emitAddPragmas(*pyclass, realname, tab8);
  1142. *pyclass << tab8 << "return val\n"; */
  1143. }
  1144. }
  1145. /* -----------------------------------------------------------------------------
  1146. * PYTHON::cpp_constructor()
  1147. * ----------------------------------------------------------------------------- */
  1148. void
  1149. PYTHON::cpp_constructor(char *name, char *iname, ParmList *l) {
  1150. char *realname;
  1151. int oldshadow = shadow;
  1152. char cname[1024];
  1153. if (shadow) shadow = shadow | PYSHADOW_MEMBER;
  1154. this->Language::cpp_constructor(name,iname,l);
  1155. shadow = oldshadow;
  1156. if (shadow) {
  1157. realname = iname ? iname : class_name;
  1158. /* Check to see if we've already seen this */
  1159. sprintf(cname,":python:constructor:%s::%s",class_name,realname);
  1160. if (Getattr(symbols,cname)) return;
  1161. Setattr(symbols,cname,cname);
  1162. if (!have_constructor) {
  1163. if (use_kw)
  1164. Printv(construct, tab4, "def __init__(self,*args,**kwargs):\n", 0);
  1165. else
  1166. Printv(construct, tab4, "def __init__(self,*args):\n",0);
  1167. if (use_kw)
  1168. Printv(construct, tab8, "self.this = apply(", module, ".", Swig_name_construct(realname), ",args,kwargs)\n", 0);
  1169. else
  1170. Printv(construct, tab8, "self.this = apply(", module, ".", Swig_name_construct(realname), ",args)\n", 0);
  1171. Printv(construct, tab8, "self.thisown = 1\n", 0);
  1172. emitAddPragmas(construct,(char*)"__init__",(char*)tab8);
  1173. have_constructor = 1;
  1174. } else {
  1175. /* Hmmm. We seem to be creating a different constructor. We're just going to create a
  1176. function for it. */
  1177. if (use_kw)
  1178. Printv(additional, "def ", realname, "(*args,**kwargs):\n", 0);
  1179. else
  1180. Printv(additional, "def ", realname, "(*args):\n", 0);
  1181. Printv(additional, tab4, "val = ", class_name, "Ptr(apply(", 0);
  1182. if (use_kw)
  1183. Printv(additional, module, ".", Swig_name_construct(realname), ",args,kwargs))\n", 0);
  1184. else
  1185. Printv(additional, module, ".", Swig_name_construct(realname), ",args))\n", 0);
  1186. Printv(additional,tab4, "val.thisown = 1\n",
  1187. tab4, "return val\n\n", 0);
  1188. }
  1189. }
  1190. }
  1191. /* -----------------------------------------------------------------------------
  1192. * PYTHON::cpp_destructor()
  1193. * ----------------------------------------------------------------------------- */
  1194. void
  1195. PYTHON::cpp_destructor(char *name, char *newname) {
  1196. char *realname;
  1197. int oldshadow = shadow;
  1198. if (shadow) shadow = shadow | PYSHADOW_MEMBER;
  1199. this->Language::cpp_destructor(name,newname);
  1200. shadow = oldshadow;
  1201. if (shadow) {
  1202. if (newname) realname = newname;
  1203. else realname = class_renamed ? class_name : name;
  1204. Printv(pyclass, tab4, "def __del__(self,", module, "=", module, "):\n", 0);
  1205. emitAddPragmas(pyclass,(char*)"__del__",(char*)tab8);
  1206. Printv(pyclass, tab8, "if self.thisown == 1 :\n",
  1207. tab8, tab4, module, ".", Swig_name_destroy(realname), "(self)\n", 0);
  1208. have_destructor = 1;
  1209. }
  1210. }
  1211. /* -----------------------------------------------------------------------------
  1212. * PYTHON::cpp_close_class() - Close a class and write wrappers
  1213. * ----------------------------------------------------------------------------- */
  1214. void
  1215. PYTHON::cpp_close_class() {
  1216. String *ptrclass;
  1217. String *repr;
  1218. ptrclass = NewString("");
  1219. repr = NewString("");
  1220. if (shadow) {
  1221. if (!have_constructor) {
  1222. /* Build a constructor that takes a pointer to this kind of object */
  1223. Printv(construct,
  1224. tab4, "def __init__(self,this):\n",
  1225. tab8, "self.this = this\n",
  1226. 0);
  1227. }
  1228. /* First, build the pointer base class */
  1229. if (base_class) {
  1230. Printv(ptrclass, "class ", class_name, "(", base_class, "):\n", 0);
  1231. } else {
  1232. Printv(ptrclass, "class ", class_name, ":\n", 0);
  1233. }
  1234. Printv(getattr,
  1235. tab8, "method = ", class_name, ".__getmethods__.get(name,None)\n",
  1236. tab8, "if method: return method(self)\n",
  1237. tab8, "raise AttributeError,name\n",
  1238. 0);
  1239. Printv(setattr, tab8, "self.__dict__[name] = value\n",0);
  1240. Printv(cgetattr, tab4, "}\n", 0);
  1241. Printv(csetattr, tab4, "}\n", 0);
  1242. Printv(ptrclass,cinit,construct,"\n",0);
  1243. Printv(classes,ptrclass,pyclass,0);
  1244. if (have_setattr) {
  1245. Printv(classes, csetattr, setattr, 0);
  1246. }
  1247. if (have_getattr) {
  1248. Printv(classes,cgetattr,getattr,0);
  1249. }
  1250. if (!have_repr) {
  1251. /* Supply a repr method for this class */
  1252. Printv(repr,
  1253. tab4, "def __repr__(self):\n",
  1254. tab8, "return \"<C ", class_name," instance at %s>\" % (self.this,)\n",
  1255. 0);
  1256. Printv(classes,repr,0);
  1257. emitAddPragmas(classes,(char*)"__class__",(char*)tab4);
  1258. }
  1259. /* Now build the real class with a normal constructor */
  1260. Printv(classes,
  1261. "class ", class_name, "Ptr(", class_name, "):\n",
  1262. tab4, "def __init__(self,this):\n",
  1263. tab8, "self.this = this\n",
  1264. tab8, "self.thisown = 0\n",
  1265. tab8, "self.__class__ = ", class_name, "\n",
  1266. "\n", additional, "\n",
  1267. 0);
  1268. Printv(classes,imethod,"\n",0);
  1269. Delete(pyclass);
  1270. Delete(imethod);
  1271. Delete(setattr);
  1272. Delete(getattr);
  1273. Delete(additional);
  1274. }
  1275. Delete(ptrclass);
  1276. Delete(repr);
  1277. }
  1278. /* -----------------------------------------------------------------------------
  1279. * PYTHON::cpp_inherit() - Handle inheritance
  1280. * ----------------------------------------------------------------------------- */
  1281. void
  1282. PYTHON::cpp_inherit(char **baseclass,int) {
  1283. char *bc;
  1284. int i = 0, first_base = 0;
  1285. if (!shadow) {
  1286. this->Language::cpp_inherit(baseclass);
  1287. return;
  1288. }
  1289. /* We'll inherit variables and constants, but not methods */
  1290. this->Language::cpp_inherit(baseclass, INHERIT_VAR);
  1291. if (!baseclass) return;
  1292. base_class = NewString("");
  1293. /* Now tell the Python module that we're inheriting from a base class */
  1294. while (baseclass[i]) {
  1295. String *bs = NewString(baseclass[i]);
  1296. SwigType_add_pointer(bs);
  1297. bc = GetChar(hash,bs);
  1298. if (bc) {
  1299. if (first_base) Putc(',',base_class);
  1300. Printv(base_class,bc,0);
  1301. first_base = 1;
  1302. }
  1303. Delete(bs);
  1304. i++;
  1305. }
  1306. if (!first_base) {
  1307. Delete(base_class);
  1308. base_class = 0;
  1309. }
  1310. }
  1311. /* -----------------------------------------------------------------------------
  1312. * PYTHON::cpp_variable() - Add a member variable
  1313. * ----------------------------------------------------------------------------- */
  1314. void
  1315. PYTHON::cpp_variable(char *name, char *iname, SwigType *t) {
  1316. char *realname;
  1317. int inhash = 0;
  1318. int oldshadow = shadow;
  1319. char cname[512];
  1320. if (shadow) shadow = shadow | PYSHADOW_MEMBER;
  1321. this->Language::cpp_variable(name,iname,t);
  1322. shadow = oldshadow;
  1323. if (shadow) {
  1324. have_getattr = 1;
  1325. have_setattr = 1;
  1326. realname = iname ? iname : name;
  1327. /* Check to see if we've already seen this */
  1328. sprintf(cname,"python:%s::%s:",class_name,realname);
  1329. if (Getattr(symbols,cname)) return;
  1330. Setattr(symbols,cname,cname);
  1331. /* Figure out if we've seen this datatype before */
  1332. if (is_shadow(t)) inhash = 1;
  1333. if (Status & STAT_READONLY) {
  1334. /* *setattr << tab8 << tab4 << "raise RuntimeError, \'Member is read-only\'\n"; */
  1335. } else {
  1336. Printv(csetattr, tab8, "\"", realname, "\" : ", module, ".", Swig_name_set(Swig_name_member(class_name,realname)), ",\n", 0);
  1337. }
  1338. if (inhash) {
  1339. Printv(cgetattr, tab8, "\"", realname, "\" : lambda x : ", is_shadow(t), "Ptr(", module, ".", Swig_name_get(Swig_name_member(class_name,realname)), "(x)),\n", 0);
  1340. } else {
  1341. Printv(cgetattr, tab8, "\"", realname, "\" : ", module, ".", Swig_name_get(Swig_name_member(class_name,realname)),",\n", 0);
  1342. }
  1343. }
  1344. }
  1345. /* -----------------------------------------------------------------------------
  1346. * PYTHON::cpp_declare_const()
  1347. * ----------------------------------------------------------------------------- */
  1348. void
  1349. PYTHON::cpp_declare_const(char *name, char *iname, SwigType *type, char *value) {
  1350. char *realname;
  1351. int oldshadow = shadow;
  1352. char cname[512];
  1353. if (shadow) shadow = shadow | PYSHADOW_MEMBER;
  1354. this->Language::cpp_declare_const(name,iname,type,value);
  1355. shadow = oldshadow;
  1356. if (shadow) {
  1357. realname = iname ? iname : name;
  1358. /* Check to see if we've already seen this */
  1359. sprintf(cname,"python:%s::%s", class_name, realname);
  1360. if (Getattr(symbols,cname)) return;
  1361. Setattr(symbols,cname,cname);
  1362. Printv(cinit, tab4, realname, " = ", module, ".", Swig_name_member(class_name,realname), "\n", 0);
  1363. }
  1364. }
  1365. /* -----------------------------------------------------------------------------
  1366. * PYTHON::add_typedef() - Manage typedef's for shadow classes
  1367. * ----------------------------------------------------------------------------- */
  1368. void
  1369. PYTHON::add_typedef(SwigType *t, char *name) {
  1370. if (!shadow) return;
  1371. if (is_shadow(t)) {
  1372. cpp_class_decl(name,Char(is_shadow(t)),"");
  1373. }
  1374. }