PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/mkoeppe-1-3-mzscheme/SWIG/Source/Modules1.1/python.cxx

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